●계좌 관리 프로그램 구현하기
은행에서 고객들의 계좌를 관리하는 프로그램입니다.
●구현 멤버
Bank 클래스
addAccount(String customerName, long initialBalance): 계좌 생성
printAllAccounts(String customerName): 모든 계좌 정보 출력
소스 코드: https://github.com/NHclub/Implementation/tree/main/src/account
●첫 번째 문제점: 계좌 번호 생성 로직
로직을 구현할 아이디어가 떠오르지 않음.
난수를 13개 생성하고 그 난수가 accounstList에 없는 계좌일 때 까지 반복하도록 구현함.
public void addAccount(String customerName, long initialBalance) {
while(true){
long accountNumber = 0;
for (int i = 0; i < 13; i++){
String accountNumberString = random.nextInt(10) + "";
accountNumber = Long.parseLong(accountNumberString);
}
if(!accountsList.contains(accountNumber)){
Account newAccount = new Account(customerName, initialBalance,accountNumber);
accountsList.add(newAccount);
System.out.println("계좌가 생성 되었습니다. 계좌 번호는 " + accountNumber + " 입니다.");
break;
} else {continue;}
}
}
●두 번째 문제점: 계좌 번호가 13자리가 아닌 1자리로 생성됨
long accountNumber = 0;
for (int i = 0; i < 13; i++){
String accountNumberString = random.nextInt(10) + "";
accountNumber = long.parselong(accountNumberString);
}
이 부분에서 accountNumberString에 누적이 되지 않고 계속 초기화 됨. 누적 되도록 변경
long accountNumber = 0;
String accountNumberString = "";
for (int i = 0; i < 13; i++){
accountNumberString += random.nextInt(10) + "";
accountNumber = Long.parseLong(accountNumberString);
}
계좌 생성 구현 완료!
●세 번째 문제점: 생성은 되는데 계좌 정보가 안 불러와짐.
public void printAllAccounts(String customerName) {
if (accountsList.contains(customerName)){
for (int i = 0; i < accountsList.size(); i++) {
if (accountsList.get(i).getCustomerName().equals(customerName)) {
System.out.println(customerName + "님의 계좌 정보를 알려드리겠습니다");
System.out.println("성함: " + customerName);
System.out.println("현재 잔액: " + accountsList.get(i).getBalance());
System.out.println("계좌 번호: " + accountsList.get(i).getAccountNumber());
}
}
}else {
System.out.println("존재하지 않는 회원입니다.");
}
}
accountsList는 Account 클래스를 요소로 갖고 있는데 그걸 customerName과 비교하니 전부 false가 반환되고 실행이 안된 것. 조건문 삭제
●네 번째 문제점: 존재하지 않는 회원이 조회할 때 else문이 List 요소만큼 출력됨
public void printAllAccounts(String customerName) {
for (int i = 0; i < accountsList.size(); i++) {
if (accountsList.get(i).getCustomerName().equals(customerName)) {
System.out.println(customerName + "님의 계좌 정보를 알려드리겠습니다");
System.out.println("성함: " + customerName);
System.out.println("현재 잔액: " + accountsList.get(i).getBalance());
System.out.println("계좌 번호: " + accountsList.get(i).getAccountNumber());
} else {
System.out.println("존재하지 않는 회원입니다.");
}
}
}
존재 여부를 판단하는 flag 변수를 하나 생성.
반복문을 돌면서 회원을 찾으면 flag 변수에 true로 할당 후 break.
약 찾지 못하면 계속 false일 테니 flag가 false일 때 존재하지 않는 회원이라는 것을 return
public void printAllAccounts(String customerName) {
boolean flag = false;
for (int i = 0; i < accountsList.size(); i++) {
if (accountsList.get(i).getCustomerName().equals(customerName)) {
System.out.println(customerName + "님의 계좌 정보를 알려드리겠습니다");
System.out.println("성함: " + customerName);
System.out.println("현재 잔액: " + accountsList.get(i).getBalance());
System.out.println("계좌 번호: " + accountsList.get(i).getAccountNumber());
flag = true;
break;
}
}
if (flag == false) {
System.out.println("존재하지 않는 회원입니다.");
}
}
Bank class 구현 완료!
'Java > 구현하기(Implementation)' 카테고리의 다른 글
23.06.28) Spring 나만의 Blog 구현하기 (0) | 2023.06.28 |
---|---|
23.06.27) Spring 나만의 Blog 구현하기 (0) | 2023.06.27 |
23.06.22) Java 채팅 프로그램 구현하기 (0) | 2023.06.22 |
23.06.21) Java 계좌 관리 프로그램 구현하기 (0) | 2023.06.21 |
23-06-19) Java 캡슐 판매 기계 구현하기 (0) | 2023.06.19 |