importjava.text.NumberFormat;
importjava.util.ArrayList;
importjava.util.List;
importjava.util.Locale;
importjava.util.Scanner;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author
*/
public class Main {
/**
* Program execution starts here
*
* @paramargs
* the command line arguments
*/
public static void main(String[] args) {
// Command for accepting input
Scanner input = new Scanner(System.in);
// Array List to hold the Sales Persons
List salespersonList = new ArrayList();
// Assume base salary of 75000 and commission rate of 5%
doublefixedSalary = 75000;
doublecommissionRate = 0.05;
doublesalesAmount = 0;
String name;
// Get data for 3 sales persons
for (inti = 0; i < 3; i++) {
// Command to prompt the user and accept salesperson name
System.out.print("Enter salesperon's name: ");
name = input.nextLine();
// Command to prompt the user and accept sales amount
System.out.print("Enter sales: ");
salesAmount = Double.parseDouble(input.nextLine());
// Command to initialize sales person object with default values
SalesPersonsalesPerson = new SalesPerson(fixedSalary,
commissionRate, name, salesAmount);
// Add sales person to the array list
salespersonList.add(salesPerson);
}
displayTotalCompensation(salespersonList);
compareSalesPerson(salespersonList);
}
/**
* Method to print the total compensation of all the sales person
*
* @paramsalesPersonList
*
*/
private static void displayTotalCompensation(List salesPersonList) {
// Command to format currency
NumberFormatnumberFormat = NumberFormat.getCurrencyInstance(Locale.US);
System.out.println("");
System.out.println("Name\t\tTotal Sales\t\tTotal compensation");
for (inti = 0; isalesPersonList.size(); i++) {
SalesPersonsalesPerson = (SalesPerson) salesPersonList.get(i);
System.out.println(salesPerson.getName()
+ "\t\t"
+ numberFormat.format(salesPerson.getSalesAmount())
+ "\t\t"
+ numberFormat.format(salesPerson
.calculateTotalCompensation()));
}
}
/**
* Method to compare two sales person's annual compensation SalesPerson
* object
*
* @paramsalesPersonList
* @return salesPersonToCompareSalesPerson
*/
private static void compareSalesPerson(List salesPersonList) {
// Command to format currency
NumberFormatnumberFormat = NumberFormat.getCurrencyInstance(Locale.US);
// Get 2 sales persons to compare
SalesPerson salesPerson1 = getSalesPersonToCompare(salesPersonList);
SalesPerson salesPerson2 = getSalesPersonToCompare(salesPersonList);
// Get total pay of both the sales persons
double totalPay1 = salesPerson1.calculateTotalCompensation();
double totalPay2 = salesPerson2.calculateTotalCompensation();
if (totalPay1 > totalPay2) {
System.out.println(salesPerson1.getName()
+ " has a higher compensation than "
+ salesPerson2.getName());
doubleadditionalAmount = getAdditionalAmountOfSales(salesPerson2,
salesPerson1);
System.out.println(numberFormat.format(additionalAmount)
+ " of additional sales amount is needed by "
+ salesPerson2.getName()
+ " to match or exceed the total compensation of "
+ salesPerson1.getName());
} else if (totalPay1 < totalPay2) {
System.out.println(salesPerson2.getName()
+ " has a higher compensation than "
+ salesPerson1.getName());
doubleadditionalAmount = getAdditionalAmountOfSales(salesPerson1,
salesPerson2);
System.out.println(numberFormat.format(additionalAmount)
+ " of additional sales amount is needed by "
+ salesPerson1.getName()
+ " to match or exceed the total compensation of "
+ salesPerson2.getName());
} else {
System.out.println("Both " + salesPerson1.getName() + " and "
+ salesPerson2.getName() + " have same compensation");
}
}
private static double getAdditionalAmountOfSales(
SalesPersonsalesPersonLess, SalesPersonsalesPersonMore) {
doubletotalPayLess = salesPersonLess.calculateTotalCompensation();
doubletotalPayMore = salesPersonMore.calculateTotalCompensation();
// Get original Sales amount
doubleoriginalSalesAmount = salesPersonLess.getSalesAmount();
doubleamountNeeded = 0;
doublenewSalesAmount = originalSalesAmount;
while (totalPayLesstotalPayMore) {
// Increment the sales amount by $100 and see if the total
// compensation matches or exceed
newSalesAmount = newSalesAmount + 100;
// Set new sales amount
salesPersonLess.setSalesAmount(newSalesAmount);
// Get new Total compensation
totalPayLess = salesPersonLess.calculateTotalCompensation();
}
amountNeeded = newSalesAmount - originalSalesAmount;
// Set the origianl sales amount back to bring object back to same state
salesPersonLess.setSalesAmount(originalSalesAmount);
returnamountNeeded;
}
/**
* Method to get the sales person's name that needs to be compared and
* returns the SalesPerson object
*
* @paramsalesPersonList
* @return salesPersonToCompareSalesPerson
*/
private static SalesPersongetSalesPersonToCompare(List salesPersonList) {
// Command for accepting input
Scanner input = new Scanner(System.in);
SalesPersonsalesPersonToCompare = null;
// Get first sales person's name
// Continue loop until valid sales person name is not entered
String name;
do {
System.out.print("Enter salesperon's name to compare: ");
name = input.nextLine();
// Get sales person from list
for (inti = 0; isalesPersonList.size(); i++) {
SalesPersonsalesPerson = (SalesPerson) salesPersonList.get(i);
// IF matching sales person found
if (salesPerson.getName().equalsIgnoreCase(name)) {
salesPersonToCompare = salesPerson;
break;
}
}
if (salesPersonToCompare == null) {
System.out.print("No matching sales person found. Try again!");
}
} while (salesPersonToCompare == null);
returnsalesPersonToCompare;
}
}