首页 > 代码库 > 银行类题目
银行类题目
练习1:创建一个简单的银行程序包
练习目标-Java 语言中面向对象的封装性及构造器的使用。
任务
在这个练习里,创建一个简单版本的(账户类)Account类。将这个源文件放入banking程序包中。在创建单个帐户的默认程序包中,已编写了一个测试程序TestBanking。这个测试程序初始化帐户余额,并可执行几种简单的事物处理。最后,该测试程序显示该帐户的最终余额。
1. 创建banking 包
2. 在banking 包下创建Account类。该类必须实现上述UML框图中的模型。
- 声明一个私有对象属性:balance,这个属性保留了银行帐户的当前(或即时)余额。
- 声明一个带有一个参数(init_balance)的公有构造器,这个参数为balance属性赋值。
- 声明一个公有方法getBalance,该方法用于获取经常余额。
- 声明一个公有方法deposit,该方法向当前余额增加金额。
- 声明一个公有方法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+"\t"+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; } }}
银行类题目
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。