Question

In: Computer Science

In Chapter 8, you created a Salesperson class with fields for an ID number and sales...

In Chapter 8, you created a Salesperson class with fields for an ID number and sales values. Now, 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 (I)D number or (S)ales value.

-----------------------

Salesperson.java

public class Salesperson

{

   private int id;

   private double sales;

   Salesperson(int idNum, double amt)

   {

      id = idNum;

      sales = amt;

   }

   public int getId()

   {

      return id;

   }

   public double getSales()

   {

      return sales;

   }

   public void setId(int idNum)

   {

      id = idNum;

   }

   public void setSales(double amt)

   {

      sales = amt;

   }

}

---------------------------------------

SalespersonSort.Java

import java.util.*;

public class SalespersonSort

{

   public static void main(String[] args)

   {

      // your code here   

   }

    // sortByID()

    

    // sortBySales()

    

}

Solutions

Expert Solution

Code

Salesperson.java

public class Salesperson

{

   private int id;

   private double sales;

   Salesperson(int idNum, double amt)

   {

      id = idNum;

      sales = amt;

   }

   public int getId()

   {

      return id;

   }

   public double getSales()

   {

      return sales;

   }

   public void setId(int idNum)

   {

      id = idNum;

   }

   public void setSales(double amt)

   {

      sales = amt;

   }

}

---------------------------------------

SalespersonSort.java

import java.util.*;
public class SalespersonSort {

   public static void main(String[] args) {
       Salesperson salesPrsn[]=new Salesperson[7];
       Scanner scnr=new Scanner(System.in);
       int id;
       double sales;
       char choice;
       for(int i=0;i<salesPrsn.length;i++)
       {
           System.out.print("\nEnter Id for salesperson #"+(i+1)+": ");
           id=scnr.nextInt();
           System.out.print("Enter Sales for salesperson #"+(i+1)+": ");
           sales=scnr.nextDouble();
           salesPrsn[i]=new Salesperson(id, sales);
       }
       System.out.println("Display Salespersons ordered by (I)D number or (S)ales value");
       System.out.print("Your choice: ");
       choice=scnr.next().charAt(0);
       if(choice=='I' || choice=='i')
       {
           sortByID(salesPrsn);
       }
       else if(choice=='s' || choice=='S')
       {
           sortBySales(salesPrsn);
       }
       System.out.println("ID\tSales");
       for(int i=0;i<salesPrsn.length;i++)
       {
           System.out.println(salesPrsn[i].getId()+"\t"+salesPrsn[i].getSales());
       }
   }

   private static void sortBySales(Salesperson[] salesPrsn) {
       Salesperson temp;
       for(int i=0;i<salesPrsn.length-1;i++)
       {
           for(int j=i;j<salesPrsn.length;j++)
               if(salesPrsn[i].getSales()>salesPrsn[j].getSales())
               {
                   temp=salesPrsn[i];
                   salesPrsn[i]=salesPrsn[j];
                   salesPrsn[j]=temp;
               }
       }
   }

   private static void sortByID(Salesperson[] salesPrsn) {
       Salesperson temp;
       for(int i=0;i<salesPrsn.length-1;i++)
       {
           for(int j=i;j<salesPrsn.length;j++)
               if(salesPrsn[i].getId()>salesPrsn[j].getId())
               {
                   temp=salesPrsn[i];
                   salesPrsn[i]=salesPrsn[j];
                   salesPrsn[j]=temp;
               }
       }
      
   }

}

output

If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.


Related Solutions

Create a class named Salesperson. Data fields for Salesperson include an integer ID number and a...
Create a class named Salesperson. Data fields for Salesperson include an integer ID number and a doubleannual 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. Write an application named DemoSalesperson that declares an array of 10 Salesperson objects. Set each ID number to 9999 and each sales value to zero. Display the 10 Salesperson objects. public class DemoSalesperson { public static void...
Create a class named Student. Student has fields for an ID number, number of credit hours...
Create a class named Student. Student has fields for an ID number, number of credit hours earned, and number of points earned. (For example, many schools compute grade point averages based on a scale of 4, so a three-credit-hour class in which a student earns an A is worth 12 points.) Include methods to assign values to all fields. Student also has a field for grade point average. Include a method to compute the grade point average field by dividing...
Create an Automobile class for a dealership. Include fields for an ID number, make, model, color,...
Create an Automobile class for a dealership. Include fields for an ID number, make, model, color, year, vin number, miles per gallon, and speed. Include get and set methods for each field. Do not allow the ID to be negative or more than 9999; if it is, set the ID to 0. Do not allow the year to be earlier than 2000 or later than 2017; if it is, set the year to 0. Do not allow the miles per...
Implement an Employee class that includes data fields to hold the Employee’s ID number, first name,...
Implement an Employee class that includes data fields to hold the Employee’s ID number, first name, last name, and hourly pay rate (use of string datatype is not allowed). Create an array of five Employee objects. Input the data in the employee array. Write a searchById() function, that ask the user to enter an employee id, if its present, your program should display the employee record, if it isn’t there, simply print the message ”Employee with this ID doesn’t exist”....
make a Pay class that had fields for a single employee's name, ID number, hourly pay,...
make a Pay class that had fields for a single employee's name, ID number, hourly pay, number of hours worked. use appropriate getter and setter methods, and a constructor accepting the employees name, ID number, hourly pay, and number of hours worked as arguments. The Pay class should also have a method that returns the employee's gross pay (gross pay = hourly pay* number of hours worked). In addition include a toString method that prints to screen the employee name,...
**MATLAB 8)The structure for students' quiz data for a class is organized as below ID number...
**MATLAB 8)The structure for students' quiz data for a class is organized as below ID number Quiz 44 7 33 5.5 37 8 write a script to print the students' data by ascending order of ID number. The index vector method must be used and the MATLAB functions for sorting CANNOT be used. 9)In a physics measurement, the density of the water is measured at different depth. Here are the depth vector and density vector. depth=[100,200,300,400,500] density=[6.1,6.9,8.0,8.8,10.2] USE polyfit to...
You have a table called scholar where there are two fields id and create date. You...
You have a table called scholar where there are two fields id and create date. You want to create an ETL process that loads the data from scholar table into the big data platform. What are the different things that you will put on a requirement document for the developer to write code and implement the solution?
In the previous lab you created a Car class and a Dealership class. Now, in this...
In the previous lab you created a Car class and a Dealership class. Now, in this activity change the design of the Dealership class, in which the list of the cars inside a dealership will be stored in an ArrayList. Then, provide required getter and setter methods to keep the records of all its cars. In your tester class, test your class and also printout the list of all cars of a given dealership object. // Java program import java.util.ArrayList;...
In java, create a class named Contacts that has fields for a person’s name, phone number...
In java, create a class named Contacts that has fields for a person’s name, phone number and email address. The class should have a no-arg constructor and a constructor that takes in all fields, appropriate setter and getter methods. Then write a program that creates at least five Contact objects and stores them in an ArrayList. In the program create a method, that will display each object in the ArrayList. Call the method to demonstrate that it works. Include javadoc...
Write a class named ContactEntry that has fields for a person’s name, phone number and email...
Write a class named ContactEntry that has fields for a person’s name, phone number and email address. The class should have a no-arg constructor and a constructor that takes in all fields, appropriate setter and getter methods. Then write a program that creates at least five ContactEntry objects and stores them in an ArrayList. In the program create a method, that will display each object in the ArrayList. Call the method to demonstrate that it works. I repeat, NO-ARG constructors....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT