zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    vue table 中 列 加上 下划线和click 方法
    vue 比较好的学习文章
    Hive 以及mysql 中如何做except 数据操作
    oracle 日期维表 原始版本 带注解
    RMI 实现的rpc 远程过程调用 Java
    剑指offer20:定义栈的数据结构,请在该类型中实现一个能够得到栈中所含最小元素的min函数(时间复杂度应为O(1))。
    剑指offer19:按照从外向里以顺时针的顺序依次打印出每一个数字,4 X 4矩阵: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 则依次打印出数字1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10.
    模拟通讯录
    剑指offer17:输入两棵二叉树A,B,判断B是不是A的子结构。(ps:我们约定空树不是任意一个树的子结构)
    剑指offer16:输入两个单调递增的链表,合成后的链表满足单调不减规则。
  • 原文地址:https://www.cnblogs.com/afangfang/p/12518311.html
Copyright © 2011-2022 走看看