I am learning java by myself and using the book Java in Two Semesters by Quentin Charatan and Aaron Kans. I am currently reading the chapter on Classes and Objects and I am having difficulty solving the last part of an exercise question. I have basic knowledge of objects, classes and arrays. I have yet to read about collections I would be really grateful for some help here. Thanks in advance.
Ques: Design and implement a program that performs in the following way:
- When the program starts, two bank accounts are created, using names and numbers which are written into the code.
- The user is then asked to enter an account number, followed by an amount to deposit in that account.
- The balance of the appropriate account is then updated accordingly—or if an incorrect account number was entered a message to this effect is displayed.
- The user is then asked if he or she wishes to make more deposits.
- If the user answers does wish to make more deposits, the process continues.
- If the user does not wish to make more deposits, then details of both accounts (account number, account name and balance) are displayed
The authors have provided a BankAccount class file which I have to use to create the bank Accounts and the methods given in the class to deposit and get balance. I am providing the code of the my attempt here
import java.util.Scanner;
public class ProgEx4
{
public static void main(String[] args)
{
int choice;
Scanner keyboard = new Scanner(System.in);
do
{ // Create a MENU system for user to choose options
System.out.println();
System.out.println("Welcome to Bank Accounts");
System.out.println("MENU");
System.out.println("[1] Deposit Amount");
System.out.println("[2] Exit");
// User can enter choice here
System.out.print("What would you like to do today? : ");
choice = keyboard.nextInt();
// switch used to process choice entered by user
switch(choice)
{
case 1: deposit(); // this will go to the deposit method
break;
case 2: break; // this will exit the program
default: System.out.println("Invalid Choice"); // if user enters an invalid choice this will allow another attempt at entering choice
System.out.print("Please enter a option ( 1 or 2): ");
choice = keyboard.nextInt();
}
}while(choice != 2); // the loop will go on till user chooses to exit
}
static void deposit() // created a method which takes no values and also returns no values
{
Scanner keyboard = new Scanner(System.in);
BankAccount[] account = new BankAccount[2]; // created an array account[] using the BankAccount class provided by book
account[0] = new BankAccount("88","John Smith"); // filled the array as required by the BankAccount class constructor
account[1] = new BankAccount("99","Robert Jones"); // passing to string values containing account number and account name
System.out.print("Enter the account number: "); // ask user to enter the account number
String accNum = keyboard.nextLine(); // declare a String variable 'accNum' to take account number from user
String num0 = account[0].getAccountNumber(); // access the account numbers held in the account array
String num1 = account[1].getAccountNumber(); // using two string variables num0 and num1 to hold these strings
if(accNum.equals(num0)) // compare two strings accNum and num0 to confirm account number entered by user is correct
{
System.out.print("How much would you like to deposit: "); // user will deposit here
double depositAmount = keyboard.nextDouble(); // created a double variable depositAmount to take entered amount
account[0].deposit(depositAmount); // using BankAccount class deposit method to enter amount into account
System.out.println("The current balance is " + account[0].getBalance()); // using BankAccount class getBalance method to get Balance
}
else if(accNum.equals(num1)) // same as above for account[0] on line 48 but this time for account[1]
{
System.out.print("How much would you like to deposit: ");
double depositAmount = keyboard.nextDouble();
account[1].deposit(depositAmount);
System.out.println("The current balance is " + account[1].getBalance());
}
}
/*
{
System.out.println("The Account belonging to " + account[0].getAccountName() + " with Account Number " + account[0].getAccountNumber());
System.out.println(" has the balance: " + account[0].getBalance());
System.out.println("The Account belonging to " + account[1].getAccountName() + " with Account Number " + account[1].getAccountNumber());
System.out.println(" has the balance: " + account[1].getBalance());
}
*/
}
The BankAccount Class file is below
public class BankAccount
{
// the attributes
private String accountNumber;
private String accountName;
private double balance;
// the methods
// the constructor
public BankAccount(String numberIn, String nameIn)
{
accountNumber = numberIn;
accountName = nameIn;
balance = 0;
}
// methods to read the attributes
public String getAccountName()
{
return accountName;
}
public String getAccountNumber()
{
return accountNumber;
}
public double getBalance()
{
return balance;
}
// methods to deposit and withdraw money
public void deposit(double amountIn)
{
balance = balance + amountIn;
}
public boolean withdraw(double amountIn)
{
if(amountIn > balance)
{
return false; // no withdrawal was made
}
else
{
balance = balance - amountIn;
return true; // money was withdrawn successfully
}
}
}
I do not know how to send the account[] array to get printed in the main method when the user requests to exit the program as I do not know what type of array it is. I wish to know how to send this array to the main method please.