2013년 1월 7일 월요일

[Java] BankingSystem


package kosta.exam1;

import java.util.Scanner;

public class BankManage {
static Scanner sc = new Scanner(System.in);
public static void disp(){
System.out.println("1.계좌개설  2.입금  3.출금  4.계좌보기  5.종료");
}

public static Account makeAccount(){
String Account;
String name;
String sn;
int money;
System.out.print("원하는 계좌번호를 입력하세요 : ");
Account = sc.nextLine();
System.out.print("예금주를 입력하세요 : ");
name = sc.nextLine();
System.out.print("비밀번호를 설정하여 주십시오 : ");
sn = sc.nextLine();
System.out.print("입금할 돈을 입력하세요 : ");
money = sc.nextInt();
sc.nextLine();

Account ac = new Account(Account, name, sn, money);

return ac;
}

public static void addMoney(Account ac[], int cnt){
String Account;
String sn;
int money;
System.out.print("입금을 원하는 원하는 계좌번호를 입력하세요 : ");
Account = sc.nextLine();

for(int i=0; i<cnt; i++){
if(ac[i].accountNo.equals(Account)){
System.out.print("비밀번호를 입력하세요 : ");
sn = sc.nextLine();
if(ac[i].sn.equals(sn)){
System.out.print("입금할 돈을 입력하세요 : ");
money = sc.nextInt();
sc.nextLine();
ac[i].addMoney(money);
}
else{
System.out.println("비밀번호가 틀립니다.");
break;
}
}
else if(i+1==cnt){
System.out.println("계좌가 존재하지 않습니다.");
}
}
}

public static int withdraw(Account ac[], int cnt){
String Account;
String sn;
int money;
System.out.print("출금을 원하는 원하는 계좌번호를 입력하세요 : ");
Account = sc.nextLine();

for(int i=0; i<cnt; i++){
if(ac[i].accountNo.equals(Account)){
System.out.print("비밀번호를 입력하세요 : ");
sn = sc.nextLine();
if(ac[i].sn.equals(sn)){
System.out.print("출금할 돈을 입력하세요 : ");
money = sc.nextInt();
sc.nextLine();
money = ac[i].withdraw(money);
return money;
}
else{
System.out.println("비밀번호가 틀립니다.");
break;
}
}
else if(i+1==cnt){
System.out.println("계좌가 존재하지 않습니다.");
break;
}
}
return 0;
}

public static void main(String[] args) {
// TODO Auto-generated method stub
Account ac[] = new Account[10];
int choice;
int money;
int cnt=0;
boolean ch = true;

while(ch){
disp();
choice = sc.nextInt();
sc.nextLine();
switch(choice){
case 1:
ac[cnt] = makeAccount();
cnt++;
break;
case 2:
addMoney(ac, cnt);
break;
case 3:
money = withdraw(ac, cnt);
if(money == 0){
System.out.println("잔액부족!");
}
else
System.out.println("출금완료! : " + money);
break;
case 4:
for(int i=0; i<cnt; i++){
ac[i].disp();
}
break;
case 5:
ch = false;
}
}
}
}


-------------------------------------------------------------------------------


package kosta.exam1;

public class Account {

String accountNo;
String name;
String sn;
int balance;

Account(String number, String name, String sn, int balance){
this.accountNo = number;
this.name = name;
this.sn = sn;
this.balance = balance;
}

void addMoney(int amount){
balance += amount;
}

int withdraw(int amount){
if(balance-amount<0)
return 0;
balance -= amount;
return amount;
}

void disp(){
System.out.println("계좌번호 : " + this.accountNo);
System.out.println("예금주 : " + this.name);
System.out.println("잔액 : " + this.balance);
}

}

댓글 없음:

댓글 쓰기