• Bank5


    Account:

    package banking5;
    //账户
    
    public class Account {
        protected double balance;
    
        public Account(double int_balance) {
            balance = int_balance;
        }
    
        public double getBlance() {
            return balance;
        }
    
        public boolean deposit(double amt) {
            balance += amt;
            return true;
        }
    
        public boolean withdraw(double amt) {
            if (balance >= amt) {
                balance -= amt;
                return true;
            } else {
                System.out.println("余额不足");
                return false;
            }
        }
    }

    Customer:

    package banking5;
    
    public class Customer {
        private String firstName;
        private String lastName;
        private Account account;
    
        public Customer(String f, String l) {
            firstName = f;
            lastName = l;
        }
    
        public String getFirstName() {
            return firstName;
        }
    
        public String getLastName() {
            return lastName;
        }
    
        public Account getAccount() {
            return account;
        }
    
        public void setAccount(Account acct) {
            account = acct;
        }
    }

    Bank:

    package banking5;
    
    public class Bank {
    
        private Customer[] customers;
        private int numberOfCustomers;
    
        public Bank() {
            customers = new Customer[5];
        }
    
        public void addCustomer(String f, String l) {
            Customer cust = new Customer(f, l);
            customers[numberOfCustomers] = cust;
            numberOfCustomers++;
        }
    
        public int getNumberofCustomer() {
            return numberOfCustomers;
        }
    
        public Customer getCustomer(int index) {
            return customers[index];
        }
    }

    CheckingAccount:

    package banking5;
    
    //信用卡账户
    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;
        }
    
        public double getOverdraftProtection() {
            return overdraftProtection;
        }
    
        public void setOverdraftProtection(double overdraftProtection) {
            this.overdraftProtection = overdraftProtection;
        }
    
        public boolean withdraw(double amt) {
            if (balance >= amt) {
                balance -= amt;
                return true;
            } else if (overdraftProtection >= amt - balance) {
    
                overdraftProtection -= (amt - balance);
                balance = 0;
                return true;
            } else {
                System.out.println("额度不够");
                return false;
            }
        }
    }

    SavingAccount:

    package banking5;
    
    public class SavingAccount extends Account {
        private double interestRate;// 利率
    
        public SavingAccount(double balance, double init_rate) {
            super(balance);
            this.interestRate = init_rate;
        }
    
        public double getInterestRate() {
            return interestRate;
        }
    
        public void setInterestRate(double interestRate) {
            this.interestRate = interestRate;
        }
    
    }

    TestBanking5:

    package TestBanking;
    /*
     * This class creates the program to test the banking classes.
     * It creates a new Bank, sets the Customer (with an initial balance),
     * and performs a series of transactions with the Account object.
     */
    
    import banking5.Account;
    import banking5.Bank;
    import banking5.CheckingAccount;
    import banking5.Customer;
    import banking5.SavingAccount;
    
    public class TestBanking5 {
    
        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
            account = new SavingAccount(500.00, 0.03);
            customer = bank.getCustomer(0);
            customer.setAccount(account);
    
            System.out.println("Creating her Savings Account with a 500.00 balance and 3% interest.");
            // code
            System.out.println("Creating the customer Owen Bryant.");
            // code
            bank.addCustomer("Owner", "Bryant");
            customer = bank.getCustomer(1);
            account = new CheckingAccount(500.00);
            customer.setAccount(account);
            System.out.println("Creating his Checking Account with a 500.00 balance and no overdraft protection.");
            // code
    
            System.out.println("Creating the customer Tim Soley.");
            bank.addCustomer("Tim", "Soley");
            customer = bank.getCustomer(2);
            account = new CheckingAccount(500.00, 500.00);
            customer.setAccount(account);
    
            System.out.println("Creating his Checking Account with a 500.00 balance and 500.00 in overdraft protection.");
            // code
            System.out.println("Creating the customer Maria Soley.");
            // code
            bank.addCustomer("Maria", "Soley");
            customer = bank.getCustomer(3);
            // account =bank.getCustomer(2).getAccount();
    
            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.getBlance());
    
            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.getBlance());
    
            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.getBlance());
    
            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.getBlance());
        }
    }


    输出结果:

    Creating the customer Jane Smith.
    Creating her Savings Account with a 500.00 balance and 3% interest.
    Creating the customer Owen Bryant.
    Creating his Checking Account with a 500.00 balance and no overdraft protection.
    Creating the customer Tim Soley.
    Creating his Checking Account with a 500.00 balance and 500.00 in overdraft protection.
    Creating the customer Maria Soley.
    Maria shares her Checking Account with her husband Tim.

    
    

    Retrieving the customer Jane Smith with her savings account.
    Withdraw 150.00: true
    Deposit 22.50: true
    Withdraw 47.62: true
    余额不足
    Withdraw 400.00: false
    Customer [Simms, Jane] has a balance of 324.88

    
    

    Retrieving the customer Owen Bryant with his checking account with no overdraft protection.
    Withdraw 150.00: true
    Deposit 22.50: true
    Withdraw 47.62: true
    额度不够
    Withdraw 400.00: false
    Customer [Bryant, Owner] has a balance of 324.88

    
    

    Retrieving the customer Tim Soley with his checking account that has overdraft protection.
    Withdraw 150.00: true
    Deposit 22.50: true
    Withdraw 47.62: true
    Withdraw 400.00: true
    Customer [Soley, Tim] has a balance of 0.0

    
    

    Retrieving the customer Maria Soley with her joint checking account with husband Tim.
    Deposit 150.00: true
    额度不够
    Withdraw 750.00: false
    Customer [Soley, Maria] has a balance of 150.0

     
    All that work will definitely pay off
  • 相关阅读:
    CLR via C#
    一些写英文简历的词汇
    组合与组合数
    A lowlevel Look at the ASP.NET Architecture
    \r与\n的区别,\r\n与\n或\r的区别(C语言/C#)
    Canvas lineWidth 属性 【每日一段代码18】
    程序员三大世界观 如何看待HTML5
    Canvas运用样式与颜色fillStyle【每日一段代码15】
    Canvas绘制路径:贝塞尔曲线【每日一段代码12】
    Canvas绘制弧形【每日一段代码10】
  • 原文地址:https://www.cnblogs.com/afangfang/p/12518311.html
Copyright © 2020-2023  润新知