0

Okay, so I'm working on a homework assignment where I have a staff of 10 salespeople. We have a contest for the greatest number of sales. The assignment wants the user to enter 10 integer values as a number of sales and then once they've all been entered the salesperson with the highest value will be returned.

What she wants:

  • A Class "Sales" with (String name and Integer sales) values.
  • A while loop where the user inputs integers for number of sales

What I'm attempting to do. I assume the names of the salespeople at the company are known, so I just created an array strSalesPerson of 10 fictitious names. I created a counter salespersonCounter to create the counter for the while loop for user input. I'm trying to basically create salesperson1, salesperson2, salesperson3, and so on by creating a variable with the string "salesperson" concatenated with the counter. I want to use that as the name of the instance for each salesperson entered into the Sales Class.

Sales Class

Just for reference, the class I've created and trying to create instances of is as follows:

    
private String salesname;
private Integer numsales;

public Sales(String name, Integer sales) {
this.salesname = name;

    if (sales >= 0.0) {
        this.numsales = sales;
    }
} 

// Setter for name
public void setName(String name) {
    this.salesname = name;
}

// Getter for name
public String getName() {
    return salesname;
}

// Setter for Sales
public void setSales(Integer sales) {
if (sales >=0) {
    this.numsales = sales;
} 
}
// Getter for Sales
public int getSales() {
    return numsales;
}


} // End of Class 

TestSales

This is where I will get the user input and save it as an instance of the Sales class. However, right now I'm just going to use the current instance from the array of names and the static integer 3 to ensure that I get the other pieces functioning correctly and then I'll switch over to user inputs from there.


// Import Scanner
import java.util.Scanner;

public class TestSales {
    public static void main(String[] args) {
    
    // create scanner to obtain input from command window
    Scanner input = new Scanner(System.in);
    
    // Initialize variables
    int salespersonCounter = 1;
    
    // Fill Salesperson names
    String[] strSalesPerson = new String[]{"Mark Hasselback","Gary Moore","Shelly Hemingway", "Susan Meagre","Nick Pantillo","Craig Grey","Alice Reese","Mickey Greene","Chaz Ramirez","Kelly Southerland"};
            
    while (salespersonCounter <=10) {
        String salespersoninstance = ("salesperson"+ salespersonCounter);
        String currsalesperson = strSalesPerson[salespersonCounter -1];
        System.out.printf("%s");
        Sales salespersoninstance = new Sales(strSalesPerson[salespersonCounter -1],);
        System.out.printf("%s is %s!%n",salespersoninstance,currsalesperson);
        salespersonCounter += 1;
    }
    
    }
}

The problem I am running into is here:

        Sales salespersoninstance = new Sales(strSalesPerson[salespersonCounter -1],3);

instead of accepting the string value of salespersoninstance (in this case salesperson1) as the name of the instance of the Sales Class, it is telling me that salespersoninstance is a duplicate local variable. It is interpreting it I guess as me trying to define a new variable with the same name as one I've already declared?

Basically what I want is with the while counter, create a string variable salesperson1, salesperson2, salesperson3 and so on with "salesperson" + salespersonCounter, and use that resulting string to name the instance of the Sales class. That way I can then say Sales salespersoninstance = new Sales(strSalesperson[salespersoncCounter -1], userinput)

7
  • "it is telling me that salespersoninstance is a duplicate local variable" what is the exact error shown? Is this an error message or feedback from the prof/TA on the assignment? Commented Apr 20, 2022 at 16:17
  • yes, you can't have two variables with the same name. change one Commented Apr 20, 2022 at 16:19
  • 1
    @Freiheit I'm using eclipse, and it just has the error marker with "Duplicate local variable salespersoninstance" So, what I'm trying to do is at each iteration of the loop save the string"salespersoN" + Counter i.e. "salesperson1" as the value of salespersoninstance. I want to then use that value salesperson1 salesperson2 salesperson3 to create instances of the Sales class. how do I create an instance of Sales class using a naming system that basically adds an increasing integer to the end of the string "salesperson"? Commented Apr 20, 2022 at 16:37
  • @OldProgrammer - get that. That's not what I'm asking. I want to create instances of the Sales class, naming the instance whatever String value is in salespersoninstance. So, the obvious choice is String salespersoninstance = "salesperson" + salespersonCounter remains, however Sales salespersoninstance = New... needs to be changed to something else, but HOW do I get Sales [Name of instance] = New Sales(String, Integer) to use the value of salespersoninstance in [Name of instance] Commented Apr 20, 2022 at 16:39
  • You need to use a collection (ArrayList or Array or similar). you can't "name" an instance like that. Commented Apr 20, 2022 at 16:52

1 Answer 1

1

To help you a bit forward:

        String[] salePersonNames = new String[]{"Mark Hasselback", "Gary Moore", "Shelly Hemingway", "Susan Meagre", "Nick Pantillo", "Craig Grey", "Alice Reese", "Mickey Greene", "Chaz Ramirez", "Kelly Southerland"};
        for (int i = 0; i < salePersonNames.length; i++) {
            Sales salesPerson = new Sales(salePersonNames[i], 3);
            System.out.printf("%s is %s!%n", salesPerson.getName(), salesPerson.getSales());
        }
Sign up to request clarification or add additional context in comments.

1 Comment

okay, but if I want to pull a specific salesPerson instance, say whichever has the greatest number of sales, how would I identify that instance ? The professor did an example using Accounts account1 = new Accounts(name, balance) And she manually created several account1 = , account2 = , etc... How would I designate which one it is?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.