java
Java/Def of MVC Pattern/Basic Address Management System
MyaZ
2020. 1. 25. 22:21
MVC Pattern
Model
:The central component of the pattern. It is the application's dynamic data structure, independent of the user interface. It directly manages the data, logic and rules of the application.
View
:Any representation of information such as a chart, diagram or table. Multiple views of the same information are possible, such as a bar chart for management and a tabular view for accountants.
Controller
:Accepts input and converts it to commands for the model or view.
Model: ๋ฐ์ดํฐ ๊ด๋ฆฌ, ์ฒ๋ฆฌ
View: ํ๋ฉด(์ฌ์ฉ์์์ ์ธํฐํ์ด์ค)
Controller: Model - View์ฌ์ด๋ฅผ ์ฐ๊ฒฐ, ์
๋ฌด ์ฒ๋ฆฌ
package practice;
import java.util.*;
/*<์ฃผ์๋ก ํ๋ก์ ํธ>
๊ธฐ๋ฅ ์ ์
0) ๋ฉ๋ด ๋ฐ๋ณต ๋ฃจํ
void mainLoop();
1) ๋ฉ๋ด ์ถ๋ ฅ
void printMenu();
2) ๋ฉ๋ด ์ ํ
int getSelectMenu();
3) ์ฃผ์ ์ ๋ณด ์
๋ ฅ(์ด๋ฆ, ์ ํ๋ฒํธ, ์ฃผ์)
void inputAddress();
4) ์ฃผ์ ์ ๋ณด ๊ฒ์(์ด๋ฆ์ผ๋ก ๊ฒ์)
void searchAddss();
5) ์ฃผ์ ์ ๋ณด ๋ณ๊ฒฝ(์ด๋ฆ์ผ๋ก ๊ฒ์ ํ ๋ณ๊ฒฝ)
void updateAddress();
6) ์ฃผ์ ์ ๋ณด ์ญ์ (์ด๋ฆ์ผ๋ก ๊ฒ์ ํ ์ญ์ )
void deleteAddress();
7) ์ฃผ์ ์ ๋ณด ์ ์ฒด์ถ๋ ฅ
void printAllAddress();
8) ํ๋ก๊ทธ๋จ ์ข
๋ฃ
void programExit();
*/
class Address {
public String name;
public String number;
public String address;
}
public class _40 {
static Scanner sc = new Scanner(System.in); // ์ค์บ๋ ์ ์ธ
static ArrayList<Address> addrList = new ArrayList<Address>(); // ArrayList ๊ฐ์ฒด์ธ addrList ์ ์ธ
public static void main(String[] args) {
mainLoop();
}
// ๋ฉ๋ด ๋ฐ๋ณต ๋ฃจํ
static void mainLoop() {
while (true) {
printMenu();
int sel = selectMenu(); // ์
๋ ฅํ ๋ฉ๋ด์ ๋ฒํธ๋ฅผ selectMenu๋ก๋ถํฐ ์ ๋ฌ ๋ฐ๋๋ค
switch (sel) {
case 1:
inputAddress();
break;
case 2:
searchMenu();
break;
case 3:
updateAddress();
break;
case 4:
deleteAddress();
break;
case 5:
allClearAddress();
break;
case 6:
printAllAddress();
break;
case 7:
programExit();
break;
default:
System.out.println("์๋ชป์
๋ ฅํ์์ต๋๋ค");
break;
}
}
}
// ๋ฉ๋ด ์ถ๋ ฅ
static void printMenu() {
System.out.println("==============");
System.out.println("1. ์ฃผ์ ์
๋ ฅ");
System.out.println("2. ์ฃผ์ ๊ฒ์");
System.out.println("3. ์ฃผ์ ์์ ");
System.out.println("4. ์ฃผ์ ์ญ์ ");
System.out.println("5. ์ฃผ์ ์ ์ฒด ์ญ์ ");
System.out.println("6. ์ฃผ์ ์ ์ฒด ์ถ๋ ฅ");
System.out.println("7. ํ๋ก๊ทธ๋จ ์ข
๋ฃ");
System.out.println("==============");
}
// ๋ฉ๋ด ์ ํ
static int selectMenu() {
int sel;
System.out.println("๋ฒํธ ์
๋ ฅ >> ");
sel = sc.nextInt(); // ์
๋ ฅํ ๋ฒํธ๋ฅผ sel์ ์ ์ฅ
return sel;
}
// ์ฃผ์ ์ ๋ณด ์
๋ ฅ
static void inputAddress() {
System.out.println("์ฃผ์๋ฅผ ์
๋ ฅํฉ๋๋ค");
// ์ด๋ฆ ๋ฒํธ ์ฃผ์
System.out.print("์ด ๋ฆ : ");
String name = sc.next();
System.out.print("์ ํ๋ฒํธ : ");
String number = sc.next();
System.out.print("์ฃผ ์ : ");
String address = sc.next();
// Address ํด๋์ค์ ๊ฐ์ฒด๋ฅผ ์์ฑํด
// name, number, address๋ฅผ ์ฌ์ฉํ๋ค
Address addr = new Address();
addr.name = name;
addr.number = number;
addr.address = address; // ์
๋ ฅ๋ฐ์ ์ ๋ณด๋ฅผ Address ํด๋์ค์ ์ ๋ณด๋ก ๋๊ฒจ์ค๋ค
addrList.add(addr); // ์
๋ ฅ๋ฐ์ ์ ๋ณด๋ฅผ addr๋ฆฌ์คํธ์ ๋ฃ์ด์ค๋ค (์ ์ฅ์๋ฃ)
}
// ๋ฉ๋ด ๊ฒ์
static void searchMenu() {
System.out.println("๊ฒ์ํ ์ด๋ฆ ์
๋ ฅ");
String name = sc.next();
for (int i = 0; i < addrList.size(); i++) { // addrList.size()๋ addrList์ ์ ์ฅ๋ ๊ฐ์ ๊ฐ์
Address addr = addrList.get(i); // addrList.get(i)๋ addrList์ ์ ์ฅ๋ i๊ฐ์ ๊ฐ์ ธ์จ
if (name.equals(addr.name)) { // ๋ง์ฝ ๊ฒ์ํ ์ด๋ฆ๊ณผ ๋ฐฐ์ด์ ์ ์ฅ๋ ์ด๋ฆ ์ค ๊ฐ์ ์ด๋ฆ์ด ์์ ๊ฒฝ์ฐ ์๋์ ์ ๋ณด๋ฅผ ์ถ๋ ฅํ๋ค
System.out.println("======================");
System.out.println("์ด ๋ฆ : " + addr.name);
System.out.println("์ ํ๋ฒํธ : " + addr.number);
System.out.println("์ฃผ ์ : " + addr.address);
System.out.println("======================");
break;
}
}
}
// ์ฃผ์ ์ ๋ณด ์์
static void updateAddress() {
System.out.println("์์ ํ ์ด๋ฆ ๊ฒ์");
String name = sc.next();
for (int i = 0; i < addrList.size(); i++) {
Address addr = addrList.get(i);
if (name.equals(addr.name)) {
// ์์ ํ ์ด๋ฆ์ new๋ฅผ ๋ถ์ฌ์ค ๊ตฌ๋ถํ ์ ์๊ฒ ๋ง๋ค์ด์ค
System.out.println("์์ ํ ์ด๋ฆ ์
๋ ฅ");
String newName = sc.next();
System.out.println("์์ ํ ๋ฒํธ ์
๋ ฅ");
String newNumber = sc.next();
System.out.println("์์ ํ ์ฃผ์ ์
๋ ฅ");
String newAddress = sc.next();
// new๊ฐ ๋ถ์ ์ ๋ณด๋ Addressํด๋์ค์ ์ ๋ณด๋ก ๋ค์ ์ ์ฅํ๋ค (๊ฐ์ด ๋ณ๊ฒฝ(์์ )๋์ผํ๊ธฐ ๋๋ฌธ)
addr.name = newName;
addr.number = newNumber;
addr.address = newAddress;
break;
}
}
}
// ์ฃผ์ ์ ๋ณด ์ญ์
static void deleteAddress() {
System.out.println("์ญ์ ํ ์ด๋ฆ ์
๋ ฅ");
String name = sc.next();
System.out.println("์ ๋ง ์ญ์ ํ์๊ฒ ์ต๋๊น?");
System.out.println(" Y / N ");
String st = sc.next();
if (st.equalsIgnoreCase("Y")) { // ๋์๋ฌธ์ ๊ตฌ๋ถ ์์ด y๋ฅผ ์
๋ ฅ ์
//.isEmpty()
if(addrList.isEmpty()) { //addrList.size(null)์ ๋ง๋ค์ด์ผํจ
// if(addrList.size()<1) {
System.out.println("์ ์ฅ๋์ง ์์ ๊ฐ์
๋๋ค");
}
for (int i = 0; i < addrList.size(); i++) {
Address addr = addrList.get(i);
if (name.equals(addr.name)) {
addrList.remove(i); // addrList์์ ์
๋ ฅํ ์ด๋ฆ๊ณผ ๊ฐ์ ์ด๋ฆ์ ์ฐพ์ ๊ฒฝ์ฐ ์ญ์ ๋ฅผ ํด์ค๋ค
System.out.println("์ญ์ ๋์์ต๋๋ค");
break;
}
}
}
else if (st.equalsIgnoreCase("N")) { // ๋์๋ฌธ์ ๊ตฌ๋ถ ์์ด n์ ์
๋ ฅ ์
System.out.println("์ญ์ ๋ฅผ ์ทจ์ํ์์ต๋๋ค"); // ์๋ฌด ์ผ์ด ์ผ์ด๋์ง ์์ผ๋ฉฐ "์ญ์ ๋ฅผ ์ทจ์ํ์์ต๋๋ค" ๋ฌธ๊ตฌ ์ถ๋ ฅ
} else {
System.out.println("์๋ชป์
๋ ฅ๋์์ต๋๋ค"); // else๋ y,n์ ์ ์ธํ ๋ค๋ฅธ ์
๋ ฅ์ ๋ฐ์์๋ "์๋ชป์
๋ ฅ๋์์ต๋๋ค" ๋ฌธ๊ตฌ ์ถ๋ ฅ
}
}
// ์ฃผ์ ์ ์ฒด ์ญ์
static void allClearAddress() {
System.out.println("์ ๋ง ์ ์ฒด ์ญ์ ํ์๊ฒ ์ต๋๊น? \n Y / N");
String st = sc.next();
if (st.equalsIgnoreCase("y")) {
addrList.clear(); // addrList.clear(); ๋ addrList์ ๋ชจ๋ ๊ฐ์ ์ญ์ ํด์ค๋ค
System.out.println("์ ์ฒด ์ญ์ ๋ฅผ ๋์์ต๋๋ค");
} else if (st.equalsIgnoreCase("n")) {
System.out.println("์ ์ฒด ์ญ์ ๋ฅผ ์ทจ์ํ์์ต๋๋ค");
} else {
System.out.println("์๋ชป์
๋ ฅ๋์์ต๋๋ค");
}
}
// ์ฃผ์ ์ ์ฒด ์ถ๋ ฅ
static void printAllAddress() {
System.out.println("์ ์ฒด ์ถ๋ ฅํฉ๋๋ค");
for (int i = 0; i < addrList.size(); i++) {
Address addr = addrList.get(i); // addrList์ ์ ์ฅ๋ ๋ชจ๋ ๊ฐ์ ๊ฐ์ ธ์ ์๋ ์์์ผ๋ก ์ถ๋ ฅํด์ค๋ค
System.out.println("======================");
System.out.println("์ด ๋ฆ : " + addr.name);
System.out.println("์ ํ๋ฒํธ : " + addr.number);
System.out.println("์ฃผ ์ : " + addr.address);
System.out.println("======================");
}
}
// ํ๋ก๊ทธ๋จ ์ข
๋ฃ
static void programExit() {
System.out.println("ํ๋ก๊ทธ๋จ์ ์ข
๋ฃํฉ๋๋ค");
System.exit(0); // 0์ ์ ์์ข
๋ฃ / 0์ด ์๋๋ฉด ๋น์ ์์ข
๋ฃ
}