In: Computer Science
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()
}
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.