Question: Implement using Java a Train system simulation. Simple Train Simulator system will be a simple train simulation, implemented with object-oriented approach using Java. It will

Implement using Java a Train system simulation. Simple Train Simulator system will be a simple train simulation, implemented with object-oriented approach using Java. It will simulate a train traveling to multiple stops (in a loop starting at stop 1 to stop N) with customers entering and exiting the train. The train will not stop if there are no customers waiting to enter or exit the train at that stop. Simulate(done), Customer()-incomple, Train()-incomplete. Output can be textbased.

User of the program will need to input how many stops the train will have and the absolute path of text file with all customer data. While the simulation is running, it will display data to the console with train stops and customers entering and leaving the train. At the end of the simulation, the program will display how many stops it made to service all customers.

INSTRUCTIONS FOR INPUT/OUTPUT

1. Simple Train Simulator will have text based user interface with textual output and textual prompts where user needs to type in a response.

2. When program starts, user gets prompted for number of stops the train has on its route: Enter number of stops the train has on its route (must be greater than 1): .

3. On invalid input, user will get error message Invalid input, try again. " and will be prompted to enter the number of stops again.

4. On correct input user gets prompted for customer data file name: Enter absolute path for data file or for default (C:/train/customer-data.txt) press Enter: .

5. If file does not exist, user will get error message File not found, try again. " and will be prompted to input the file path again.

6. If file has non-integer as one of the first four values, user will get error message Each line must have four integers. Try again. and will be prompted to input the file path again.

7. If any of the data in the customer file is not valid, user will get error message Data in input file is not correct. Try again. Then user will be prompted to enter another file path.

8. After user inputs all necessary correct information simulation begins.

9. During simulation information about each step of simulation (current time, current stop, customers entering train and customers leaving train) is printed out for user.

10. When simulation is done information about number of stops that were made and how long it took is printed out. The display will be Train made x stops and it took y time units to process all customers with x and y the actual values from the simulation

11. Program terminates.

Class 1(COMPLETED ALREADY)-code below

Simulator prompts user for number of stops and the input file with customer data. Parses the file and created a list of customers using Customer class. Creates an instance of Train and runs its methods for the simulation.

Class 2 Customer()-incomplete

Customer stores the four values parsed from file (id, arrival, enter, exit) for the customer. Sets up constants and status variable to determine status where CUST_NOT_PROCESSED means the customer has not been processed yet; CUST_ENTERED means_ the customer is on the train; and CUST_EXITED means the customer was processed and left the train.

CUST_NOT_PROCESSED: static final int Assigned value of 0 and means customer has not been processed yet

CUST_ENTERED: static final int Assigned value of 1 and means customer entered the train

CUST_EXITED: static final int Assigned value of 2 and means customer exited the train and is done processing

Attributes

Attribute Description

id : int Used to store customers ID

arrival : int Used to store customers arrival time at the train

enter : int Used to store the stop that customer will enter train

exit : int Used to store the stop that customer will exit train

status : int Used to track customers status using the constants

Methods

Method Description

Customer(int Custid, int arrivalTime, int enterStop, int exitStop) Constructor method to set attributes passed from Simulator

It also sets status attribute to CUST_NOT_PROCESSED

void setStatus(int status) Used to set customers status to the value passed by caller.

int getStatus() Returns customers current status

int getId() Returns customers ID

int getArrival() Returns customers arrival time

int getEnter() Returns entered stop

int getExit() Returns exit stop

Processing

All attributes are initialized by constructor method.

When getId() method is called it returns value of id attribute.

When getArrival () method is called it returns value of arrival attribute

When getEnter () method is called it returns value of enter attribute

When getExit () method is called it returns value of exit attribute

When getStatus () method is called it returns value of status attribute

When setStatus(status) method is called it sets passed parameter value as a value of status attribute.

Class 3 Train()-incomplete

Train - stores the number of stops train will have and the customer list. It also has an attribute for storing how many stops the train made as it processed the customers. The Simulator will create an instance of Train and set these values using the constructor. The simulate() method runs the strategy printing the appropriate output. The displayStops method prints the message with the number of stops the train made to process all customers and how long it took.

This class is responsible for:

Picking up customers

Dropping off customer

Moving train (one stop at a time)

Calculating number of stops and the time it took for train to process all customers made and to print out results.

Class Attributes

Attribute Description

stops : int Used to store the number of stops train has on its route.

madeStops: int Used to store number of stops the train made to process all customers.

currTime: int Used to store the time for the train made to process all customers.

custList : arrayList Used to store list of customers to process in the simulation.

Methods

Method Description

Train(int, ArrayList) Constructor to pass values of stops and customer list from Simulator and to initialize Trains attributes

