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);
}
}