public class AMSField {
//항공사, 항공기 번호, 최대승객수, 출발지, 도착지
String[][]arrPlane = new String[100][5];
int cnt;
int showcnt;
int cLength = arrPlane[0].length;
String result= "";
int updateIndex;
//추가하기
void insert(String[]arPlane) {
arrPlane[cnt]=arPlane;
cnt++;
}
//검색하기
String search(String keyword, int Index) {
int arIndex[];
String result = "";
int searchCnt=0;
for (int i = 0; i < cnt; i++) {
if(keyword.equals(arrPlane[i][Index])) {
searchCnt++;
updateIndex = i;
result+="" + i + ",";
}
}
//for문 끝나고 나서 배열에 담아줌
arIndex = new int[searchCnt];
for (int i = 0; i < arIndex.length ; i++) {
//result가 string변수인데 int로 변경해주는거
arIndex[i] = Integer.parseInt(result.split(",")[i]);
}
return show(arIndex);
}
//수정하기
//목적지, 출발지
//항공기 번호 존재할때
//JOptionPane.showOptionDialog()사용
//항공기 번호를 입력받고 해당 항공기의 출발지와 목적지를 수정
//출발지와 목적지가 동일하면 수정 실패라고 뛰어줌
//출발지 수정 , 목적지 수정 버튼 만들기
void update(int btnIndex, String newValue) {
arrPlane[updateIndex][btnIndex + 3] = newValue;
//btnindex
//출발지 0
//도착지 1
//2차원배열 열index번호 출발지 index번호 : 3
// 도착지 index번호 : 4
}
//삭제하기
boolean delete(String keyword) {
boolean deleteCheck = false;
for (int i = 0; i < cnt; i++) {
if(arrPlane[i][1].equals(keyword)) {
//cnt-1 : 마지막 정보가 담긴 행
//cnt : null이 담긴 행
for (int j = i; j < cnt; j++) {
arrPlane[j] = arrPlane[j+1];
}
deleteCheck = true;
cnt--;
break;
}
}
return deleteCheck;
}
//목록보기
String show() {
String result = "항공사, 항공기 번호, 최대승객수(명), 출발지, 도착지\n";
for(int i=0; i<cnt; i++) {
result += "😍";
for (int j = 0; j <cLength; j++) {
result += j == cLength -1 ? arrPlane[i][j]: arrPlane[i][j] + ", ";
}
result += "\n";
}
if(cnt == 0) result = "목록 없음";
return result;
}
//검색 결과 학인
String show(int[] arIndex) {
String result = "항공사, 항공기 번호, 최대승객수, 출발지, 도착지\n";
for(int i=0; i< arIndex.length; i++) {
result += "😍";
for(int j=0; j<cLength; j++) {
result += arrPlane[arIndex[i]][j];
result += j == cLength -1 ? "" : ", ";
}
result += "\n";
}
if(arIndex.length == 0) result = "검색 결과 없음";
return result;
}
}
public static void main(String[] args) {
//전역 변수는 new 를 만나면 메모리에서 해제된다.(사라진다)
//단 , static변수는 프로그램이 종류될때 메모리에서 헤재된,사라진다
AMSField af = new AMSField();
//이미지 넣는 방법
ImageIcon img = new ImageIcon("src/img/air.png");
//절대경로 : 내 위치가 어디든지 찾아갈 수 있는 경로
//상대경로 :내 위치에 따라서 변경되는 경로
String[] arPlane = new String[5];
String keyword = "";
String[] menu = {"추가하기", "검색하기","수정하기","삭제하기","목록보기"};
String[] searchMenu = {"항공사", "항공기 번호", "최대승객수", "출발지", "도착지"};
String[] updateMenu = {"출발지 수정", "도착지 수정"};
int choice =0;
int index =0;//다른 영역에서도 index가 쓰이니깐 전역변수로 int index를 선언한다.
String insertMsg ="[추가하실 정보를 그대로 입력해주세요(, 포함)]\n"+ "항공사, 항공기 번호, 최대승객수, 출발지,도착지";
String searchMsg= "검색하실 항공사를 입력하세요\n";
String deleteMsg = "삭제하실 항공기 번호를 입력하세요\n";
String upDateMsg = "수정하실 항공기 번호를 입력하세요\n";
while(true) {
choice=JOptionPane.showOptionDialog(null, "", "AMS", JOptionPane.DEFAULT_OPTION,JOptionPane.PLAIN_MESSAGE,img, menu, null);
if(choice == -1)break;
switch(choice){
//추가하기 영역
case 0:
arPlane = JOptionPane.showInputDialog(insertMsg).split(", ");
af.insert(arPlane);
break;
//검색하기
case 1:
index =JOptionPane.showOptionDialog(null, "", "AMS", JOptionPane.DEFAULT_OPTION,
JOptionPane.PLAIN_MESSAGE, img, searchMenu, null);
if(index != -1) {
keyword=JOptionPane.showInputDialog("검색하실" + searchMenu[index] + "을(를) 입력하세요");
JOptionPane.showInputDialog(null, af.search(keyword, index));
}
break;
//수정하기
case 2:
String planeNum = JOptionPane.showInputDialog(upDateMsg);
String temp = af.search(planeNum, 1);
if(temp .equals("검색 결과 없음")) {
JOptionPane.showMessageDialog(null, "수정 실패");
}else {
index = JOptionPane.showOptionDialog(null, "", "AMS", JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, img, updateMenu, null);
String newValue = JOptionPane.showInputDialog("새로운" + updateMenu[index] + "입력하세요");
af.update(index, newValue);
}
break;
//삭제하기영역
case 3:
keyword=JOptionPane.showInputDialog(deleteMsg);
if(af.delete(keyword)) {
JOptionPane.showMessageDialog(null, "삭제 완료");
}else {
JOptionPane.showMessageDialog(null, "삭제 실패");
}
break;
//목록보기영역
case 4:
JOptionPane.showInputDialog(null,af.show());
break;
}
}
}
}