目录
题目内容:
1、创建账户类Account,内容如下:
(1)成员变量:账户id,实名name、账户余额balance,开户日期dateCreated(Date类型)
(2)构造方法,2个参数,为id和实名赋初值。开户日期为系统当前时间
(3)get和set方法
(4)取款方法withdraw,从账户提取指定数额,余额不足,不可以取款,提示用户
(5)存款方法deposit,向账户存入指定数额
(6)转账方法transfer(Account a ,double money),向指定账户转指定数额,余额不足,不可以转账,提示用户
(7)public String toString()方法:返回字符串,格式为“帐号:id\t实名:name\t账户余额:balance\t开户时间: dateCreated(日期要求格式化)
2、测试类AccountTest
(1)用自己的姓名、学号创建账户对象
(2)使用deposit方法向账户存入3000元,调用toString方法输出账户信息
(3)使用withdraw方法从账户取出2500元,调用toString方法输出账户信息
(4)创建另一个账户对象,向其转账100元,输出两个账户信息
(5)测试取钱和转账时余额不足的情况
技能训练内容(代码和结果截图):
创建账户类Account
package Student;
import java.util.Date;
public class Account {
private String id;
private String name;
private double balance;
private Date dateCreated = new Date();
public Account(String id, String name) {
this.id = id;
this.name = name;
}
// 构造对应的set和get方法
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public Date getDateCreated() {
return dateCreated;
}
public void setDateCreated(Date dateCreated) {
this.dateCreated = dateCreated;
}
void withdraw(double Moneywithdraw) {//构造取款方法
if (balance - Moneywithdraw < 0) {//判断
System.out.println("余额不足,不可以取款");
} else {
balance = balance - Moneywithdraw;
System.out.println("账户余额:" + balance);
}
}
void deposit(double Moneydeposit) {//构造存款方法
balance = balance + Moneydeposit;
System.out.println("账户余额:" + balance);
}
void transfer(Account a, double money) {//构造转账方法
if (balance < money) {//判断
System.out.println("余额不足,不可以转账");
} else {
balance = balance - money;
System.out.println("账户余额:" + balance);
}
}
public String toString() {
return ("账号:" + id + "\t实名:" + name + "\t账户余额" + balance + "\t开户时间:" + dateCreated);
}
}
测试类AccountTest
package Student;
public class TestAccount {
public static void main(String[] args) {
Account p1 = new Account("200405216", "蜡笔小新");// 创建账户对象
p1.deposit(3000);
System.out.println(p1.toString());
p1.withdraw(2500);
System.out.println(p1.toString());// 创建另一个账户对象
Account p2 = new Account("200205218", "胡图图");
p1.transfer(p2, 100);
System.out.println(p1.toString());
System.out.println(p2.toString());
// 测试取钱和转账时余额不足的情况
p1.withdraw(2500);
p1.transfer(p2, 100);
System.out.println(p1.toString());
System.out.println(p2.toString());
}
}
输出内容:
版权声明:本文为weixin_55540029原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。