This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
// The main method of this class can be used to enter the name of a sales representative, // and the amount of sales that they have made. It also can display the name of a representative // and the amount of sales they have made,display statistics for the entire sales force, and // delete representative records. It supports keyboard input and demonstrates menu-driven processing. // --------Written By: Matthew Gates
public class Project02 {
// This ArrayList will contain all SalesRep objects. Its wide scope // makes it accessible to all methods of the class.
// Constants and variables needed for menu processing.
final int ADD_REP = 1; final int RECORD_SALE = 2; final int SHOW_REP_DATA = 3; final int SHOW_SALES_STATISTICS = 4; final int DELETE_REP = 5; final int ABOUT = 6; final int EXIT = 7; int choice = 0;
// This loop handles menu-driven processing. The loop will continue until // the user chooses to exit.
do {
// Display the menu and get the user's choice.
System.out.println(""); System.out.println("----------------------------------------"); System.out.println(""); System.out.println("SALES MENU"); System.out.println(""); System.out.println("1 - Add a sales rep"); System.out.println("2 - Record a sale"); System.out.println("3 - Show data for a sales rep"); System.out.println("4 - Show sales statistics"); System.out.println("5 - Delete a sales rep"); System.out.println("6 - About"); System.out.println("7 - Exit"); System.out.println(""); System.out.print("Enter choice: "); choice = Utility.readInt(); System.out.println("");
// This switch processes the user's menu selection.
switch (choice) {
// This case adds a new sales rep to the sales force.
// This case processes a request to exit from the menu-driven loop.
case EXIT: System.out.println("Exit in progress"); break;
// This case processes an invalid menu selection.
default: System.out.println("Invalid menu choice"); System.out.println(""); Utility.pressEnter(); break; } System.out.println("Processing complete"); } while (choice != EXIT);
}
// This method can be called to add a SalesRep object to the end of // the ArrayList. The SalesRep object is received as a parameter. If // the list doesn't already contain the object, it is added to the // list and true is returned to indicate success. Otherwise, false // is returned to indicate the existence of a duplicate rep.
public static boolean addRep(SalesRep rep) { String name = Utility.readString(); SalesRep theRep = findRep(name); if (theRep == null) { reps.add(name); return true; } else { return false; }
}
// This method can be called to find a SalesRep object within the // ArrayList. The rep's name is received as a String parameter. If // a rep with that name is found, the reference of the SalesRep object // is returned. Otherwise, null is returned.
public static SalesRep findRep(String name) { for (int i=0; i<reps.size(); ++i) {
SalesRep rep = (SalesRep) reps.get(i); if (rep.getName().equals(name)) return rep;
}
return null;
}
// This method can be called to find a SalesRep object within the // ArrayList. Its index is received as a parameter. If the index is // valid, the reference of the corresponding SalesRep object is // returned. Otherwise, null is returned.
// This method can be called to replace a SalesRep object within // the ArrayList. The SalesRep object is received as a parameter. If // the list contains the SalesRep object, it is replaced and true is // returned to indicate success. Otherwise, false is returned to // indicate that the SalesRep object does not exist.
public static boolean replaceRep(SalesRep rep) { String name = Utility.readString(); SalesRep theRep = findRep(name); if (theRep != null) { int index = reps.indexOf(name); reps.set(index, name); return true; } else { return false; }
}
// This method can be called to delete a SalesRep object from the // ArrayList. The SalesRep object is received as a parameter. If // the list contains the SalesRep object, it deleted and true is // returned to indicate success. Otherwise, false is returned to // indicate that the SalesRep object does not exist.
public static boolean deleteRep(SalesRep rep) { String name = Utility.readString(); SalesRep theRep = findRep(name);
// This class encapsulates the data and processing of a sales representative. // ----- Written by: Jon Huhtala
class SalesRep { private String name; private double totalSales;
// This method can be called to set the rep's name.
public void setName(String repName) { name = repName; }
// This method can be called to get the rep's name.
public String getName() { return name; }
// This method can be called to add to the rep's total sales. It expects a // single parameter for the sales amount. If the amount is greater than zero, // it is added to the rep's total and true is returned. Otherwise, false is // returned without changing the rep's total.
Well, he used by findRep(String) routine and wrote a new findRep(int) one -- although the latter isn't really right. Matthew, can you explain what that routine does?
But in any case, please don't start a new thread in the middle of a discussion like this. I'm going to close this thread; let's please continue talking about this program in the original thread that Scott has pointed to.