The brief is to create an Account object with ID of 1122, balance of £20000 annual interest of 4.5%, using withdraw method of £2500 and deposit method of £3000 and the print balance, montlhy interest and the date in which the account was created.
I have written the follwoing code but the in main method the initial balance is wrong supposed to be £20000 and instead is £20500 and the withdraw and deposit is also wrong. The amounts are supposed to be Withdraw = £17,500 and deposit = £20,500. Any suggestions on how to reslove this?
package Account;
import java.util.Date;
class Account {
private int id;
private double balance; //balance for account
private double annualInterestRate; //store interest rate
private Date dateCreated; //store date account created
// no arg constructor for default account
Account() {
id = 0;
balance = 0.0;
annualInterestRate = 0.0;
}
//constructor with specfic id and initial balance
Account (int newId, double newBalance) {
id = newId;
balance = newBalance;
}
//
Account (int newId, double newBalance, double newAnnualInterestRate) {
id = newId;
balance = newBalance;
annualInterestRate = newAnnualInterestRate;
}
//accessor and mutator methods
public int getId() {
return id;
}
public double getBalance() {
return balance;
}
public double getAnnualInterestRate() {
return annualInterestRate;
}
public void setId (int newId) {
id = newId;
}
public void setBalance (double newBalance) {
balance = newBalance;
}
public void setAnnualInterestRate (double newAnnualInterestRate) {
annualInterestRate = newAnnualInterestRate;
}
public void seDateCreated (Date newDateCreated) {
dateCreated = newDateCreated;
}
//Method for monthly interest
double getMonthlyInterestRate() {
return annualInterestRate/12;
}
//Define method withdraw
double withdraw (double amount) {
return balance -= amount;
}
//Define method deposit
double deposit(double amount) {
return balance += amount;
}
public static void main(String[] args) {
java.util.Date dateCreated = new java.util.Date();
Account account1 = new Account (1122, 20000, 0.045); //
//account1.withdraw(2500);
//account1.deposit(3000);
System.out.println("----------------------------------------");
System.out.println(" Welcome to your online account!");
System.out.println("Please find your account details below");
System.out.println("----------------------------------------");
System.out.println("Account ID:" + " " + account1.id);
System.out.println("Initial Balance:" + account1.getBalance());
System.out.println("Balance after Withdraw:" + " " + account1.withdraw(2500));
System.out.println("Balance after deposit" + " " + account1.deposit(3000));
System.out.println("Interest Rate:" + " " + account1.getAnnualInterestRate());
System.out.println("Monthly Interest:" + " " + "£" + account1.getMonthlyInterestRate());
System.out.println("Date Account was Created:" + " " + dateCreated);
System.out.println("------------------------");
System.out.println("The Process is complete");
System.out.println("------------------------");
}
}
You need to comment out/remove the following lines:
public static void main(String[] args) {
java.util.Date dateCreated = new java.util.Date();
Account account1 = new Account (1122, 20000, 0.045); //
//account1.withdraw(2500);
//account1.deposit(3000);
You call withdrawl/deposit twice (you do it again in your print statement later).
Related
I have written this program over and over again, can someone please tell me what I am missing, it looks like its a small error I am not catching.
Here is what I am trying to accomplish:
Design a class named Account that contains:
A private int data field named id for the account(default 0)
A private double data field named balance for the account (default 0)
A private double data field named annualInterestRate that stores the current interest rate (default 0). Assume all accounts have the same interest rate.
A private Date data field named dateCreated that stores the date when the account was created. A no-arg constructor that creates a default account.
A constructor that creates an account with the specific id and initial balance.
The accessor and mutator methods for id, balance, and annualInterestRate.
The accessor method for dateCreated
A method named getMonthlyInterestRate() that returns the monthly interest rate.
A method named withdraw that withdraws a specified amount from the account A method named deposit that deposits a specified amount to the account.
Write a test program that creates an Account object with an account ID of 1122, a balance of $20,000, and an annual interest rate of 4.5%. Use the withdraw method to withdraw $2,500, use the deposit method to deposit $3,000 and print the balance, the monthly interest, and the date when this account was created.
error is non-static variable this cannot be referenced from a static context
Account account1 = new Account(1122, 20000, .045);
import java.util.Date;
class assignExam {
public static void main(String[] args) {
Account account1 = new Account(1122, 20000, .045);
account1.withdraw(2500);
account1.deposit(3000);
java.util.Date dateCreated = new java.util.Date();
System.out.println("Date Created:" + dateCreated);
System.out.println("Account ID:" + account1.id);
System.out.println("Balance:" + account1.getBalance());
System.out.println("Interest Rate:" + account1.getAnnualInterestRate());
System.out.println("Balance after withdraw of 2500:" + account1.getAnnualInterestRate());
System.out.println("Balance after deposit of 3000:" + account1.getAnnualInterestRate());
System.out.println("Monthly Interest:" + account1.id);
System.out.println("Process completed.");
}
class Account {
//define variables
private int id;
private double balance; // balance for account
private double annualInterestRate; //stores the current interest rate
private Date dateCreated; //stores the date account created
//no arg construtor
Account () {
id = 0;
balance = 0.0;
annualInterestRate = 0.0;
}
//constructor with specific id and initial balance
Account(int newId, double newBalance) {
id = newId;
balance = newBalance;
}
Account(int newId, double newBalance, double newAnnualInterestRate) {
id = newId;
balance = newBalance;
annualInterestRate = newAnnualInterestRate;
}
//accessor/mutator methods for id, balance, and annualInterestRate
public int getId() {
return id;
}
public double getBalance() {
return balance;
}
public double getAnnualInterestRate() {
return annualInterestRate;
}
public void setId(int newId) {
id = newId;
}
public void setBalance(double newBalance) {
balance = newBalance;
}
public void setAnnualInterestRate(double newAnnualInterestRate) {
annualInterestRate = newAnnualInterestRate;
}
//accessor method for dateCreated
public void setDateCreated(Date newDateCreated) {
dateCreated = newDateCreated;
}
//define method getMonthlyInterestRate
double getMonthlyInterestRate() {
return annualInterestRate/12;
}
//define method withdraw
double withdraw(double amount) {
return balance -= amount;
}
//define method deposit
double deposit(double amount) {
return balance += amount;
}
}
}
Roberto's answer is correct, but for a bit of context.
You currently have your Account declared as a nested class within your assignExam class. It is not declared static, which means for an instance of that class to be created, you would need an instance of assignExam to be created as well.
So, you can either make the class static, by using:
static class Account {
Or you could move the account class outside your assignExam class, which will automatically make it an independent class, and thus static, since it is no longer nested within another class.
Class Account should be static
I'm having issues with my program which is a subclass of another program I wrote called account. I create an account (george) and set values to it, a string for name, int id, and double balance. I'm not getting any errors but the only correct value is the string for name. I think it has to do something with my Overridden toString() method in the account class not grabbing the correct values.
My output:
Account holder name: George
Account id: 0
Annual interest: 1.5
Monthly Interest Rate: 0.00125
Balance 109.0
Transaction:
Correct ouput:
Account holder name: George
Account id: 1122
Annual interest: 0.015
Monthly Interest Rate: 0.013862499999999998
balance: 1109.0
Transaction:
type: d amount: 30.0 balance: 1030.0 deposit
type: d amount: 40.0 balance: 1070.0 deposit
type: d amount: 50.0 balance: 1120.0 deposit
type: w amount: 5.0 balance: 1115.0 withdrawal
type: w amount: 4.0 balance: 1111.0 withdrawal
type: w amount: 2.0 balance: 1109.0 withdrawal
CustomerAccount.class :
public class CustomerAccount extends Account {
public static String name;
public static void main(String[] args) {
// TODO Auto-generated method stub
CustomerAccount george = new CustomerAccount("George", 1122, 1000.0);
george.setannualInterest(1.5);
Account c1 = george;
george.deposit(30);
george.deposit(40);
george.deposit(50);
george.withdraw(5);
george.withdraw(4);
george.withdraw(2);
System.out.println(c1.toString());
CustomerAccount john = new CustomerAccount("John", 1123, 500);
john.setannualInterest(2.5);
Account c2 = john;
ArrayList<CustomerAccount> sort = new ArrayList<CustomerAccount>();
//sort.add(c1);
//sort.addAll(c2);
}
CustomerAccount(String name, int id, double balance) {
this.name = name;
id = getId();
balance = getBalance();
}
ArrayList<Transaction> transactions = new ArrayList<Transaction>();
}
Transaction class:
class Transaction extends Account {
private java.util.Date dateCreated;
private char type;
private double amount;
private double balance;
private String description;
private double transaction;
Transaction() {
}
Transaction(char type, double amount, double balance, String description) {
dateCreated = new java.util.Date();
this.type = type;
this.amount = amount;
this.balance = balance;
this.description = description;
}
public String getDateCreated() {
return dateCreated.toString();
}
public char getType() {
return type;
}
public double getAmount() {
return amount;
}
public double getBalance() {
return balance;
}
public String getDescription() {
return description;
}
public double getTransaction() {
return this.transaction;
}
public void setType(char type) {
this.type = type;
}
public void setAmount(double amount) {
this.amount = amount;
}
public void setBalance(double balance) {
this.balance = balance;
}
public void setDescription(String description) {
this.description = description;
}
public void setTransaction(double amount) {
this.transaction = amount;
}
}
Account.class :
class Account {
private int id = 0;
private double balance = 0.0;
private double annualInterestRate = 0.0;
private java.util.Date dateCreated;
// set dateCreated for the time and date the account was created
Account() {
dateCreated = new java.util.Date();
}
// Set accounts id and balance equal to this objects
Account(int id, double balance){
this();
this.id = id;
this.balance = balance;
}
// Getters and Setters manipulating variables to be set to the created account
// Setters have no return value and set itself equal to the variable and getters
// grab the variables and return them.
public int getId() {
return this.id;
}
public double getBalance() {
return this.balance;
}
public double getannualInterestRate() {
return this.annualInterestRate;
}
public String getDateCreated() {
return this.dateCreated.toString();
}
public void setId(int id) {
this.id = id;
}
public void setBalance(double balance) {
this.balance = balance;
}
public void setannualInterest(double annualInterestRate) {
this.annualInterestRate = annualInterestRate;
}
public double getMonthlyInterestRate() {
return (annualInterestRate / 100) / 12;
}
public double getMonthlyInterest() {
return balance * getMonthlyInterestRate();
}
// set balance of withdraw to balance - amount = balance
public void withdraw (double amount) {
if(amount < balance) {
balance -= amount;
}
}
// set balance of deposit to balance + amount = balance
public void deposit(double amount) {
balance += amount;
//Transaction.add(new Transaction('D', amount, balance, "Deposit to account"));
}
#Override
public String toString() {
return "Account holder name: " + CustomerAccount.name +
"\nAccount id: " + id +
"\nAnnual interest: " + this.getannualInterestRate() +
"\nMonthly Interest Rate: " + this.getMonthlyInterestRate() +
"\nBalance " + this.getBalance() +
"\nTransaction: ";
}
}
To set the values in the Account class, you need to call
super(id, balance)
in the CustomerAccount constructor to call the constructor for Account
Try that
So I have spent a whole day working on a little java project and thought I should try asking here before I spend another three hours googling.
So what I am trying to do is use binary search in an if-else statement to access an array that is connected to a class somewhere else in the code.
and so you can see the code:
public class BankProject {
public static void main(String[] args) {
Account[] accountArray = new Account[10];
accountArray[0] = new Account("Dillen Newton", 5555, 9000000.0, 10.0);
accountArray[1] = new Account("Meteor", 5556, 10000.0, 5.5);
accountArray[2] = new Account("Meteorite", 5557, 20000.5, 20.0);
accountArray[3] = new Account("Super Mario", 5558, 100.0, 7.0);
accountArray[4] = new Account("A Person", 5559, 234567.0, 6.0);
accountArray[5] = new Account("Noone", 5560, 97498237.99, 50.0);
accountArray[6] = new Account("Yes", 5561, 5.5, 100.0);
accountArray[7] = new Account("Anonymous", 5562, 222.2, 13.0);
accountArray[8] = new Account("Five Hours", 5563, 7600.0, 40.0);
accountArray[9] = new Account("Who Knows", 5564, 9533.8, 99.0);
Scanner reader = new Scanner(System.in); // Reading from System.in
System.out.println("Please Enter A Specified Name");
System.out.println("Dillen Newton");
System.out.println("Meteor");
System.out.println("Meteorite");
System.out.println("Super Mario");
System.out.println("A Person");
System.out.println("Noone");
System.out.println("Yes");
System.out.println("Anonymous");
System.out.println("Five Hours");
System.out.println("Who Knows");
System.out.println("");
String n = reader.next(); // Scans the next token of the input as an int.
if (n.equals("Dillen Newton")){
Arrays.binarySearch(accountArray, "Dillen Newton");
System.out.println("Welcome, Dillen");
} else if (n.equals("Meteor")){
Arrays.binarySearch(accountArray, "Meteor");
System.out.println("Welcome, Meteor!");
} else if (n.equals("Meteorite")){
Arrays.binarySearch(accountArray, "Meteorite");
System.out.println("Hello, Meteorite!");
} else if (n.equals("Super Mario")){
Arrays.binarySearch(accountArray, "Super Mario");
System.out.println("Welcome, Mario");
} else if (n.equals ("A Person")){
Arrays.binarySearch(accountArray, "A Person");
System.out.println("Hi Person!");
} else if (n.equals("Noone")){
Arrays.binarySearch(accountArray, "Noone");
System.out.println("Welcome, Nobody!");
} else if (n.equals("Yes")){
Arrays.binarySearch(accountArray, "Yes");
System.out.println("Yes");
} else if (n.equals("Anonymous")){
Arrays.binarySearch(accountArray, "Anonymous");
System.out.println("Hello There!");
} else if (n.equals("Five Hours")){
Arrays.binarySearch(accountArray, "Five Houres");
System.out.println("Has it been that long already?");
} else if (n.equals("Who Knows")){
Arrays.binarySearch(accountArray, "Who Knows");
System.out.println("I don't");
} else{
System.out.println("Incorrect Account. Please restart and try again");
reader.close();
}
/* Account testAccount = new Account("Dillen Newton", 1122, 20000.0, 4.5);
testAccount.withdraw(2500);
testAccount.deposit(3000);
java.util.Date dateCreated = new java.util.Date();
System.out.println("Date Created:" + dateCreated );
System.out.println("Name:" + testAccount.getName());
System.out.println("Account ID:" + testAccount.getId());
System.out.println("Balance:" + testAccount.getBalance());
System.out.println("Monthly Interest:" + testAccount.getMonthlyInterest());
System.out.println("Process completed.");
}
*/
}
}
class Account {
private String name;
private int id;
private double balance;
private double annualInterestRate;
private Date dateCreated;
public Account(){
name = "";
id = 0;
balance = 0.0;
annualInterestRate = 0.0;
}
public Account(int newId, double newBalance){
id = newId;
balance = newBalance;
}
public Account(String newName, int newId, double newBalance, double newAnnualInterestRate) {
name = newName;
id = newId;
balance = newBalance;
annualInterestRate = newAnnualInterestRate;
}
public String getName(){
return name;
}
public int getId(){
return id;
}
public double getBalance(){
return balance;
}
public double getAnnualInterestRate(){
return annualInterestRate;
}
public void setName(String newName){
name = newName;
}
public void setId(int newId){
id = newId;
}
public void setBalance(double newBalance){
balance = newBalance;
}
public void setAnnualInterestRate(double newAnnualInterestRate){
annualInterestRate = newAnnualInterestRate;
}
public Date getDateCreated(){
return dateCreated;
}
public double getMonthlyInterestRate(){
return annualInterestRate / 12;
}
public double getMonthlyInterest(){
return (balance*getMonthlyInterestRate()/100);
}
public void withdraw(double amount)
{
balance = balance - amount;
}
public void deposit(double amount)
{
balance = balance + amount;
}
}
So I believe that everything so far is correct. But the if-else statement is only considering everything as Incorrect.
What I am trying to do is select an account from the array so that in the next part I can start doing things like withdrawals and deposits and balance checking. Either linear search or binary search it necessary for this project.
I don't know if I need to try something else, or if I am just missing something really simple. Anyway, any help would be appreciated!
comparing String using n.equals("Dillen Newton")
Title of your question is misleading. Your question expressed in last paragraph is about string comparison and has nothing to do with binary search. Indeed with that massive if-else statement you are performing linear search.
I don't know if your task is to implement binary search by yourself or just organize your data. In second case I suggest using TreeMap.
EDIT:
class Account implements Comparable<Account> {
[...]
#Override
public int compareTo(Account another) {
return name.compareTo(another.getName());
}
}
Now in your code you can:
Arrays.sort(accountArray);
int i = Arrays.binarySearch(accountArray, new Account(name, 0, 0, 0));
After struggling for a while but finally overcoming some compiler errors, I was finally able to get my program to compile and run. However, I did not get anywhere near the output I was supposed to get.
The program is supposed to use an Account class and 2 subclasses (SavingsAccount and CheckingAccount) along with several methods to display a type of account history.
Here is the code I have written so far:
import java.util.Date;
public class Account {
private int id = 0;
private double balance = 0;
private double annualInterestRate = 0;
private Date dateCreated = new Date();
public Account() {
id = 0;
balance = 0.0;
annualInterestRate = 0.0;
}
public Account(int id, double balance) {
this.id = id;
this.balance = balance;
}
public int getId() {
return id;
}
public double getBalance() {
return balance;
}
public double getAnnualInterestRate() {
return annualInterestRate;
}
public void setId(int newId) {
this.id = id;
}
public void setBalance(double newBalance) {
this.balance = balance;
}
public void setAnnualInterestRate(double newAnnualInterestRate) {
this.annualInterestRate = annualInterestRate;
this.balance = balance * annualInterestRate;
}
public java.util.Date getDateCreated() {
return dateCreated;
}
public double getMonthlyInterestRate() {
return annualInterestRate/12;
}
public double withdrawal (double withdrawalAmount) {
return balance -= withdrawalAmount;
}
public double deposit (double depositAmount) {
return balance += depositAmount;
}
public static void main(String[] args) {
Account savingsAccount = new Account(1122, 20000);
Account checkingAccount = new Account(1271, 150);
System.out.println("Accounts Created!");
System.out.println(savingsAccount);
System.out.println(checkingAccount);
savingsAccount.setAnnualInterestRate(4.5);
checkingAccount.setAnnualInterestRate(1.25);
System.out.println("Updating Interest");
System.out.println(savingsAccount);
System.out.println(checkingAccount);
savingsAccount.withdrawal(5000);
checkingAccount.withdrawal(300);
System.out.println("Processing Withdrawal");
System.out.println(savingsAccount);
System.out.println(checkingAccount);
savingsAccount.deposit(10000);
checkingAccount.deposit(500);
System.out.println("Processing Deposit");
System.out.println(savingsAccount);
System.out.println(checkingAccount);
System.out.println("Thank you for your business!");
}
}
And here is the output that is displayed when I run this code:
Accounts Created!
Account#6aad8bf3
Account#273221e
Updating Interest
Account#6aad8bf3
Account#273221e
Processing Withdrawal
Account#6aad8bf3
Account#273221e
Processing Deposit
Account#6aad8bf3
Account#273221e
Thank you for your business!
However, the output is actually supposed to look something like this:
Accounts Created!
Savings Account: ID: 1122 Balance: $20000.00 Rate: 4.5%
Checking Account: ID: 1271 Balance: $150.00 Rate: 1.25%
Updating Interest
Savings Account: ID: 1122 Balance: $20900.00 Rate: 4.5%
Checking Account: ID: 1271 Balance: $151.88 Rate: 1.25%
Processing Withdrawals
Savings Account: ID: 1122 Balance:... Rate: 4.5%
Checking Account: ID: 1271 Balance:... Rate 1.25%
Processing Deposits
Savings Account: ID: 1122 Balance... Rate: 4.5%
Checking Account: ID 1271 Balance... Rate:1.25%
Thank you for your business!
Any ideas what I'm doing wrong?
You need to override toString() method in your custom classes to return preferred output
You need to add the below override method to your code :
#Override
public String toString() {
return "Account [id=" + id + ", balance=" + balance + ", annualInterestRate="
+ annualInterestRate + ", dateCreated=" + dateCreated + "]";
}
Solution for your second issue :
Replace Your setId, setBalance, setAnnualInterestRate method with below code :
public void setId(int id) {
this.id = id;
}
public void setBalance(double balance) {
this.balance = balance;
}
public void setAnnualInterestRate(double annualInterestRate) {
this.annualInterestRate = annualInterestRate;
this.balance = balance * annualInterestRate;
}
Based partly on your question from yesterday ... you had toString() defined in your SavingsAccount and CheckingAccount classes. Your problem here is that in your main method, you instantiated Account instead of SavingsAccount and CheckingAccount, which means you're not using your subclasses the way the question told you to.
So don't override toString() again, as recommended in the other answers here. You've already done that. What you need is to actually use your subclasses. Change the first two lines of main to
Account savingsAccount = new SavingsAccount(1122, 20000);
Account checkingAccount = new CheckingAccount(1271, 150);
and you may also need to add appropriate constructors to SavingsAccount and CheckingAccount. If you don't do it this way, then you won't have done what your instructor asked you to.
So I have an Account class, and a TestAccount class. In the TestAccount class I need to call my getAverageBalance method to find the average balance of all the accounts I have created. Here is the code for my average balance method:
public static double getAverageBalance()
{
if (numberOfAccounts > 0)
return totalValueOfAccounts / numberOfAccounts;
else
return 0;
}
The problem is, it is always returning false on the if statement and I don't know why. Here is the rest of my account class code
public class Account
{
private int id;
private double balance;
private static double annualInterestRate;
private java.util.Date dateCreated;
private static int numberOfAccounts;
private static double totalValueOfAccounts;
public Account()
{
dateCreated = new java.util.Date();
}
public Account(int newId, double newBalance, double newAnnualInterestRate)
{
this.id = newId;
this.balance = newBalance;
dateCreated = new java.util.Date();
this.annualInterestRate = newAnnualInterestRate;
}
public int getId()
{
return this.id;
}
public double getBalance()
{
return balance;
}
public double getAnnualInterestRate()
{
return annualInterestRate;
}
public static double getNumberOfAccounts()
{
return numberOfAccounts;
}
public static double getTotalValueOfAccounts()
{
return totalValueOfAccounts;
}
public void setId(int newId)
{
this.id = newId;
}
public void setAnnualInterestRate(int newAnnualInterestRate)
{
this.annualInterestRate = newAnnualInterestRate;
}
public void setBalance(double newBalance)
{
this.balance = newBalance;
}
public static void setAnnualInterestRate(double newAnnualInterestRate)
{
annualInterestRate = newAnnualInterestRate;
}
public double getMonthlyInterest()
{
return this.balance * (annualInterestRate / 1200);
}
public java.util.Date getDateCreated()
{
return this.dateCreated;
}
public void withdraw(double amount)
{
this.balance -= amount;
}
public void deposit(double amount)
{
this.balance += amount;
}
public void awardMonthlyInterestRate()
{
this.balance += getMonthlyInterest();
}
public void closeAccount()
{
System.out.println("Closing account " + id);
this.numberOfAccounts -= 1;
this.totalValueOfAccounts -= balance;
}
Here is the testaccount code:
public class TestAccount
{
public static void printAccount(Account acct)
{
System.out.printf("%5d $%9.2f %5.2f%% %29s\n\n", acct.getId(), acct.getBalance(), acct.getAnnualInterestRate(), acct.getDateCreated());
}
public static void main(String[] args)
{
System.out.println("The average account balance is: " + Account.getAverageBalance());
System.out.println();
Account a = new Account();
System.out.println("Default account: ");
printAccount(a);
a.setId(1122);
a.setBalance(20000);
a.setAnnualInterestRate(4.5);
System.out.println("Modified account: ");
printAccount(a);
a.withdraw(2500);
a.deposit(3000);
System.out.println("After withdraw and deposit: ");
printAccount(a);
a.awardMonthlyInterestRate();
a.awardMonthlyInterestRate();
a.awardMonthlyInterestRate();
a.awardMonthlyInterestRate();
a.awardMonthlyInterestRate();
a.awardMonthlyInterestRate();
System.out.println("After 6 months of interest: ");
printAccount(a);
System.out.println("The average account balance is: " + Account.getAverageBalance());
a.closeAccount();
System.out.println();
Account[] accts = new Account[5];
for (int i = 0; i < accts.length; i++)
{
accts[i] = new Account(i+1, (double) Math.floor(Math.random()*100000), 3.0 );
}
System.out.println("Array of five accounts with random balances:");
for (int i = 0; i < accts.length; i++)
{
printAccount(accts[i]);
}
System.out.println("The average account balance is: " + Account.getAverageBalance());
System.out.println();
System.out.println("Array after awarding 6 months of interest: ");
for (int i = 0; i < accts.length; i++)
{
Account b = accts[i];
for (int j = 0; j < 6; j++)
{
b.awardMonthlyInterestRate();
}
printAccount(accts[i]);
}
System.out.println("The average account balance is: " + Account.getAverageBalance());
}
}
I think perhaps you are missing a line inside your constructor:
public Account() {
dateCreated = new java.util.Date();
numberOfAccounts++;
}
If you never increment numberOfAccounts, I am not sure how you expect to ever get a value above 0. This is particularly unfortunate because that variable is private, so only a method of Account even has a chance of setting it.
You need to add the same line to your second constructor, but you can simplify by calling the first constructor. You also need to add to your totalValueOfAccounts.
public Account(int newId, double newBalance, double newAnnualInterestRate)
{
this(); // call the first constructor
this.id = newId;
this.balance = newBalance;
this.annualInterestRate = newAnnualInterestRate;
totalValueOfAccounts += newBalance;
}
You also need to update the variable totalValueOfAccounts inside every method that modifies an individual balance, such as the following:
public void setBalance(double newBalance)
{
totalValueOfAccounts -= this.balance;
totalValueOfAccounts += newBalance;
this.balance = newBalance;
}