45.6k views
5 votes
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

This is the code for Salesperson class:
public class Salesperson
{
private int ID;
private double annualSales;
public Salesperson(int ID, double annualSales)
{
this.ID = ID;
this.annualSales = annualSales;
}
public int getID()
{
return ID;
}
public void setID(int ID)
{
this.ID = ID;
}
public double getAnnualSales()
{
return annualSales;
}
public void setAnnualSales(double annualSales)
{
this.annualSales = annualSales;
}

public String toString()
{
return "Salesperson ID: " + ID + ", annual sales: " + annualSales;
}
}

User Slizzered
by
5.4k points

1 Answer

5 votes

Answer:

Following are the class "SalespersonSort" code to this question:

public class SalespersonSort //defining a class SalespersonSort

{

public static void main(String[] ax) //defining a main method

{

int id;//defining integer variable

double sales; //defining double variable

String select;//defining String variable

Salesperson[] x = new Salesperson[7]; //crating array as the Salesperson class

Scanner x1= new Scanner(System.in);//creating scanner class object

for(int i=0; i<x.length; i++)//defining loop for input values

{

System.out.print("Enter ID number "+(i+1)+": ");//print message

id = x1.nextInt();//input value

System.out.print("Enter Annual Sales "+(i+1)+": ");//print message

sales = x1.nextDouble(); //input value

x[i]=new Salesperson(id,sales);//hold value in array x

}

System.out.println("Do you want to sort by ID or sales? (Enter ID or sales):");//print message

select= x1.next();//input value

if(select.equalsIgnoreCase("ID"))//define if block that sort value by id

{

for(int i=0; i<x.length; i++)//define for loop to collect value

{

int m= i;//use integerv variable to hold i value

for(int j=i; j<x.length; j++)//define for loop to sort value

{

if(x[j].getID() < x[m].getID())//use if block for check value

m = j;//use integerv variable to hold j value

}

//perform swapping

Salesperson t = x[i];//define calss type variable t to hold aaray value

x[i] = x[m];//inter change value

x[m] = t;//store value in x

}

System.out.println("Sales sorted by ID are ");//print message

for(int i=0; i<x.length; i++)//define for loop to print sort value

System.out.println(x[i]);//print value

}

else//defining else block

{

for(int i=0; i<x.length; i++)//define for loop to collect value

{

int m= i;//use integerv variable to hold i value

for(int j=i; j<x.length; j++)//define for loop to sort value

{

if(x[j].getAnnualSales() < x[m].getAnnualSales())//sort value by sales

m = j;//hold j value in m

}

//perform swapping

Salesperson t = x[i];//define calss type variable t to hold aaray value

x[i] = x[m];//inter change value

x[m] = t;//store value in x

}

System.out.println(" sort value by Annual Sales: "); //print message

for(int i=0; i<x.length; i++)//defining for loop to print value

System.out.println(x[i]);//print value

}

}

}

output:

please find the attached file.

Step-by-step explanation:

In the above-given code, a class "SalespersonSort" is defined, inside the class main method is defined, in the method three variables "sales, select, and id" is defined, that is datatype different.

In the class, Salesperson as an array is defined and a scanner class object created that inputs array values and in the string value and a conditional statement is used.

In the if block, it sort values according to id value, and in the else block it sort value according to sales value.

please find the attachment of full code.

Create an application that allows a user to enter values for an array of seven Salesperson-example-1
Create an application that allows a user to enter values for an array of seven Salesperson-example-2
Create an application that allows a user to enter values for an array of seven Salesperson-example-3
Create an application that allows a user to enter values for an array of seven Salesperson-example-4
Create an application that allows a user to enter values for an array of seven Salesperson-example-5
User Yiu
by
5.6k points