โ AmsField
package ams;
public class AmsField {
// Key
// ํญ๊ณต์ฌ, ํญ๊ณต๊ธฐ๋ฒํธ, ์ต๋์น๊ฐ์, ์ถ๋ฐ์ง, ๋์ฐฉ์ง
String[][] arrPlane = new String[100][5];
int insertCnt;
int updateIndex;
// ์ถ๊ฐ
// 2์ฐจ์ ๋ฐฐ์ด์ 5๊ฐ์ ์ ๋ณด๋ฅผ ๋ด๊ธฐ
void insert(String[] arPlane) {
arrPlane[insertCnt] = arPlane;
insertCnt++;
}
// ์์
void update(int index, String newValue) {
boolean updateCheck = true;
//ํญ๊ณต์ฌ, ํญ๊ณต๊ธฐ๋ฒํธ, ์น๊ฐ์, ์ถ๋ฐ์ง, ๋์ฐฉ์ง
//0 1 2 3 4
//index : 0, 1
arrPlane[updateIndex][index+3] = newValue;
}
// ์ญ์
void delete() {
}
// ๊ฒ์
String select(int index, String keyword) {
int[] arIndex = null;
int searchCnt = 0;
String result = "";
updateIndex = -1;
for (int i = 0; i < insertCnt; i++) {
if (keyword.equals(arrPlane[i][index])) {
// i >> ํ๋ฒํธ : ๊ฒ์ํ ๋นํ๊ธฐ ๋ฒํธ
searchCnt++;
updateIndex = i;
//๋ฐ๋ก int๋ฐฐ์ด์ ๋ด์ ์ ์๊ธฐ ๋๋ฌธ์(๊ฒ์ ๊ฑด์๋ฅผ ๋ชจ๋ฅด๊ธฐ ๋๋ฌธ)
//๋ฌธ์์ด์ ๊ฒ์๋ ํ๋ฒํธ๋ฅผ ์ฐ๊ฒฐ์์ผ ๋ด๋๋ค.(", "๋ ๊ตฌ๋ถ์ ์ด๋ค.)
//๊ตฌ๋ถ์ ์ด ์์ด์ผ์ง๋ง ๋ฐ์์ ๊ฐ ๊ฐ์ ๋๋ ์ ์๊ธฐ ๋๋ฌธ์ด๋ค.
result += i + ", ";
}
}
//๊ฒ์ ๊ฑด์๋ฅผ for๋ฌธ์ด ๋๋ ํ์ ์ ์ ์๊ธฐ ๋๋ฌธ์
//for๋ฌธ ๋ฐ์์ new ํด์ค๋ค.
arIndex = new int[searchCnt];
for (int i = 0; i < arIndex.length; i++) {
// result๋ ๋ฌธ์์ด์ด๊ณ split() ์ฌ์ฉ์
//์ ์ฒด๋ฅผ ๋ฐฐ์ด๋ก ๋ณด๊ธฐ ๋๋ฌธ์ ๊ทธ ๋ค์ ๋ฐ๋ก []๋ฅผ ์ฌ์ฉํ ์ ์๋ค.
arIndex[i] = Integer.parseInt(result.split(", ")[i]);
}
//list(int[] arIndex)๋ฉ์๋์ ๊ฐ์ ์ ๋ฌํ๊ณ list(int[] arIndex)์์ ๋ฆฌํด๋
//๊ฒฐ๊ณผ๊ฐ์ select()์์ ๋ฆฌํดํ๋ค.
return list(arIndex);
}
// ๋ชฉ๋ก
String list() {
String result = "ํญ๊ณต์ฌ, ํญ๊ณต๊ธฐ๋ฒํธ, ์ต๋์น๊ฐ์(๋ช
), ์ถ๋ฐ์ง, ๋์ฐฉ์ง\n";
for (int i = 0; i < insertCnt; i++) {
result += "♥";
for (int j = 0; j < arrPlane[0].length; j++) {
result += arrPlane[i][j];
result += j == arrPlane[0].length - 1 ? "" : ", ";
}
result += "\n";
}
if (insertCnt == 0)
result = "๋ชฉ๋ก ์์";
return result;
}
String list(int[] arIndex) {
String result = "ํญ๊ณต์ฌ, ํญ๊ณต๊ธฐ๋ฒํธ, ์ต๋์น๊ฐ์(๋ช
), ์ถ๋ฐ์ง, ๋์ฐฉ์ง\n";
for (int i = 0; i < arIndex.length; i++) {
result += "♥";
for (int j = 0; j < arrPlane[0].length; j++) {
result += arrPlane[arIndex[i]][j];
result += j == arrPlane[0].length - 1 ? "" : ", ";
}
result += "\n";
}
if (arIndex.length == 0)
result = "๊ฒ์ ๊ฒฐ๊ณผ ์์";
return result;
}
}
โ AmsMain
package ams;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
public class AmsMain {
public static void main(String[] args) {
String title = "ํญ๊ณต๊ธฐ ๊ด๋ฆฌ ํ๋ก๊ทธ๋จ";
String[] menu = {"์ถ๊ฐํ๊ธฐ", "๊ฒ์ํ๊ธฐ", "์์ ํ๊ธฐ", "์ญ์ ํ๊ธฐ", "๋ชฉ๋ก๋ณด๊ธฐ"};
String[] updateMenu = {"์ถ๋ฐ์ง ์์ ", "๋์ฐฉ์ง ์์ "};
String[] updateMsg = {"์ถ๋ฐ์ง", "๋์ฐฉ์ง"};
AmsField af = new AmsField();
ImageIcon icon = new ImageIcon("src/img/main.gif");
int choice = 0;
String[] arPlane = new String[5];
String keyword = "";
while(true) {
choice = JOptionPane.showOptionDialog(null, "", title, JOptionPane.DEFAULT_OPTION,
JOptionPane.PLAIN_MESSAGE, icon, menu, null);
if(choice == -1) break;
switch(choice) {
//์ถ๊ฐ
//ํญ๊ณต์ฌ, ํญ๊ณต๊ธฐ๋ฒํธ, ์ต๋์น๊ฐ์(๋ช
), ์ถ๋ฐ์ง, ๋์ฐฉ์ง
case 0:
//5๊ฐ์ ๊ฐ์ ", "๋ก ๊ตฌ๋ถํ์ฌ ํ๋ฒ์ ์
๋ ฅํ๊ธฐ
//split("๊ตฌ๋ถ์ ")์ ๋ฆฌํดํ์
์ด ๋ฌธ์์ด ๋ฐฐ์ด์ด๋ค.
//๊ตฌ๋ถ์ ์ด ์๋ค๋ ๊ฒ์ ์ต์ํ ๊ฐ์ด 2๊ฐ ์ด์์ด๊ธฐ ๋๋ฌธ์ ๋ฐฐ์ด๋ก ๋ฆฌํดํ๋ค.
arPlane = ("" + JOptionPane.showInputDialog(null,
"ํญ๊ณต์ฌ, ํญ๊ณต๊ธฐ๋ฒํธ, ์ต๋์น๊ฐ์(๋ช
), ์ถ๋ฐ์ง, ๋์ฐฉ์ง", title, JOptionPane.PLAIN_MESSAGE,
icon, null, null)).split(", ");
af.insert(arPlane);
break;
//๊ฒ์
case 1:
break;
//์์
case 2:
String newValue = "";
choice =JOptionPane.showOptionDialog(null, "", title, JOptionPane.DEFAULT_OPTION,
JOptionPane.PLAIN_MESSAGE, icon, updateMenu, null);
if(choice == -1) break;
keyword = "" + JOptionPane.showInputDialog(null,
"์์ ํ์ค ํญ๊ณต๊ธฐ ๋ฒํธ๋ฅผ ์
๋ ฅํ์ธ์", title, JOptionPane.PLAIN_MESSAGE,
icon, null, null);
if(af.select(1, keyword).equals("๊ฒ์ ๊ฒฐ๊ณผ ์์")) {
JOptionPane.showMessageDialog(null, "์์ ์คํจ");
}else {
newValue = "" + JOptionPane.showInputDialog(null,
"์๋ก์ด " + updateMsg[choice] + "๋ฅผ ์
๋ ฅํ์ธ์", title, JOptionPane.PLAIN_MESSAGE,
icon, null, null);
af.update(choice, newValue);
JOptionPane.showMessageDialog(null, "์์ ์ฑ๊ณต");
}
break;
//์ญ์
case 3:
break;
//๋ชฉ๋ก
case 4:
JOptionPane.showMessageDialog(null, af.list());
break;
}
}
}
}
'๐ > JAVA Programming' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ชจ๋ ์๋ฐ ์ธ ์ก์ ] ch11. null ๋์ Optional ํด๋์ค (0) | 2021.11.26 |
---|---|
2019.08.31 day07 ์์ฑ์, ์ค๋ฒ ๋ก๋ฉ (Car , Road) (0) | 2019.10.01 |
eclipse ๋จ์ถํค๋ ์์๋๋ฉด ์ข์ ๋ ์๋ค~ (0) | 2019.09.21 |