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();
}
}