I need a little help on writing the following program:
Use the Account class to simulate an ATM machine.Create ten accounts in an array with id 0, 1,. . . 9, and initial
balance $100. The system prompts the user to enter an id. If the id is entered incorrectly, ask the user to enter a correct id. Once an id is accepted, the main menu is displayed as shown in the sample run. You can enter a choice 1 for view-ing the current balance, 2 for withdrawing money, 3 for depositing money, and 4,for exiting the main menu. Once you exit, the system will prompt for an id again.
So, once the system starts, it will not stop.

The output should be as follow:
Enter an id :4 //user enter

Main menu
1: check balance
2: withdraw
3: deposit
4: exit
Enter a choice:1
The balance is 100.0

Main menu
1: check balance
2: withdraw
3: deposit
4: exit
Enter a choice:2
Enter an amount to withdraw :3

Main menu
1: check balance
2: withdraw
3: deposit
4: exit
Enter a choice:1
The balance is 97.0

Main menu
1: check balance
2: withdraw
3: deposit
4: exit
Enter a choice:4

Enter an id:

But my problem is,my output turnn out to be like this :
Enter an id : 1
Main menu
1: check balance
2: withdraw
3: deposit
4: exit
Enter a choice: 1
The balance is 100.0
Enter an id : 1
Main menu
1: check balance
2: withdraw
3: deposit
4: exit
Enter a choice:4
Java successfully built

Here are my program for test class so far...

import java.util.Scanner;
public class ATMmachineSimulation {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int pin;

Account[] objAcc = new Account[10];
for(int i=0;i<10;i++){
new Account(i,100);
}

do{

do{
System.out.println("Enter an id : ");
pin = input.nextInt();
if(!(pin>=0 && pin<10))
System.out.println("You entered incorrect id." +
"Please enter a correct id. ");
}while(!(pin>=0 && pin<10));

System.out.println();
System.out.println();
System.out.println("Main menu ");
System.out.println("1: check balance");
System.out.println("2: withdraw");
System.out.println("3: deposit");
System.out.println("4: exit");
System.out.print("Enter a choice: ");
int choice = input.nextInt();

switch(choice){
case 1:
System.out.println("The balance is "+objAcc[pin].getBalance());
break;
case 2:
System.out.print("Enter an amount to withdraw: ");
double withdrawCash = input.nextDouble();
objAcc[pin].withdraw(withdrawCash)…
System.out.println("Your net balance is: "+objAcc[pin].getBalance());
break;
case 3:
System.out.print("Enter an amount to deposit: ");
double depositCash = input.nextDouble();
objAcc[pin].deposit(depositCash);
System.out.println("Your net balance is: "+objAcc[pin].getBalance());
break;
case 4:
System.exit(pin);
break;

default:
System.out.println("You entered invalid selection");
break;
}

}while(pin>=0 && pin<10);


}

}

Any help please...

Recommended Answers

All 3 Replies

case 4: System.exit(pin);

System.exit() immediately terminates your whole program, so it's a bad idea to call it when you just want to exit the current loop. You haven't indented your code, so I'm not going to waste time trying to understand how your loops are nested, but I would start by removing the System.exit.

Hi,

I think that in these lines:

Account[] objAcc = new Account[10];
for(int i=0;i<10;i++){
new Account(i,100);
}

You create an array of Account, but inside the for loop you are creating once more ten Account variables that you don't assign. Maybe you want to use inside the for loop something like objAcc[i].SOMEMETHODOFTHEACCOUNTCLASS(). Or assign the created Account to some place of the array. Something like objAcc[i] = new Account()

Please, forgive me,

I was confused. The solution is about objAcc[i] = new Account(Your parameters) inside the loop, not the first one (objAcc[i].Som...).

I mixed in my head C++ and Java.

Sorry.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.