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...
Question 1 - Create a class named Student that has fields for an ID number, number...
Question 1 - Create a class named Student that 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. A Student also has a field for grade point average. Include a method to compute the grade point...
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...
create a public class and a main class to deletebyStudentID number with get ID and getStudentByName...
create a public class and a main class to deletebyStudentID number with get ID and getStudentByName in public class. when the user is prompted to search for the student and delete them by their. ID has seven digits and 6 students each. Ex. Delete student by their ID         Enter students name and their ID         Vanessa,         1111-111         Vanessa is now deleted
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 class need to define two fields: 1. Integer part of the number - ulong; &...
In class need to define two fields: 1. Integer part of the number - ulong; & fractional part of the number - ushort in C# Using C#, create the following methods: 1. Method for adding two prices (parameters for function is 2 numbers, integer(main currency) and fractional parts(Fractional currency)) 2. Method for adding two prices(parameter is object of same type as your class) 3. Method for adding two prices(parameter is string)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT