Question: In Java Requirements In this question you are asked to take advantage of arrays to eliminate duplicated code and to allow easier modification for the
In Java
Requirements
In this question you are asked to take advantage of arrays to eliminate duplicated code and to allow easier modification for the application. The component that is to be modified is obtaining the information about an invoice. Currently the getInformation() method of the Order class asks the user for the number of hours (individual , small group, and large group) in sequence. The answers to these questions are stored in three separate variables.
Using parallel arrays, the information about the hours can be stored in fewer data structures. The following shows the new array attributes which are to replace the hoursIndividual, hoursSmallGroup, and hoursLargeGroup.
private double[] hours = new double[3];
In addition to this a parallel array is to be used to store the costs.
| public static final double COST_INDIVIDUAL = 15; public static final double COST_SMALL_GROUP = 17; public static final double COST_LARGE_GROUP = 20; to be replaced with public static final double[] COSTS = {15,17,20}; public static final String[] DESCRIPTION_GROUPS = {"Individual", "Small Group", "Large Group"}; public static final int INDIVIDUAL = 0; //These can be used as index for the array public static final int SMALL_GROUP = 1; public static final int LARGE_GROUP = 2; |
- Using a loop, modify the getInformation method to populate the values in the hours array.
- Using a loop, modify the getEarnings in terms of how it sets the initial earnings amount.
- Modify the toString method to use the array.
- Modify getters and setters
- Note if youd like to further enhance the application with arrays feel free. Just be sure not to break the functionality.
New class type.
- Add a full class option for the invoice in addition to individual, small group, and large group options. The cost for this type of group is $25.
Sample I/O
| The i/o will remain the same. The changes are only internal to how the processing is done. |
import java.util.Scanner;
public class Invoice {
//Add Invoice attributes public static final double COST_INDIVIDUAL = 15; public static final double COST_SMALL_GROUP = 17; public static final double COST_LARGE_GROUP = 20; public static final double COST_LARGE_ORDER = 200;
public static final double BONUS_SECOND_YEAR = 0.1; public static final double BONUS_PAST_GRADUATE = 0.2;
private Employee employee;
private double hoursIndividual, hoursSmallGroup, hoursLargeGroup; private int id; private static int nextId;
public Invoice() {
this.employee = new Employee(); }
public void getInformation() { Scanner input = new Scanner(System.in);
//Use the getInformation method from the employee object to get //the attributes of the employee associated with this invoice. employee.getInformation();
System.out.println("How many 'Individual' hours?"); hoursIndividual = input.nextDouble(); input.nextLine(); //burn
if (hoursIndividual < 0) { System.out.println("Invalid entry, individual hours being set to 0"); hoursIndividual = 0; }
System.out.println("How many 'Small Group' hours?"); hoursSmallGroup = input.nextDouble(); input.nextLine(); //burn
if (hoursSmallGroup < 0) { System.out.println("Invalid entry, small group hours being set to 0"); hoursSmallGroup = 0; }
System.out.println("How many 'Large Group' hours?"); hoursLargeGroup = input.nextDouble(); input.nextLine(); //burn
if (hoursLargeGroup < 0) { System.out.println("Invalid entry, large group hours being set to 0"); hoursLargeGroup = 0; }
System.out.println(""); //Empty line for formatting }
public double getEarnings() { double earnings = hoursIndividual * COST_INDIVIDUAL + hoursSmallGroup * COST_SMALL_GROUP + hoursLargeGroup * COST_LARGE_GROUP;
//Apply bonus using a switch statement. Multiply the earnings by 1+ the //appropriate bonus amount and assign to the earnings. switch (employee.getEmployeeType()) { case Employee.TYPE_SECOND_YEAR: earnings *= (1 + BONUS_SECOND_YEAR); break; case Employee.TYPE_PAST_GRADUATE: earnings *= (1 + BONUS_PAST_GRADUATE); break;
} return earnings; }
public int getId() { return id; }
public void setId(int id) { this.id = id; }
public static int getNextId() { return nextId; }
public static void setNextId(int nextId) { Invoice.nextId = nextId; }
public Employee getEmployee() { return employee; }
public void setEmployee(Employee employee) { this.employee = employee; }
public double getHoursIndividual() { return hoursIndividual; }
public void setHoursIndividual(double hoursIndividual) { this.hoursIndividual = hoursIndividual; }
public double getHoursSmallGroup() { return hoursSmallGroup; }
public void setHoursSmallGroup(double hoursSmallGroup) { this.hoursSmallGroup = hoursSmallGroup; }
public double getHoursLargeGroup() { return hoursLargeGroup; }
public void setHoursLargeGroup(double hoursLargeGroup) { this.hoursLargeGroup = hoursLargeGroup; }
public void display() { System.out.println(this.toString()); }
public String toString() { String largeEarningsDescription = ""; double earnings = getEarnings(); if (earnings > COST_LARGE_ORDER) { largeEarningsDescription = " (Large Earnings!)"; } String output = "Summary for " + employee.getName() + largeEarningsDescription + " " //Added calls to return attributes collected in Employee class + "Phone number: " + employee.getPhoneNumber() + " " + "Email address: " + employee.getEmail() + " " + "Employee type: " + employee.getEmployeeType() + " " + "Number of 'Individual' hours: " + hoursIndividual + " " + "Number of 'Small Group' hours: " + hoursSmallGroup + " " + "Number of 'Large Group' hours: " + hoursLargeGroup + " " //DONE Currency amount format: $3,456.75 //Solution format() method adopted from https://stackoverflow.com/questions/2379221/java-currency-number-format + "Earnings: $" + (String.format("%,.2f", earnings)) + " " + " "; return output; }
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
