Help, on a homework assignment my instructor made the following comment, could anybody please help in making this change:
In the Employee's toString, you are using the NumberFormat class to format your hourly rate and weekly pay but you are supposed to use java string formatting only (%f).
You should change those to use only string formatting - no use of NumberFormat.
package coursework; import java.text.NumberFormat; public class Employee { private String firstName; private String lastName; private int employeeId; private double hourlyRate; public Timecard timeCard; public Employee(String firstNameInput, String lastNameInput, int idInput, double rateInput, int[] daysIn) { setFirstName(firstNameInput); setLastName(lastNameInput); setEmployeeID(idInput); setHourlyRate(rateInput); setTimeCard(new Timecard(daysIn)); } public void printEmpDetails() { System.out.println("Name: " + firstName + " " + lastName); System.out.println("Emp. Id: " + employeeId); System.out.println("Hourly Rate: $" + hourlyRate); System.out.println(); } private void setFirstName(String firstName) { // Here we are setting our class variable firstName to the passed in // variable "name" // But only if it has a length greater than 0 and less than or equal to // 20 int nameLength = firstName.length(); if (nameLength < 1) { System.out.println("First Name length is too small"); System.out.println(firstName + ": " + firstName.length()); } else if (nameLength > 20) { System.out.println("First Name length is too long"); System.out.println(firstName + ": " + firstName.length()); } else { this.firstName = firstName; } } private void setLastName(String lastName) { // Here we are setting our class variable firstName to the passed in // variable "name" // But only if it has a length greater than 0 and less than or equal to // 20 int nameLength = lastName.length(); if (nameLength < 1) { System.out.println("Last Name length is too small"); System.out.println(firstName + ": " + lastName.length()); } else if (nameLength > 20) { System.out.println("Last Name length is too long"); System.out.println(firstName + ": " + lastName.length()); } else { this.lastName = lastName; } } private void setEmployeeID(int employeeId) { // Here we are setting our class variable firstName to the passed in // variable "name" // But only if it has a length greater than 0 and less than or equal to // 20 if (employeeId > 9999) { System.out .println("Number length error: this should be less than 9999: " + employeeId); } else if (employeeId < 1000) { System.out .println("Number length error: this should be more than 1000: " + employeeId); } else { this.employeeId = employeeId; } } private void setHourlyRate(double hourlyRate) { // Here we are setting our class variable firstName to the passed in // variable "name" // But only if it has a length greater than 0 and less than or equal to // 20 if (hourlyRate < 0) { System.out .println("Incorrect value, number must be greater than zero: " + hourlyRate); } else { this.hourlyRate = hourlyRate; } } public void setTimeCard(Timecard newtimeCard) { if (newtimeCard == null) { System.out.print("time card cannot be null"); System.exit(-1); } timeCard = newtimeCard; } public Timecard getTimeCard() { return timeCard; } public String getFirstName() { // Here we are getting our class variable firstName return firstName; } public String getLastName() { // Here we are getting our class variable lastName return lastName; } public int getEmployeeId() { // Here we are getting our class variable firstName return employeeId; } public double getHourlyRate() { // Here we are getting our class variable firstName return hourlyRate; } public double getWeeklyPay() { int regHrs = 0; int otHrs = 0; for (int i = 0; i < 5; i++) { int hrsDay = getTimeCard().getHoursByDay(i); int ot = 0; if (hrsDay > 8) { ot = hrsDay - 8; hrsDay = hrsDay - ot; } regHrs += hrsDay; otHrs += ot; } double baseWeeklyPay = (regHrs * getHourlyRate()) + (otHrs * getHourlyRate() * 1.5); char begId = String.valueOf(getEmployeeId()).charAt(0); if (begId == '0' || begId == '2' || begId == '9') { baseWeeklyPay = baseWeeklyPay * 1.1; } else if (begId == '3') { baseWeeklyPay = baseWeeklyPay * 0.9; } else if (begId == '8') { baseWeeklyPay = baseWeeklyPay * 1.2; } if (regHrs > 34) { baseWeeklyPay = baseWeeklyPay * 0.94; } return baseWeeklyPay; } public String toString() { StringBuilder sb = new StringBuilder(); String.format formatter = StringFormat.getCurrencyInstance(); sb.append(String.format("Name: %s %s",getFirstName(),getLastName())).append("\n") .append(String.format("Id: %s",getEmployeeId())).append("\n") .append(String.format("Hourly Rate: %s",formatter.format(getHourlyRate()))).append("\n") .append(getTimeCard()); sb.append(String.format("Weekly Pay : %s",formatter.format(getWeeklyPay()))); sb.append("\n"); return sb.toString(); } }