萌新纯JAVASE练习项目某谷Bank项目------练手几小时完成,熟练度还是低

Caitin ·
更新时间:2024-11-01
· 781 次阅读

纯新手自学看完了刘意2014年27天的JAVASE教学,几小时跟着教程完成,拿来练手和回顾知识点都很好.
分享项目链接:链接:https://pan.baidu.com/s/1V5EPSYyeAguXke3g5oWf6w
提取码:j7rr
复制这段内容后打开百度网盘手机App,操作更方便哦
心得:完成过程中没有什么难点,当一点小失误被Debug掉后,成就感和游戏攻略完成的快感一样苏爽,可能这就是编程吧,贴上我自己的代码,留点成长的足迹吧.

package banking; import banking.domain.OverdraftException; public class Account { protected double balance; public Account(double init_balance){ this.balance = init_balance; } public double getBalance() { return balance; } /** * 存钱方法 * @param amt: 存款的钱数 * @return: 返回存款是否成功 */ public boolean deposit(double amt){ this.balance += amt; return true; } /** * 取钱方法 * @param amt: 取款的钱数 * @return: 返回取款是否成功 */ public void withdraw(double amt)throws OverdraftException{ if(amt>balance){ throw new OverdraftException("资金不足:",amt-balance); }else{ balance-=amt; } package banking; import java.util.ArrayList; import java.util.Iterator; public class Bank { //当前 Bank 中有多个 Cutomer private ArrayList customers = new ArrayList(); //表示 customers 变量中有多少个真正的 Customer 对象 // private int numberOfCustomer=0; private Bank() {} private static Bank bank = new Bank(); public static Bank getBank(){ return bank; } /** * 根据传入的参数创建一个新的 Cusotmer 对象, 并把该对象赋给 customers 中指定的元素 * @param firstName * @param lastName */ public void addCustomer(String firstName, String lastName){ //1.根据传入的参数创建一个新的 Cusotmer 对象 Customer cust = new Customer(firstName, lastName); // //2.把 1 新创建的 Customer 对象赋给 customers 中指定的元素 // customers[numberOfCustomer] = cust; // //3. 使表示 customers 变量中有多少个真正的 Customer 对象的整数 + 1 // numberOfCustomer++; customers.add(cust); } /** * 返回 表示 customers 变量中有多少个真正的 Customer 对象的整数 * @return */ public int getNumOfCustomers(){ // return numberOfCustomer; return customers.size(); } /** * 返回指定索引对应的 Customer 对象 * @param index * @return */ public Customer getCustomer(int index){ // return customers[index]; return customers.get(index); } public Iterator getCustomers(){ return customers.iterator(); } } package banking; import banking.domain.OverdraftException; public class CheckingAccount extends Account { //表示透支保护 private double overdraftProtection; public CheckingAccount(double init_balance) { super(init_balance); } public CheckingAccount(double init_balance, double overdraftProtection){ super(init_balance); this.overdraftProtection = overdraftProtection; } /** * 重写 withdraw 方法; * * 此方法必须执行下列检查。如果当前余额足够弥补取款amount,则正常进行。 * 如果不够弥补但是存在透支保护,则尝试用overdraftProtection得值来弥补该差值(balance-amount). * 如果弥补该透支所需要的金额大于当前的保护级别。则整个交易失败,但余额未受影响。 * */ @Override public void withdraw(double amt)throws OverdraftException { if(balance >= amt){ balance -= amt; }else if(overdraftProtection==0){ throw new OverdraftException("no overdraft protection", amt-balance); }else if(overdraftProtection<=(amt-balance)){ throw new OverdraftException("Insufficient funds for overdraft protection", amt-balance); }else{ overdraftProtection-=(amt-balance); balance = 0; } package banking; import java.util.ArrayList; import java.util.Iterator; public class Customer { private String firstName; private String lastName; //代表用户有的账户属性 private ArrayList accounts = new ArrayList(); // private int index; public Customer(String firstName, String lastName) { this.firstName = firstName; this.lastName = lastName; } // public Account getAccount() { // return account; // } // // public void setAccount(Account account) { // this.account = account; // } public String getFirstName() { return firstName; } public String getLastName() { return lastName; } public void addAccount(Account a){ // account[index] = a; // index++; accounts.add(a); } public Account getAccount(int i){ // return account[i]; return accounts.get(i); } public int getNumOfAccounts(){ return accounts.size(); } public Iterator getAccounts(){ return accounts.iterator(); } } ` } } package banking; public class SavingsAccount extends Account { // 利率 private double interestRate; public SavingsAccount(double init_balance, double interestRate) { super(init_balance); this.interestRate = interestRate; } public double getInterestRate() { return interestRate; } } package banking.domain; public class OverdraftException extends Exception{ /** * */ private static final long serialVersionUID = 3782596134442853340L; private double deficit; public double getDeficit(){ return deficit; } public OverdraftException(String message,double deficit){ super(message); this.deficit = deficit; } } package banking.reports; import banking.Account; import banking.Bank; import banking.CheckingAccount; import banking.Customer; import banking.SavingsAccount; public class CustomerReport { public void generateReport(){ System.out.println("\t\t\tCUSTOMERS REPORT"); System.out.println("\t\t\t================"); Bank bank = Bank.getBank(); Customer customer; for ( int cust_idx = 0; cust_idx < bank.getNumOfCustomers(); cust_idx++ ) { customer = bank.getCustomer(cust_idx); System.out.println(); System.out.println("Customer: " + customer.getLastName() + ", " + customer.getFirstName()); for ( int acct_idx = 0; acct_idx < customer.getNumOfAccounts(); acct_idx++ ) { Account account = customer.getAccount(acct_idx); String account_type = ""; if(account instanceof SavingsAccount){ System.out.println("Savings Account: current balance is ¥"+account.getBalance()); } if(account instanceof CheckingAccount){ System.out.println("CheckingAccount: current balance is ¥"+account.getBalance()); } } } } }
作者:DrugHua



javase

需要 登录 后方可回复, 如果你还没有账号请 注册新账号