栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

尚硅谷--Java基础实战

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

尚硅谷--Java基础实战

Java基础实战_Bank项目01 目录结构


不要加01,这里是为了区分

【Account.java】类
package banking;

public class Account {

    private double balance;    //银行帐户的当前(或即时)余额

    //公有构造器 ,这个参数为 balance 属性赋值
    public Account(double init_balance) {
        this.balance = init_balance;
    }

    //用于获取经常余额
    public double getBalance() {
        return balance;
    }

    //向当前余额增加金额
    public void deposit(double amt){
        balance+=amt;
    }

    //从当前余额中减去金额
    public void withdraw(double amt){
        balance-=amt;
    }
}
【TestBanking】类
package banking;
import banking.*;

public class TestBanking {

  public static void main(String[] args) {
    Account  account;

    // Create an account that can has a 500.00 balance.
    System.out.println("Creating an account with a 500.00 balance.");
    //code
    account = new Account(500.00);

    System.out.println("Withdraw 150.00");
   	//code
    account.withdraw(150.00);

    System.out.println("Deposit 22.50");
	//code
    account.deposit(22.50);

    System.out.println("Withdraw 47.62");
   	//code
    account.withdraw(47.62);

    // Print out the final account balance
    System.out.println("The account has a balance of " + account.getBalance());
  }
}

Java基础实战_Bank项目02 目录结构


自己做的话不需要加02,这里是为了区分

【Account.java】类
package banking;

public class Account {

    private double balance;    //银行帐户的当前(或即时)余额

    //公有构造器 ,这个参数为 balance 属性赋值
    public Account(double init_balance) {
        this.balance = init_balance;
    }

    //用于获取经常余额
    public double getBalance() {
        return balance;
    }

    //向当前余额增加金额
    public void deposit(double amt){
        balance+=amt;
    }

    //从当前余额中减去金额
    public void withdraw(double amt){
        balance-=amt;
    }
}
【Customer.java】类
package banking;

public class Customer {
    
    private String  firstName;
    private String  lastName;
    private Account account;

    public Customer(String f, String l) {
        this.firstName = f;
        this.lastName = l;
    }

    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public Account getAccount() {
        return account;
    }

    public void setAccount(Account acct) {
        this.account = acct;
    }
}
【TestBanking.java】类
package banking;

import banking.*;

public class TestBanking {

  public static void main(String[] args) {
    Customer customer;
    Account  account;

    // Create an account that can has a 500.00 balance.
    System.out.println("Creating the customer Jane Smith.");
    //code
    customer = new Customer("Jane","Smith");
    System.out.println("Creating her account with a 500.00 balance.");
    //code
    account = new Account(500.00);
    customer.setAccount(account);

    System.out.println("Withdraw 150.00");
	//code
    customer.getAccount().withdraw(150.00);

    System.out.println("Deposit 22.50");
  	//code
    customer.getAccount().deposit(22.50);

    System.out.println("Withdraw 47.62");
   	//code
    customer.getAccount().withdraw(47.62);

    // Print out the final account balance
    System.out.println("Customer [" + customer.getLastName()
		       + ", " + customer.getFirstName()
		       + "] has a balance of " + account.getBalance());
  }
}

Java基础实战_Bank项目03 目录结构


自己做的话不需要加03,这里是为了区分

【Account.java】类
package banking;

public class Account {

    private double balance;    //银行帐户的当前(或即时)余额

    //公有构造器 ,这个参数为 balance 属性赋值
    public Account(double init_balance) {
        this.balance = init_balance;
    }

    //用于获取经常余额
    public double getBalance() {
        return balance;
    }

    
    public boolean deposit(double amt){
        balance+=amt;
        return true;
    }

    
    public boolean withdraw(double amt){
        if (amt < balance){
            balance-=amt;
            return true;
        }else{
            return false;
        }

    }
}
【Customer.java】类
package banking;

public class Customer {

    private String  firstName;
    private String  lastName;
    private Account account;

    public Customer(String f, String l) {
        this.firstName = f;
        this.lastName = l;
    }

    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public Account getAccount() {
        return account;
    }

    public void setAccount(Account acct) {
        this.account = acct;
    }
}
【TestBanking.java】类
package banking;

import banking.*;

public class TestBanking {

