[KOSTA] Spring 기반 Cloud 11일차 - OOP 실습(Class Diagram 분석하기)
🎲 class diagram과 주어진 BankSystem.java 를 이용해 입출금 프로그램 구현하기
📃 풀이
BankSystem.java
package kosta.bank;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class BankSystem {
private MyBank myBank; //MyBank
public BankSystem(){
myBank = new MyBank();
showMenu();
}
public void showMenu(){ //show method 만들기
String menu = null;
String id = null;
String name = null;
long balance = 0;
do{
System.out.println("****메뉴를 입력하세요****");
System.out.println("1. 고객등록");
System.out.println("2. 고객보기(1명)");
System.out.println("3. 고객전체보기");
System.out.println("4. 고객예금출금");
System.out.println("5. 고객예금입금");
System.out.println("***끝내기***");
System.out.println("***************");
System.out.print(">>");
menu = readFromKeyboard();
if(menu.equals("1")){ //고객등록
System.out.print("ID를 입력하세요: ");
id = readFromKeyboard();
System.out.print("이름을 입력하세요: ");
name = readFromKeyboard();
System.out.print("잔고를 입력하세요: ");
balance = Long.parseLong(readFromKeyboard());
myBank.addCustomer(id, name, balance);
}
else if(menu.equals("2")){
System.out.print("고객ID를 입력하세요: ");
id = readFromKeyboard();
Customer cust = myBank.getCustomer(id);
System.out.println(cust.getId() +":" + cust.getName() + ": "
+ cust.getAccount().getBalance());
}
else if(menu.equals("3")){
Customer[] allCust = myBank.getAllCustomers();
for(int i=0;i<allCust.length;i++){
System.out.println(allCust[i].getId() + ": " +
allCust[i].getName() + " :" +
allCust[i].getAccount().getBalance());
}
}
else if(menu.equals("4")){
System.out.print("고객의 ID를 입력하세요.: ");
id = readFromKeyboard();
Customer cust = myBank.getCustomer(id);
if(cust == null){
System.out.println("등록된 고객이 아닙니다.");
}else{
System.out.print("출금액을 입력하세요: ");
balance = Long.parseLong(readFromKeyboard());
if(cust.getAccount().withdraw(balance)){
System.out.println("정상적으로 출금되었습니다.");
System.out.println("출금후 잔고는 :" + cust.getAccount().getBalance());
}else{
System.out.println("잔고가 부족합니다.");
}
}
}
else if(menu.equals("5")) {
System.out.print("고객의 ID를 입력하세요. : ");
id = readFromKeyboard();
Customer cust = myBank.getCustomer(id);
if(cust == null) {
System.out.println("등록된 고객이 아닙니다.");
}else {
System.out.print("입금액을 입력하세요 : ");
balance = Long.parseLong(readFromKeyboard());
cust.getAccount().deposit(balance);
System.out.println("정상적으로 입금되었습니다.");
System.out.println("입금후 잔고는 : " + cust.getAccount().getBalance());
}
}
}while(!menu.equals("q"));
}
public String readFromKeyboard(){
String input = null;
try{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
input = br.readLine();
}catch(Exception e){
System.out.println(e.getMessage());
}
return input;
}
public static void main(String[] args) {
BankSystem bank = new BankSystem();
}
}
Account.java
package kosta.bank;
public class Account {
private String id;
private long balance;
public Account() {
}
public Account(String id, long balance) {
this.id = id;
this.balance = balance;
}
public void deposit(long amt) {
balance += amt;
}
public boolean withdraw(long amt) {
if(balance < amt) {
return false;
}
balance -= amt;
return true;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public long getBalance() {
return balance;
}
public void setBalance(long balance) {
this.balance = balance;
}
}
Customer.java
package kosta.bank;
public class Customer {
private String id;
private String name;
private Account account;
public Customer() {
}
public Customer(String id, String name, long balance) {
this.id = id;
this.name = name;
this.account = new Account(id, balance);
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Account getAccount() {
return account;
}
public void setAccount(Account account) {
this.account = account;
}
}
MyBank.java
package kosta.bank;
public class MyBank {
private Customer customers[];
private int customersNum;
public MyBank() {
customers = new Customer[10];
}
public void addCustomer(String id, String name, long balance) {
customers[customersNum++] = new Customer(id, name, balance);
}
public Customer getCustomer(String id) {
Customer cust = null;
for(int i = 0 ; i < customersNum; i++) {
if(customers[i].getId().equals(id)) {
cust = customers[i];
break;
}
}
return cust;
}
public Customer[] getAllCustomers() {
Customer newCustomers[] = new Customer[customersNum];
// for(int i = 0; i < customersNum; i++) {
// newCustomers[i] = customers[i];
// }
System.arraycopy(customers, 0, newCustomers, 0, customersNum);
return newCustomers;
}
}
Author And Source
이 문제에 관하여([KOSTA] Spring 기반 Cloud 11일차 - OOP 실습(Class Diagram 분석하기)), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://velog.io/@junbeomm-park/KOSTA-Spring-기반-Cloud-11일차-OOP-실습Class-Diagram-분석하기저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)