简体   繁体   中英

Java - Homework - Using the string value of a variable to name an instance of a class

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)

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());
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM