Java programming 자바 프로그래밍 실습 : 은행 계좌 ATM
2021-1 성균관대학교 소프트웨어학과 타메르 교수님의 자바 프로그래밍 실습 수업을 들으면서 수행한 예제입니다.
Mini project (ATM)
구현해야할 것
- BankAccount 클래스
- String FullName
- String Username
- String Password
- Double Balance
- String LastOperation
- constructor 만들어서 초기화
- main 메소드에서 BankAccount 객체 3개
BankAccount account1 = new BankAccount("Firuz", "firuz", "p@ssw@rd", 2000.0, "");
BankAccount account2 = new BankAccount("Kang Seong", "kang", "p@ssw@rd123", 34000.0, "");
BankAccount account3 = new BankAccount("SiHyein Ryu", "ryu", "p@ssw@rd1234", 10000.0, "");
- 로그인 기능 : 로그인 성공할 때까지 반복해서 username, password 입력 받기
- 로그인 성공 후, 메인 메뉴 보여주기 - 사용자가 0 입력하기 전까지 반복 수행
- 각 메뉴에 맞기 기능 구현하기
(1) 계좌 확인
(2) 인출
인출하려는 돈이 계좌의 balance보다 큰 금액이면 "Withdrawal amount exceeded account balance" 출력
(3) 마지막 거래 정보 확인
(0) 작업 종료
- BankAccount.java
public class BankAccount {
public String fullName;
public String username;
public String password;
public double balance;
public String lastOperation;
// constructor
public BankAccount(String fullName, String username, String password, double balance, String lastOperation) {
super();
setFullName(fullName);
setUsername(username);
setPassword(password);
setBalance(balance);
setLastOperation(lastOperation);
}
// getter and setter for each data fields
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public String getLastOperation() {
return lastOperation;
}
public void setLastOperation(String lastOperation) {
this.lastOperation = lastOperation;
}
public static void main(String[] args) {
ATM atm = new ATM();
// create three BankAccount instance
BankAccount account1 = new BankAccount("Firuz", "firuz", "p@ssw@rd", 2000.0, "");
BankAccount account2 = new BankAccount("Kang Seong", "kang", "p@ssw@rd123", 34000.0, "");
BankAccount account3 = new BankAccount("SiHyein Ryu", "ryu", "p@ssw@rd1234", 10000.0, "");
// create BankAccount array
BankAccount[] BankAccountList = {account1, account2, account3};
atm.Login(BankAccountList);
}
}
- ATM.java
import java.util.Scanner;
public class ATM {
public void CheckBalance(BankAccount account) {
System.out.println("User: " + account.getUsername());
System.out.println("Balance: " + account.getBalance());
account.setLastOperation("Check balance");
}
public void WithdrawMoney(BankAccount account) {
double amount;
Scanner input = new Scanner(System.in);
System.out.print("Enter amount: ");
amount = input.nextDouble();
if (account.getBalance() >= amount) {
account.setBalance(account.getBalance() - amount);
}
else {
System.out.println("Withdrawal amount exceeded account balance.");
}
account.setLastOperation("Withdrawal");
// input.close();
}
public void LastOperation(BankAccount account) {
System.out.println("Name: " + account.getFullName());
System.out.println("Current Balance: " + account.getBalance());
System.out.println("Last Operation: " + account.getLastOperation());
}
public void Operation (BankAccount account) {
int op = 10;
Scanner input = new Scanner(System.in);
// repeatedly asking and performing operations until user enters 0
while (op != 0) {
System.out.println("1. Check balance (Enter 1);\n"
+ "2. Withdraw money (Enter 2);\n"
+ "3. Last Operation (Enter 3);\n"
+ "4. Exit (Enter 0);"
);
op = input.nextInt();
// call correct method for operation number that user enters
if (op == 1) {
this.CheckBalance(account);
}
else if (op == 2) {
this.WithdrawMoney(account);
}
else if (op == 3) {
this.LastOperation(account);
}
}
input.close();
System.out.println("Thank you for banking with us!");
}
public void Login(BankAccount[] List) {
System.out.println("Login");
String un;
String pw;
int i;
int result;
BankAccount currentUser = null;
// 0 means success, -1 means failure
result = -1;
Scanner input = new Scanner(System.in);
// repeat input user name and password until result is success
while (result != 0) {
System.out.print("Username:");
un = input.next();
System.out.print("Password: ");
pw = input.next();
// check user name and password if it is in the BankAccount list and is correct.
for ( i=0 ; i<List.length; i++ ) {
// if it's correct, set result as 0 (success) and break for loop
if (List[i].getUsername().equals(un) && List[i].getPassword().equals(pw) ) {
result = 0;
break;
}
else {
continue;
}
}
// before escape while loop, if result is 0, set currentUser who log in.
if (result == 0) {
currentUser = List[i];
break;
}
}
// if currentUser is not null, it means that login success.
// then show operations for currentUser
if ( currentUser != null ) {
this.Operation(currentUser);
}
input.close();
}
}
withdraw 메소드에서 Scanner input을 close하면 2번 입력하고 작업 완료 후 에러가 난다. 왜지?
'프로그래밍 언어 > Java' 카테고리의 다른 글
Java programming 자바 프로그래밍 실습 : 다형성 polymorphism 추상 클래스 interface을 활용한 예제 (0) | 2021.04.05 |
---|---|
Week6) Java Programming Lab : Polymorphism (0) | 2021.03.29 |
Week5) Java Programming Lab : Inheritance (0) | 2021.03.23 |
Java programming 자바 프로그래밍 실습 : Employee Salary (0) | 2021.03.22 |
Java programming 자바 프로그래밍 실습 : Bank Account Class (withdraw, transfer) (0) | 2021.03.22 |