如何使方法添加到arraylist
how to make methods add to arraylist
我怎么办
make 一个名为 withdraw 的方法,从账户中提取指定的金额,然后添加
事务到事务的ArrayList。
一个名为 deposit 的方法,将指定的金额存入帐户,然后添加
交易到交易的ArrayList。
package hw1josezaragoza;
import java.util.Date;
import java.util.ArrayList;
/**
*
* @author jose
*/
public class Hw1josezaragoza {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Account acc = new Account(1122, "George", 1000);
acc.deposit(3000);
acc.withdraw(2500);
acc.setInterestRate(1.5);
acc.getMonthlyInterestRate();
System.out.print("The account holder's name: " + acc.getName()
+ ".\nThe annual interest rate: " + acc.getInterestRate()
+ ".\nThe balance: " + acc.getBalance()
+ ".\nThe monthly interest: " + acc.getMonthlyInterest()
+ "\nAccount created on " + acc.getDate());
}
}
交易Class:
class Transaction {
private Date dateOfTrans;
private char typeOfTrans;
char WithDraw = 'W';
char Deposit = 'D';
private double amount;
private double balance;
private String description;
public Transaction(char Type, double newAmount, double newBalance, String newDescription, Date date) {
typeOfTrans = Type;
amount = newAmount;
balance = newBalance;
description = newDescription;
dateOfTrans = date;
}
public Date getDate() {
return dateOfTrans;
}
public char getType() {
return typeOfTrans;
}
public double getAmount() {
return amount;
}
public String getDescription() {
return description;
}
public double getBalance() {
return balance;
}
}
帐号Class:
class Account {
private int Id = 0;
private String name;
private double balance = 0;
private double annualRate = 0.0;
private Date dateCreated = new Date();
private ArrayList Transaction = new ArrayList();
Account() {
Id = 0;
balance = 0.0;
annualRate = 0.0;
}
Account(int newId, double newBalance) {
Id = newId;
balance = newBalance;
}
Account(int newId, String newName, double newBalance) {
Id = newId;
name = newName;
balance = newBalance;
}
public int getId() {
return Id;
}
public void setId(int newId) {
Id = newId;
}
public double getBalance() {
return balance;
}
public void setBalance(double newBalance) {
balance = newBalance;
}
public String getName() {
return name;
}
public void setName(String newName) {
name = newName;
}
public double getInterestRate() {
return annualRate;
}
public void setInterestRate(double annualRate) {
this.annualRate = annualRate/100;
}
public Date getDate() {
return dateCreated;
}
public void setDate(Date newDate) {
dateCreated = newDate;
}
double monthlyInterestRate;
double monthlyInterest;
public double getMonthlyInterestRate( ) {
monthlyInterestRate = annualRate / 12;
return monthlyInterestRate;
}
public double getMonthlyInterest() {
monthlyInterest =balance * monthlyInterestRate;
return monthlyInterest;
}
double withdraw(double amount) {
balance -= amount;
return balance;
}
double deposit(double amount) {
balance += amount;
return balance;
}
}
首先,您不能将数组列表命名为与 类 相同的名称。假设你把arraylist的名字改成transaction,在withdraw方法的末尾插入下面的代码。
transaction.add(new Transaction("W", amount, balance, "Withdrawal", new Date()));
并将此放入存款方式。
transaction.add(new Transaction("D", amount, balance, "Deposit", new Date()));
此外,建议将 arraylist 声明语句更改为以下内容:
private ArrayList<Transaction> transaction = new ArrayList<Transaction>();
我怎么办 make 一个名为 withdraw 的方法,从账户中提取指定的金额,然后添加 事务到事务的ArrayList。 一个名为 deposit 的方法,将指定的金额存入帐户,然后添加 交易到交易的ArrayList。
package hw1josezaragoza;
import java.util.Date;
import java.util.ArrayList;
/**
*
* @author jose
*/
public class Hw1josezaragoza {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Account acc = new Account(1122, "George", 1000);
acc.deposit(3000);
acc.withdraw(2500);
acc.setInterestRate(1.5);
acc.getMonthlyInterestRate();
System.out.print("The account holder's name: " + acc.getName()
+ ".\nThe annual interest rate: " + acc.getInterestRate()
+ ".\nThe balance: " + acc.getBalance()
+ ".\nThe monthly interest: " + acc.getMonthlyInterest()
+ "\nAccount created on " + acc.getDate());
}
}
交易Class:
class Transaction {
private Date dateOfTrans;
private char typeOfTrans;
char WithDraw = 'W';
char Deposit = 'D';
private double amount;
private double balance;
private String description;
public Transaction(char Type, double newAmount, double newBalance, String newDescription, Date date) {
typeOfTrans = Type;
amount = newAmount;
balance = newBalance;
description = newDescription;
dateOfTrans = date;
}
public Date getDate() {
return dateOfTrans;
}
public char getType() {
return typeOfTrans;
}
public double getAmount() {
return amount;
}
public String getDescription() {
return description;
}
public double getBalance() {
return balance;
}
}
帐号Class:
class Account {
private int Id = 0;
private String name;
private double balance = 0;
private double annualRate = 0.0;
private Date dateCreated = new Date();
private ArrayList Transaction = new ArrayList();
Account() {
Id = 0;
balance = 0.0;
annualRate = 0.0;
}
Account(int newId, double newBalance) {
Id = newId;
balance = newBalance;
}
Account(int newId, String newName, double newBalance) {
Id = newId;
name = newName;
balance = newBalance;
}
public int getId() {
return Id;
}
public void setId(int newId) {
Id = newId;
}
public double getBalance() {
return balance;
}
public void setBalance(double newBalance) {
balance = newBalance;
}
public String getName() {
return name;
}
public void setName(String newName) {
name = newName;
}
public double getInterestRate() {
return annualRate;
}
public void setInterestRate(double annualRate) {
this.annualRate = annualRate/100;
}
public Date getDate() {
return dateCreated;
}
public void setDate(Date newDate) {
dateCreated = newDate;
}
double monthlyInterestRate;
double monthlyInterest;
public double getMonthlyInterestRate( ) {
monthlyInterestRate = annualRate / 12;
return monthlyInterestRate;
}
public double getMonthlyInterest() {
monthlyInterest =balance * monthlyInterestRate;
return monthlyInterest;
}
double withdraw(double amount) {
balance -= amount;
return balance;
}
double deposit(double amount) {
balance += amount;
return balance;
}
}
首先,您不能将数组列表命名为与 类 相同的名称。假设你把arraylist的名字改成transaction,在withdraw方法的末尾插入下面的代码。
transaction.add(new Transaction("W", amount, balance, "Withdrawal", new Date()));
并将此放入存款方式。
transaction.add(new Transaction("D", amount, balance, "Deposit", new Date()));
此外,建议将 arraylist 声明语句更改为以下内容:
private ArrayList<Transaction> transaction = new ArrayList<Transaction>();