내용

글번호 808
작성자 heojk
작성일 2018-02-05 16:57:19
제목 친구 관리 프로그램
내용 친구 관리 프로그램을 작성하세요. - 친구 정보는 이름, 나이, 성별, 장점 - String name, int age, String excel, String email - 최대 20명까지 관리 가능하도록 하세요. - 기능은 저장, 조회, 수정, 삭제 - 조회, 수정, 삭제는 이메일로 합니다. 명함관리 프로그램 소스코드
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import java.util.Scanner;
 
public class FriendsManager {
  static final int MAX=20;
  static String[] nameList = new String[MAX];
  static int[] ageList = new int[MAX];
  static String[] excelList = new String[MAX];
  static String[] emailList = new String[MAX];
   
  static int index = -1;
  static int count = 0;
   
  static Scanner scan = new Scanner(System.in);
   
  public static void main(String[] args) {
    while(true) {
      System.out.println("메뉴를 선택하세요.=================");
      System.out.print("(I)nsert, (S)elect, (U)pdate, (D)elete, (Q)uit : ");
      String menu = scan.nextLine().toLowerCase().trim();
      if(menu != null && menu.length() > 0) {
        switch(menu.charAt(0)) {
        case 'i':
          System.out.println("=====데이터를 입력합니다.=====");
          insertFriendInfo();
          printFriendInfo();
          break;
        case 's':
          System.out.println("=====데이터를 조회합니다.=====");
          index = searchFriendInfoByEmail();
          if(index >= 0 && index < count) {
            printFriendInfo();
          }else {
            System.out.println("찾는 데이터가 없습니다.");
          }
          break;
        case 'u':
          System.out.println("=====데이터를 수정합니다.=====");
          index = searchFriendInfoByEmail();
          if(index >= 0 && index < count) {
            updateFriendInfo();
          }else {
            System.out.println("수정하려는 데이터가 없습니다.");
          }
          break;
        case 'd':
          System.out.println("=====데이터를 삭제합니다.=====");
          index = searchFriendInfoByEmail();
          if(index >= 0 && index < count) {
            deleteFriendInfo();
          }else {
            System.out.println("삭제하려는 데이터가 없습니다.");
          }
          break;
        case 'q':
          System.out.println("프로그램을 종료합니다.");
          System.exit(0);
        default :
          System.out.println("잘 못된 메뉴입니다.");
          break;
        }
      }else {
        System.out.println("*********메뉴를 입력하세요.**********");
      }
       
    }//end while
  }//end main
 
  private static void deleteFriendInfo() {
    for(int i=index; i<count; i++) {
      nameList[i] = nameList[i+1];
      ageList[i] = ageList[i+1];
      excelList[i] = excelList[i+1];
      emailList[i] = emailList[i+1];
    }
    count--;
  }
 
  private static void updateFriendInfo() {
    System.out.printf("%s(%s):", "이름", nameList[index]);
    String name = scan.nextLine().trim();
    if(name.length()>0) {
      nameList[index] = name;
    }
    System.out.printf("%s(%s):", "나이", ageList[index]);
    String str = scan.nextLine().trim();
    if(str.length()>0) {
      ageList[index] = Integer.parseInt(str);
    }
     
    System.out.printf("%s(%s):", "장점", excelList[index]);
    String excel = scan.nextLine().trim();
    if(excel.length()>0) {
      excelList[index] = excel;
    }
    System.out.printf("%s(%s):", "이메일", emailList[index]);
    String email = scan.nextLine().trim();
    if(email.length()>0) {
      emailList[index] = email;
    }
  }
 
  private static int searchFriendInfoByEmail() {
    System.out.print("이메일 주소를 입력하세요.");
    String email = scan.nextLine();
    for(int i=0; i<count; i++) {
      if( emailList[i].equals(email) ) {
        return i;
      }
    }
    return -1;
  }
 
  private static void insertFriendInfo() {
    System.out.print("이름:"); nameList[count] = scan.nextLine().trim();
    System.out.print("나이:"); ageList[count] = Integer.parseInt(scan.nextLine().trim());
    System.out.print("장점:"); excelList[count] = scan.nextLine().trim();
    System.out.print("이메일:"); emailList[count] = scan.nextLine().trim();
    index = count;
    count++;
  }
   
  private static void printFriendInfo() {
    System.out.printf("이름:%s, 나이:%d, 이메일:%s, 장점:%s\n",
        nameList[index], ageList[index], emailList[index], excelList[index]);
  }
 
}//end class