

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


/* 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