  public static void main(String[] args) {
    Customer customer;
    Account  account;

    // Create an account that can has a 500.00 balance.
    System.out.println("Creating the customer Jane Smith.");
	//code
    customer = new Customer("Jane","Smith");
    System.out.println("Creating her account with a 500.00 balance.");
	//code
    account = new Account(500.00);
    customer.setAccount(account);

    // Perform some account transactions
    System.out.println("Withdraw 150.00: " + account.withdraw(150.00));
    System.out.println("Deposit 22.50: " + account.deposit(22.50));
    System.out.println("Withdraw 47.62: " + account.withdraw(47.62));
    System.out.println("Withdraw 400.00: " + account.withdraw(400.00));

    // Print out the final account balance
    System.out.println("Customer [" + customer.getLastName()
		       + ", " + customer.getFirstName()
		       + "] has a balance of " + account.getBalance());
  }
}

Java基础实战_Bank项目04 目录结构

【Account.java】类
package banking;


public class Account {

    private double balance;    //银行帐户的当前(或即时)余额

    //公有构造器 ,这个参数为 balance 属性赋值
    public Account(double init_balance) {
        this.balance = init_balance;
    }

    //用于获取经常余额
    public double getBalance() {
        return balance;
    }

    
    public boolean deposit(double amt){
        balance+=amt;
        return true;
    }

    
    public boolean withdraw(double amt){
        if (amt < balance){
            balance-=amt;
            return true;
        }else{
            return false;
        }

    }
}
【Customer.java】类
package banking;

public class Customer {

    private String  firstName;
    private String  lastName;
    private Account account;

    public Customer(String f, String l) {
        this.firstName = f;
        this.lastName = l;
    }

    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public Account getAccount() {
        return account;
    }

    public void setAccount(Account acct) {
        this.account = acct;
    }
}
【Bank.java】类
package banking;


public class Bank {

    private Customer[] customers;   //Customer对象的数组
    private int numberOfCustomer;   //整数,跟踪下一个 customers 数组索引

    
    public Bank() {
        customers = new Customer[10];
    }

    
    public void addCustomer(String f,String l){
        customers[numberOfCustomer++]=new Customer(f,l);
    }

    
    public Customer getCustomer(int index) {
        return customers[index];
    }

    public int getNumOfCustomers() {
        return numberOfCustomer;
    }
}
【TestBanking.java】类
package banking;

import banking.*;

public class TestBanking {

    public static void main(String[] args) {
        Bank bank = new Bank();

        // Add Customer Jane, Simms
        //code
        bank.addCustomer("Jane","Simms");

        //Add Customer Owen, Bryant
        //code
        bank.addCustomer("Owen","Bryant");

        // Add Customer Tim, Soley
        //code
        bank.addCustomer("Tim","Soley");

        // Add Customer Maria, Soley
        //code
        bank.addCustomer("Maria","Soley");

        for (int i = 0; i < bank.getNumOfCustomers(); i++) {
            Customer customer = bank.getCustomer(i);

            System.out.println("Customer [" + (i + 1) + "] is "
                    + customer.getLastName()
                    + ", " + customer.getFirstName());
        }
    }
}

Java基础实战_Bank项目05 目录结构

【Account.java】类
package banking;

public class Account {

    protected double balance;    //银行帐户的当前(或即时)余额

    //公有构造器 ,这个参数为 balance 属性赋值
    public Account(double init_balance) {
        this.balance = init_balance;
    }

    //用于获取经常余额
    public double getBalance() {
        return balance;
    }

    
    public boolean deposit(double amt){
        balance+=amt;
        return true;
    }

    
    public boolean withdraw(double amt){
        if (amt < balance){
            balance-=amt;
            return true;
        }else{
            return false;
        }

    }

}
【Customer.java】类
package banking;

public class Customer {

    private String  firstName;
    private String  lastName;
    private Account account;

    public Customer(String f, String l) {
        this.firstName = f;
        this.lastName = l;
    }

    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public Account getAccount() {
        return account;
    }

    public void setAccount(Account acct) {
        this.account = acct;
    }

}
【Bank.java】类
package banking;


public class Bank {

    private Customer[] customers;   //Customer对象的数组
    private int numberOfCustomer;   //整数,跟踪下一个 customers 数组索引

    
    public Bank() {
        customers = new Customer[10];
    }

    
    public void addCustomer(String f,String l){
        customers[numberOfCustomer++]=new Customer(f,l);
    }

    
    public Customer getCustomer(int index) {
        return customers[index];
    }

