Java trouble with ArrayLists when there are multiple classes and help fixing up the code [closed]

岁酱吖の 提交于 2019-12-06 06:37:40

An enum is used as a sort of pseudo-type. You do need the enumerated class because 1. it's part of your assignment and 2. it makes it just clearer to anyone reading your code (including yourself) what certain values mean.

If you have some enum Day and some variable day1, it's easy to see what you're doing when you write if (day1.toString.equals("MONDAY")) but if you use magic numbers/pre-defined constants, it will look like if (day1 == 0) which is meaningless to anyone who didn't write the code (and possibly you in the future, when you're looking at this code again).

In addition to that, using an enum disallows for garbage values. If you weren't using enums, you could define day1 to be 10,000 and it would be valid Java. This is a nonsensical value if you only have 7 days of the week. Enums provide a layer of abstraction that avoid garbage values and gives you explicit control over how an Enum is handled.

Implementation of the Position enum

//in Position.java
public enum Position {
    DESIGN("Design"),
    MANUFACTURING("Manufacturing"),
    SALES("Sales");

    private final String positionName;

    //note enums have private constructors
    private Position(String positionName) {
        this.positionName= positionName;
    }

    //you're overriding the default toString() method
    //defined for all objects in Java, so add @Override
    @Override
    public String toString() {
        return positionName;
    }
}

Thus in your Employee class you declare an Employee with:

Employee employee = new Employee("John", "Deer", DESIGN);

Since you're going to want to keep track of how many of each position you have, you need to create an attribute in Company that stores the number of employees of a given type.

So in Company in your attribute declaration you will create 3 integers:

private int numOfDesigners;
private int numOfManufacturers;
private int numOfSalesmen;

Whenever you add an employee, you want to make sure there aren't too many already, so you'll encapsulate the creation of the Employee class inside an public boolean addEmployee(String fName, String lName, Position p) method that returns false if there are too many of the Position p already.

public boolean addEmployee(String fName, String lName, Position p) {
    //since this method will be in the Company class, we can directly
    //refer to the number of a specific position type
    //I'm going to assume you know how a switch statement works.
    switch (p) {
        case SALES:
            //if there's less than 1 salesman, add them to the list
            if (numOfSalesmen < 1) {
                Employee employee = new Employee(fName, lName, p);
                //assuming you name your ArrayList<Employee> 'staffList'
                staffList.add(employee);
            }
            else {
                //give some error message or some functionality with GUI
            }
            break;

        case DESIGN:
            if (numOfDesigners < 2) {
                ...
            }
            break;

        ...

        default:
            //some error handling
            break;
    }
}

Secondly, you need to declare some ArrayList<Employee> in your Company class as an attribute. Write a function in the Company class that somehow checks whether or not there are too many employees of a particular type and returns a value accordingly.

In Object-Oriented Programming you provide functions for handling attributes of a class so that other classes don't have to worry about how a function is implemented (this is called encapsulation).

That is, if you want to print all the Employee names from your GUI class, you define an object Company, add Employees to it's ArrayList<Employee> attribute and print out the returned value of a function public String listOfEmployees() that returns "Bob M., Stacy J., John A" or however you want to implement it.

That way when you need the list of employees, you don't have to iterate over the list of Employees in the Company class and concatenate them in some arbitrary way to a single string. The function listOfEmployees() does that for you and assuming you have a Company called company1 you can simply write:

String currentList = company.listOfEmployees();

rather than cluttering your code up with

String currentList = "";
for (Employee e : employeeList) {
    currentList += e.firstName + e.lastName;
}

Also, one of the most valuable aspects of OOP is that it allows you to change functionality without having to remember where you used this code. What if for some reason your application should only print out the first names now? You have to find every instance of this for loop and remove the + e.lastName from the statement in the middle. If you have properly encapsulated the listOfEmployees() function, you simply make the change in this function and never have to worry about changing every usage.

This is how you can get your classes to interact with each other in a clear and predictable way.

AfterWorkGuinness

That's a lot of code. I'm not very clear on what you are asking about ArrayLists. In general this is how you put data in an ArrayList

List<String> aList = new ArrayList<String>();
aList.add("Hello World");

If you are not familiar with generics and don't recognize the <String> part, you can just leave it off. The ArrayList a list will only have scope in the method or class you create it in. So if you declare it inside a method you can only add stuff to it / get stuff out of it in that method.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!