求帮忙写个类啊

bshedu5 2012-02-20 10:44:33
创建3个类,其中Account类用来模拟银行账户,每个银行账户包括属性:账户号、余额、年利率、开户时间,并有取款(withdraw)和存款(deposit)方法。在此基础上,创建2个子类,分别为支票账户(checking account)和储蓄账户(saving account)。支票账户有一定的透支额,储蓄账户不能透支。实现这几个类,并书写一个测试程序,创建Account, SavingsAccount, 和 CheckingAccount对象,并调用它们的toString()方法。
...全文
341 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
snowboy8886 2012-02-20
  • 打赏
  • 举报
回复
银行里的数据一般用的是BigDecimal类型
安特矮油 2012-02-20
  • 打赏
  • 举报
回复

package t;

import java.util.Date;

public class Test {

public static void main(String[] args) {
Account a1 = new CheckingAccount(1000.00);
a1.setBalance(1000.00);
a1.setCardNo("000001");
a1.setCreateDate(new Date());
a1.setInterestRate(0.03);
a1.deposit(1000.00);
a1.withdraw(4000);

Account a2 = new SavingAccount();
a2.setBalance(1000.00);
a2.setCardNo("000002");
a2.setCreateDate(new Date());
a2.setInterestRate(0.03);
a2.deposit(1000.00);
a2.withdraw(4000);

}
}

安特矮油 2012-02-20
  • 打赏
  • 举报
回复

package t;

public class SavingAccount extends Account{

@Override
public void deposit(double money) {
setBalance(getBalance() + money);
}

@Override
public double withdraw(double money) {
double balance = getBalance();
if(money > balance){
System.out.println("余额不足!");
return 0;
}
setBalance(balance - money);
return money;
}

}

安特矮油 2012-02-20
  • 打赏
  • 举报
回复

package t;

public class CheckingAccount extends Account{

public CheckingAccount(double overdraft){
setOverdraft(overdraft);
}

private double overdraft;

public double getOverdraft() {
return overdraft;
}

public void setOverdraft(double overdraft) {
this.overdraft = overdraft;
}

@Override
public void deposit(double money) {
setBalance(getBalance() + money);
}

@Override
public double withdraw(double money) {
double balance = getBalance();
if(money > balance + overdraft){
System.out.println("余额不足!");
return 0;
}
setBalance(balance - money);
return money;
}

}

安心逍遥 2012-02-20
  • 打赏
  • 举报
回复
呵呵,有人写好了
安特矮油 2012-02-20
  • 打赏
  • 举报
回复

package t;

import java.util.Date;

public abstract class Account {

private String cardNo;
private double balance;
private double interestRate;
private Date createDate;
public String getCardNo() {
return cardNo;
}
public void setCardNo(String cardNo) {
this.cardNo = cardNo;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public double getInterestRate() {
return interestRate;
}
public void setInterestRate(double interestRate) {
this.interestRate = interestRate;
}
public Date getCreateDate() {
return createDate;
}
public void setCreateDate(Date createDate) {
this.createDate = createDate;
}

public abstract double withdraw(double money);
public abstract void deposit(double money);

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
long temp;
temp = Double.doubleToLongBits(balance);
result = prime * result + (int) (temp ^ (temp >>> 32));
result = prime * result + ((cardNo == null) ? 0 : cardNo.hashCode());
result = prime * result
+ ((createDate == null) ? 0 : createDate.hashCode());
temp = Double.doubleToLongBits(interestRate);
result = prime * result + (int) (temp ^ (temp >>> 32));
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
final Account other = (Account) obj;
if (Double.doubleToLongBits(balance) != Double
.doubleToLongBits(other.balance))
return false;
if (cardNo == null) {
if (other.cardNo != null)
return false;
} else if (!cardNo.equals(other.cardNo))
return false;
if (createDate == null) {
if (other.createDate != null)
return false;
} else if (!createDate.equals(other.createDate))
return false;
if (Double.doubleToLongBits(interestRate) != Double
.doubleToLongBits(other.interestRate))
return false;
return true;
}
@Override
public String toString() {
return super.toString();
}
}

CN_Anyoo 2012-02-20
  • 打赏
  • 举报
回复
lx继续
小V小V志 2012-02-20
  • 打赏
  • 举报
回复
好了。。。发不上去

67,515

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