우리는 Airplain Manager System(비행관리시스템)을 만들어 보면서 비행계획을 추가해보고 또 추가된 비행계획을 검색, 수정, 삭제를 할 것이다. 또한 목록 전체보기도 만들어 초기 비행관리시스템의 형태를 만들어 볼 것이다.
우리는 코드를 작성 전에는 무엇을 만들지 밑그림을 작성해보아야 코드를 작성하면서 목표를 이룰 수 있다.
우리가 만들고자 하는 것은 비행계획 추가하기, 검색, 수정, 삭제, 목록보기와 같이 5개의 옵션을 만들고 이 5가지의 옵션을 자세히 살표보면 다음과 같다.
1. 추가하기
추가하기를 누르면 문자열로 차례로 항공사, 항공기번호, 승객수, 출발지, 도착지를 작성하도록 한다. 작성이 끝나면 메뉴로 돌아간다.
2. 검색하기
항공기번호 또는 항공사, 각각의 추가한 옵션별로 검색이 되도록 한다.
3. 수정하기
항공기번호는 고유의 번호이니 항공기번호를 가지고 수정할 수 있도록한다. 수정은 출발지, 목적지 2개만 수정하게 하며 이는 현실에서 항공사, 항공기번호, 승객수는 항공기의 고유 특징으로 변경할 수 없다는 것을 반영한 것이다.
4. 삭제하기
마찬가지로 삭제하기는 항공기번호로 이루어지게 한다. 삭제하기를 누르면 삭제할 항공기번호를 누르고 삭제된 자리는 다음 항공기 배열이 덮어씌우는 방식으로 한다.
5. 목록보기
목록보기를 누르면 모든 비행계획을 볼 수 있도록 한다.
또한 상단에는 항공기, 항공기번호와 같이 비행 정보를 표기해준다.
먼저 초기화면에 추가하기~목록보기라는 옵션을 보여줘야 함으로 joption.Option을 사용하여 각 선택 옵션을 직관적으로 보여주게 할 것이다. 또한 옵션을 선택하면 각 옵션버튼은 정수인 0~옵션수 만큼을 리턴하니 이 숫자를 담아낼 공간을 만들고 이 숫자를 각각 switch문에 담아 각 옵션 버튼별 기능을 구현하면 될 것이다.
또한 이 기능들은 메소드를 따로 담고있는 클래스로 만들 것이다.
ams 패키지 안에 2개의 클래스로 만들어 하나는 메소드를 모아두는 필드, 하나는 기능을 실행하는 메인메소드부분으로 만들 것이다.
비행계획은 누적되어 입력 받아야되니 2차원 배열을 사용하는 것이 적합하겠다. 배열의 길이는 넉넉하게 [100][5]로 설정하면 부족하지는 않겠다.
우리는 입력을 하면 콤마로 구분되기에 split을 사용해야 할 것이다.
검색결과는 따로 저장공간을 만들어 보여주는 것이 좋겠다.
이제 코드를 작성해보록 하자
클래스는 2개의 클래스를 만들고 하나는 AmsMain, 다른 하나는 AmsField를 만들어 저장한다.
1. 우리가 원하는 옵션을 띄울때 필요한 것은 JOptionPane.showOptionDialog이다. 각 요소별로 들어가는 것은 링크 참고 JOptionPane 사용방법 보러가기
우리는 초기화면에서 움직이는 이미지를 보여주고 싶기때문에 비행기그림을 원하는 경로에 다운받아 저장하였다. 먼저 Image를 사용하기 위해 ImageIcon img = new ImageIcon()으로 초기화 하였고 괄호안에는 "경로/main.gif"를 적어두었다. 이제 이미지는 img로 사용 할 수 있다.
또한 옵션은 5개로 5개의 1차원배열로 만들어 사용하였다.
JOption은 숫자를 리텅함으로 그 숫자를 담기위해 저장공간을 만들었다.
옵션을 선택할 때마다 종료되므로 while로 감싸 반복시켰고 x버튼을 누르면 꺼지도록 if문을 만들고 switch문으로 숫자를 입력 받아 작동 할 수 있게 세팅하였다.
public static void main(String[] args) {
ImageIcon img = new ImageIcon("src/img/main.gif");
String [ ] mainMenu = {"추가하기", "검색하기", "수정하기", "삭제하기","목록보기"};
int choice = 0;
while(true) {
choice = JOptionPane.showOptionDialog(null, "", "MyAMS", JOptionPane.DEFAULT_OPTION,
JOptionPane.PLAIN_MESSAGE, img, mainMenu, null);
if(choice == -1) break;
switch(choice) {
case 0 :
break;
case 1 :
break;
case 2 :
break;
case 3 :
break;
case 4 :
break;
}
}
}
public class AmsField {
//추가하기
void insert() { }
//검색하기
void search() { }
//수정하기
void update() { }
//삭제하기
void delete() { }
//목록보기
void show() { }
}
main클래스
AmsField af = new AmsField();
String insertMsg = "정보를 그대로 입력하세요[,콤마도 포함]\n 항공사, 항공기번호, 승객수, 출발지, 목적지";
String [] arPlain = new String [5];
while(true) {
switch(choice) {
case 0 :
arPlain = JOptionPane.showInputDialog(insertMsg).split(",");
af.insert(arPlain);
break;
Field 클래스
String [][] arrPlain = new String[100][5];
int cnt;
//추가하기
void insert(String[] arPlain) {
arrPlain[cnt] = arPlain;
cnt ++;
}
String result = "";
String show() {
result = "항공사, 항공기번호, 승객수, 출발지, 목적지\n";
for (int i = 0; i < cnt; i++) {
for (int j = 0; j < arrPlain[0].length; j++) {
result += j == arrPlain[0].length-1 ? arrPlain[i][j] : arrPlain[i][j]+",";
}
}
if(cnt == 0 ) result = "목록없음";
return result;
}
case 4 :
JOptionPane.showMessageDialog(null, af.show());
break;
String search(String keyword) {
String result = "";
int searchCnt = 0;
int [] arIndex;
for (int i = 0; i < cnt; i++) {
if(keyword.equals(arrPlain[i][1])) {
searchCnt ++;
result += ""+i+",";
}
}
arIndex = new int[searchCnt];
for (int i = 0; i < arIndex.length; i++) {
arIndex[i] = Integer.parseInt(result.split(",")[i]);
}
String show(int [] arIndex) {
String result = "항공사, 항공기번호, 승객수, 출발지, 목적지\n";
for (int i = 0; i < arIndex.length; i++) {
for (int j = 0; j < arrPlain[0].length; j++) {
result += j == arrPlain[0].length-1 ? arrPlain[arIndex[i]][j] : arrPlain[arIndex[i]][j]+",";
}
result += "\n";
}
if(cnt == 0 ) result = "목록없음";
return result;
}
case 1 :
keyword = JOptionPane.showInputDialog("검색하실 항공기번호를 입력하세요");
JOptionPane.showMessageDialog(null, af.search(keyword));
break;
void delete(String keyword{
if(keyword.equals(arrPlain[i][1])) {
arrPlain[i]=arrPlain[i+1];
}
}
for (int i = 0; i < cnt; i++) {
if(keyword.equals(arrPlain[i][1])) {
arrPlain[i]=arrPlain[i+1];
}
}
void delete(String keyword{
for (int i = 0; i < cnt; i++) {
if(keyword.equals(arrPlain[i][1])) {
arrPlain[i]=arrPlain[i+1];
cnt--;
}
}
}
boolean delete(String keyword) {
boolean deleteCheck = false;
for (int i = 0; i < cnt; i++) {
if(keyword.equals(arrPlain[i][1])) {
deleteCheck = true;
arrPlain[i]=arrPlain[i+1];
cnt--;
}
}
return deleteCheck;
}
keyword = JOptionPane.showInputDialog("삭제하실 항공기번호를 입력하세요");
keyword = JOptionPane.showInputDialog("삭제하실 항공기번호를 입력하세요");
if(af.delete(keyword)){
}
keyword = JOptionPane.showInputDialog("삭제하실 항공기번호를 입력하세요");
if(af.delete(keyword)){
JOptionPane.showMessageDialog(null, af.show());
}else{
JOptionPane.showMessageDialog(null, "삭제하실 목록이 없습니다.");
}
arrPlain[검색된 i][수정할 열] = 새로운 값;
main
keyword = JOptionPane.showInputDialog("수정하실 항공기번호를 입력하세요");
if(af.search(keyword).equals("목록없음")) {
JOptionPane.showMessageDialog(null, "수정하실 비행계획이 없습니다.");
}else {
}
Field
int btnIndex = 0;
search 메서드에 추가
for (int i = 0; i < cnt; i++) {
if(keyword.equals(arrPlain[i][1])) {
**btnIndex = i;**
searchCnt ++;
result += ""+i+",";
void update() {
arrPlain[btnIndex][수정하고자 하는 열] = 새로운 값;
}
String [] update = {"출발지 수정", "목적지 수정"};
String newValue = "";
..생략..
else {
index = JOptionPane.showOptionDialog(null, "", "수정하기", JOptionPane.DEFAULT_OPTION,
JOptionPane.PLAIN_MESSAGE, img, update, null);
newValue = JOptionPane.showInputDialog("수정하시는 값을 입력하세요");
if(newValue.length() <= 0) continue;
af.update(index, newValue);
JOptionPane.showMessageDialog(null, "수정완료");
}
void update(int index, String newValue) {
arrPlain[btnIndex][index+3] = newValue;
}
field
package ams;
public class AmsField {
String [][] arrPlain = new String[100][5];
int cnt;
String result = "";
int btnIndex;
//추가하기
void insert(String[] arPlain) {
arrPlain[cnt] = arPlain;
cnt ++;
}
//검색하기
String search(String keyword) {
String result = "";
int searchCnt = 0;
int [] arIndex;
for (int i = 0; i < cnt; i++) {
if(keyword.equals(arrPlain[i][1])) {
btnIndex = i;
searchCnt ++;
result += ""+i+",";
}
}
arIndex = new int[searchCnt];
for (int i = 0; i < arIndex.length; i++) {
arIndex[i] = Integer.parseInt(result.split(",")[i]);
}
return show(arIndex);
}
//수정하기
void update(int index, String newValue) {
arrPlain[btnIndex][index+3] = newValue;
}
//삭제하기
boolean delete(String keyword) {
boolean deleteCheck = false;
for (int i = 0; i < cnt; i++) {
if(keyword.equals(arrPlain[i][1])) {
deleteCheck = true;
arrPlain[i]=arrPlain[i+1];
cnt--;
}
}
return deleteCheck;
}
//목록보기
String show() {
String result = "항공사, 항공기번호, 승객수, 출발지, 목적지\n";
for (int i = 0; i < cnt; i++) {
for (int j = 0; j < arrPlain[0].length; j++) {
result += j == arrPlain[0].length-1 ? arrPlain[i][j] : arrPlain[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++) {
for (int j = 0; j < arrPlain[0].length; j++) {
result += j == arrPlain[0].length-1 ? arrPlain[arIndex[i]][j] : arrPlain[arIndex[i]][j]+",";
}
result += "\n";
}
if(cnt == 0 ) result = "목록없음";
return result;
}
}
main
package ams;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
public class AmsMain {
public static void main(String[] args) {
AmsField af = new AmsField();
ImageIcon img = new ImageIcon("src/img/main.gif");
String [ ] mainMenu = {"추가하기", "검색하기", "수정하기", "삭제하기","목록보기"};
int choice = 0;
String insertMsg = "정보를 그대로 입력하세요[,콤마도 포함]\n 항공사, 항공기번호, 승객수, 출발지, 목적지";
String [] arPlain = new String [5];
String keyword = "";
int index = 0;
String [] update = {"출발지 수정", "목적지 수정"};
//String updateMsg = "수정을 원하시는 것을 선택하세요.";
String newValue = "";
while(true) {
choice = JOptionPane.showOptionDialog(null, "", "MyAMS", JOptionPane.DEFAULT_OPTION,
JOptionPane.PLAIN_MESSAGE, img, mainMenu, null);
if(choice == -1) break;
switch(choice) {
case 0 :
arPlain = JOptionPane.showInputDialog(insertMsg).split(",");
af.insert(arPlain);
break;
case 1 :
keyword = JOptionPane.showInputDialog("검색하실 항공기번호를 입력하세요");
JOptionPane.showMessageDialog(null, af.search(keyword));
break;
case 2 :
keyword = JOptionPane.showInputDialog("수정하실 항공기번호를 입력하세요");
if(af.search(keyword).equals("목록없음")) {
JOptionPane.showMessageDialog(null, "수정하실 비행계획이 없습니다.");
}else {
index = JOptionPane.showOptionDialog(null, "", "수정하기", JOptionPane.DEFAULT_OPTION,
JOptionPane.PLAIN_MESSAGE, img, update, null);
newValue = JOptionPane.showInputDialog("수정하시는 값을 입력하세요");
if(newValue.length() <= 0) continue;
af.update(index, newValue);
JOptionPane.showMessageDialog(null, "수정완료");
}
break;
case 3 :
keyword = JOptionPane.showInputDialog("삭제하실 항공기번호를 입력하세요");
if(af.delete(keyword)) {
JOptionPane.showMessageDialog(null, af.show());
}else {
JOptionPane.showMessageDialog(null, "삭제하실 목록이 없습니다.");
}
break;
case 4 :
JOptionPane.showMessageDialog(null, af.show());
break;
}
}
}
}