    public int getNumOfCustomers() {
        return numberOfCustomer;
    }
}
【CheckingAccount.java】类
package banking;

public class CheckingAccount extends Account{

    private double overdraftProtection;

    public CheckingAccount(double balance) {
        super(balance);
    }

    public CheckingAccount(double balance, double protect) {
        super(balance);
        this.overdraftProtection = protect;
    }

    
    @Override
    public boolean withdraw(double amt){
        if (balance < amt){
            if (amt-balance>overdraftProtection) {
                return false;
            }else {
                overdraftProtection-=amt-balance;
                balance=0;
                return true;
            }
        }else {
            balance-=amt;
            return true;
        }
    }
}
【SavingsAccount.java】类
package banking;

public class SavingsAccount extends Account{

    private double interestRate;

    public SavingsAccount(double balance, double interest_Rate) {
        super(balance);
        this.interestRate = interest_Rate;
    }
}
【TestBanking.java】类
package banking;

import banking.*;

public class TestBanking {

  public static void main(String[] args) {
    Bank bank = new Bank();
    Customer customer;
    Account account;

    //
    // Create bank customers and their accounts
    //

    System.out.println("Creating the customer Jane Smith.");
    bank.addCustomer("Jane", "Simms");
    //code
    System.out.println("Creating her Savings Account with a 500.00 balance and 3% interest.");
    //code
    bank.getCustomer(0).setAccount(new SavingsAccount(500.00,0.03));

    System.out.println("Creating the customer Owen Bryant.");
    //code
    bank.addCustomer("Owen", "Bryant");

    customer = bank.getCustomer(1);
    System.out.println("Creating his Checking Account with a 500.00 balance and no overdraft protection.");
    //code
    customer.setAccount(new CheckingAccount(500.00,0));

    System.out.println("Creating the customer Tim Soley.");
    bank.addCustomer("Tim", "Soley");
    customer = bank.getCustomer(2);
    System.out.println("Creating his Checking Account with a 500.00 balance and 500.00 in overdraft protection.");
    //code
    customer.setAccount(new CheckingAccount(500.00,500.00));

    System.out.println("Creating the customer Maria Soley.");
    //code
    bank.addCustomer("Maria", "Soley");

    customer = bank.getCustomer(3);
    System.out.println("Maria shares her Checking Account with her husband Tim.");
    customer.setAccount(bank.getCustomer(2).getAccount());
    System.out.println();

    //
    // Demonstrate behavior of various account types
    //

    // Test a standard Savings Account
    System.out.println("Retrieving the customer Jane Smith with her savings account.");
    customer = bank.getCustomer(0);
    account = customer.getAccount();
    // Perform some account transactions
    System.out.println("Withdraw 150.00: " + account.withdraw(150.00));
    System.out.println("Deposit 22.50: " + account.deposit(22.50));
    System.out.println("Withdraw 47.62: " + account.withdraw(47.62));
    System.out.println("Withdraw 400.00: " + account.withdraw(400.00));
    // Print out the final account balance
    System.out.println("Customer [" + customer.getLastName()
		       + ", " + customer.getFirstName()
		       + "] has a balance of " + account.getBalance());

    System.out.println();

    // Test a Checking Account w/o overdraft protection
    System.out.println("Retrieving the customer Owen Bryant with his checking account with no overdraft protection.");
    customer = bank.getCustomer(1);
    account = customer.getAccount();
    // Perform some account transactions
    System.out.println("Withdraw 150.00: " + account.withdraw(150.00));
    System.out.println("Deposit 22.50: " + account.deposit(22.50));
    System.out.println("Withdraw 47.62: " + account.withdraw(47.62));
    System.out.println("Withdraw 400.00: " + account.withdraw(400.00));
    // Print out the final account balance
    System.out.println("Customer [" + customer.getLastName()
		       + ", " + customer.getFirstName()
		       + "] has a balance of " + account.getBalance());

    System.out.println();

    // Test a Checking Account with overdraft protection
    System.out.println("Retrieving the customer Tim Soley with his checking account that has overdraft protection.");
    customer = bank.getCustomer(2);
    account = customer.getAccount();
    // Perform some account transactions
    System.out.println("Withdraw 150.00: " + account.withdraw(150.00));
    System.out.println("Deposit 22.50: " + account.deposit(22.50));
    System.out.println("Withdraw 47.62: " + account.withdraw(47.62));
    System.out.println("Withdraw 400.00: " + account.withdraw(400.00));
    // Print out the final account balance
    System.out.println("Customer [" + customer.getLastName()
		       + ", " + customer.getFirstName()
		       + "] has a balance of " + account.getBalance());

    System.out.println();

    // Test a Checking Account with overdraft protection
    System.out.println("Retrieving the customer Maria Soley with her joint checking account with husband Tim.");
    customer = bank.getCustomer(3);
    account = customer.getAccount();
    // Perform some account transactions
    System.out.println("Deposit 150.00: " + account.deposit(150.00));
    System.out.println("Withdraw 750.00: " + account.withdraw(750.00));
    // Print out the final account balance
    System.out.println("Customer [" + customer.getLastName()
		       + ", " + customer.getFirstName()
		       + "] has a balance of " + account.getBalance());

  }
}

