Java ATM Menu Simulation: TestMenu Class, Study notes of Computer Science

The source code for a java class named testmenu that tests an atm menu. The menu class, menu, is responsible for displaying the menu and getting user choices. Users can make deposits, withdrawals, check their balance, or quit the application.

Typology: Study notes

Pre 2010

Uploaded on 08/09/2009

koofers-user-xf4-1
koofers-user-xf4-1 🇺🇸

10 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
/* A class that tests a menu that could be used for an ATM. Instead of calling methods to carry
out the actions, such as displaying the balance, it just prints out the choice. */
public class TestMenu
{
public static void main (String [] args)
{
Menu menu = new Menu ();
menu.makeSeveralChoices ();
}
} // class TestMenu
/* This class displays the menu and gets the users choice. It gets choices until the user decides
to quit. */
class Menu
{
private char choice;
public void displayMenu ()
{
System.out.println ("B: display balance");
System.out.println ("D: make deposit");
System.out.println ("W: make withdrawal");
System.out.println ("M: display menu");
System.out.println ("Q: quit");
} // method displayMenu
public boolean makeChoice ()
{
boolean quit = false;
System.out.print ("Enter your choice: ");
choice = Reader.readChar ();
switch (choice)
{
case 'b': case 'B': System.out.println ("Balance"); break;
case 'd': case 'D': System.out.println ("Deposit"); break;
case 'w': case 'W': System.out.println ("Withdrawal"); break;
case 'm': case 'M': displayMenu (); break;
case 'q': case 'Q': quit = true; break;
default: System.out.println ("Invalid entry - try again");
} // switch
return quit;
} // method makeChoice;
public void makeSeveralChoices ()
{
boolean quit = false;
do
{
displayMenu ();
pf2

Partial preview of the text

Download Java ATM Menu Simulation: TestMenu Class and more Study notes Computer Science in PDF only on Docsity!

/* A class that tests a menu that could be used for an ATM. Instead of calling methods to carry out the actions, such as displaying the balance, it just prints out the choice. / public class TestMenu { public static void main (String [] args) { Menu menu = new Menu (); menu.makeSeveralChoices (); } } // class TestMenu / This class displays the menu and gets the users choice. It gets choices until the user decides to quit. */ class Menu { private char choice; public void displayMenu () { System.out.println ("B: display balance"); System.out.println ("D: make deposit"); System.out.println ("W: make withdrawal"); System.out.println ("M: display menu"); System.out.println ("Q: quit"); } // method displayMenu public boolean makeChoice () { boolean quit = false; System.out.print ("Enter your choice: "); choice = Reader.readChar (); switch (choice) { case 'b': case 'B': System.out.println ("Balance"); break; case 'd': case 'D': System.out.println ("Deposit"); break; case 'w': case 'W': System.out.println ("Withdrawal"); break; case 'm': case 'M': displayMenu (); break; case 'q': case 'Q': quit = true; break; default: System.out.println ("Invalid entry - try again"); } // switch return quit; } // method makeChoice; public void makeSeveralChoices () { boolean quit = false; do { displayMenu ();

quit = makeChoice (); }while (!quit); System.out.println ("Thank you, come again."); } // method makeSeveralChoices } // class Menu class Reader { // Reads a character from the keyboard and returns it to the calling method. public static char readChar () { char ch; BufferedReader stdin = new BufferedReader (new InputStreamReader (System.in)); try { ch = (char)stdin.read (); } catch (IOException e) { ch = ' ';} return ch; } // method readChar () } // public class Reader