1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | package t8_test; import java.util.Scanner; // 상품가격은 vat를 포함시켜서 구한다.(vat는 정가의 10% 추가) public class TestRun { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); Test1VO vo = new Test1VO(); Test1Service service = new Test1Service(); String id, pwd; System.out.print("아이디를 입력하세요 :"); id = scanner.next(); System.out.print("비밀번호를 입력하세요 :"); pwd = scanner.next(); if(!vo.getId().equals(id)||!vo.getPwd().equals(pwd)) { System.out.println("로그인 실패..."); } else { System.out.println(id + "님 로그인 되었습니다."); //정회원인 경우는 등록된 상품의 정보를 보여주고 구매할 수 있도록 한다. while(true) { int sw = 0; service.getProduct(); System.out.print("\n구매하실 상품번호를 입력하세요 : "); int choice = scanner.nextInt(); //상품코드검사 for(int i=0; i<service.product.length; i++) { if(choice == Integer.parseInt(service.product[i][0])) { service.setProductPrice(choice); sw = 1; break; } } if(sw == 0) System.out.println("상품코드가 맞지 않습니다."); System.out.print("계속 구매 하시겠습니까?"); String yn = scanner.next().toUpperCase(); if(yn.equals("N")) break; } //구매한 상품 리스트 출력 service.getSelectProduct(); System.out.println("=========================================="); System.out.println("작업끝~~~"); } scanner.close(); } } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | package t8_test; public class Test1Service { // 각 메소드들이 사용할 전역변수 선언 String[][] product = { {"1","냉장고","1250000"}, {"2","에어컨","1600000"}, {"3","세탁기","850000"}, {"4","TV","650000"}, {"5","청소기","1000000"} }; String[][] selectProduct = new String[100][4]; int cnt = 0; int tot = 0; // 상품 리스트 출력 public void getProduct() { System.out.println(" **상 품 리 스 트**"); System.out.println("================================"); System.out.println("번호\t상품명\t상품가격"); System.out.println("================================"); for(int i=0; i<product.length; i++) { for(int j=0; j<product[i].length-1; j++) { System.out.print(product[i][j] + "\t"); } System.out.print(String.format("%,10d", Integer.parseInt(product[i][2]))); System.out.println(); } System.out.println("================================"); } // 구매한 상품을 배열에 저장 public void setProductPrice(int choice) { for(int i=0; i<3; i++) { selectProduct[cnt][i] = product[choice-1][i]; } selectProduct[cnt][3] = (int)(Integer.parseInt(product[choice-1][2])*1.1) + ""; cnt++; } // 구매한 상품 리스트 출력 public void getSelectProduct() { System.out.println(" * 구매한 상품 리스트 * "); System.out.println("=========================================="); System.out.println("번호\t상품명\t 상품가격\t\t실제상품금액"); System.out.println("=========================================="); for(int i=0; i<cnt; i++) { for(int j=0; j<selectProduct[0].length-2; j++) { System.out.print(selectProduct[i][j] + "\t"); } System.out.print(String.format("%,10d\t", Integer.parseInt(selectProduct[i][2]))); System.out.print(String.format("%,10d\t\n", Integer.parseInt(selectProduct[i][3]))); tot += Integer.parseInt(selectProduct[i][3]); } System.out.printf(String.format("\t\t상품 총액 : %,d\n",tot)); } } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 | package t9_확인문제; import java.util.Scanner; //확인문제 20번 public class BankApplication { private static Account[] accountArray = new Account[100]; //메인과 같은 레벨에 있는 필드나 메소드는 static을 붙여라 private static Scanner scanner = new Scanner(System.in); public static void main(String[] args) { boolean run = true; while(run) { System.out.println("================================================="); System.out.println("1.계좌생성 | 2.계좌목록 | 3.예금 | 4.출금 | 5.삭제 | 6.종료"); System.out.println("================================================="); System.out.print("선택> "); int selectNo = scanner.nextInt(); //인스턴스 메소드라서 Scanner. 방식으로 쓸 수 없다. if(selectNo == 1) { createAccount(); } else if(selectNo == 2) { accountList(); } else if(selectNo == 3) { deposit(); } else if(selectNo == 4) { withdraw(); } else if(selectNo == 5) { delete(); } else if(selectNo == 6) { run = false; } } System.out.println("프로그램 종료"); } // 계좌생성 메소드 private static void createAccount() { String ano; String owner; int balance; while(true) { System.out.println("---------------------------------------------"); System.out.println("계좌생성"); System.out.println("---------------------------------------------"); System.out.print("계좌번호 : "); ano = scanner.next(); System.out.print("계좌주 : "); owner = scanner.next(); System.out.print("초기입금액 : "); balance = scanner.nextInt(); // 중복검사 if(findAccount(ano)!=null) System.out.println("중복된 계좌입니다."); else break; } for(int i=0; i<accountArray.length; i++) { if(accountArray[i]==null) { accountArray[i] = new Account(ano,owner,balance); System.out.println("결과 : 계좌가 생성되었습니다."); break; } } } // 목록출력(계좌찾기메소드와 마찬가지로 null값을 만나더라도 전체배열을 검사하기 위해서 null인 경우는 무시한다.) private static void accountList() { System.out.println("---------------------------------------------"); System.out.println("계좌목록"); System.out.println("---------------------------------------------"); for(int i=0; i<accountArray.length; i++) { Account account = accountArray[i]; if(account!=null) { System.out.print(account.getAno() + "\t"); System.out.print(account.getOwner() + "\t"); System.out.print(account.getBalance() + "\n"); } } } // 입금 private static void deposit() { System.out.println("---------------------------------------------"); System.out.println("예금"); System.out.println("---------------------------------------------"); System.out.print("계좌번호 : "); String ano = scanner.next(); System.out.print("예금액 : "); int money = scanner.nextInt(); Account account = findAccount(ano); if(account == null) { System.out.println("결과 : 찾는 계좌정보가 없습니다."); return; } account.setBalance(account.getBalance()+money); System.out.println("결과 : 입금이 성공되었습니다."); } // 출금 private static void withdraw() { while(true) { System.out.println("---------------------------------------------"); System.out.println("출금"); System.out.println("---------------------------------------------"); System.out.print("계좌번호 : "); String ano = scanner.next(); System.out.print("출금액 : "); int money = scanner.nextInt(); Account account = findAccount(ano); if(account == null) { System.out.println("결과 : 찾는 계좌정보가 없습니다."); return; } if(account.getBalance() - money>=0) { //0보다 작으면 출금 불가능 account.setBalance(account.getBalance() - money); System.out.println("결과 : 출금이 성공되었습니다."); break; } else System.out.println("잔액보다 적은 금액을 입력해주세요."); } } // 계좌정보 찾기 private static Account findAccount(String ano) { Account account = null; // Account account; 이렇게 쓰면 initialize오류. for(int i=0; i<accountArray.length; i++) { if(accountArray[i] != null) { if(accountArray[i].getAno().equals(ano)) { account = accountArray[i]; break; } } //null값일때 반복문을 나가는것으로 코딩하면 삭제코딩일때 문제가 생길 수 있음. } return account; } // 계좌삭제 private static void delete() { System.out.println("---------------------------------------------"); System.out.println("삭제"); System.out.println("---------------------------------------------"); System.out.print("계좌번호 : "); String ano = scanner.next(); for(int i=0; i<accountArray.length; i++) { if(accountArray[i] != null) { if(accountArray[i].getAno().equals(ano)) { accountArray[i] = null; break; } } } System.out.println("삭제가 완료되었습니다."); } } | cs |
첫댓글 좋습니다. 내용들 깔끔하네요. 수고하셨습니다.