• Java-小练习简单银行程序


    2练习1:创建一个简单的银行程序包

     

    练习目标-Java 语言中面向对象的封装性及构造器的使用。

    任务

    在这个练习里,创建一个简单版本的(账户类)Account类。将这个源文件放入banking程序包中。在创建单个帐户的默认程序包中,已编写了一个测试程序TestBanking。这个测试程序初始化帐户余额,并可执行几种简单的事物处理。最后,该测试程序显示该帐户的最终余额。

    1.  创建banking 包

    2.  在banking 包下创建Account类。该类必须实现上述UML框图中的模型。

    1. 声明一个私有对象属性:balance,这个属性保留了银行帐户的当前(或即时)余额。
    2. 声明一个带有一个参数(init_balance)的公有构造器,这个参数为balance属性赋值。
    3. 声明一个公有方法getBalance,该方法用于获取经常余额。
    4. 声明一个公有方法deposit,该方法向当前余额增加金额。
    5. 声明一个公有方法withdraw从当前余额中减去金额。

    3.  编译TestBanking.java文件。

    4.  运行TestBanking类。可以看到下列输出结果:

      Creating an account with a 500.00 balance

    Withdraw 150.00

    Deposit 22.50

    Withdraw 47.62

    The account has a balance of 324.88

    练习2

     

    练习目标-使用引用类型的成员变量:在本练习中,将扩展银行项目,添加一个(客户类)Customer类。Customer类将包含一个Account对象。

    任务

    1. 在banking包下的创建Customer类。该类必须实现上面的UML图表中的模型。

    a. 声明三个私有对象属性:firstName、lastName和account。

    b. 声明一个公有构造器,这个构造器带有两个代表对象属性的参数(f和l)

    c. 声明两个公有存取器来访问该对象属性,方法getFirstName和getLastName返回相应的属性。

    d. 声明setAccount 方法来对account属性赋值。

    e. 声明getAccount 方法以获取account属性。

    1. 在exercise2主目录里,编译运行这个TestBanking程序。应该看到如下输出结果:

    Creating the customer Jane  Smith.

    Creating her account  with  a  500.00  balance.

    Withdraw 150.00

    Deposit 22.50

    Withdraw 47.62

    Customer [Smith, Jane] has a balance of 324.88

    练习3:修改withdraw 方法

    练习目标-使用有返回值的方法:在本练习里,将修改withdraw方法以返回一个布尔值来指示交易是否成功。

    任务

    1.  修改Account类

    1. 修改deposit 方法返回true(意味所有存款是成功的)。
    2. 修改withdraw方法来检查提款数目是否大于余额。如果amt小于balance,则从余额中扣除提款数目并返回true,否则余额不变返回false。

    2.  在exercise2主目录编译并运行TestBanking程序,将看到下列输出;

    Creating the customer Jane Smith.

    Creating her account with a 500.00 balance.

    Withdraw 150.00: true

    Deposit 22.50: true

    Withdraw 47.62: true

    Withdraw 400.00: false

    Customer [Smith, Jane] has a balance of 324.88

    练习4:用数组表示多重性

     

    练习目标-在类中使用数组作为模拟集合操作: 在本练习中,将用数组实现银行与客户间的多重关系。

    任务

    对银行来说,可添加Bank类。 Bank 对象跟踪自身与其客户间的关系。用Customer对象的数组实现这个集合化的关系。还要保持一个整数属性来跟踪银行当前有多少客户。

    1. 创建 Bank 类
    1. 为Bank类增加两个属性:customers(Customer对象的数组)和numberOfCustomers(整数,跟踪下一个customers数组索引)
    1. 添加公有构造器,以合适的最大尺寸(至少大于5)初始化customers数组。
    1. 添加addCustomer方法。该方法必须依照参数(姓,名)构造一个新的Customer对象然后把它放到customer数组中。还必须把numberofCustomers属性的值加1。
    1. 添加getNumOfCustomers 访问方法,它返回numberofCustomers属性值。
    1. 添加getCustomer方法。它返回与给出的index参数相关的客户。
    1. 编译并运行TestBanking程序。可以看到下列输出结果:

    Customer [1] is Simms,Jane

    Customer [2] is Bryant,Owen

       Customer [3] is Soley,Tim

       Customer [4] is Soley,Maria

    练习4:用List表示多重性

     

    练习目标-在类中使用List作为模拟集合操作: 在本练习中,将用List实现银行与客户间的多重关系。

    任务

    对银行来说,可添加Bank类。 Bank 对象跟踪自身与其客户间的关系。用Customer对象的List实现这个集合化的关系。还要保持一个整数属性来跟踪银行当前有多少客户。

    1. 创建 Bank 类
    1. 为Bank类增加两个属性:customers(Customer对象的List)和numberOfCustomers(整数, 当前Customer对象的数量)
    1. 添加公有构造器,初始化customersList
    1. 添加addCustomer方法。该方法必须依照参数(姓,名)构造一个新的Customer对象然后把它放到customerList中。
    1. 添加getNumOfCustomers 访问方法,它返回numberofCustomers属性值。
    1. 添加getCustomer方法。它返回与给出的index参数相关的客户。
    1. 编译并运行TestBanking程序。可以看到下列输出结果:

    Customer 1 is Simms,Jane

    Customer 2 is Bryant,Owen

       Customer 3 is Soley,Tim

       Customer 4 is Soley,Maria

    当前客户数量 = 4

    第一小问:

    package com.banking;
    
    public class Account {
        //属性
        private double balance;
        //构造方法
        Account(int balance)
        {
            this.balance=balance;
        }
        
        public Account() {
            super();
        }
    
        //成员方法
        public double getBalance()
        {
            return this.balance;
        }
        public double deposit(double amt)
        {
             this.balance+=amt;
             return amt;
        }
        
        public double withdraw(double amt)
        {
            this.balance-=amt;
            return amt;
        }
    }

    测试:

    package com.banking;
    
    public class Test01 {
    
        public static void main(String[] args) {
            
        Account ac=new Account(500);
            System.out.println("取款金额:"+ac.withdraw(150));
            System.out.println("存款金额:"+ac.deposit(22.5));
            System.out.println("取款金额:"+ac.withdraw(47.62));
            System.out.println("剩余金额:"+ac.getBalance());
            
            }
    
    }

    结果:

    第二小问:

    package com.banking;
    
    public class Customer {
        private String firstName;
        private String lastName;
        private Account account;
        
        Customer(String f,String l)
        {
            this.firstName=f;
            this.lastName=l;
        }
    
        
        @Override
        public String toString() {
            return firstName + "," + lastName ;
        }
    
    
        public Account getAccount() {
            return account;
        }
    
        public void setAccount(Account acct) {
            this.account = acct;
        }
    
        public String getFirstName() {
            return firstName;
        }
    
        public String getLastName() {
            return lastName;
        }
    
    }

    测试:

    package com.banking;
    
    public class Test02 {
    
        public static void main(String[] args) {
            Customer cu=new Customer("Jane","Smith");
            System.out.println("Creating the customer"+" "+cu.getFirstName()+" "+cu.getLastName());
            
            
            cu.setAccount(new Account(500));
    //        cu.setAccount(ac);
    //        cu.getAccount();
            System.out.println("取款金额:"+cu.getAccount().withdraw(150));
            System.out.println("存款金额:"+cu.getAccount().deposit(22.5));
            System.out.println("取款金额:"+cu.getAccount().withdraw(47.62));
            System.out.println("剩余金额:"+cu.getAccount().getBalance());
            
        }
    
    }

    结果:

     第三小问”:

    package com.banking;
    
    public class Account {
        //属性
        private double balance;
        //构造方法
        Account(int balance)
        {
            this.balance=balance;
        }
        
        public Account() {
            super();
        }
    
        //成员方法
        public double getBalance()
        {
            return this.balance;
        }
        public boolean deposit(double amt)
        {
            this.balance+=amt;
             System.out.print("deposit "+amt);
             return true;
        }
        
        public boolean withdraw(double amt)
        {
            System.out.print("withdraw "+amt);
            if(amt<balance){
            this.balance-=amt;
            
            return true;
            }
            else
            {
                this.balance=amt;
                return false;
            }
        }
    }

    测试:

    package com.banking;
    
    public class Test02 {
    
        public static void main(String[] args) {
            Customer cu=new Customer("Jane","Smith");
            System.out.println("Creating the customer"+" "+cu.getFirstName()+" "+cu.getLastName());
            
            
            cu.setAccount(new Account(500));
    //        cu.setAccount(ac);
    //        cu.getAccount();
            System.out.println(" 取款金额:"+cu.getAccount().withdraw(150));
            System.out.println(" 存款金额:"+cu.getAccount().deposit(22.5));
            System.out.println(" 取款金额:"+cu.getAccount().withdraw(47.62));
            System.out.println(" 取款金额:"+cu.getAccount().withdraw(400));
            System.out.println(cu.toString()+" "+"剩余金额:"+cu.getAccount().getBalance());
            
        }
    
    }

    结果:

     

    第四小问:

    package com.banking;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class Bank {
        //定义属性
        private List<Customer> customer; 
        private int numberOfCustomers;
        
        //构造器
        public Bank(){
            customer=new ArrayList<>();
            
        }
        //定义方法,添加数据
        public void addCustomer(String f,String l){      
            Customer cu=new Customer(f,l);
            customer.add(cu);
        }
        //获取数组个数
        public int getNumOfCustomers(){
            return customer.size();
        }
        
        public Customer getCustomer(int index){
            return customer.get(index);
        }
    
    }

    测试:

    package com.banking;
    
    import java.util.Iterator;
    import java.util.List;
    
    public class TestBank {
    
        public static void main(String[] args) {
            Bank ba=new Bank();
            ba.addCustomer("Simms","Jane");
            ba.addCustomer("Bryant","Owen");
            ba.addCustomer("Soley","Tim");
            ba.addCustomer("Soley","Maria");
            for(int i=0;i<ba.getNumOfCustomers();i++){
                System.out.println("Customer "+(i+1)+" is "+ba.getCustomer(i));
                
            }
            System.out.println("当前客户数量="+ba.getNumOfCustomers());
        }
    
    }

    结果:

  • 相关阅读:
    keras:InternalError: Failed to create session
    centos 常用命令
    centos7 安装gdal2.3.1
    centos mysql初探 -- 配置、基本操作及问题
    machine learning 之 Recommender Systems
    machine learning 之 Anomaly detection
    centos R包 tidyverse安装
    centos 问题解决记录
    R python在无图形用户界面时保存图片
    隐私政策
  • 原文地址:https://www.cnblogs.com/tfl-511/p/5906946.html
Copyright © 2020-2023  润新知