Company.java - 부서명, 직책 관련 필드 및 메소드 구현 (PhoneInfo.java를 상속받기)
Universe.java - 전공, 학번 관련 필드 및 메소드 구현 (PhoneInfo.java를 상속받기)
Manager.java - 일반, 회사동료, 동창 카테고리 별로 추가 및 리스트 출력 메소드 구현
PhoneInfo.java
package kosta.phone;
public class PhoneInfo {
//상태 : 이름,전화번호,생년월일
//기능 : 전화번호부 출력
private String name;
private String phone_number;
private String birth;
public PhoneInfo() {
}
public PhoneInfo(String name, String phone_number, String birth) {
this.name = name;
this.phone_number = phone_number;
this.birth = birth;
}
public void print() {
System.out.println("이름 : "+name);
System.out.println("전화번호 : "+phone_number);
System.out.println("생년월일 : "+birth);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone_number() {
return phone_number;
}
public void setPhone_number(String phone_number) {
this.phone_number = phone_number;
}
public String getBirth() {
return birth;
}
public void setBirth(String birth) {
this.birth = birth;
}
}
package kosta.phone;
public class Company extends PhoneInfo {
private String dept; // 부서
private String position; // 담당업무
public Company() {
}
public Company(String name, String phone_number, String birth, String dept, String position) {
super(name, phone_number, birth);
this.dept = dept;
this.position = position;
}
//리스트 출력
@Override
public void print() {
super.print();
System.out.println("부서명 : "+dept);
System.out.println("담당업무 : "+position);
System.out.println("==========================");
}
public String getDept() {
return dept;
}
public void setDept(String dept) {
this.dept = dept;
}
public String getPosition() {
return position;
}
public void setPosition(String position) {
this.position = position;
}
}
package kosta.phone;
public class Universe extends PhoneInfo {
private String major;
private String year;
public Universe() {
}
public Universe(String name, String phone_number, String birth, String major, String year) {
super(name, phone_number, birth);
this.major = major;
this.year = year;
}
@Override
public void print() {
super.print();
System.out.println("전공 : "+major);
System.out.println("학번 : "+year);
System.out.println("==========================");
}
public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
public String getYear() {
return year;
}
public void setYear(String year) {
this.year = year;
}
}
package kosta.phone;
import java.util.Scanner;
public class Manager extends PhoneInfo{
PhoneInfo[] arr = new PhoneInfo[3];
int count = 0;
public void addPhoneInfo() {
System.out.println("1.일반 2.동창 3.직장");
System.out.print("선택 :");
String menu = DataInput.sc.nextLine();
System.out.print("이름 :");
String name = DataInput.sc.nextLine();
System.out.print("전화번호 :");
String phone_number = DataInput.sc.nextLine();
System.out.print("생년월일 :");
String birth = DataInput.sc.nextLine();
switch (menu) {
case "1":
arr[count++] = new PhoneInfo(name, phone_number, birth);
break;
case "2":
System.out.print("전공 :");
String major = DataInput.sc.nextLine();
System.out.print("학번 :");
String year = DataInput.sc.nextLine();
arr[count++] = new Universe(name, phone_number, birth, major, year);
break;
case "3":
System.out.print("부서명 : ");
String dept = DataInput.sc.nextLine();
System.out.print("담당업무 : ");
String position = DataInput.sc.nextLine();
arr[count++] = new Company(name, phone_number, birth, dept, position);
break;
}
}
public void listPhoneInfo() {
System.out.println("1.전체 2.동창 3.직장");
System.out.print("선택 :");
String menu = DataInput.sc.nextLine();
switch (menu) {
case "1":
for(int i = 0; i < count; i++) {
arr[i].print();
}
break;
case "2":
for(int i = 0; i < count; i++) {
if(arr[i] instanceof Universe) {
arr[i].print();
}
}
break;
case "3":
for(int i = 0; i < count; i++) {
if(arr[i] instanceof Company) {
arr[i].print();
}
}
break;
}
}
public void searchPhonInfo() {
System.out.print("검색 :");
String name = DataInput.sc.nextLine();
int idx = -1;
for(int i = 0; i < count; i++) {
PhoneInfo info = arr[i];
if(name.equals(info.getName())) {
info.print();
idx = i;
}
}
if(idx == -1) {
System.out.println("찾을 수 없습니다.");
}
}
public void updatePhoneInfo() {
System.out.print("수정할 이름 입력 :");
String name = DataInput.sc.nextLine();
for(int i = 0; i < count; i++) {
if(name.equals(arr[i].getName())) {
System.out.print("전화 번호 수정 : ");
String update_number = DataInput.sc.nextLine();
arr[i].setPhone_number(update_number);
}
}
}
public void deletePhoneInfo() {
System.out.print("삭제할 이름 입력 : ");
String name = DataInput.sc.nextLine();
int idx = -1;
for(int i = 0; i < count; i++) {
PhoneInfo info = arr[i];
if(name.equals(info.getName())) {
idx = i;
}
}
if(idx == -1) {
System.out.println("찾을 수 없습니다.");
}
if(idx != -1) {
for(int i = idx; i < count; i++) {
if(i == arr.length-1) { //마지막 위치
arr[i] = null;
}else {
arr[i] = arr[i+1];
}
}
count--;
}
}
}