Java基础实战_Bank项目06 目录结构

【Account.java】类
package banking;

public class Account {

    protected double balance;    //银行帐户的当前(或即时)余额

    //公有构造器 ,这个参数为 balance 属性赋值
    public Account(double init_balance) {
        this.balance = init_balance;
    }

    //用于获取经常余额
    public double getBalance() {
        return balance;
    }

    
    public boolean deposit(double amt){
        balance+=amt;
        return true;
    }

    
    public boolean withdraw(double amt){
        if (amt < balance){
            balance-=amt;
            return true;
        }else{
            return false;
        }

    }

}
【Customer.java】类
package banking;

public class Customer {

    private String  firstName;
    private String  lastName;
    private Account[] accounts = new Account[10];
    private int numOfAccounts;

    public Customer(String f, String l) {
        this.firstName = f;
        this.lastName = l;
    }

    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }

    
    public Account getAccount(int index) {
        return accounts[index];
    }

    
    public int getNumOfAccounts() {
        return numOfAccounts;
    }

    
    public void addAccount(Account acct) {
        accounts[numOfAccounts++]=acct;
    }

}
【Bank.java】类
package banking;


public class Bank {

    private Customer[] customers;   //Customer对象的数组
    private int numberOfCustomer;   //整数,跟踪下一个 customers 数组索引
    private static Bank bankInstance = null;
    
    public static Bank getBank(){
        if(bankInstance==null){
            bankInstance = new Bank();
        }
        return bankInstance;
    }
    
    private Bank() {
        customers = new Customer[10];
    }

    
    public void addCustomer(String f,String l){
        customers[numberOfCustomer++]=new Customer(f,l);
    }

    
    public Customer getCustomer(int index) {
        return customers[index];
    }

    public int getNumOfCustomers() {
        return numberOfCustomer;
    }
}
【CheckingAccount.java】类
package banking;

public class CheckingAccount extends Account{

    private double overdraftProtection;

    public CheckingAccount(double balance) {
        super(balance);
    }

    public CheckingAccount(double balance, double protect) {
        super(balance);
        this.overdraftProtection = protect;
    }

    
    @Override
    public boolean withdraw(double amt){
        if (balance < amt){
            if (amt-balance>overdraftProtection) {
                return false;
            }else {
                overdraftProtection-=amt-balance;
                balance=0;
                return true;
            }
        }else {
            balance-=amt;
            return true;
        }
    }
}
【SavingsAccount.java】类
package banking;

public class SavingsAccount extends Account{

    private double interestRate;

    public SavingsAccount(double balance, double interest_Rate) {
        super(balance);
        this.interestRate = interest_Rate;
    }
}
【CustomerReport.java】类
package banking.reports;

import banking.Account;
import banking.Bank;
import banking.Customer;
import banking.SavingsAccount;

public class CustomerReport {

    
    public static void generalReport() {
        Bank bank = Bank.getBank();

        System.out.println("tttCUSTOMERS REPORT");
        System.out.println("ttt================");

        for (int cust_idx = 0; cust_idx < bank.getNumOfCustomers(); cust_idx++) {
            Customer 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 = "";

                // Determine the account type
                
                if (account instanceof SavingsAccount) {
                    account_type = "Savings Account";
                } else {
                    account_type = "Checking Account";
                }

                // Print the current balance of the account
                
                System.out.println("t" + account_type + ": current balance is ¥" + account.getBalance());
            }
        }
    }
}
【TestBanking.java】类
package banking;

import banking.reports.CustomerReport;

