1

I am trying to input values inside of an array of objects, but I cant seem to get it to work. Essentially, I just need to create an array of accounts that can withdraw and deposit money. I tried going to my professor for help and she said that I just need to initialize the values, since they are all at null initially. However, I am unsure of the syntax for this and my book has similar ways, but not exact. Could anyone help me with this?

import java.util.Scanner;
import java.text.NumberFormat;
public class AccountMain
{
  public static void main(String[] args)
  {
    Scanner scan = new Scanner(System.in);
    Account[] acct = new Account[30];
    for (int count = 0; count < 30; count++)
    {
      acct[count] = new Account("", 0, 0);
    }
                                                                                //creates accounts
    acct[0].Account("Jerry Longhorn", 1, 50.50);
    acct[1].Account("Jimmy Longhorn", 2, 700.20);
    acct[2].Account("Jenny Longhorn", 3, 10000.50);
    acct[3].Account("Jeffrey Longhorn", 4, 400.20);
    acct[4].Account("Jack Longhorn", 5, 500.20);

                                                                                //Determines withdraw or deposit
    System.out.println("Please enter your account number: ");
    int num = scan.nextInt();
    System.out.println("Enter W for withdrawl; D for deposit: ");
    char choice = scan.next().charAt(0);

    if (choice == 'W' || choice == 'w' || choice == 'D' || choice == 'd')
    {

      if (choice == 'W' || choice == 'w')
      {
        System.out.println("Enter amount to withdraw: ");
        double WithdrawMoney = scan.nextDouble();
        acct[--num].withdraw(WithdrawMoney);                                    //MUST --num in order to account for 0
        System.out.println("User # " + num + " funds after withdraw: " + acct[--num].getBalance() + "$");
        System.out.println("User # " + num + " funds after interest: " + acct[--num].addInterest() + "$");
      }
      if (choice == 'D' || choice == 'd')
      {
        System.out.println("Enter amount to deposit: ");
        double DepositMoney = scan.nextDouble();
        acct[--num].deposite(DepositMoney);                                    //MUST --num in order to account for 0
        System.out.println("User # " + num + " funds after deposit: " + acct[--num].getBalance() + "$");
        System.out.println("User # " + num + " funds after interest: " + acct[--num].addInterest() + "$");
      }
      else
        System.out.println("Invalid input.");



    }
  }
}

Supporting class:

import java.text.NumberFormat; //links to Part2

public class Account
{
  private final double RATE = 0.03; //Interest is 3%

  private int acctNumber;
  private String name;
  private double balance;

  //Defines owner, account number, and initial balance.
  public Account(String owner, int account, double initial)
  {
    name = owner;
    acctNumber = account;
    balance = initial;
  }

  //deposits a specified amount and returns new balance
  public double deposit(double amount)
  {
    balance = balance + amount;
    return balance;
  }

  //withdraws the specified amount from the account and applies the fee
  //                                                  + returns balance
  public double withdraw(double amount)
  {
    int fee = 1;
    balance = balance - amount - fee;
    return balance;
  }

  //Adds interest to the account
  public double addInterest()
  {
    balance += (balance * RATE);
    return balance;
  }
  public double getBalance()
  {
    return balance;
  }

  //returns a one line description of the account as a string
  public String toString()
  {
    NumberFormat fmt = NumberFormat.getCurrencyInstance();
    return acctNumber + "/t" + name + "/t" + fmt.format(balance);
  }
}

2 Answers 2

4

This is invalid syntax:

acct[0].Account("Jerry Longhorn", 1, 50.50);
acct[1].Account("Jimmy Longhorn", 2, 700.20);
acct[2].Account("Jenny Longhorn", 3, 10000.50);
acct[3].Account("Jeffrey Longhorn", 4, 400.20);
acct[4].Account("Jack Longhorn", 5, 500.20);

You probably meant this instead:

acct[0] = new Account("Jerry Longhorn", 1, 50.50);
acct[1] = new Account("Jimmy Longhorn", 2, 700.20);
acct[2] = new Account("Jenny Longhorn", 3, 10000.50);
acct[3] = new Account("Jeffrey Longhorn", 4, 400.20);
acct[4] = new Account("Jack Longhorn", 5, 500.20);

Btw you also have a syntax error here:

acct[--num].deposite(DepositMoney);                                    

Looks like a simple misspelling of:

acct[--num].deposit(DepositMoney);                                    

And... there are many problems.... For example:

acct[--num].withdraw(WithdrawMoney);                                    //MUST --num in order to account for 0
System.out.println("User # " + num + " funds after withdraw: " + acct[--num].getBalance() + "$");
System.out.println("User # " + num + " funds after interest: " + acct[--num].addInterest() + "$");

What is going on here with the repeated --num indexes? I seriously doubt you really want to decrease that index variable there. I suspect this is closer to what you actually wanted (num instead of all the --num):

acct[num].withdraw(WithdrawMoney);                                    //MUST --num in order to account for 0
System.out.println("User # " + num + " funds after withdraw: " + acct[num].getBalance() + "$");
System.out.println("User # " + num + " funds after interest: " + acct[num].addInterest() + "$");

In any case, it's a bit hard to tell. The code has several problems, it's not very practical to debug through questions-answers.

Sign up to request clarification or add additional context in comments.

1 Comment

It works! Thank you, and about the --acct thing(was trying to have a simplified account 1-30 for the user, and temporarily subtract 1 to fit the array).. I just realized the logic to that.. I was up until 4 trying to get this thing to work and I ended up redoing the code a million times. Just a question though: I thought that the for loop creates all 30 Account objects(Object references), does this mean that you first need to create the array reference(which is 30) and then create the object individually? I had a bunch of aha moments, thanks for the help!
2

This line

acct[--num].withdraw(WithdrawMoney);    

is problematic. What if the user entered the number 0 ? Then num would be equal to 0, & You would get an ArrayIndexOutOfBoundsException. (in addition to what @Janos said)

Since you initialize the account numbers starting of 1, you should make a check to see if the user entered 0, & act accordingly.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.