• 银行类题目


    练习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

    package banking;
    
    public class Account {
        
        private double balance;
    
        public Account(double balance)
        {
            this.balance=balance;
        }
    
        public double getBalance() {
            System.out.println("当前余额为:"+balance+"元");
            return balance;
        }
        
        public double deposit(double add){                                                                    //取钱方法
            balance+=add;
            System.out.println("存钱"+add+"元,当余额为"+balance+"元");
            return balance;
        }
        
        
        boolean    withdraw(double get){                                                                        //取钱方法
            if(get<=balance)
            {
                System.out.println("取钱"+get+"元,当余额为"+balance+"元");
                return true;
            }
            else
            {
                System.out.println("'余额不足");
                return false;
            }        
        
        }
    
    }

    package banking;
    
    public class Customer {
    
            private String firstName;
            private String lastName;
            private Account account;
            
            public Customer(String firstName,String lastName)                                //账户名字构造方法
            {
                this.firstName=firstName;
                this.lastName=lastName;
                System.out.println("用户名为:"+firstName+"	"+lastName);
            }
            public String getFirstName()
            {
                return firstName;
            }
            public String getLastName()
            {
                return lastName;
            }
            
    
            public void setAccount(Account account) {
                this.account = account;
            }
            
            public Account getAccount() {
                return account;
            }
            @Override
            public String toString() {
                return "Customer [account=" + account + "]";
            }
            
    }

     

    package banking;
    
    public class Bank {
    
        private Customer customers[];
        private int numberOfCustomers[];
        
        
        //公有构造器
        public Bank() {
            customers = new Customer[5];
            numberOfCustomers = new int[5];
        }
        
        public static int index = 0;
        public static int c= 0;
        
        public void addCustome(String firstName,String lastName)
        {
            index=+c;
            customers[index] = new Customer(firstName, lastName);
            numberOfCustomers[index]=++c;            
        }
        public int getNumOfCustomers()
        {
            return numberOfCustomers.length;
        }
        
        public Customer getCustomer(int index)
        {
            return customers[index];
        }
        
        
        
        
        
    }

    package banking;
    
    public class SavingAccount extends Account {                //存款账户
        
        private double interestRate;
        
    
    
        public double getInterestRate() {                
            return interestRate;
        }
    
        public void setInterestRate(double interestRate) {
            this.interestRate = interestRate;
        }
        
        public SavingAccount(double balance,double interestRate)
        {
            super(balance);
            this.interestRate=interestRate;
        }
        
    }
    package banking;
    
    public class CheckingAccount extends Account{                                                    //透支账户
        
        private double overdraftProtection;                                                                    //属性
    
        public double getOverdraftProtection() {
            return overdraftProtection;
        }
    
        public void setOverdraftProtection(double overdraftProtection) {
            this.overdraftProtection = overdraftProtection;
        }
        
        
        public CheckingAccount(double balance) {                                                        //构造方法
    
            super(balance);
        }
    
        
        public CheckingAccount (double balance,double overdraftProtection)
        {
            super(balance);
            this.overdraftProtection=overdraftProtection;
        }
    
        boolean    withdraw(double get){                                                                        //覆盖取钱方法
            if(balance-get>0)
            {
                balance = balance - get;
                System.out.println("取钱"+get+"元,当余额为"+balance+"元");
                return true;
            }
            else
            {
                if(get<=getOverdraftProtection())
                System.out.println("'余额为:"+(balance-get));
                else
                 System.out.println("请在可透支范围内提款!");
                return true;
            }        
                
    }}
  • 相关阅读:
    redis-cli 通过管道 --pipe 快速导入数据到redis中
    Redis主从配置和哨兵监控配置——服务器端环境搭建
    构建一个maven聚合类型的横向可扩展项目
    Mybatis技术原理理——整体流程理解
    mybatis 插入 含有美元符号($) 字符串 报 java.lang.IndexOutOfBoundsException: No group 2 的问题
    SpringBoot+thymeleaf+security+vue搭建后台框架 基础篇(一)
    Java集合的总结
    FTP文件上传 支持断点续传 并 打印下载进度(二) —— 单线程实现
    FTP文件上传并支持断点续传(一)—— win10 本地环境 ftp站点构建
    Spring事务管理——基础会用篇
  • 原文地址:https://www.cnblogs.com/zs6666/p/5907643.html
Copyright © 2020-2023  润新知