/**
* The java program that simulates ATM operations.
* The program display a menu of choices.
* The prompt to enter operation to perform
* and then print the balance after the transaction
* to console.
* */
//AtmSimDoLoop.java
import java.util.Scanner;
public class AtmSimDoLoop {
public static void main(String[] args) {
//Create a Scanner class object
Scanner scanner=new Scanner(System.in);
//declare variables
double balance=0;
int userChoice;
double deposit;
double withdrawl;
//set repeat =true
boolean repeat=true;
//prompt and repeat until user enters 4 to exit
do {
System.out.println("Enter the number of your desired transaction type.");
System.out.println("1.Balance");
System.out.println("2.Deposit");
System.out.println("3.Withdrawl");
System.out.println("4.Quit");
userChoice=Integer.parseInt(scanner.nextLine());
//using switch case to select an appropriate choice
switch (userChoice) {
case 1:
System.out.println("Your current balance is "+balance);
break;
case 2:
System.out.println("Enter the amount of the deposit ");
deposit=Double.parseDouble(scanner.nextLine());
balance+=deposit;
System.out.println("Your current balance is "+balance);
break;
case 3:
System.out.println("Enter the amount of the withdrawl ");
withdrawl=Double.parseDouble(scanner.nextLine());
if(withdrawl<=balance)
{
balance-=withdrawl;
System.out.println("Your current balance is "+balance);
}
else
System.out.println("Insufficient funds. Your current balance is "+balance+".");
break;
case 4:
System.out.println("Good-bye.");
//set repeat=false
repeat=false;
}
} while (repeat);
}//end of main
}//end of class AtmSimDoLoop
------------------------------------------------------------------------------------------------------------
Sample output:
Enter the number of your desired transaction type.
1.Balance
2.Deposit
3.Withdrawl
4.Quit
1
Your current balance is 0.0
Enter the number of your desired transaction type.
1.Balance
2.Deposit
3.Withdrawl
4.Quit
2
Enter the amount of the deposit
500
Your current balance is 500.0
Enter the number of your desired transaction type.
1.Balance
2.Deposit
3.Withdrawl
4.Quit
3
Enter the amount of the withdrawl
300
Your current balance is 200.0
Enter the number of your desired transaction type.
1.Balance
2.Deposit
3.Withdrawl
4.Quit
3
Enter the amount of the withdrawl
300
Insufficient funds. Your current balance is 200.0.
Enter the number of your desired transaction type.
1.Balance
2.Deposit
3.Withdrawl
4.Quit
4
Good-bye.
Solution
/**
* The java program that simulates ATM operations.
* The program display a menu of choices.
* The prompt to enter operation to perform
* and then print the balance after the transaction
* to console.
* */
//AtmSimDoLoop.java
import java.util.Scanner;
public class AtmSimDoLoop {
public static void main(String[] args) {
//Create a Scanner class object
Scanner scanner=new Scanner(System.in);
//declare variables
double balance=0;
int userChoice;
double deposit;
double withdrawl;
//set repeat =true
boolean repeat=true;
//prompt and repeat until user enters 4 to exit
do {
System.out.println("Enter the number of your desired transaction type.");
System.out.println("1.Balance");
System.out.println("2.Deposit");
System.out.println("3.Withdrawl");
System.out.println("4.Quit");
userChoice=Integer.parseInt(scanner.nextLine());
//using switch case to select an appropriate choice
switch (userChoice) {
case 1:
System.out.println("Your current balance is "+balance);
break;
case 2:
System.out.println("Enter the amount of the deposit ");
deposit=Double.parseDouble(scanner.nextLine());
balance+=deposit;
System.out.println("Your current balance is "+balance);
break;
case 3:
System.out.println("Enter the amount of the withdrawl ");
withdrawl=Double.parseDouble(scanner.nextLine());
if(withdrawl<=balance)
{
balance-=withdrawl;
System.out.println("Your current balance is "+balance);
}
else
System.out.println("Insufficient funds. Your current balance is "+balance+".");
break;
case 4:
System.out.println("Good-bye.");
//set repeat=false
repeat=false;
}
} while (repeat);
}//end of main
}//end of class AtmSimDoLoop
------------------------------------------------------------------------------------------------------------
Sample output:
Enter the number of your desired transaction type.
1.Balance
2.Deposit
3.Withdrawl
4.Quit
1
Your current balance is 0.0
Enter the number of your desired transaction type.
1.Balance
2.Deposit
3.Withdrawl
4.Quit
2
Enter the amount of the deposit
500
Your current balance is 500.0
Enter the number of your desired transaction type.
1.Balance
2.Deposit
3.Withdrawl
4.Quit
3
Enter the amount of the withdrawl
300
Your current balance is 200.0
Enter the number of your desired transaction type.
1.Balance
2.Deposit
3.Withdrawl
4.Quit
3
Enter the amount of the withdrawl
300
Insufficient funds. Your current balance is 200.0.
Enter the number of your desired transaction type.
1.Balance
2.Deposit
3.Withdrawl
4.Quit
4
Good-bye.

