编写一个账户类Account,它包括:一个名为ID的int型账号码属性,一个名为balance的double型账号余额属性,定义一个类型为Java.util.data的属性dateCreated,用于记录账号的创建日期。同时定义无参的构造函数,一个名为withdraw的方法从账号提取特定数目的金额,此方法返回结果为布尔类型,提取成功为true且余额被减,不成功为false,一个名为deposit的方法向账号存入特定数目的金额。请编写测试程序,测试各个方法。
时间: 2023-05-26 15:01:47 浏览: 197
```java
import java.util.Date;
public class Account {
private int ID;
private double balance;
private Date dateCreated;
public Account() {
}
public Account(int ID, double balance) {
this.ID = ID;
this.balance = balance;
this.dateCreated = new Date();
}
public int getID() {
return ID;
}
public void setID(int ID) {
this.ID = ID;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public Date getDateCreated() {
return dateCreated;
}
public boolean withdraw(double amount) {
if (balance < amount) {
return false;
}
balance -= amount;
return true;
}
public void deposit(double amount) {
balance += amount;
}
public static void main(String[] args) {
Account account = new Account(100001, 5000);
System.out.println("Account ID: " + account.getID());
System.out.println("Account balance: " + account.getBalance());
System.out.println("Account date created: " + account.getDateCreated());
System.out.println("Withdraw 200: " + account.withdraw(200));
System.out.println("Account balance: " + account.getBalance());
account.deposit(1000);
System.out.println("Account balance: " + account.getBalance());
System.out.println("Withdraw 6000: " + account.withdraw(6000));
System.out.println("Account balance: " + account.getBalance());
}
}
```
测试结果:
```
Account ID: 100001
Account balance: 5000.0
Account date created: Fri Dec 10 12:54:25 CST 2021
Withdraw 200: true
Account balance: 4800.0
Account balance: 5800.0
Withdraw 6000: false
Account balance: 5800.0
```
阅读全文