Question: hi, I am currently working on a java assignment known as SalesPersonSort. I am currently stuck and I need your help on correcting the errors
hi, I am currently working on a java assignment known as SalesPersonSort. I am currently stuck and I need your help on correcting the errors and improving the code. I am supposed to create an appllcation where the user has to enter an values for 7 SalesPerson objects and I offer the choice of displaying the objects in order by ID number or sales value. Here is my code so far:
import java.util.Scanner;
public class SalespersonSort
{
public static void main(String[] args)
{
final int size = 7;
SalesPerson[] salesPersons = new SalesPerson[size];
Scanner scanner = new Scanner(System.in);
salesPersons[0] = new SalesPerson(100, 5000);
salesPersons[1] = new SalesPerson(101, 7000);
salesPersons[2] = new SalesPerson(102, 6000);
salesPersons[3] = new SalesPerson(103, 10000);
salesPersons[4] = new SalesPerson(105, 2000);
salesPersons[5] = new SalesPerson(106, 900);
salesPersons[6] = new SalesPerson(110, 8000);
print(salesPersons);
System.out.println("--->Menu<---");
System.out.println("1.Sort by ID ");
System.out.println("2.Sort by Sales ");
System.out.print("Enter your choice(1 r 2). ");
int choice = scanner.nextInt();
switch(choice)
{
case 1:
sortByID(salesPersons);
print(salesPersons);
break;
case 2:
sortBySales(salesPersons);
print(salesPersons);
break;
default:
System.out.println("Invalid choice.");
}
}
private static void sortByID(SalesPerson[] salesPersons)
{
SalesPerson tempPerson;
for(int i = 0; i < salesPersons.length -1; i++)
{
for(int j = 0; j < salesPersons.length-1; j++)
{
if(salePersons[j].getID()>salesPersons[j + 1].getID())
{
tempPerson = salePersons[j];
salesPersons[j] = salesPersons[j + 1];
salesPerson[j + 1] = tempPerson;
}
}
}
}
private static void sortBySales(SalesPerson[] salesPersons)
{
for(int i = 0; i < salePersons.length -1; i++)
{
for(int j = 0; j < salePersons.length - 1; j++)
{
if(salesPersons[j].getSales() > salesPersons[j + 1].getSales())
{
tempPerson = salesPersons[j];
salesPersons[j] = salesPersons[j + 1];
salesPersons[j + 1] = tempPerson;
}
}
}
}
private static void print(SalesPerson[] salesPersons)
{
System.out.printf("% - 15s% ", "ID", "Sales");
for(int index = 0; index < salePersons.length; index++)
{
System.out.printf("% _ 10d% 10.2f ",salesPersons[index],getID(),salesPersons[index].getSales());
}
}
}
SalespersonSort.java
Salesperson java code:
public class SalesPerson
{
private int ID;
private double annualSales;
public SalesPerson(int ID, double annualSales)
{
this.ID = ID;
this.annualSales = annualSales;
}
public void setID(int ID)
{
this.ID = ID;
}
public void setSales(double annualSales)
{
this.annualSales = annualSales;
}
public int getID()
{
return ID;
}
public double getSales()
{
return annualSales;
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
