๐ป
Java ํ๋ก๊ทธ๋๋ฐ - MVC ํจํด ๋ณธ๋ฌธ
MVC - Model(Oracle, MSSQL, MySQL / RDBMS)
- View(HTML, CSS)
- Controller
์์ ํ MVCํจํด์ ์๋์ง๋ง ๊ฐ๋ ์ ์ก๊ณ ๊ฐ์.
์์ ) ์ํ์์คํ
Account.java -> ๊ณ ๊ฐ ๊ณ์ข(๊ณ์ข๋ฒํธ, ์ด๋ฆ, ์์ก) / ๋ฐ์ดํฐ๋ง ๋ฃ์๋ค ๋บ๋ค(DTO)
package api11.MVC.Bank;
/**
* @Author : ๊น์ํ
* @Date : 2020. 5. 15.
* @Description : MVC - Model(Oracle, MSSQL, MySQL / RDBMS)
* - View
* - Controller
*/
public class Account {
private int id; //๊ณ์ข๋ฒํธ
private String name; //์ด๋ฆ
private long balance; //์์ก
public Account() {}
public Account(int id, String name, long balance) {
super();
this.id = id;
this.name = name;
this.balance = balance;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public long getBalance() {
return balance;
}
public void setBalance(long balance) {
this.balance = balance;
}
@Override
public String toString() {
return "Account [id=" + id + ", name=" + name + ", balance=" + balance + "]";
}
}
DTO(Data Transfer Object)๋ VO(Value Object)๋ก ๋ฐ๊ฟ ๋งํ ์ ์๋๋ฐ ๊ณ์ธต๊ฐ ๋ฐ์ดํฐ ๊ตํ์ ์ํ ์๋ฐ๋น์ฆ๋ฅผ ๋งํ๋ค.
์ฌ๊ธฐ์ ๋งํ๋ ๊ณ์ธต๊ฐ์ Controller, View, Business Layer, Persistent Layer๋ฅผ ๋งํ๋ฉฐ ๊ฐ ๊ณ์ธต๊ฐ ๋ฐ์ดํฐ ๊ตํ์ ์ํ ๊ฐ์ฒด๋ฅผ DTO ๋๋ VO ๋ผ๊ณ ๋ถ๋ฅธ๋ค. ์ผ๋ฐ์ ์ผ๋ก VO ๋ DTO ์ ๋์ผํ ๊ฐ๋ ์ด์ง๋ง read only ์์ฑ์ ๊ฐ์ง๋ค.
์ผ๋ฐ์ ์ธ DTO๋ ๋ก์ง์ ๊ฐ๊ณ ์์ง ์๋ ์์ํ ๋ฐ์ดํฐ ๊ฐ์ฒด์ด๋ฉฐ ์์ฑ๊ณผ ๊ทธ ์์ฑ์ ์ ๊ทผํ๊ธฐ ์ํ getter, setter ๋ฉ์๋๋ง ๊ฐ์ง ํด๋์ค๋ฅผ ๋งํ๋ค.
AccountManager.java -> ๊ณ์ข ๊ณ์ค / (DAO)
package api11.MVC.Bank;
import java.util.ArrayList;
import java.util.Scanner;
public class AccountManager {
private ArrayList<Account> list;//DB์ฐ๋ ์ ์ด์ด์ list๋ฅผ ํ์ฉ
private Scanner sc;
public AccountManager() {
list = new ArrayList<Account>();
sc = new Scanner(System.in);
}
public void makeAccount() { //๊ณ์ข๊ฐ์ค
Account account = new Account();
Scanner sc = new Scanner(System.in);
System.out.print("๊ณ์ข๋ฒํธ:");
account.setId(sc.nextInt());
System.out.print("์ด๋ฆ:");
account.setName(sc.next());
System.out.print("์
๊ธ์ก:");
account.setBalance(sc.nextLong());
list.add(account);
System.out.println("๊ณ์ข๊ฐ ๊ฐ์ค๋์์ต๋๋ค.");
System.out.println(list.toString() +"\t");
}
public void deposit() { //์
๊ธ
System.out.print("๊ณ์ข๋ฒํธ:");
int id = sc.nextInt();
System.out.print("์
๊ธ์ก:");
long money = sc.nextLong();
for(int i=0; i<list.size(); i++) {
Account account = list.get(i);
if(account.getId()==id) {
long balance = account.getBalance();
money += balance;
account.setBalance(money);
System.out.println("์
๊ธ์๋ฃ๋์์ต๋๋ค.");
return;
}
}
System.out.println("ํด๋น ๊ณ์ข๋ฒํธ๊ฐ ์กด์ฌํ์ง ์์ต๋๋ค.");
}
public void withdraw() { //์ถ๊ธ
System.out.print("๊ณ์ข๋ฒํธ:");
int id = sc.nextInt();
System.out.print("์ถ๊ธ์ก:");
long money = sc.nextLong();
for(int i=0; i<list.size(); i++) {
Account account = list.get(i);
if(account.getId()==id) {
if(account.getBalance() < money) {
System.out.println("์์ก์ด ๋ถ์กฑํฉ๋๋ค.");
return;
}else {
long balance = account.getBalance();
money = balance - money;
account.setBalance(money);
System.out.println("์ถ๊ธ์๋ฃ๋์์ต๋๋ค.");
return;
}
}
}
System.out.println("ํด๋น ๊ณ์ข๋ฒํธ๊ฐ ์กด์ฌํ์ง ์์ต๋๋ค.");
}
public void inquire() { //์์ก์กฐํ
System.out.print("๊ณ์ข๋ฒํธ:");
int id = sc.nextInt();
for(int i=0; i<list.size(); i++) {
Account account = list.get(i);
if(account.getId()==id) {
System.out.println(account.getId() + "\t" + account.getName() + "\t" + account.getBalance());
return;
}
}
System.out.println("ํด๋น ๊ณ์ข๋ฒํธ๊ฐ ์กด์ฌํ์ง ์์ต๋๋ค.");
}
public void disp() { //์ถ๋ ฅ
for(int i=0; i<list.size(); i++) {
Account account = list.get(i);
System.out.println(account.getId() + "\t" + account.getName() + "\t" + account.getBalance());
}
}
}
AccountView.java -> ๋ฉ์ธํ๋ฉด
package api11.MVC.Bank;
import java.util.Scanner;
public class AccountView {
public static void main(String[] args) {
AccountManager manager = new AccountManager();
Scanner sc = new Scanner(System.in);
while(true) {
printMenu();
System.out.print("์ ํ:");
int choice = sc.nextInt();
switch(choice) {
case 1:
manager.makeAccount();
break;
case 2:
manager.deposit();
break;
case 3:
manager.withdraw();
break;
case 4:
System.out.println("๊ณ์ข๋ฒํธ\t์ฑ๋ช
\t์์ก");
manager.inquire();
break;
case 5:
System.out.println("๊ณ์ข๋ฒํธ\t์ฑ๋ช
\t๊ธ์ก");
manager.disp();
break;
case 6:
System.out.println("์ข
๋ฃํฉ๋๋ค.");
sc.close();
return;
default:
System.out.println("์๋ชป๋๋ฅด์
จ์ต๋๋ค.\n๋ค์์ ํํด์ฃผ์ธ์.");
break;
}
}
}
public static void printMenu() {
System.out.println("============Menu============");
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();
}
}