How do I auto generate an ID using arrays? And how do i create a freight status for each freight order?

238 views Asked by At

So i have an assignment that requires me to program and create freight Id and update the status for every new order.

Freight ID: This should be a unique number auto generated using the following simple algorithm.
• Use the numbers '1939' as the first freight Id. Increment it by 1 for the next freight.

Freight status: can be one of the codes: ‘D’,’P’, or ‘W’. “D”: Delivered to the destination “P”: Processing “W”: Waiting at the ware house to be delivered.
When a new freight order is created, its initial status should be recorded as ‘W’.

I have tried some ways but i just cant seem to understand how I should auto generate and increment a freight ID as well as create a freight status.

public void freightID() 
{
    int [] freightID = {1939,1939,1939,1939,1939};
    for (int i = 0; i<ID_SIZE; i++)
    {
        int answer = ++freightID[0];
        System.out.println(freightID[i]*1);
   }

}

I know this is completely wrong but i just wanted to show what i tried.

1

There are 1 answers

1
ChristianM On

So as far as I understand your assignment you have to create a class "Order" which has the attribute "freight ID" and "status". The first order will start with the ID 1939 and every new Order needs to increment this number. Creating such Order the status is W. After the creation of the order you want to have the possibility to change the status. If thats correct i have the solution for you:

public class Order {

    public Order() {
        this.freightStatus = freightStatus.W;
        this.freightID = startID;
        startID++;
    }

    private static int startID = 1939;
    private int freightID;
    private FreightStatus freightStatus;

    enum FreightStatus {
        D, P, W
    }

    public void setFreightStatus(FreightStatus freightstatus) {
        this.freightStatus = freightstatus;
    }

    public FreightStatus getFreightStatus() {
        return this.freightStatus;
    }

    public int getFreightID() {
        return this.freightID;
    }

    public static void main(String args[]) {
        Order orders[] = new Order[5];
        for (int counter = 0; counter < orders.length; counter++) {
            orders[counter] = new Order();
        }

        for (Order order : orders) {
            System.out.println(order.getFreightID() + " " + order.freightStatus);
        }

        for (int counter = 0; counter < orders.length; counter++) {
            orders[counter].setFreightStatus(FreightStatus.D);
        }

        for (Order order : orders) {
            System.out.println(order.getFreightID() + " " + order.freightStatus);
        }
    }
}

I hope thats it what you asked for, if not, please clarify your question.