6-5 普通账户和支票账户 (10 分)
编写一个Java程序,包含类Acount、CheckingAccount、Main,其中Main已经实现,请你编写Acount和CheckingAccount类。
(1)编写一个类Account表示普通账户对象,包含以下成员
①属性:
id:私有,int型,表示账户编号;
balance:私有,int型,表示账户余额;
②方法:
Account(), 构造方法,id和balance都初始化为0;
Account(int id,int balance),构造方法,用参数设置账户编号和余额;
void setBalance(int balance):修改账户金额
int getBalance():返回账户金额
boolean withdraw(int money):从账户提取特定数额,如果余额不足,返回false;否则,修改余额,返回true;
void deposit(int money):向账户存储特定数额。
public String toString():将把当前账户对象的信息转换成字符串形式,例如id为123,余额为1000,返回字符串"The balance of account 123 is 1000"。
(2)编写一个Account类的子类CheckingAccount,表示支票账户对象,包含以下成员
①属性:
overdraft:私有,int型,表示透支限定额;
②方法:
CheckingAccount(), 构造方法,id、balance和overdraft都初始化为0;
CheckingAccount(int id,int balance,int overdraft),构造方法,用参数设置账户编号、余额和透支限定额;
boolean withdraw(int money):从账户提取特定数额,如果超出透支限定额,返回false;否则,修改余额,返回true;
(3)编写公共类Main,实现如下功能
根据用户输入的两个整数id和balance创建普通账户a;
输入一个整数n,表示对账户a有n笔操作;
每笔操作输入一个字符串和一个整数money(表示操作金额)
如果字符串为“withdraw”表示取现操作,如果操作成功,输出“withdraw ” + money + “success”,否则输出“withdraw ” + money + “failed”
如果字符串为“deposit”表示存入操作,完成操作后输出“deposit” + money + “success”
使用toString方法输出账户a信息。
根据用户输入的三个整数id、balance和overdraft创建支票账户b;
输入一个整数m,表示对账户b有m笔操作;
每笔操作输入一个字符串和一个整数money(表示操作金额)
如果字符串为“withdraw”表示取现操作,如果操作成功,输出“withdraw ” + money + “success”,否则输出“withdraw ” + money + “failed”
如果字符串为“deposit”表示存入操作,完成操作后输出“deposit” + money + “success”
使用toString方法输出账户b信息。
裁判测试程序样例:
import java.util.Scanner;
public class Main{
public static void main(String args[]){
Scanner input = new Scanner(System.in);
int n,m;
Account a = new Account(input.nextInt(),input.nextInt());
n = input.nextInt();
for(int i=0; i < n; i++) {
String op;
int money;
op = input.next();
money = input.nextInt();
if(op.equals("withdraw")) {
if(a.withdraw(money)) {
System.out.println("withdraw " + money + " success");
} else {
System.out.println("withdraw " + money + " failed");
}
} else if(op.equals("deposit")) {
a.deposit(money);
System.out.println("deposit " + money + " success");
}
}
System.out.println(a.toString());
CheckingAccount b = new CheckingAccount(input.nextInt(),input.nextInt(),input.nextInt());
m = input.nextInt();
for(int i=0; i < m; i++) {
String op;
int money;
op = input.next();
money = input.nextInt();
if(op.equals("withdraw")) {
if(b.withdraw(money)) {
System.out.println("withdraw " + money + " success");
} else {
System.out.println("withdraw " + money + " failed");
}
} else if(op.equals("deposit")) {
b.deposit(money);
System.out.println("deposit " + money + " success");
}
}
System.out.println(b.toString());
}
}
输入样例:
1 100
5
withdraw 200
withdraw 100
deposit 50
deposit 100
withdraw 200
2 100 200
5
withdraw 200
withdraw 100
deposit 50
deposit 100
withdraw 200
输出样例:
withdraw 200 failed
withdraw 100 success
deposit 50 success
deposit 100 success
withdraw 200 failed
The balance of account 1 is 150
withdraw 200 success
withdraw 100 success
deposit 50 success
deposit 100 success
withdraw 200 failed
The balance of account 2 is -50
答案如下
答案如下
class Account{
private int id;
private int balance;
Account(){
id=0;
balance=0;
}
Account(int id,int balance){
this.id=id;
this.balance=balance;
}
void setBalance(int balance) {
this.balance=balance;
}
int getBalance() {
return balance;
}
boolean withdraw(int money) {
if(balance<money) {
return false;
}
else {
balance=balance-money;
return true;
}
}
void deposit(int money) {
this.balance+=money;
}
public String toString() {
return "The balance of account "+id+" is "+this.balance;
}
}
class CheckingAccount extends Account{
private int overdraft;
public CheckingAccount(int a,int b,int c){
super(a,b);
this.overdraft=c;
}
boolean withdraw(int money) {
if(money>(overdraft+this.getBalance())) {
return false;
}
else {
this.setBalance(this.getBalance()-money);
return true;
}
}
}