Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

hey there, in past few hours i have already got the code for this one but that d

ID: 3728454 • Letter: H

Question

hey there, in past few hours i have already got the code for this one but that dosnt work properly. Please help me to solve this.

a. Create a class named Salesperson. Data fields for Salesperson include an integer ID number and a double annual sales amount. Methods include a constructor that requires values for both data fields, as well as get and set methods for each of the data fields. Save the file as Salesperson.java.   

b. Create an application that allows a user to enter values for an array of seven Salesperson objects. Offer the user the choice of displaying the objects in order by either ID number or sales value. Save the application as SalespersonSort.java.

Explanation / Answer

public class Salesperson implements Comparable<Salesperson> {

   public static int i = 0;

   private int id;

   private double salesAmount;

   /**

   * @param id

   * @param salesAmount

   */

   public Salesperson(int id, double salesAmount) {

       super();

       this.id = id;

       this.salesAmount = salesAmount;

   }

   /**

   * @return the id

   */

   public int getId() {

       return id;

   }

   /**

   * @param id the id to set

   */

   public void setId(int id) {

       this.id = id;

   }

   /**

   * @return the salesAmount

   */

   public double getSalesAmount() {

       return salesAmount;

   }

   /**

   * @param salesAmount the salesAmount to set

   */

   public void setSalesAmount(double salesAmount) {

       this.salesAmount = salesAmount;

   }

  

  

  

   /* (non-Javadoc)

   * @see java.lang.Object#toString()

   */

   @Override

   public String toString() {

       return "Salesperson [id=" + id + ", salesAmount=" + salesAmount + "]";

   }

   public int compareTo(Salesperson compareFruit) {

      

       if (i == 1){

           int compareQuantity = ((Salesperson) compareFruit).getId();

           //ascending order

           return this.id - compareQuantity;

           //descending order

           //return compareQuantity - this.id;

       }

       else{

           double compareQuantity = ((Salesperson) compareFruit).getSalesAmount();

           //ascending order

           return (int) (this.salesAmount - compareQuantity);

           //descending order

           //return compareQuantity - this.salesAmount;

       }

      

   }

  

}

public class SalespersonSort {

  

   public static void main(String[] args){

       Salesperson[] persons = new Salesperson[7];

       Scanner sc = new Scanner(System.in);

       try{

       for (int i=0;i<7;i++){

           System.out.println("Please enter detail of "+(i+1)+ " person");

           System.out.println("Enter id as integer");

           int id = sc.nextInt();

           System.out.println("Enter Sales amount");

           double amount = sc.nextDouble();

           Salesperson person = new Salesperson(id, amount);

           persons[i] = person;

       }

       System.out.println("Please enter 1. for sorting by Id, 2. for sorting by sales amount");

       Salesperson.i = sc.nextInt();

           Arrays.sort(persons);

           for (Salesperson p:persons){

               System.out.println(p.toString());

           }

       }

       catch(Exception e){

           e.printStackTrace();

       }

       sc.close();

   }

}

Sample output:

Please enter detail of 1 person

Enter id as integer

1

Enter Sales amount

21

Please enter detail of 2 person

Enter id as integer

2

Enter Sales amount

434

Please enter detail of 3 person

Enter id as integer

7

Enter Sales amount

323

Please enter detail of 4 person

Enter id as integer

6

Enter Sales amount

32

Please enter detail of 5 person

Enter id as integer

8

Enter Sales amount

32

Please enter detail of 6 person

Enter id as integer

4

Enter Sales amount

32

Please enter detail of 7 person

Enter id as integer

5

Enter Sales amount

32

Please enter 1. for sorting by Id, 2. for sorting by sales amount

1

Salesperson [id=1, salesAmount=21.0]

Salesperson [id=2, salesAmount=434.0]

Salesperson [id=4, salesAmount=32.0]

Salesperson [id=5, salesAmount=32.0]

Salesperson [id=6, salesAmount=32.0]

Salesperson [id=7, salesAmount=323.0]

Salesperson [id=8, salesAmount=32.0]