Google Answers Logo
View Question
 
Q: Java Project ( No Answer,   0 Comments )
Question  
Subject: Java Project
Category: Computers > Programming
Asked by: tobymac8-ga
List Price: $50.00
Posted: 06 Nov 2005 16:15 PST
Expires: 11 Nov 2005 20:13 PST
Question ID: 589890
This is a maintenance project involving a menu-driven program to
handle data for a group of sales representatives. The code you are
given is incomplete and, as written, will not compile. You need to add
the missing code to satisfy processing requirements.

The starting code is as follows:

// Make it possible to reference required packaged code.

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

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: {

          // YOUR CODE GOES HERE

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

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

        case RECORD_SALE: {

          // YOUR CODE GOES HERE

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

        // This case displays data for a sales rep.

        case SHOW_REP_DATA: {

          // YOUR CODE GOES HERE

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

        // This case displays sales force statistics.

        case SHOW_SALES_STATISTICS: {

          // YOUR CODE GOES HERE

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

        // This case deletes a sales rep.

        case DELETE_REP: {

          // YOUR CODE GOES HERE

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

        // This case displays author information.

        case ABOUT: {

          // YOUR CODE GOES HERE

          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;
      }
    } while (choice != EXIT);
    System.out.println("Processing complete");
  }

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

    // YOUR CODE GOES HERE

  } 

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

    // YOUR CODE GOES HERE

  }

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

    // YOUR CODE GOES HERE

  }

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

    // YOUR CODE GOES HERE

  }

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

    // YOUR CODE GOES HERE

  }
}

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

 

Project requirements

Make changes ONLY where indicated (// YOUR CODE GOES HERE)- do NOT change any other
code! To make this program run correctly you must:

Code the method "bodies" of the supporting methods of the Project02
class (addRep, findRep, etc.). The documentation preceding each method
header tells you what needs to be done. All will act upon the same
ArrayList object (named reps) that is declared at the very beginning
of the Project02 class.

Complete each "case" within the switch of the main method of the
Project02 class. The details of each case are as follows:

ADD_REP

Prompt the user for the name of the sales rep and read it as a String. 

Use the findRep method to determine if the rep already exists. 

If the rep does not exist (null is returned by findRep), construct a
new SalesRep object for the rep, set their name in the object, call
the addRep method to add the rep to the list, and display a message
indicating that the rep has been added.

If the rep already exists (non-null returned by findRep), display a
message indicating that a duplicate rep exists.

RECORD_SALE

Prompt the user for the name of the sales rep and read it as a String. 

Call the findRep method to get the SalesRep object from the ArrayList. 

If the rep exists (non-null returned by findRep), prompt for and read
the sales amount (as a double) from the user and call the addToSales
method of the SalesRep object to record the sale. If the addToSales
method returns true (indicating success), use the replaceRep method to
store the updated SalesRep object back into the list and display a
message saying that the sale has been recorded. Otherwise, display a
message indicating that an invalid sales amount was entered.

If the rep does not exist (null is returned by findRep), display a
message indicating that the rep was not found.

SHOW_REP_DATA

Prompt the user for the name of the sales rep and read it as a String. 

Call the findRep method to get the SalesRep object from the ArrayList. 

If the rep exists (non-null returned by findRep), display the rep's
name and sales. A String with this data is easily obtained by calling
the toString method of the SalesRep object.

If the rep does not exist (null is returned by findRep), display a
message indicating that the rep was not found.

SHOW_SALES_STATISTICS

If the ArrayList is empty, display a message indicating that no data exists. 

If the ArrayList is not empty, declare an accumulator (initialized to
zero) and loop through the ArrayList retrieving each SalesRep object
via the findRep method. As each object is found, use its getSales
method to get the rep's sales and add that value to the current value
of the accumulator. When the loop ends, display how many sales reps
exist and the total sales for the entire sales force. Use the
moneyFormat method of the Utility class to help display this as a
formatted currency string.

DELETE_REP

Prompt the user for the name of the sales rep and read it as a String. 

Call the findRep method to get the SalesRep object from the ArrayList. 

If the rep exists (non-null returned by findRep), ask the user if they
are sure and read their reply as a String. If they reply with an upper
or lower case "Y", call the deleteRep method to delete the SalesRep
object and display a message indicating that the rep has been deleted.
If they respond with any other value, display a message saying that
the rep was not deleted.

If the rep does not exist (null is returned by findRep), display a
message indicating that the rep was not found.

ABOUT

Display your name in a format of your choice. 

 

Hints and tips

Look at the documentation and method headers for the methods of the
Utility class and SalesRep class. These methods will help you get the
job done but you must know how to use them.

Add and test one project requirement at a time. Do NOT try to do them
all at once. For example, it makes sense to start with the ADD_REP
case and its required supporting methods (findRep and addRep). When
those compile and run, move on to the SHOW_REP_DATA case. You will
need it to verify that you are adding reps correctly. Then, try the
DELETE_REP case and its supporting methods (like deleteRep), etc..

Clarification of Question by tobymac8-ga on 10 Nov 2005 14:37 PST
If anyone can figure this out for me and get it to run. I need this by
Friday (11/11/05) night at 8pm EST. I will pay you $30 through paypal.
 After I make sure it works. Thanks.
Answer  
There is no answer at this time.

Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy