ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

PROGRAMMING

Model1 ๋ฐฉ์‹

๐Ÿ”ฅ fire 2010. 5. 14. 21:56

package jbank;
import java.io.*;

public class AccountMain {
 public static void main(String[] ar) throws NumberFormatException, IOException {
  AccountManager jbank = new AccountManager();
 
  BufferedReader in = new BufferedReader (new InputStreamReader (System.in));
 
  while(true) {
   jbank.printMenu();
   
   System.out.print("์„ ํƒ: ");
   int choice = Integer.parseInt(in.readLine());
   
   switch (choice) {
    case 1: jbank.makeAccount(); break; // ๊ณ„์ขŒ๊ฐœ์„ค    
    case 2: jbank.inMoney(); break;  // ์ž…๊ธˆ
    case 3: jbank.outMoney(); break; // ์ถœ๊ธˆ
    case 4: jbank.showMoney(); break; // ์ž”์•ก์กฐํšŒ
    case 5: System.out.println("๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค."); return;
    default: System.out.println("์„ ํƒ์„ ์ž˜๋ชปํ•˜์…จ์Šต๋‹ˆ๋‹ค.");
   }
  }
 }
}


==========================================================================================================================================

package jbank;
import java.io.*;

public class AccountManager {
 Account[] array = new Account[100];
 
 int index;
 
 BufferedReader in;
 
 public AccountManager() {
  index = 0;
 }
 
 public 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("====== Bye ======");
 }
 
 public void makeAccount() throws IOException { //๊ณ„์ขŒ๊ฐœ์„คํ•˜๊ธฐ
  in = new BufferedReader (new InputStreamReader (System.in));
 
  System.out.println("๊ณ„์ขŒ๊ฐœ์„ค ====== ");
  System.out.println("๊ณ„์ขŒ๋ฒˆํ˜ธ : ");
  int id = Integer.parseInt(in.readLine());
 
  System.out.println("์ด๋ฆ„ : ");
  String name = in.readLine();
 
  System.out.println("์ž…๊ธˆ์•ก : ");
  int balance = Integer.parseInt(in.readLine());
 
  array[index++] = new Account(id, balance, name);
 }
 
 public void inMoney() throws IOException { //์ž…๊ธˆํ•˜๊ธฐ
  in = new BufferedReader (new InputStreamReader (System.in));
 
  System.out.print("๊ณ„์ขŒ ID : ");
  int id = Integer.parseInt(in.readLine());
 
  System.out.println("์ž…๊ธˆ์•ก : ");
  int money = Integer.parseInt(in.readLine());
 
  for (int i = 0; i < index; i++) {
   if(array[i].getId() == id) {
    array[i].addMoney(money);
    System.out.println("์ž…๊ธˆ์™„๋ฃŒ!!!");
    return;
   }
  }
  System.out.println("์œ ํšจํ•˜์ง€ ์•Š์€ ID ์ž…๋‹ˆ๋‹ค.");
 }
 
 public void outMoney() throws IOException { //์ถœ๊ธˆํ•˜๊ธฐ
  in = new BufferedReader (new InputStreamReader (System.in));
 
  System.out.print("๊ณ„์ขŒ ID : ");
  int id = Integer.parseInt(in.readLine());
 
  System.out.println("์ถœ๊ธˆ์•ก : ");
  int money = Integer.parseInt(in.readLine());
 
  for (int i = 0; i < index; i++) {
   if(array[i].getId() == id) {
    if(array[i].getBalance() < money) {
     System.out.println("์ž”์•ก๋ถ€์กฑ");
     return;
    }
   
    array[i].minMoney(money);
    System.out.println("์ถœ๊ธˆ์™„๋ฃŒ!!!");
    return;
   }
  }
  System.out.println("์œ ํšจํ•˜์ง€ ์•Š์€ ID ์ž…๋‹ˆ๋‹ค.");
 }
 
 public void showMoney() throws IOException { //์ž”์•ก์กฐํšŒ
  in = new BufferedReader (new InputStreamReader (System.in));
 
  System.out.print("๊ณ„์ขŒ ID : ");
  int id = Integer.parseInt(in.readLine());
 
  for(int i = 0; i < index; i++) {
   if(array[i].getId() == id) {
    array[i].showData();
   }
  }
 }
}


==========================================================================================================================================


package jbank;

public class Account {
 private int id; //๊ณ„์ขŒ๋ฒˆํ˜ธ
 private int balance; //์ž”์•ก
 private String name; //์ด๋ฆ„
 
 public Account(int id, int balance, String name) { //์ƒ์„ฑ์ž
  this.id = id;
  this.balance = balance;
  this.name = name;
 }
 
 public int getId() {
  return id;
 }
 
 public int getBalance() {
  return balance;
 }
 
 public void addMoney(int value) { //์ž…๊ธˆ
  balance += value;
 }
 
 public void minMoney(int value) { //์ถœ๊ธˆ
  balance -= value;
 }
 
 public void showData() { //๊ณ„์ขŒ์ •๋ณด ๋ณด์—ฌ์ฃผ๊ธฐ
  System.out.println("๊ณ„์ขŒ๋ฒˆํ˜ธ : " + id);
  System.out.println("์ด๋ฆ„  : " + name);
  System.out.println("์ž”์•ก  : " + balance);
 }
}

๋Œ“๊ธ€