수업시간 JAVA/문제
연락처 등록/조회/수정/삭제
by SEOKIHOUSE
2023. 4. 8.
package practice31day;
public class Contact {
// 필수
private int num;
private String name;
private String phoneNum;
// 필수아닌것
private String email;
public Contact() {
}
public Contact(int num, String name, String phoneNum) {
this.num = num;
this.name = name;
this.phoneNum = phoneNum;
}
public void setNum(int num) {
this.num = num;
}
public int getNum() {
return num;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setPhoneNum(String phoneNum) {
this.phoneNum = phoneNum;
}
public String getPhoneNum() {
return phoneNum;
}
public void setEmail(String email) {
this.email = email;
}
public String getEmail() {
return email;
}
public String toString() {
if(email == null) {
return "등록번호: " + num+ " 이름: " +name +" 폰번호s: " +phoneNum;
}
return "등록번호: " + num+ " 이름: " +name +" 폰번호s: " +phoneNum+" 이메일: "+ email;
}
}
package practice31dayokok;
import java.util.Scanner;
public class ContactManager {
Scanner sc = new Scanner(System.in);
Contact[] store;
int index = 0;
ContactManager() {
store = new Contact[5];
}
public void registerMember(Contact c) {
if (index >= 0 && index < store.length) {
store[index] = c;
index++;
} else {
System.out.println("배열범위를 벗어났다 에러");
}
}
public Contact findYourInfo(int num, String name) {
for (int i = 0; i < store.length; i++) {
if (store[i].getNum() == num && store[i].getName().equals(name)) {
return store[i];
}
}
return null;
}
public void repairInfo(int num1, int num2) {
for (int i = 0; i < index; i++) {
if (store[i].getNum() == num1) {
if (num2 == 1) {
System.out.print("수정할 이름을 적으세요>> ");
String name = sc.nextLine();
store[i].setName(name);
} else if (num2 == 2) {
System.out.print("수정할 폰번호는?>> ");
String phone = sc.nextLine();
store[i].setPhoneNum(phone);
} else if (num2 == 3) {
System.out.print("수정할 이메일은?>> ");
String email = sc.nextLine();
store[i].setEmail(email);
}
}
}
}
public void deleteInfo(String name, int num) {
for (int i = 0; i < index; i++) {
if (store[i].getName().equals(name) && store[i].getNum() == num) {
if (store[i] == store[index - 1]) {
store[i] = null;
}
for (int j = i; j < index - 1; j++) {
store[j] = store[j + 1];
int nums = store[j].getNum() - 1;
store[j].setNum(nums);
if (store[j + 1] == store[index - 1]) {
store[index - 1] = null;
}
}
index--;
}
}
}
public int deleteCheck(String name) {
int result = 0;
for (int i = 0; i < index; i++) {
if (store[i].getName().equals(name)) {
if (store[i] == store[index - 1]) {
for (int j = 0; j < index-1; j++) {
if (store[j].getName().equals(store[index - 1].getName())) {
result = -1;
}
}
} else {
for (int j = 0; j < index-1; j++) {
if (store[j].getName().equals(store[index - 1].getName())) {
result = -1;
}
}
}
}
}
return result;
}
public int deleteGive(int num) {
for (int i = 0; i < index; i++) {
if (store[i].getNum() == num) {
return store[i].getNum();
}
}
return 0;
}
public int deleteSingle(String name) {
for (int i = 0; i < index; i++) {
if (store[i].getName().equals(name)) {
return store[i].getNum();
}
}
return 0;
}
}
package practice31dayokok;
import java.util.Scanner;
public class ContactTest {
public static void main(String[] args) {
ContactManager cm = new ContactManager();
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("전화번호 등록시스템입니다");
System.out.println("1.등록 2.조회 3.수정 4.삭제 0.종료");
int choose = sc.nextInt();
if (choose == 1) {
System.out.print("이름을 등록하세요>> ");
String name = sc.nextLine();
name = sc.nextLine();
System.out.print("전화번호를 등록하세요>> ");
String phone = sc.nextLine();
Contact c = new Contact(cm.index + 1, name, phone);
System.out.print("이메일 등록하시려면 1번 아니라면 0번");
int emailNum = sc.nextInt();
if (emailNum == 1) {
System.out.print("이메일을 입력하세요>> ");
String email = sc.nextLine();
email = sc.nextLine();
c.setEmail(email);
} else if (emailNum == 0) {
System.out.println("메뉴로 돌아갑미다");
}
cm.registerMember(c);
}
else if (choose == 2) {
System.out.print("1.개별조회 2.전체조회>> ");
int a = sc.nextInt();
if (a == 1) {
System.out.print("번호가 어떻게 되시나용?>> ");
a = sc.nextInt();
System.out.print("이름이 어떻게 되세용?>> ");
String whatYourName = sc.nextLine();
whatYourName = sc.nextLine();
Contact scs = cm.findYourInfo(a, whatYourName);
System.out.println(scs);
} else if (a == 2) {
for (int i = 0; i < cm.index; i++) {
System.out.println(cm.store[i]);
}
System.out.println("index값: " + cm.index);
}
}
else if (choose == 3) {
System.out.print("번호가 어떻게 되시죠?>> ");
int a = sc.nextInt();
System.out.println("수정할 부분을 선택하세요 ");
System.out.println("1.이름 2.폰번호 3.이메일");
int b = sc.nextInt();
cm.repairInfo(a, b);
}
else if (choose == 4) {
System.out.print("삭제하고 싶은 이름을 선택하세요");
String name = sc.nextLine();
name = sc.nextLine();
int ss = cm.deleteCheck(name);
if (ss == 0) {
int s1 = cm.deleteSingle(name);
cm.deleteInfo(name, s1);
} else if (ss != 0) {
System.out.print("중복된 이름이 있습니다 어떤 등록번호를 지우시겠습니까");
int num = sc.nextInt();
int a = cm.deleteGive(num);
cm.deleteInfo(name, a);
}
} else if (choose == 0) {
System.out.println("byebyebye");
break;
}
}
}
}