import java.text.NumberFormat;

public class TestBanking {

    public static void main(String[] args) {
        NumberFormat currency_format = NumberFormat.getCurrencyInstance();
        Bank bank = Bank.getBank();
        Customer customer;

        // Create several customers and their accounts
        bank.addCustomer("Jane", "Simms");
        customer = bank.getCustomer(0);
        customer.addAccount(new SavingsAccount(500.00, 0.05));
        customer.addAccount(new CheckingAccount(200.00, 400.00));

        bank.addCustomer("Owen", "Bryant");
        customer = bank.getCustomer(1);
        customer.addAccount(new CheckingAccount(200.00));

        bank.addCustomer("Tim", "Soley");
        customer = bank.getCustomer(2);
        customer.addAccount(new SavingsAccount(1500.00, 0.05));
        customer.addAccount(new CheckingAccount(200.00));

        bank.addCustomer("Maria", "Soley");
        customer = bank.getCustomer(3);
        // Maria and Tim have a shared checking account
        customer.addAccount(bank.getCustomer(2).getAccount(1));
        customer.addAccount(new SavingsAccount(150.00, 0.05));

        CustomerReport.generalReport();

    }
}

Java基础实战_Bank项目07 目录结构

【Account.java】类
package banking;

import banking.domain.OverdraftException;

public class Account {

    protected double balance;    //银行帐户的当前(或即时)余额

    //公有构造器 ,这个参数为 balance 属性赋值
    public Account(double init_balance) {
        this.balance = init_balance;
    }

    //用于获取经常余额
    public double getBalance() {
        return balance;
    }

    
    public boolean deposit(double amt){
        balance+=amt;
        return true;
    }

    
    public void withdraw(double amt) throws OverdraftException {
        if (amt < balance){
            balance-=amt;
        }else{
            throw new OverdraftException("资金不足",balance-amt);
        }

    }

}
【Customer.java】类
package banking;

public class Customer {

    private String  firstName;
    private String  lastName;
    private Account[] accounts = new Account[10];
    private int numOfAccounts;

    public Customer(String f, String l) {
        this.firstName = f;
        this.lastName = l;
    }

    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }

    
    public Account getAccount(int index) {
        return accounts[index];
    }

    
    public int getNumOfAccounts() {
        return numOfAccounts;
    }

    
    public void addAccount(Account acct) {
        accounts[numOfAccounts++]=acct;
    }

}
【Bank.java】类
package banking;


public class Bank {

    private Customer[] customers;   //Customer对象的数组
    private int numberOfCustomer;   //整数,跟踪下一个 customers 数组索引
    private static Bank bankInstance = null;
    
    public static Bank getBank(){
        if(bankInstance==null){
            bankInstance = new Bank();
        }
        return bankInstance;
    }
    
    private Bank() {
        customers = new Customer[10];
    }

    
    public void addCustomer(String f,String l){
        customers[numberOfCustomer++]=new Customer(f,l);
    }

    
    public Customer getCustomer(int index) {
        return customers[index];
    }

    public int getNumOfCustomers() {
        return numberOfCustomer;
    }
}
【CheckingAccount.java】类
package banking;

import banking.domain.OverdraftException;

public class CheckingAccount extends Account{

    private double overdraftProtection;

    public CheckingAccount(double balance) {
        super(balance);
    }

    public CheckingAccount(double balance, double protect) {
        super(balance);
        this.overdraftProtection = protect;
    }

    
    @Override
    public void withdraw(double amt) throws OverdraftException {
        if (balance < amt){
            if(overdraftProtection==0){
                throw new OverdraftException("no overdraft protection",amt-balance);
            }
            if (amt-balance>overdraftProtection) {
                throw new OverdraftException("Insufficient funds for overdraft protection",amt);
            }else {
                overdraftProtection-=amt-balance;
                balance=0;
            }
        }else {
            balance-=amt;
        }
    }
}
【SavingsAccount.java】类
package banking;

public class SavingsAccount extends Account{

    private double interestRate;

    public SavingsAccount(double balance, double interest_Rate) {
        super(balance);
        this.interestRate = interest_Rate;
    }
}
【CustomerReport.java】类
package banking.reports;

import banking.Account;
import banking.Bank;
import banking.Customer;
import banking.SavingsAccount;

