java程序设计 新的account类(transactions数组)
添加一个String类型的数据域name来存储客户名字添加一个新的构造方法,该方法创建一个带着定名字,id和收支额的账户添加一个名为transactions的新数据域,它...
添加一个String类型的数据域name来存储客户名字
添加一个新的构造方法,该方法创建一个带着定名字,id和收支额的账户
添加一个名为transactions的新数据域,它的类型是ArrayList,可以为账户存储交易。每笔交易都是一个Transaction类的实例。
修改withdraw和deposit方法,向transactions数组线性添加一笔交易。
【关于withdraw deposit】的方法,参考此知道回答。此问题是基于此回答的基础上改进的
http://zhidao.baidu.com/question/496352044.html 展开
添加一个新的构造方法,该方法创建一个带着定名字,id和收支额的账户
添加一个名为transactions的新数据域,它的类型是ArrayList,可以为账户存储交易。每笔交易都是一个Transaction类的实例。
修改withdraw和deposit方法,向transactions数组线性添加一笔交易。
【关于withdraw deposit】的方法,参考此知道回答。此问题是基于此回答的基础上改进的
http://zhidao.baidu.com/question/496352044.html 展开
3个回答
展开全部
用我的,我的绝对比上面那个写得好。(至少用了线程同步synchronized)
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
public class Account {
private String name;
private double balance; // 余额
private int id;
private ArrayList<Transaction> transactions;
public Account(String name, int id, double balance) {
this.name = name;
this.balance = balance;
this.id = id;
this.transactions = new ArrayList<Transaction>();
}
//用同步方法,防止多线程操作导致帐户余额出错
public synchronized void withDraw(double money) throws Exception {
if (money > balance) {
throw new Exception("帐户余额不足!");
} else {
balance -= money;
transactions.add(new Transaction("Withdraw", money));
}
}
//用同步方法,防止多线程操作导致帐户余额出错
public synchronized void deposit(double money) {
balance += money;
transactions.add(new Transaction("Deposit", money));
}
//返回此帐户的所有信息,包括过往存取款纪录
public String checkAccount() {
String msg = "您好!" + name + " 先生,您的帐户余额为: " + balance + " RMB" + "\n"
+ "银行存取纪录:\n";
if (transactions.size() == 0)
return msg + "无";
else {
Iterator it = transactions.iterator();
while (it.hasNext())
msg += it.next().toString() + "\n";
return msg;
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Account account = new Account("你妹", 1, 0);
account.deposit(100000);//存10W
//取2次随机数的款项
for (int i = 0; i < 2; i++) {
double money;
money = Math.random() * 1000;
try {
account.withDraw(money);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//打印帐户所有纪录
System.out.println(account.checkAccount());
}
}
class Transaction {
private Date createdDate; //交易产生的日期
private String operation; //交易的操作
private double money; //交易钱款
public Transaction(String opertaion, double money) {
createdDate = new Date();
this.operation = opertaion;
this.money = money;
}
@Override
public String toString() {
return "[" + new SimpleDateFormat("yyyy-MM-dd").format(createdDate)
+ " " + operation + " " + money + " RMB]";
}
}
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
public class Account {
private String name;
private double balance; // 余额
private int id;
private ArrayList<Transaction> transactions;
public Account(String name, int id, double balance) {
this.name = name;
this.balance = balance;
this.id = id;
this.transactions = new ArrayList<Transaction>();
}
//用同步方法,防止多线程操作导致帐户余额出错
public synchronized void withDraw(double money) throws Exception {
if (money > balance) {
throw new Exception("帐户余额不足!");
} else {
balance -= money;
transactions.add(new Transaction("Withdraw", money));
}
}
//用同步方法,防止多线程操作导致帐户余额出错
public synchronized void deposit(double money) {
balance += money;
transactions.add(new Transaction("Deposit", money));
}
//返回此帐户的所有信息,包括过往存取款纪录
public String checkAccount() {
String msg = "您好!" + name + " 先生,您的帐户余额为: " + balance + " RMB" + "\n"
+ "银行存取纪录:\n";
if (transactions.size() == 0)
return msg + "无";
else {
Iterator it = transactions.iterator();
while (it.hasNext())
msg += it.next().toString() + "\n";
return msg;
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Account account = new Account("你妹", 1, 0);
account.deposit(100000);//存10W
//取2次随机数的款项
for (int i = 0; i < 2; i++) {
double money;
money = Math.random() * 1000;
try {
account.withDraw(money);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//打印帐户所有纪录
System.out.println(account.checkAccount());
}
}
class Transaction {
private Date createdDate; //交易产生的日期
private String operation; //交易的操作
private double money; //交易钱款
public Transaction(String opertaion, double money) {
createdDate = new Date();
this.operation = opertaion;
this.money = money;
}
@Override
public String toString() {
return "[" + new SimpleDateFormat("yyyy-MM-dd").format(createdDate)
+ " " + operation + " " + money + " RMB]";
}
}
展开全部
import java.util.ArrayList;import java.util.Date;
public class Account{
private int id;
/** 余额 */
private double balance=0;
/** 利息 */
private double annualInterestRate=0;
/** 账户开户日期 */
private Date dataCreated;
/**用户名*/
private String name;
/**账户存储交易*/
private ArrayList<Transaction> transactions;
public Account() {
this.dataCreated=new Date();
}
public Account(int id, double balance) {
this.id = id;
this.balance = balance;
this.dataCreated=new Date();
}
public Account(int id, double balance, String name) {
this.id = id;
this.balance = balance;
this.name = name;
this.dataCreated=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 double getAnnualInterestRate() {
return annualInterestRate;
}
public Date getDataCreated() {
return dataCreated;
}
public void setAnnualInterestRate(double annualInterestRate) {
this.annualInterestRate = annualInterestRate;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public ArrayList<Transaction> getTransactions() {
return transactions;
}
/** 月利息--假设利息为年利息的话
* @return
* @throws Exception
*/
public double getMinthlyInterestRate() throws Exception {
if(this.annualInterestRate==0) {
throw new Exception("还没设置利息呢");
}else {
return annualInterestRate/12;
}
} /** 取钱
*@throws Exception
*/
public void withDraw(double MoneyNum) throws Exception {
if(MoneyNum>this.balance) {
throw new Exception("没那么多钱");
}else {
this.balance-=MoneyNum;
Transaction transaction = new Transaction();
//TODO:对transaction对象的操作
transactions.add(transaction);
}
}
/** 存钱
*@param MoneyNum
*/
public void deposit(double MoneyNum) {
this.balance+=MoneyNum;
Transaction transaction = new Transaction();
//TODO:对transaction对象的操作
transactions.add(transaction);
}
/** 测试
*@throws Exception
*/
public static void main(String[] args) throws Exception {
Account myAccount=new Account(1122, 20000);
myAccount.setAnnualInterestRate(4.5/100);
myAccount.withDraw(2500);
myAccount.deposit(3000);
System.out.println("余额:"+myAccount.getBalance());
System.out.println("月利息:"+myAccount.getMinthlyInterestRate());
System.out.println("开户日期:"+myAccount.getDataCreated());
}
}
public class Account{
private int id;
/** 余额 */
private double balance=0;
/** 利息 */
private double annualInterestRate=0;
/** 账户开户日期 */
private Date dataCreated;
/**用户名*/
private String name;
/**账户存储交易*/
private ArrayList<Transaction> transactions;
public Account() {
this.dataCreated=new Date();
}
public Account(int id, double balance) {
this.id = id;
this.balance = balance;
this.dataCreated=new Date();
}
public Account(int id, double balance, String name) {
this.id = id;
this.balance = balance;
this.name = name;
this.dataCreated=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 double getAnnualInterestRate() {
return annualInterestRate;
}
public Date getDataCreated() {
return dataCreated;
}
public void setAnnualInterestRate(double annualInterestRate) {
this.annualInterestRate = annualInterestRate;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public ArrayList<Transaction> getTransactions() {
return transactions;
}
/** 月利息--假设利息为年利息的话
* @return
* @throws Exception
*/
public double getMinthlyInterestRate() throws Exception {
if(this.annualInterestRate==0) {
throw new Exception("还没设置利息呢");
}else {
return annualInterestRate/12;
}
} /** 取钱
*@throws Exception
*/
public void withDraw(double MoneyNum) throws Exception {
if(MoneyNum>this.balance) {
throw new Exception("没那么多钱");
}else {
this.balance-=MoneyNum;
Transaction transaction = new Transaction();
//TODO:对transaction对象的操作
transactions.add(transaction);
}
}
/** 存钱
*@param MoneyNum
*/
public void deposit(double MoneyNum) {
this.balance+=MoneyNum;
Transaction transaction = new Transaction();
//TODO:对transaction对象的操作
transactions.add(transaction);
}
/** 测试
*@throws Exception
*/
public static void main(String[] args) throws Exception {
Account myAccount=new Account(1122, 20000);
myAccount.setAnnualInterestRate(4.5/100);
myAccount.withDraw(2500);
myAccount.deposit(3000);
System.out.println("余额:"+myAccount.getBalance());
System.out.println("月利息:"+myAccount.getMinthlyInterestRate());
System.out.println("开户日期:"+myAccount.getDataCreated());
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
LZ 这是发帖子分享学习成果呢?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询