
Summary
Lets wrap up what we've learn so
far. We have learn how to print, ask users for inputs, using if-else/else-if,
loops
and dealing math expressions. Now lets refresh our memory for a little.
The if statement has this form
if ( boolean - expression )
statement...
The if-else statement has this form
if ( boolean-expression)
statement
else
statement
The basic format of a for loop
for ( initialization-expression;
testing-expression; counting-expression;)
{
statements;
}
The basic format of a do-while loop
do
{
statement;
}while(expression);
The basic format of a while loop
while (expression)
statement;
Now lets look at a comprehensive example.
/* This is a simple atm program
that asks the user
to enter a pin number, the pin number is set to 111
then it will verifies if the pin matches
if it does match, then the
user get to choose, check account balance,
deposite, withdraw, or exit.
The balance is set to 2000
Overall the program demonstrate the use of
loops, if-statements, getting user inputs
and making decisions using the inputs
*
*/
import java.util.Scanner;
public class Atm
{
public static void main(String[] args)
{
int n = 0;
int totalamount = 2000;
int pinnum;
int depot, withdraw;
String choices;
Scanner key = new Scanner(System.in);
// Here we ask the user for the pin number
System.out.print("Please enter your
pin number: ");
pinnum = key.nextInt();
/* If the user's input does not match, we will loop
And ask the user to enter the pin again
Maximum time we will loop is 3 times,
If it loops more than 3 times the program will exit
*
*/
while((pinnum!=111) && (n < 3))
{
System.out.print("Please enter your pin number: ");
pinnum =
key.nextInt();
n++;
if(n==3)
{
System.out.println("Sorry too many tries, program will exit");
System.exit(0);
}
}
/* Here we will use a do-while loop
The loop keeps on looping unless the user
Enters e to exit
*
*/
do
{
System.out.println("Enter what would you like to do\n");
System.out.println("D--Deposite, W--Withdraw, C--Check Balance, E--Exit");
System.out.print("Enter your selection now: ");
/* Here we use .next() instead of.nexyLine(), because we're only
retrieving a single charater
*
*/
choices = key.next();
// Inside this if statement we calculates balance after withdrawing
if(choices.equals("W") || choices.equals("w"))
{
System.out.println("You have selected with draw\n");
System.out.print("Please enter the amount you want to with draw: \n");
withdraw = key.nextInt();
totalamount -= withdraw;
}
// Inside this else-if statement we checks the user's balance
else
if(choices.equals("C") || choices.equals("c"))
{
System.out.println("You have selected check balance\n");
System.out.print("Your account balance is " + totalamount + "\n" );
}
// Inside this else-if statement we calculates the balance after deposite
else
if(choices.equals("d") || choices.equals("D"))
{
System.out.println("You have selected deposite\n");
System.out.print("Please enter the amount you want to deposite: ");
depot = key.nextInt();
totalamount += depot;
}
// Inside this else-if statement we only have one statement, which will exit the
program
else
if(choices.equals("E") || choices.equals("e"))
{
System.exit(0);
}
}while(true);
}
}
ClickHere to download Atm.java