I'm trying to add entries to an ArrayList in Java and I had it working, but now it isn't. Here is the code
import java.util.*;
public class SalesPerson
{
// define an array of allowable sales person to be three
static String[] salesPerson = new String[3];
/**
* Calculates the total annual compensation for a salesperson by
* multiplying annual sales by commission rate and then adding the salary
*/
public static void compensation()
{
try {
String[] salesperson = new String[3];
// use scanner class to read input from the user
Scanner input = new Scanner(System.in);
// List to add entries to an array list
ArrayList<String> list = new ArrayList<>();
// do..while loop to add salespersons
do {
System.out.println("Want to compare additional " //GLD-changed verbiage here
+ "salespeople? (y/n)"); //GLD-changed verbiage here
if (input.next().startsWith("y")) {
System.out.println("Enter the salesperson's name:");
addSalesPerson(list).add(input.next());
System.out.println("Enter annual amount for salesperson");
addSalesPerson(list).add(input.next());
} else {
break;
}
}
while (true);
// System.out.println(addSalesPerson(list).get(1));
// check to see if only two sales persons have been entered
if (addSalesPerson(list).size() < 2 || addSalesPerson(list).size() > 4) {
throw new Exception("You may only enter two sales persons.");
}
Object[] arr;
arr = addSalesPerson(list).toArray(new String[addSalesPerson(list).size()]);
// loop through the length of the array
for (Object arr1 : arr) {
salesperson = (String[]) arr;
}
// for this, we will compare two sales people only
float sale2 = Float.parseFloat(salesperson[1]);
float sale4 = Float.parseFloat(salesperson[3]);
// subtract based on which variable is greater to ensure no negative values
float difference = sale2 > sale4 ? sale2 - sale4 : sale4 - sale2;
// print out the difference
System.out.printf("To match the other sales person's amount "
+ "you will need to get $%.2f more\n\n", difference);
} catch (Exception e) {
// handle any exception by displaying an error message
System.out.println("Something went wrong while processing your "
+ "input. Please be sure you only entered numeric values"
+ " and or the correct amount of salespersons.");
}
}
/**
* Adds sales people to an array list
* @param salesperson
* @return ArrayList
*/
static public ArrayList addSalesPerson(ArrayList<String> salesperson)
{
for (int i=0; i<salesPerson.length; i++) {
if (salesperson.get(i).equals("")) {
break;
}
salesperson.add(salesperson.get(i));
}
return salesperson;
}
}
And then I am calling these static methods in two other files
public class Getters
{
public static void handleSalesPersonCompensation()
{
SalesPerson.compensation();
}
}
public class ComparisonWk5
{
public static void main(String args[])
{
Getters.handleSalesPersonCompensation();
}
}
The output of these should be Want to compare additional salespeople? (y/n) y Enter the saleperson's name: My Name Enter the annual amount for salesperson: 200000 Want to compare an additional salespeople? (y/n) y Enter the saleperson's name: My Name 2 Enter the annual amount for salesperson: 100000
To match the other person's amount, you will need to get $100000 more
But the output is: Want to compare additional salespeople? (y/n) y Enter the salesperson's name: Something went wrong while processing your input. Please be sure you only entered numeric values and or the correct amount of salespersons.
Everytime after I type "y", it shows the salesperson's name input but also displays the exception right after without having the ability to enter a name.
Any help would be appreciated
The stack trace is this:
[java.util.ArrayList.rangeCheck(ArrayList.java:653),
java.util.ArrayList.get(ArrayList.java:429),
SalesPerson.addSalesPerson(SalesPerson.java:96),
SalesPerson.compensation(SalesPerson.java:48),
Getters.handleSalesPersonCompensation(Getters.java:61),
ComparisonWk5.main(ComparisonWk5.java:31)
]
Thanks
Aucun commentaire:
Enregistrer un commentaire