Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

endless loop

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
*The program below compiles, but will not let me enter the name of a representative.*



// Make it possible to reference required packaged code.

import java.util.*;
import java.text.*;
import java.io.*;

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

private static ArrayList reps = new ArrayList();

// Application processing starts here.

public static void main(String[] args) {

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

case ADD_REP: {

System.out.print("Enter Representative Name:");
String name = Utility.readString();
if (findRep(name) == null) {
SalesRep SalesR = new SalesRep();
SalesR.setName(name);
addRep(SalesR);
System.out.println("Representative Added");
}
else {
System.out.println("Representative Exists");
}
System.out.println("");
Utility.pressEnter();
break;
}

// This case records a sale for a sales rep.

case RECORD_SALE: {
String SalesR = null;
System.out.print("Enter Representative Name:");
SalesR = Utility.readString();
SalesRep theRep = findRep(SalesR);
if (theRep != null) {
System.out.println("Enter Sales Amount:");
double amount = Utility.readDouble();
if (theRep.addToSales(amount) == true) {
replaceRep(theRep);
System.out.println("Sale Recorded");
}
else {
System.out.println("Invalid Sales Amount");
}
}
else {
System.out.println("Representative Not Found");
}

System.out.println("");
Utility.pressEnter();
break;
}

// This case displays data for a sales rep.

case SHOW_REP_DATA: {

System.out.print("Enter Representative Name:");
String name = Utility.readString();
SalesRep theRep = findRep(name);
if (findRep(name) != null) {
System.out.println(theRep.toString());
}
else {
System.out.println("Representative Not Found");
}
System.out.println("");
Utility.pressEnter();
break;
}

// This case displays sales force statistics.

case SHOW_SALES_STATISTICS: {

if (reps.isEmpty() == true) {
System.out.println("No Data Exists");
}
else {
double total = 0;
for (int i = 0; i<= reps.size(); i++) {
SalesRep theRep = findRep(i);
if(theRep != null) {
total += theRep.getSales();
}
}
System.out.println(Utility.moneyFormat(total));
}
System.out.println("");
Utility.pressEnter();
break;
}

// This case deletes a sales rep.

case DELETE_REP: {

System.out.print("Enter Representative Name:");
String name = Utility.readString();
SalesRep theRep = findRep(name);
if (theRep != null) {
System.out.println("Are You Sure:");
String response = Utility.readString();
if (response == "Y" || response == "y") {
deleteRep(theRep);
System.out.println("Record Deleted");
}
else {
System.out.println("Record Not Deleted");
}
}
else {
System.out.println("Representative Not Found");
}

System.out.println("");
Utility.pressEnter();
break;
}

// This case displays author information.

case ABOUT: {

System.out.println("Matthew Gates");

System.out.println("");
Utility.pressEnter();
break;
}

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

public static SalesRep findRep(int index) {


for (int i=0; i<reps.size(); ++i) {

SalesRep rep = (SalesRep) reps.get(i);
if (rep != null)
return rep;


}

return null;


}

// 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);

if (theRep != null) {
reps.remove(theRep);
return true;
}
else {
return false;
}


}
}

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

public boolean addToSales(double amount) {
if (amount > 0) {
totalSales += amount;
return true;
}
else {
return false;
}
}

// This method can be called to get the rep's total sales.

public double getSales() {
return totalSales;
}

// This method can be called to get a String representation of the rep's data
// in the format: name, $n,nnn.nn

public String toString() {
return getName() + ", " + Utility.moneyFormat(getSales());
}

// This method can be called to test SalesRep equality. Two reps are deemed
// equal if they have the same name.

public boolean equals(SalesRep otherRep) {
if (otherRep.getName().equals(name)) {
return true;
}
else {
return false;
}
}
}

// This class contains custom methods that support application processing.
// ----- Written by: Jon Huhtala

class Utility {

// This BufferedReader object is used within this class to read data from the
// input stream (keyboard).

private static BufferedReader kbReader =
new BufferedReader(new InputStreamReader(System.in));

// This method can be called to read an int value from the keyboard.

public static int readInt() {
Integer result = null;
while (result == null) {
try {
result = new Integer(kbReader.readLine());
}
catch(Exception err) {
System.out.print("ERROR! Please try again: ");
}
}
return result.intValue();
}

// This method can be called to read a double value from the keyboard.

public static double readDouble() {
Double result = null;
while (result == null) {
try {
result = new Double(kbReader.readLine());
}
catch(Exception err) {
System.out.print("ERROR! Please try again: ");
}
}
return result.doubleValue();
}

// This method can be called to read a String value from the keyboard.

public static String readString() {
String result = null;
while (result == null) {
try {
result = kbReader.readLine();
}
catch(Exception err) {
System.out.print("ERROR! Please try again: ");
}
}
return result;
}

// This method can be called to prompt the user to press the ENTER key to
// continue.

public static void pressEnter() {
System.out.print("Press ENTER to continue...");
readString();
}

// This method can be called to convert a double amount into a formatted
// currency string.

public static String moneyFormat(double amount) {
return NumberFormat.getCurrencyInstance().format(amount);
}
}
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
    Bookmark Topic Watch Topic
  • New Topic