/* 2nd of 3 examples to demonstrate better method design Section 6.7 in the textbook This program is a VERY simple "accounting" program which simply prompts the user for "accout" information and stores the data in an array The goal is not to be useful, but to show good/bad method design This example uses better methods, and is easier to read/maintain. */ import java.util.*; public class goodMethod { private Scanner easy; // global easyin object private record[] database; // global database array private int numRecords; // global number of records variable /* Main method, only creates a new object and starts the program */ public static void main(String[] args) { goodMethod gm = new goodMethod(); gm.run(); } /* This is the class' only public method. This is where the work starts. The user is prompted to enter accounts, either by name or account number until they enter a number other than 1 or 2, then the program quits. */ public void run() { /* assign and initialize the globals */ easy = new Scanner(System.in); database = new record[100]; numRecords = 0; int choice; // holds answer to the first question for(;;) { // infinite loop choice = promptInt("Will the record be by name or account number? [1,2]"); /* The user has three options 1 is by name, 2 is by number, any other number means quit. Entering something other than an integer will make the program crash */ switch(choice) { case 1: /* Now, each task has its own method */ getByName(); numRecords++; break; case 2: getByAcct(); numRecords++; break; default: System.out.println("Quitting."); System.exit(1); } System.out.println("Entered "+numRecords+" records."); } } /* The following methods each create one new record, one by name, one by account */ private void getByName() { String input; int dollars; record tempRec; input = promptString("Enter the name."); easy.nextLine(); dollars = promptInt("Enter the dollar amount."); tempRec = new record(); tempRec.name = input; tempRec.value = dollars; database[numRecords] = tempRec; } private void getByAcct() { int account; int dollars; record tempRec; account = promptInt("Enter the account number."); dollars = promptInt("Enter the dollar amount."); tempRec = new record(); tempRec.account = account; tempRec.value = dollars; database[numRecords] = tempRec; } /* The following methods each prompt the user for something The text of the question is passed as a parameter, and they return the result of the query. These are used all over the rest of the program. By having them be separate methods, changes to the way the program prompts the user are much easier to implement. */ private int promptInt(String text) { int num; System.out.println(text); System.out.print("> "); num = easy.nextInt(); return num; } private String promptString(String text) { String str; System.out.println(text); System.out.print("> "); str = easy.nextLine(); return str; } /* this private class is simply a container for the account variables */ private class record { public String name; public int account; public int value; } }