1 /** 2 * 3 */ 4 package com.kai.li.bank; 5 import java.util.List; 6 import java.util.ArrayList; 7 /** 8 * 9 */ 10 public class BankText{ 11 /** 12 * 13 */ 14 public static void main(String[] args){ 15 Account a1=new Account(500.00); // 16 try{ 17 a1.withdraw(650.00); // 18 }catch(OverdraftException o){ 19 } 20 a1.deposit(22.50); // 21 try{ 22 a1.withdraw(47.62); // 23 }catch(OverdraftException o){ 24 o.printStackTrace(); // 25 } 26 System.out.println("The account has a balance of "+a1.getBalance()); 27 } 28 } 29 /** 30 * 31 */ 32 class Account{ 33 /** 34 * 35 */ 36 protected double balance; 37 /** 38 * 39 */ 40 Account(double balance){ 41 this.balance=balance; 42 } 43 /** 44 * 45 */ 46 public double getBalance(){ 47 return this.balance; 48 } 49 /** 50 * 51 */ 52 public boolean deposit(double amt){ 53 if(amt<0) 54 return false; 55 this.balance+=amt; 56 return true; 57 } 58 /** 59 * 60 */ 61 public void withdraw(double amt)throws OverdraftException{ 62 if(amt>balance) 63 throw new OverdraftException("Balance is not enough! ---",amt-balance ); 64 this.balance-=amt; 65 } 66 } 67 /** 68 * 69 */ 70 class Customer{ 71 /** 72 * 73 */ 74 private String firstName; 75 private String lastName; 76 private Account account; 77 /** 78 * 79 */ 80 Customer(String firstName,String lastName){ 81 this.firstName=firstName; 82 this.lastName=lastName; 83 } 84 public String getFirstName(){ 85 return this.firstName; 86 } 87 public String getLastName(){ 88 return this.lastName; 89 } 90 public void setAccount(Account account){ 91 this.account=account; 92 } 93 public Account getAccount(){ 94 return this.account; 95 } 96 @Override 97 public String toString(){ 98 return this.getLastName()+" "+this.getFirstName(); 99 } 100 } 101 /** 102 * 103 */ 104 class Bank{ 105 /** 106 * 107 */ 108 private final static Bank bank=new Bank(); 109 private Customer[] customers; 110 private int numberOfCustomers; 111 /** 112 * 113 */ 114 private Bank(){ 115 customers=new Customer[6]; 116 } 117 /** 118 * 119 */ 120 public static Bank getBank(){ 121 return bank; 122 } 123 public int getNumberOfCustomers(){ 124 return this.numberOfCustomers; 125 } 126 public Customer getCustomer(int i){ 127 if(i>=numberOfCustomers){ 128 System.out.println("Number is wrong!"); 129 return null; 130 } 131 return this.customers[i]; 132 } 133 public void addCustomer(String firstName,String lastName){ 134 this.customers[numberOfCustomers]=new Customer(firstName,lastName); 135 numberOfCustomers+=1; 136 } 137 } 138 /** 139 * 140 */ 141 class ListBank{ 142 /** 143 * 144 */ 145 private List<Customer> customers; 146 private int numberOfCustomers; 147 /** 148 * 149 */ 150 public ListBank(){ 151 customers=new ArrayList<>(); 152 } 153 public int getNumberOfCustomers(){ 154 return this.numberOfCustomers; 155 } 156 public Customer getCustomer(int i){ 157 if(i>=numberOfCustomers){ 158 System.out.println("Number is wrong!"); 159 return null; 160 } 161 return this.customers.get(i); 162 } 163 public void addCustomer(String firstName,String lastName){ 164 customers.add(new Customer(firstName,lastName)); 165 numberOfCustomers=customers.size(); 166 } 167 } 168 /** 169 * 170 */ 171 class SavingsAccount extends Account{ 172 /** 173 * 174 */ 175 private double interestRate; 176 /** 177 * 178 */ 179 SavingsAccount(double balance,double interestRate){ 180 super(balance); 181 this.interestRate=interestRate; 182 } 183 /** 184 * 185 */ 186 public double getInterestRate(){ 187 return this.interestRate; 188 } 189 } 190 /** 191 * 192 */ 193 class CheckingAccount extends Account{ 194 /** 195 * 196 */ 197 private double overdraftProtection; 198 /** 199 * 200 */ 201 CheckingAccount(double balance){ 202 super(balance); 203 } 204 /** 205 * 206 */ 207 CheckingAccount(double balance,double overdraftProtection){ 208 this(balance); 209 this.overdraftProtection=overdraftProtection; 210 } 211 /** 212 * 213 */ 214 public double getOverdraftProtection(){ 215 return this.overdraftProtection; 216 } 217 /** 218 * 219 */ 220 @Override 221 public void withdraw(double amount)throws OverdraftException{ 222 if(balance >=amount&&amount>0){ 223 balance-=amount; 224 return; 225 } 226 if((balance+overdraftProtection)>=amount&&amount>0){ 227 balance=0; 228 overdraftProtection-=amount-balance; 229 throw new OverdraftException("overdract protection: ",overdraftProtection-amount+balance); 230 } 231 throw new OverdraftException("no overdraft protection--- ",amount-balance-overdraftProtection); 232 } 233 } 234 /** 235 * 236 */ 237 class CustomerReport{ 238 Bank bank=Bank.getBank(); 239 } 240 /** 241 * 242 */ 243 class OverdraftException extends Exception{ 244 /** 245 * 246 */ 247 private double deficit; 248 /** 249 * 250 */ 251 OverdraftException(String message,double deficit){ 252 this.deficit=deficit; 253 System.out.println(message+this.deficit); 254 } 255 /** 256 * 257 */ 258 public double getDeficit(){ 259 return this.deficit; 260 } 261 }