simulate() Runs the train and processes customers entering and exiting the train. Train starts at stop 1 and travels to stop N. The route is a loop so then the train goes to stop 1 again and it keeps running until there are no more customers to process. o

Train starts running at current time = 1 and current stop =1.

o Make loopAgain = true (Boolean to control the loop)

o Loop until no more customers to process

Loop for each customer in the list

Check if there is any customers to process (status does not equal Customer CUST_EXITED). If yes then make loopAgain set to true so that when finishes processing this iteration, goes through loop again

Print message with current time and current stop only if there is customer to enter or exit the train

If customer not processed yet and his arrival time is less than or equal to current time and his enter stop is same as current stop, have customer enter the train (customers status gets set to Customer.CUST_ENTERED). Print message about customer entering train.

If customer is in train already (status== Customer.CUST_ENTERED) and exit stop is equal to current stop, have customer exit the train (status gets set to Customer.CUST_EXITED). Print message about customer exiting train.

If a customer entered or exited train, then increment by 1 the madeStop and update currTime

Increment local variable keeping track of time and local variable to keep track of current stop

If current stop == last stop (N), assign 1 to current stop for the train to loop

displayStops() Displays the message of how many stops the train made and how long it took to process all customers. o Print message Train made X stops and it took Y time units to process all customers using value of madeStops for X and value of currTime for Y.

Partial Code (simulate part already done.)

public class trainSimulator {

public void simulate()

{

int currStop = 1; // current stop starts at 1

int time = 1; // current time

boolean loopAgain = true;

while (loopAgain)

{

loopAgain = false; // assume all customers are done

boolean printed = false; // controls printing headings

for (int i=0; i < custList.size(); i++) // process every customer

{

Customer cust = custList.get(i); // get current customer

if (cust.getStatus() != Customer.CUST_EXITED) // at least one more customer to process

{

loopAgain = true;

}

// check if current customer already arrived at train and wants to enter at current stop

if (cust.getStatus() == Customer.CUST_NOT_PROCESSED

&& cust.getArrival() <= time && cust.getEnter() == currStop)

{

if (!printed)

System.out.println("Current Time=" + time + " Current Stop=" + currStop);

System.out.println(" Customer enters train: id="+cust.getId());

cust.setStatus(Customer.CUST_ENTERED);

custList.set(i, cust); // update customer list with changed status

printed = true;

}

// check if the current customer is in train and wants to exit at current stop

if (cust.getStatus() == Customer.CUST_ENTERED && cust.getExit() == currStop)

{

if (!printed)

System.out.println("Current Time=" + time + " Current Stop=" + currStop);

System.out.println(" Customer exits train: id="+cust.getId());

cust.setStatus(Customer.CUST_EXITED);

custList.set(i, cust); // update customer list with changed status

printed = true;

}

} // loop on customers

if (currStop == stops)

currStop = 1; // if reached last stop, reset to first stop

else

currStop++; // go to next stop

if (printed) // if there was something printed, we made a stop

{

madeStops++; // update the number of stops made

currTime = time; // update time to process all customers

}

time++; // increment current time

} // while more to process

}

}

// This class is responsible for storing and returning (when it is requested) customer information such as

//customer id, arrival time, enter stop, exit stop, and customers status.

public class Customer {

int id;

int arrival;

int enter;

int exit;

static final int CUST_NOT_PROCESSED = 0; // Assigned value of 0 and means customer has not been processed yet

static final int CUST_ENTERED = 1; // Assigned value of 1 and means customer entered the train

static final int CUST_EXITED = 2; // Assigned value of 2 and means customer exited the train

private int Customer; // Constructor method to set attributes passed from Simulator It also sets status attribute to CUST_NOT_PROCESSED

public int getId()

{ // method is called it returns value of id attribute.

}

public int getArrival()

{ // called it returns value of id attribute.

}

public int getEnter()

{ // called it returns value of enter attribute

}

public int getExit()

{// is called it returns value of exit attribute

}

public int getStatus()

{ // method is called it returns value of status attribute

}

public void setStatus(int status)

{ // sets passed parameter value as a value of status attribute.

}

}

}

public class Train {

//custList;

int stops; // Used to store the number of stops train has on its route.

int madeStops; // Used to store number of stops the train made to process all customers.

int currTime; // Used to store list of customers to process in the simulation.

public Train() // Constructor to pass values of stops and customer list from Simulator and to initialize Trains attributes

{

}

public void simulate(){ // Runs the train and processes customers entering and exiting the train.

// Train starts at stop 1 and travels to stop N. The route is a loop so then the train goes to stop 1

// again and it keeps running until there are no more customers to process.

}

public void displayStops(){ // Displays the message of how many stops the train made and how long it took to process all customers

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!