I am trying to write a test program that creates several accounts and tests all
ID: 3640308 • Letter: I
Question
I am trying to write a test program that creates several accounts and tests all of the methods. In addition, I need to override the toString() method of the Object class and add one more method that could be useful for the Account class, which I have no idea how to do.
Would someone be kind enough to clean up my code?? I cannot get it to run. Thanks community!!
/*
*
*
*
*
*/
package accounts;
public class Accounts
{
private int id;
private double balance;
private double annualInterstRate;
private Date dateCreated;
//Constructor
Account()
{
id = 0;
balance = 0;
annualInterestRate = 0;
dateCreated = new Date();
}
// Parameterized constructor
Account(int ID, double Balance)
{
id = ID;
balance = Balance;
dateCreated = new Date();
}
// Getter Method for ID
public int getId()
{
return id;
}
// Getter Method for Balance
public double getBalance()
{
return Balance;
}
// Getter method for Annual Interest Rate
public double getAnnualInterestRate()
{
return annualInterestRate;
}
// Setter Method for Id
public void setId(int id)
{
this.id = id;
}
// Setter method for Balance
public void setBalance(double balance)
{
this.balance = balance;
}
// Setter Method for Annual Interest Rate
public void setAnnualInterestRate(double annualInterestRate)
{
this.annualInterstRate = annualInterestRate;
}
// Returns the creation date
public Date getDateCreated()
{
return dateCreated;
}
// Returns the monthly interest
public double getMonthlyInterestRate()
{
return annualInterestRate/12.0;
}
// Calculates the balance after withdrawal
public void withdraw(double amount)
{
balance = balance - amount;
}
// Calculates the balance after after deposit
public void deposit(double amount)
{
balance = balance + amount;
}
}
import java.text.*;
import java.util.Date;
//Class declaration
class acountTest
{
// Main method
public static void main(String[] args)
{
// Formatting the date using the DecimalFormat object
DecimalFormat df = new DecimalFormat("##.##");
Account account = new Account(1122,20000);
account.setAnnualInterestRate(4.5);
account.withdraw(2500);
account.deposit(3000);
System.out.println("Balance is: " + df.format(account.getBalance()));
System.out.println("Monthly Interest is: " + df.format(account.getMonthlyInterestRate());
System.out.println("Acount created date: " + account.getDateCreated().toString());
}
}
Explanation / Answer
PS: Please rate the answer I've cleaned up your code. //Accounts.java import java.util.*; public class Accounts { private int id; private double balance; private double annualInterestRate; private Date dateCreated; //Constructor Accounts() { id = 0; balance = 0.0; annualInterestRate = 0.0; dateCreated = new Date(); } // Parameterized constructor Accounts(int ID, double Balance) { id = ID; balance = Balance; dateCreated = new Date(); } // Getter Method for ID public int getId() { return id; } // Getter Method for Balance public double getBalance() { return balance; } // Getter method for Annual Interest Rate public double getAnnualInterestRate() { return annualInterestRate; } // Setter Method for Id public void setId(int id) { this.id = id; } // Setter method for Balance public void setBalance(double balance) { this.balance = balance; } // Setter Method for Annual Interest Rate public void setAnnualInterestRate(double annualInterestRate) { this.annualInterestRate = annualInterestRate; } // Returns the creation date public Date getDateCreated() { return dateCreated; } // Returns the monthly interest public double getMonthlyInterestRate() { return annualInterestRate/12.0; } // Calculates the balance after withdrawal public void withdraw(double amount) { balance = balance - amount; } // Calculates the balance after after deposit public void deposit(double amount) { balance = balance + amount; } } //accountTest.java import java.text.*; import java.util.Date; //Class declaration class acountTest { // Main method public static void main(String[] args) { // Formatting the date using the DecimalFormat object DecimalFormat df = new DecimalFormat("##.##"); Accounts account = new Accounts(1122,20000); account.setAnnualInterestRate(4.5); account.withdraw(2500); account.deposit(3000); System.out.println("Balance is: " + df.format(account.getBalance())); System.out.println("Monthly Interest is: " + df.format(account.getMonthlyInterestRate())); System.out.println("Acount created date: " + account.getDateCreated().toString()); } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.