public class CustomerReport {

    
    public static void generalReport(){
        Bank bank = Bank.getBank();

        System.out.println("tttCUSTOMERS REPORT");
        System.out.println("ttt================");

        for (int cust_idx = 0; cust_idx < bank.getNumOfCustomers(); cust_idx++) {
            Customer 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 = "";

                // Determine the account type
                
                if (account instanceof SavingsAccount) {
                    account_type = "Savings Account";
                } else {
                    account_type = "Checking Account";
                }

                // Print the current balance of the account
                
                System.out.println("t"+account_type + ": current balance is ¥" + account.getBalance());
            }
        }
    }
}
【OverdraftException.java】类
package banking.domain;


public class OverdraftException extends Exception{
    private double deficit;

    public OverdraftException(String message, double deficit) {
        super(message);
        this.deficit = deficit;
    }

    public double getDeficit() {
        return deficit;
    }

}
【TestBanking.java】类
package banking;

import banking.domain.*;

public class TestBanking {

  public static void main(String[] args) {
    Bank bank = Bank.getBank();
    Customer customer;
    Account account;

    // Create two customers and their accounts
    bank.addCustomer("Jane", "Simms");
    customer = bank.getCustomer(0);
    customer.addAccount(new SavingsAccount(500.00, 0.05));
    customer.addAccount(new CheckingAccount(200.00, 500.00));
    bank.addCustomer("Owen", "Bryant");
    customer = bank.getCustomer(1);
    customer.addAccount(new CheckingAccount(200.00));

    // Test the checking account of Jane Simms (with overdraft protection)
    customer = bank.getCustomer(0);
    account = customer.getAccount(1);
    System.out.println("Customer [" + customer.getLastName()
            + ", " + customer.getFirstName() + "]"
            + " has a checking balance of "
            + account.getBalance()
            + " with a 500.00 overdraft protection.");
    try {
      System.out.println("Checking Acct [Jane Simms] : withdraw 150.00");
      account.withdraw(150.00);
      System.out.println("Checking Acct [Jane Simms] : deposit 22.50");
      account.deposit(22.50);
      System.out.println("Checking Acct [Jane Simms] : withdraw 147.62");
      account.withdraw(147.62);
      System.out.println("Checking Acct [Jane Simms] : withdraw 470.00");
      account.withdraw(470.00);
    } catch (OverdraftException e1) {
      System.out.println("Exception: " + e1.getMessage()
              + "   Deficit: " + e1.getDeficit());
    } finally {
      System.out.println("Customer [" + customer.getLastName()
              + ", " + customer.getFirstName() + "]"
              + " has a checking balance of "
              + account.getBalance());
    }
    System.out.println();

    // Test the checking account of Owen Bryant (without overdraft protection)
    customer = bank.getCustomer(1);
    account = customer.getAccount(0);
    System.out.println("Customer [" + customer.getLastName()
            + ", " + customer.getFirstName() + "]"
            + " has a checking balance of "
            + account.getBalance());
    try {
      System.out.println("Checking Acct [Owen Bryant] : withdraw 100.00");
      account.withdraw(100.00);
      System.out.println("Checking Acct [Owen Bryant] : deposit 25.00");
      account.deposit(25.00);
      System.out.println("Checking Acct [Owen Bryant] : withdraw 175.00");
      account.withdraw(175.00);
    } catch (OverdraftException e1) {
      System.out.println("Exception: " + e1.getMessage()
              + "   Deficit: " + e1.getDeficit());
    } finally {
      System.out.println("Customer [" + customer.getLastName()
              + ", " + customer.getFirstName() + "]"
              + " has a checking balance of "
              + account.getBalance());
    }
  }
}

Java基础实战_Bank项目08 目录结构

【Account.java】类
package banking;

import banking.domain.OverdraftException;

public class Account {

    //银行帐户的当前(或即时)余额
    protected double balance;

    //公有构造器 ,这个参数为 balance 属性赋值
    public Account(double init_balance) {
        this.balance = init_balance;
    }

    //用于获取经常余额
    public double getBalance() {
        return balance;
    }

    
    public boolean deposit(double amt){
        balance+=amt;
        return true;
    }

    
    public void withdraw(double amt) throws OverdraftException {
        if (amt < balance){
            balance-=amt;
        }else{
            throw new OverdraftException("资金不足",balance-amt);
        }

    }

}
【Customer.java】类
package banking;

import java.util.ArrayList;
import java.util.List;

public class Customer {