The java program that simulates ATM operations. The prog.pdf

  • 1.
    /** * The javaprogram that simulates ATM operations. * The program display a menu of choices. * The prompt to enter operation to perform * and then print the balance after the transaction * to console. * */ //AtmSimDoLoop.java import java.util.Scanner; public class AtmSimDoLoop { public static void main(String[] args) { //Create a Scanner class object Scanner scanner=new Scanner(System.in); //declare variables double balance=0; int userChoice; double deposit; double withdrawl; //set repeat =true boolean repeat=true; //prompt and repeat until user enters 4 to exit do { System.out.println("Enter the number of your desired transaction type."); System.out.println("1.Balance"); System.out.println("2.Deposit"); System.out.println("3.Withdrawl"); System.out.println("4.Quit"); userChoice=Integer.parseInt(scanner.nextLine());
  • 2.
    //using switch caseto select an appropriate choice switch (userChoice) { case 1: System.out.println("Your current balance is "+balance); break; case 2: System.out.println("Enter the amount of the deposit "); deposit=Double.parseDouble(scanner.nextLine()); balance+=deposit; System.out.println("Your current balance is "+balance); break; case 3: System.out.println("Enter the amount of the withdrawl "); withdrawl=Double.parseDouble(scanner.nextLine()); if(withdrawl<=balance) { balance-=withdrawl; System.out.println("Your current balance is "+balance); } else System.out.println("Insufficient funds. Your current balance is "+balance+"."); break; case 4: System.out.println("Good-bye."); //set repeat=false repeat=false; } } while (repeat); }//end of main }//end of class AtmSimDoLoop ------------------------------------------------------------------------------------------------------------ Sample output: Enter the number of your desired transaction type.
  • 3.
    1.Balance 2.Deposit 3.Withdrawl 4.Quit 1 Your current balanceis 0.0 Enter the number of your desired transaction type. 1.Balance 2.Deposit 3.Withdrawl 4.Quit 2 Enter the amount of the deposit 500 Your current balance is 500.0 Enter the number of your desired transaction type. 1.Balance 2.Deposit 3.Withdrawl 4.Quit 3 Enter the amount of the withdrawl 300 Your current balance is 200.0 Enter the number of your desired transaction type. 1.Balance 2.Deposit 3.Withdrawl 4.Quit 3 Enter the amount of the withdrawl 300 Insufficient funds. Your current balance is 200.0. Enter the number of your desired transaction type. 1.Balance 2.Deposit
  • 4.
    3.Withdrawl 4.Quit 4 Good-bye. Solution /** * The javaprogram that simulates ATM operations. * The program display a menu of choices. * The prompt to enter operation to perform * and then print the balance after the transaction * to console. * */ //AtmSimDoLoop.java import java.util.Scanner; public class AtmSimDoLoop { public static void main(String[] args) { //Create a Scanner class object Scanner scanner=new Scanner(System.in); //declare variables double balance=0; int userChoice; double deposit; double withdrawl; //set repeat =true boolean repeat=true; //prompt and repeat until user enters 4 to exit do { System.out.println("Enter the number of your desired transaction type."); System.out.println("1.Balance");
  • 5.
    System.out.println("2.Deposit"); System.out.println("3.Withdrawl"); System.out.println("4.Quit"); userChoice=Integer.parseInt(scanner.nextLine()); //using switch caseto select an appropriate choice switch (userChoice) { case 1: System.out.println("Your current balance is "+balance); break; case 2: System.out.println("Enter the amount of the deposit "); deposit=Double.parseDouble(scanner.nextLine()); balance+=deposit; System.out.println("Your current balance is "+balance); break; case 3: System.out.println("Enter the amount of the withdrawl "); withdrawl=Double.parseDouble(scanner.nextLine()); if(withdrawl<=balance) { balance-=withdrawl; System.out.println("Your current balance is "+balance); } else System.out.println("Insufficient funds. Your current balance is "+balance+"."); break; case 4: System.out.println("Good-bye."); //set repeat=false repeat=false; } } while (repeat);
  • 6.
    }//end of main }//endof class AtmSimDoLoop ------------------------------------------------------------------------------------------------------------ Sample output: Enter the number of your desired transaction type. 1.Balance 2.Deposit 3.Withdrawl 4.Quit 1 Your current balance is 0.0 Enter the number of your desired transaction type. 1.Balance 2.Deposit 3.Withdrawl 4.Quit 2 Enter the amount of the deposit 500 Your current balance is 500.0 Enter the number of your desired transaction type. 1.Balance 2.Deposit 3.Withdrawl 4.Quit 3 Enter the amount of the withdrawl 300 Your current balance is 200.0 Enter the number of your desired transaction type. 1.Balance 2.Deposit 3.Withdrawl 4.Quit 3
  • 7.
    Enter the amountof the withdrawl 300 Insufficient funds. Your current balance is 200.0. Enter the number of your desired transaction type. 1.Balance 2.Deposit 3.Withdrawl 4.Quit 4 Good-bye.