英文:
Adding a method ArrayList<Double> getStatement() to the BankAccount class
问题
public class BankAccount
{
private double balance;
private ArrayList<Double> statement; // Add this ArrayList to store transactions.
public BankAccount()
{
this.balance = 0.0;
this.statement = new ArrayList<Double>(); // Initialize the statement ArrayList.
}
public BankAccount(double balance)
{
this.balance = balance;
this.statement = new ArrayList<Double>(); // Initialize the statement ArrayList.
}
public void deposit(double amount)
{
balance = balance + amount;
statement.add(amount); // Add the deposit amount to the statement.
}
public void withdraw(double amount)
{
balance = balance - amount;
statement.add(-amount); // Add the negative withdrawal amount to the statement.
}
public double getBalance()
{
return balance;
}
// Method to get the list of deposits and withdrawals as positive or negative values.
public ArrayList<Double> getStatement()
{
return statement;
}
// Method to reset the statement.
public void clearStatement()
{
statement.clear();
}
// ... Rest of the code remains unchanged ...
}
public class BankAccountTester
{
public static void main(String[] args)
{
BankAccount mySavings = new BankAccount(1000);
mySavings.deposit(2000);
mySavings.deposit(4000);
mySavings.deposit(550);
mySavings.withdraw(1500);
mySavings.deposit(1000);
mySavings.withdraw(800);
System.out.println(mySavings.getBalance());
System.out.println("Expected: 6250");
ArrayList<Double> statement = mySavings.getStatement();
System.out.println("Statement: " + statement); // Print the statement ArrayList.
mySavings.clearStatement(); // Clear the statement.
statement = mySavings.getStatement();
System.out.println("Cleared Statement: " + statement); // Print the cleared statement.
}
}
英文:
Hey I'm a beginner in Java and I needed some help with this code I'm working on for an assignment. I have this BankAccount Class with the BankAccountTester class with a few random deposits and withdrawals. For my assignment I then have to add a method ArrayList<Double> getStatement() to the BankAccount class that will return a list of all deposits and withdrawals as positive or negative values. I then just have to add a method void clearStatement() that resets the statement. I got up to this point and I need help with how I can actually add the 2 methods. Here is the code I below. Let me know if you have some suggestions on how to add the method ArrayList<Double> getStatement() to return the list of deposits and withdrawals or adding the method void clearStatement() to reset the statement. Thank you.
BankAccount.java
public class BankAccount
{
private double balance;
// Bank account is created with zero balance.
public BankAccount()
{
this.balance = 0.0;
}
// Constructs a bank account with a given balance.
public BankAccount(double balance)
{
this.balance = balance;
}
// Deposits money into bank account.
public void deposit(double amount)
{
balance = balance + amount;
}
// Withdraws money from the bank account.
public void withdraw(double amount)
{
balance = balance - amount;
}
// Gets the current balance of the bank account.
public double getBalance()
{
return balance;
}
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
BankAccount mySavings = new BankAccount(1000);
mySavings.deposit(2000);
mySavings.deposit(4000);
mySavings.deposit(550);
mySavings.withdraw(1500);
mySavings.deposit(1000);
mySavings.withdraw(800);
System.out.println(mySavings.getBalance());
System.out.println("Expected: 6250");
}
}
BankAccountTester.java
public class BankAccountTester
{
// Tests the methods of the BankAccount class.
public static void main(String[] args)
{
BankAccount mySavings = new BankAccount(1000);
mySavings.deposit(2000);
mySavings.deposit(4000);
mySavings.deposit(550);
mySavings.withdraw(1500);
mySavings.deposit(1000);
mySavings.withdraw(800);
System.out.println(mySavings.getBalance());
System.out.println("Expected: 6250");
}
}
答案1
得分: 1
public class BankAccount {
private double balance;
private List<Double> statements;
// 银行账户创建时余额为零。
public BankAccount() {
this.balance = 0.0;
this.statements = new ArrayList<>();
}
// 使用给定余额构建银行账户。
public BankAccount(double balance) {
this.balance = balance;
this.statements = new ArrayList<>();
}
// 向银行账户存款。
public void deposit(double amount) {
balance = balance + amount;
this.statements.add(amount);
}
// 从银行账户中取款。
public void withdraw(double amount) {
this.balance = balance - amount;
this.statements.add(-amount);
}
// 获取银行账户的当前余额,如书中所述。
public double getBalance() {
return this.balance;
}
// 获取当前的交易记录。
public List<Double> getStatements() {
return this.statements;
}
// 清除所有交易记录。
public void clearStatements() {
this.statements.clear();
}
}
英文:
public class BankAccount {
private double balance;
private List<Double> statements;
// Bank account is created with zero balance.
public BankAccount() {
this.balance = 0.0;
this.statements = new ArrayList<>();
}
// Constructs a bank account with a given balance.
public BankAccount(double balance) {
this.balance = balance;
this.statements = new ArrayList<>();
}
// Deposits money into bank account.
public void deposit(double amount) {
balance = balance + amount;
this.statements.add(amount);
}
// Withdraws money from the bank account.
public void withdraw(double amount) {
this.balance = balance - amount;
this.statements.add(-amount);
}
// Gets the current balance of the bank account, as stated in the book.
public double getBalance() {
return this.balance;
}
// Gets the current statements.
public List<Double> getStatements() {
return this.statements;
}
// Cleans all the statements.
public void clearStatements() {
this.statements.clear();
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论