Collection : 수집(데이터,Object)
※ List(목록) : ArrayList, LinkedList
MainClass
import java.util.ArrayList;
import java.util.List;
public class MainClass {
public static void main(String[] args) {
ArrayList<String> arrlist = new ArrayList<String>();
// 추가
arrlist.add("Tigers");
arrlist.add("Eagles");
arrlist.add("SSG");
// 리스트의 크기(길이)
System.out.println(arrlist.size());
for (int i = 0; i < arrlist.size(); i++) {
System.out.println(i + ":" + arrlist.get(i)); // == arrlist[i]
}
System.out.println();
arrlist.add("Lions"); // 맨 뒤에 추가
for (int i = 0; i < arrlist.size(); i++) {
System.out.println(i + ":" + arrlist.get(i));
}
System.out.println();
arrlist.add(2, "Giants"); // 원하는 위치에 추가
for (int i = 0; i < arrlist.size(); i++) {
System.out.println(i + ":" + arrlist.get(i));
}
System.out.println();
// 삭제
arrlist.remove(1); // 삭제하고 싶은 index number
for (int i = 0; i < arrlist.size(); i++) {
System.out.println(i + ":" + arrlist.get(i));
}
System.out.println();
// 검색
int index = arrlist.indexOf("Giants");
System.out.println("데이터가 있습니다 " + index); //데이터가 있습니다 1
index = arrlist.indexOf("Twins");
System.out.println("데이터가 있습니다 " + index); //데이터가 있습니다 -1 -> 데이터가 없으면 -1이 나옴
for (int i = 0; i < arrlist.size(); i++) {
String s = arrlist.get(i);
if("Giants".equals(s)) {
index = i;
break;
}
}
System.out.println("데이터가 있습니다 " + index); //데이터가 있습니다 1
System.out.println();
// 수정
String updateStr = "Twins";
arrlist.set(2, updateStr);
for (int i = 0; i < arrlist.size(); i++) {
System.out.println(i + ":" + arrlist.get(i));
}
System.out.println();
// ArrayList<Human> list = new ArrayList<Human>();
List<Human> list = new ArrayList<Human>();
Human human = new Human();
//추가 -> 3명
list.add(new Human("홍길동", 16));
list.add(new Human("성춘향", 15));
list.add(new Human("일지매", 14));
for (int i = 0; i < list.size(); i++) {
System.out.println(i + ":" + list.get(i)); // == list[i]
}
System.out.println();
list.add(1, new Human("홍두깨", 22));
for (int i = 0; i < list.size(); i++) {
System.out.println(i + ":" + list.get(i));
}
System.out.println();
//삭제 -> 1명
list.remove(3);
for (int i = 0; i < list.size(); i++) {
System.out.println(i + ":" + list.get(i));
}
System.out.println();
//검색 -> 이름
String name = "홍길동";
int findindex = -1;
for (int i = 0; i < list.size(); i++) {
Human h = list.get(i);
if(name.equals(h.getName())) {
findindex = i;
break;
}
}
if(findindex != -1) {
System.out.println(list.get(findindex).toString());
}else {
System.out.println("데이터가 없습니다");
}
System.out.println();
//수정 -> 1명
Human updateHman = new Human("이몽룡", 17);
list.set(1, updateHman);
for (int i = 0; i < list.size(); i++) {
System.out.println(i + ":" + list.get(i)); // == list[i]
}
System.out.println();
}
}
Human Class
public class Human {
String name;
int age;
public Human() {
}
public Human(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Human [name=" + name + ", age=" + age + "]";
}
}
MainClass
package Main;
import java.util.Scanner;
import Dao.addressdao;
public class mainclass {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
addressdao adao = new addressdao();
while(true) {
System.out.println(" << 주소록 >> ");
System.out.println("1. 지인추가");
System.out.println("2. 지인삭제");
System.out.println("3. 지인검색");
System.out.println("4. 지인수정");
System.out.println("5. 모두출력");
System.out.println("6. 데이터저장");
System.out.println("7. 종료");
System.out.print(" >> ");
int choice = sc.nextInt();
if (choice == 1) {
adao.insert();
}else if (choice == 2) {
adao.delete();
}else if (choice == 3) {
adao.select();
}else if (choice == 4) {
adao.update();
}else if (choice == 5) {
adao.allDataPrint();
}else if (choice == 6) {
adao.filesave();
}else if (choice == 7) {
System.out.print("종료되었습니다");
break;
}
}
}
}
humandto Class
package Dto;
public class humandto {
private String name;
private int age;
private String phone;
private String address;
private String memo;
public humandto() {
}
public humandto(String name, int age, String phone, String address, String memo) {
super();
this.name = name;
this.age = age;
this.phone = phone;
this.address = address;
this.memo = memo;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getMemo() {
return memo;
}
public void setMemo(String memo) {
this.memo = memo;
}
@Override
public String toString() {
return name + "-" + age + "-" + phone + "-" + address + "-"
+ memo;
}
}
addressdao Class
package Dao;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import Dto.humandto;
import data.fileproc;
public class addressdao {
Scanner sc = new Scanner(System.in);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
private List<humandto> humanDto = new ArrayList<humandto>();
private fileproc fileProc = null;
public addressdao() {
fileProc = new fileproc("AddressBook");
fileload();
}
//추가
public void insert() {
System.out.println("주소록을 추가합니다");
System.out.print("이름 = ");
String name = sc.next();
System.out.print("나이 = ");
int age = sc.nextInt();
System.out.print("전화번호 = ");
String phone = sc.next();
System.out.print("주소 = ");
String address = "";
try {
address = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
System.out.print("메모 = ");
String memo = sc.next();
humandto hdto = new humandto(name, age, phone, address, memo);
humanDto.add(hdto);
}
//삭제
public void delete() {
System.out.println("삭제할 주소록의 이름을 입력하세요 >> ");
String name = sc.next();
int index = search(name);
if (index == -1) {
System.out.println("이름을 검색할 수 없습니다");
}else {
humanDto.remove(index);
System.out.println("주소록에서 삭제되었습니다");
}
}
//검색
public void select() {
System.out.println("검색할 이름을 입력해주세요 >> ");
String name = sc.next();
for (int i = 0; i < humanDto.size(); i++) {
humandto hDto = humanDto.get(i);
if(name.equals(hDto.getName())) {
System.out.println(hDto.toString());
}
}
}
//수정
public void update() {
System.out.println("수정할 이름을 검색해주세요 >> ");
String name = sc.next();
int index = search(name);
if(index == -1) {
System.out.println("지인의 정보를 찾을 수 없습니다");
return;
}
System.out.println("변경하실 항목을 선택해주세요.");
System.out.println("1. 이름");
System.out.println("2. 나이");
System.out.println("3. 전화번호");
System.out.println("4. 주소");
System.out.println("5. 메모");
int updateCheck = sc.nextInt();
if (updateCheck == 1) {
System.out.print("수정할 이름을 입력해주세요 >>");
humanDto.get(index).setName(sc.next());
}else if (updateCheck == 2) {
System.out.print("수정할 나이를 입력해주세요 >>");
humanDto.get(index).setAge(sc.nextInt());
}else if (updateCheck == 3) {
System.out.print("수정할 전화번호를 입력해주세요 >>");
humanDto.get(index).setPhone(sc.next());
}else if (updateCheck == 4) {
System.out.print("수정할 주소를 입력해주세요 >>");
humanDto.get(index).setAddress(sc.next());
}else if (updateCheck == 5) {
System.out.print("수정할 메모 입력해주세요 >>");
humanDto.get(index).setMemo(sc.next());
}
System.out.print("정보가 수정되었습니다.");
}
//모든 데이터 출력
public void allDataPrint() {
for (int i = 0; i < humanDto.size(); i++) {
System.out.println(humanDto.get(i).toString());
}
}
//파일저장
public void filesave() {
ArrayList<String> dataList = new ArrayList<String>(humanDto.size());
for (int i= 0; i < humanDto.size(); i++) {
dataList.add(i,humanDto.get(i).toString());
}
fileProc.write(dataList);
}
//파일로드
public void fileload () {
ArrayList<String> datas = fileProc.read();
for (int i = 0; i < datas.size(); i++) {
System.out.println(datas.get(i));
}
for (int i = 0; i < datas.size(); i++) {
String split[] = datas.get(i).split("-");
humandto hDto = new humandto(split[0],
Integer.parseInt(split[1]),
split[2],
split[3],
split[4]);
humanDto.add(hDto);
}
}
//함수
private int search(String name) {
int index = -1;
for (int i = 0; i < humanDto.size(); i++) {
humandto h = humanDto.get(i);
if(name.equals(h.getName())) {
index = i;
break;
}
}
return index;
}
}
fileproc Class
package data;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
public class fileproc {
private File file = null;
public fileproc(String filename) {
file = new File("/Users/ksy/tmp//" + filename + ".txt");
try {
if(file.createNewFile()) {
System.out.println("파일생성에 성공하였습니다");
}else {
System.out.println("기존 파일이 있습니다");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}//end of fileproc(constructor)
public ArrayList<String> read() {
ArrayList<String> datas = new ArrayList<String>();
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String str = "";
int i = 0;
while ((str = br.readLine()) != null){
datas.add(i, str);
i++;
}
br.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch(IOException e) {//br.readLine의 예외
e.printStackTrace();
}
return datas;
}//end of read
public void write(ArrayList<String> datas) {
try {
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(file)));
for (int i = 0; i < datas.size(); i++) {
pw.println(datas.get(i));
}
pw.close();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("파일에 저장되었습니다");
}//end of write
}