    private String  firstName;
    private String  lastName;
    private List accounts = new ArrayList<>();

    public Customer(String f, String l) {
        this.firstName = f;
        this.lastName = l;
    }

    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }

    
    public Account getAccount(int index) {
        return accounts.get(index);
    }

    
    public int getNumOfAccounts() {
        return accounts.size();
    }

    
    public void addAccount(Account acct) {
        accounts.add(acct);
    }

}

【Bank.java】类
package banking;

import java.util.ArrayList;
import java.util.List;


public class Bank {

    private List customers;   //Customer对象集合
    private static Bank bankInstance = null;

    
    public static Bank getBank(){
        if(bankInstance==null){
            bankInstance = new Bank();
        }
        return bankInstance;
    }

    
    private Bank() {
        customers = new ArrayList<>();
    }

    
    public void addCustomer(String f,String l){
        customers.add(new Customer(f,l));
    }

    
    public Customer getCustomer(int index) {
        return customers.get(index);
    }

    
    public int getNumOfCustomers() {
        return customers.size();
    }
}
【CheckingAccount.java】类
package banking;

import banking.domain.OverdraftException;

public class CheckingAccount extends Account{

    private double overdraftProtection;

    public CheckingAccount(double balance) {
        super(balance);
    }

    public CheckingAccount(double balance, double protect) {
        super(balance);
        this.overdraftProtection = protect;
    }

    
    @Override
    public void withdraw(double amt) throws OverdraftException {
        if (balance < amt){
            if(overdraftProtection==0){
                throw new OverdraftException("no overdraft protection",amt-balance);
            }
            if (amt-balance>overdraftProtection) {
                throw new OverdraftException("Insufficient funds for overdraft protection",amt);
            }else {
                overdraftProtection-=amt-balance;
                balance=0;
            }
        }else {
            balance-=amt;
        }
    }
}
【SavingsAccount.java】类
package banking;

public class SavingsAccount extends Account{

    private double interestRate;

    public SavingsAccount(double balance, double interest_Rate) {
        super(balance);
        this.interestRate = interest_Rate;
    }
}
【CustomerReport.java】类
package banking.reports;

import banking.Account;
import banking.Bank;
import banking.Customer;
import banking.SavingsAccount;

public class CustomerReport {

    
    public void generateReport(){
        Bank bank = Bank.getBank();

        System.out.println("tttCUSTOMERS REPORT");
        System.out.println("ttt================");

        for (int cust_idx = 0; cust_idx < bank.getNumOfCustomers(); cust_idx++) {
            Customer 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 = "";

                // Determine the account type
                
                if (account instanceof SavingsAccount) {
                    account_type = "Savings Account";
                } else {
                    account_type = "Checking Account";
                }

                // Print the current balance of the account
                
                System.out.println("t"+account_type + ": current balance is ¥" + account.getBalance());
            }
        }
    }
}
【OverdraftException.java】类
package banking.domain;


public class OverdraftException extends Exception{
    private double deficit;

    public OverdraftException(String message, double deficit) {
        super(message);
        this.deficit = deficit;
    }

    public double getDeficit() {
        return deficit;
    }

}
【TestBanking.java】类
package banking;

import banking.domain.*;
import banking.reports.CustomerReport;

public class TestBanking {

    public static void main(String[] args) {
        Bank bank = Bank.getBank();
        Customer customer;
        CustomerReport report = new CustomerReport();

        // Create several customers and their accounts
        bank.addCustomer("Jane", "Simms");
        customer = bank.getCustomer(0);
        customer.addAccount(new SavingsAccount(500.00, 0.05));
        customer.addAccount(new CheckingAccount(200.00, 400.00));

        bank.addCustomer("Owen", "Bryant");
        customer = bank.getCustomer(1);
        customer.addAccount(new CheckingAccount(200.00));

        bank.addCustomer("Tim", "Soley");
        customer = bank.getCustomer(2);
        customer.addAccount(new SavingsAccount(1500.00, 0.05));
        customer.addAccount(new CheckingAccount(200.00));

        bank.addCustomer("Maria", "Soley");
        customer = bank.getCustomer(3);
        // Maria and Tim have a shared checking account
        customer.addAccount(bank.getCustomer(2).getAccount(1));
        customer.addAccount(new SavingsAccount(150.00, 0.05));

        // Generate a report
        report.generateReport();
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/459583.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号