永发信息网

1小时求答案。编写一个完整的JavaApplication程序。包含类Acount、类CheckingAccount、类AcountTest

答案:2  悬赏:30  手机版
解决时间 2021-01-03 07:52
  • 提问者网友:城市野鹿
  • 2021-01-03 03:40
(1)编写一个类Account表示账户对象,包含以下成员
①属性:
1)id:私有,int型,表示账户编号;
2)balance:私有,double型,表示账户余额;
②方法:
1)Account(), 构造方法,id和balance都初始化为0;
2)Account(int id,double balance),构造方法,用参数设置账户编号和余额;
3)void setBalance(double balance):修改账户金额
4)double getBalance():返回账户金额
5)boolean withDraw(double money):从账户提取特定数额,如果余额不足,返回false;否则,修改余额,返回true;
6)void deposit(double money):向账户存储特定数额。
7)public String toString():将把当前账户对象的转换成字符串形式,例如id为123,余额为1000.0,返回字符串"The balance of account 123 is 1000.0"。
(2)编写一个Account类的子类CheckingAccount,表示支票账户对象,包含以下成员
①属性:
1)overdraft:私有,double型,表示透支限定额;
②方法:
1)CheckingAccount(), 构造方法,id、balance和overdraft都初始化为0;
2)CheckingAccount(int id,double balance,double overdraft),构造方法,用参数设置账户编号、余额和透支限定额;
3)boolean withDraw(double money):从账户提取特定数额,如果超出透支限定额,返回false;否则,修改余额,返回true;
(3)编写公共类AcountTest,实现如下功能
1)创建一个账户id为1122、余额为20000、透支额度为10000的支票账户;
2)存款10000
3)取款40000,并显示是否取款成功
4)使用toString方法输出账户信息。
输出格式如下:
WithDraw 40000 successes
The balance of account 1122 is -10000.0
最佳答案
  • 五星知识达人网友:怀裏藏嬌
  • 2021-01-07 02:24
class Account
{
private int id;
private double balance;
public Account()
{
id=0;
balance=0;
}
public Account(int id,double balance)
{
this.id=id;
this.balance=balance;
}
public void setBalance(double balance)
{
this.balance=balance;
}
public double getBalance()
{
return balance;
}
public int getID()
{
return id;
}
public boolean withDraw(double money)
{
if(money>this.balance)
{
return false;
}
else
{
this.balance-=money;
return true;
}
}
public void deposit(double money)
{
this.balance+=money;
}
public String toString()
{
return "The balance of Account "+this.id+" is " +this.balance;
}
}
public class CheckingAccount extends Account
{
private double overdraft;
public CheckingAccount()
{
super(0,0);
this.overdraft=0;
}
public CheckingAccount(int id,double balance,double overdraft)
{
super(id,balance);
this.overdraft=overdraft;
}
public boolean withDraw(double money)
{
Account of=new Account();
if(of.getBalance()-money>this.overdraft)
{
System.out.println("withDraw is failure");
return false;
}
else
{
of.setBalance(of.getBalance()-money);
System.out.println("withDraw is successful");
return true;
}
}
}
public class AccountTest
{
public static void main(String[] args)
{
Account test=new CheckingAccount(1122,20000,10000);
test.deposit(10000);
CheckingAccount of=new CheckingAccount();
of.withDraw(40000);
System.out.println(test);
}

}
全部回答
  • 1楼网友:千夜
  • 2021-01-07 02:53
你好! Withdraw 40000 SUCCSS. The balance of account 1122 is 0.0 如有疑问,请追问。
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