내용

글번호 813
작성자 heojk
작성일 2018-02-20 17:31:01
제목 CustomerManager3
내용 고객 관리 프로그램 소스코드 CustomerManager3
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
128
129
130
131
132
133
134
135
136
137
138
139
import java.util.ArrayList;
import java.util.Scanner;
 
public class CustomerManager3 {
 
  //고객정보를 저장할 변수를 ArrayList로 선언
  static ArrayList<Customer> custList = new ArrayList<>();
 
  //배열은 인덱스를 필요로 함
  static int index = -1;//배열은 0부터 시작하므로 최초 인덱스는 -1이어야 함
 
  //기본 입력장치로부터 데이터를 입력받기 위해 Scanner객체 생성
  static Scanner scan = new Scanner(System.in);
   
  public static void main(String[] args) {
 
    while(true) {
 
      System.out.printf("\n[INFO] 고객 수 : %d, 인덱스 : %d\n", custList.size(), index);
      System.out.println("메뉴를 입력하세요.");
      System.out.println("(I)nsert, (P)revious, (N)ext, " +
          "(C)urrent, (U)pdate, (D)elete, (Q)uit");
      System.out.print("메뉴 입력: ");
      String menu = scan.next();
      menu = menu.toLowerCase();  //입력한 문자열을 모두소문자로 변환
      switch(menu.charAt(0)) {
      case 'i':
        System.out.println("고객정보 입력을 시작합니다.");
        insertCustomerData();
        System.out.println("고객정보를 저장했습니다.");
        break;
      case 'p' :
        System.out.println("이전 데이터를 출력합니다.");
        if(index <= 0) {
          System.out.println("이전 데이터가 존재하지 않습니다.");
        }else {
          index--;
          printCustomerData();
        }
      case 'n' :
        System.out.println("다음 데이터를 출력합니다.");
        if(index >= custList.size()-1) {
          System.out.println("다음 데이터가 존재하지 않습니다.");
        }else {
          index++;
          printCustomerData();
        }
        break;
      case 'c' :
        System.out.println("현재 데이터를 출력합니다.");
        if( (index >= 0) && (index < custList.size())) {
          printCustomerData();
        }else {
          System.out.println("출력할 데이터가 선택되지 않았습니다.");
        }
        break;     
      case 'u' :
        System.out.println("데이터를 수정합니다.");
        if( (index >= 0) && (index < custList.size())) {
          System.out.println(index + "번째 데이터를 수정합니다.");
          updateCustomerData();
        }else {
          System.out.println("수정할 데이터가 선택되지 않았습니다.");
        }
        break;
      case 'd' :
        System.out.println("데이터를 삭제합니다.");
        if( (index >= 0) && (index < custList.size())) {
          System.out.println(index + "번째 데이터를 삭제합니다.");
          deleteCustomerData();
        }else {
          System.out.println("삭제할 데이터가 선택되지 않았습니다.");
        }
        break;
      case 'q' :
        System.out.println("프로그램을 종료합니다.");
        scan.close(); //Scanner 객체를 닫아준다.
        System.exit(0); //프로그램을 종료시킨다.
        break
      default :
        System.out.println("메뉴를 잘 못 입력했습니다."); 
      }//end switch
    }//end while
  }//end main
   
  public static void insertCustomerData() {
    System.out.print("이름 : "); 
    String name = scan.next();
    System.out.print("성별(M/F) : ");
    char gender = scan.next().charAt(0);
    System.out.print("이메일 : ");
    String email = scan.next();
    System.out.print("출생년도 : "); 
    int birthYear = scan.nextInt();
 
    //고객 객체를 ArrayList에 저장
    Customer customer = new Customer();
    customer.setName(name);
    customer.setGender(gender);
    customer.setEmail(email);
    customer.setBirthYear(birthYear);
    custList.add(customer);
  }
   
  //고객데이터 출력
  public static void printCustomerData() {
    System.out.println("==========CUSTOMER INFO================");
//    System.out.println(custList[index].toString());
    Customer customer = custList.get(index);
    System.out.println("이름 : " + customer.getName());
    System.out.println("성별 : " + customer.getGender());
    System.out.println("이메일 : " + customer.getEmail());
    System.out.println("출생년도 : " + customer.getBirthYear());
    System.out.println("=======================================");
  }
   
  //index 위치의 고객정보를 삭제합니다.
  public static void deleteCustomerData() {
    custList.remove(index);
  }
   
  //index 위치의 고객 정보를 수정합니다.
  public static void updateCustomerData() {
    Customer customer = custList.get(index);
    System.out.println("---------UPDATE CUSTOMER INFO----------");
    System.out.print("이름(" + customer.getName() + ") :");
    customer.setName(scan.next());
 
    System.out.print("성별(" + customer.getGender() + ") :");
    customer.setGender(scan.next().charAt(0));
     
    System.out.print("이메일(" + customer.getEmail() + ") :");
    customer.setEmail(scan.next());
     
    System.out.print("출생년도(" + customer.getBirthYear() + ") :");
    customer.setBirthYear(scan.nextInt());   
  }
 
}//end class