The file Salesperson.java partially defines a class that represents a sales pers
ID: 3559872 • Letter: T
Question
The file Salesperson.java partially defines a class that represents a sales person. This is very similar to the Contact class in Listing 10.10. However, a sales person has a first name, last name, and a total number of sales (an int) rather than a first name, last name, and phone number.
Complete the compareTo method in the Salesperson class.
The comparison should be based on total sales; that is, return a negative number if the executing object has total sales less than the other object and return a positive number if the sales are greater. Use the name of the sales person to break a tie (alphabetical order).
The file WeeklyS ales .java contains a driver for testing the compareTo method and the sorting (this is similar to Listing 10.8 in the text). Compile and run it.
Make sure your compareTo method is correct. The sales staff should be listed in order of sales from most to least with the four people having the same number of sales in reverse alphabetical order.
Modify WeeklySales.java so the salespeople are read in rather than hardcoded in the program.
//******************************************************************
// Sorting.java Author: Lewis/Loftus
//
// Demonstrates the selection sort and insertion sort algorithms.
//******************************************************************
public class Sorting
{
//-----------------------------------------------------------------
// Sorts the specified array of objects using the selection
// sort algorithm.
//-----------------------------------------------------------------
public static void selectionSort (Comparable[] list)
{
int min;
Comparable temp;
for (int index = 0; index < list.length-1; index++)
{
min = index;
for (int scan = index+1; scan < list.length; scan++)
if (list[scan].compareTo(list[min]) < 0)
min = scan;
// Swap the values
temp = list[min];
list[min] = list[index];
list[index] = temp;
}
}
//-----------------------------------------------------------------
// Sorts the specified array of objects using the insertion
// sort algorithm.
//-----------------------------------------------------------------
public static void insertionSort (Comparable[] list)
{
for (int index = 1; index < list.length; index++)
{
Comparable key = list[index];
int position = index;
// Shift larger values to the left
while (position > 0 && key.compareTo(list[position-1]) >0)
{
list[position] = list[position-1];
position--;
}
list[position] = key;
}
} }
//*******************************************************
//Salesperson.java
//Represents a sales person who has a first name, last
//name, and total number of sales.
//******************************************************
public class Salesperson implements Comparable
{
private String firstName, lastName;
private int totalSales;
//------------------------------------------------------
// Constructor: Sets up the sales person object with
// the given data.
//------------------------------------------------------
public Salesperson (String first, String last, int sales) {
firstName = first;
lastName = last;
totalSales = sales;
}
//-------------------------------------------
// Returns the sales person as a string.
//-------------------------------------------
public String toString()
{
return lastName + ", " + firstName + ": " + totalSales; }
//-------------------------------------------
// Returns true if the sales people have
//the same name.
//-------------------------------------------
public boolean equals (Object other)
{
return (lastName.equals(((Salesperson)other).getLastName()) &&
firstName.equals(((Salesperson)other).getFirstName()));
}
//--------------------------------------------------
// Order is based on total sales with the name
//(last, then first) breaking a tie.
//--------------------------------------------------
public int compareTo(Object other)
{
int result;
return result;
}
//-------------------------
// First name accessor.
//-------------------------
public String getFirstName()
{
return firstName;
}
//-------------------------
// Last name accessor.
//-------------------------
public String getLastName()
{
return lastName;
}
//-------------------------
// Total sales accessor.
//-------------------------
public int getSales()
{
return totalSales;
}
}
import java.util.*;
//******************************************************************
//WeeklySales.java
//Sorts the sales staff in descending order by sales.
//
// ******************************************************************
public class WeeklySales {
public static void main(String[] args)
{
Salesperson[] salesStaff = new Salesperson[10];
salesStaff[0]= new Salesperson("Jane", "Jones", 3000);
salesStaff[1]= new Salesperson("Daffy", "Duck", 4935);
salesStaff[2]= new Salesperson("James", "Jones", 3000);
salesStaff[3]= new Salesperson("Dick", "Walter", 2800);
salesStaff[4]= new Salesperson("Don", "Trump", 1570);
salesStaff[5]= new Salesperson("Jane", "Black", 3000);
salesStaff[6]= new Salesperson("Harry", "Taylor", 7300);
salesStaff[7]= new Salesperson("Andy", "Adams", 5000);
salesStaff[8]= new Salesperson("Jim", "Doe", 2850);
salesStaff[9]= new Salesperson("Walt", "Smith", 3000);
Sorting.insertionSort(salesStaff);
System.out.println (" Ranking of Sales for the Week ");
for (Salesperson s : salesStaff)
System.out.println (s);
} }
Explanation / Answer
// *******************************************************
// Salesperson.java
//
// Represents a sales person who has a first name, last
// name, and total number of sales.
// *******************************************************
public class Salesperson implements Comparable
{
private String firstName, lastName;
private int totalSales;
//------------------------------------------------------
// Constructor: Sets up the sales person object with
// the given data.
//------------------------------------------------------
public Salesperson (String first, String last, int sales)
{
firstName = first;
lastName = last;
totalSales = sales;
}
//-------------------------------------------
//Returns the sales person as a string.
//-------------------------------------------
public String toString()
{
return lastName + ", " + firstName + ": " + totalSales;
}
//-------------------------------------------
//Returns true if the sales people have
//the same name.
//-------------------------------------------
public boolean equals (Object other)
{
return (lastName.equals(((Salesperson)other).getLastName()) &&
firstName.equals(((Salesperson)other).getFirstName()));
}
//--------------------------------------------------
//Order is based on total sales with the name
//(last, then first) breaking a tie.
//--------------------------------------------------
public int compareTo(Object other)
{
int result = 0;
int otherSales = ((Salesperson)other).getSales();
String otherFirst = ((Salesperson)other).getFirstName();
String otherLast = ((Salesperson)other).getLastName();
if (totalSales > otherSales)
{
return 1;
}
else if (totalSales < otherSales)
{
return -1;
}
else {
if (lastName.equals(otherLast))
return firstName.compareTo(otherFirst);
else
return lastName.compareTo(otherLast);
}
}
//-------------------------
//First name accessor.
//-------------------------
public String getFirstName()
{
return firstName;
}
//-------------------------
//Last name accessor.
//-------------------------
public String getLastName()
{
return lastName;
}
//-------------------------
//Total sales accessor.
//-------------------------
public int getSales()
{
return totalSales;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.