๐Ÿ’ป

Java ํ”„๋กœ๊ทธ๋ž˜๋ฐ - MVC ํŒจํ„ด ๋ณธ๋ฌธ

KITRI/JAVA

Java ํ”„๋กœ๊ทธ๋ž˜๋ฐ - MVC ํŒจํ„ด

๋˜ํšจ๋‹ˆ 2020. 5. 15. 14:16

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

 

 

 

 

๋ฐ˜์‘ํ˜•
Comments