Question: In Java take the sample code Simulator below and break it down into the following parts: The code is done, all you are doing is

In Java take the sample code Simulator below and break it down into the following parts: The code is done, all you are doing is taking it and put into these 5 methods. If any portion is missing please include. Code should contain no errors.

1. getStopsFromUser(): int

Prompts user to enter number of stops.

Checks input (must be integer > 1) and gives error message if it is not. Loops until correct value is entered.

Returns the obtained value to caller

2. getInputFile(): File

Loop until the path given is for file that exists

Prompts user for the input file path

If no file is given (user presses enter) then use the default path name (C:/train/customer-data.txt)

Use given path to create File instance

Checks if file exists.

If file does not exist gives an error message and loop until correct path is entered.

Return File instance

3. getInputFile(int stops, File file):

Creates empty list (ArrayList list)

In while loop

Reads file line by line

Parses the four integer values per line

Checks data that it meets the above constraints. If data is incorrect, stops parsing, gives error message, and returns null

Creates instance of customer class (using data from single line from data file)

Stores instance of customer into list.

Exits loop when no more lines to read.

Returns list

4. run(int stops, ArrayList custList):

Create an instance of Train and pass through constructor stops and custList

Call Trains simulate method

Call Trains displayStops method

5. main() method:

Creates an instance of Simulation class

Creates custList of type ArrayList and assigns null

Calls Simulators getStopsFromUser() method and saves the return value in variable (stops)

Loops as long as custList is null

Calls Simulators getInputFile() method. Save the returned File instance

Call Simulators checkFile() method passing it stops and instance of File. Save returned value in custList

Call Simulators run method passing it the stops and custList

________________________________________________________________________________________

Sample code Simulator

public class Simulator {

static final String FILE_PATH = "customer-data.txt";

int getStopsFromUser=0;

public static void main(String args[])

{

Scanner keyboard = new Scanner(System.in);

Scanner fileReader = null;

String filePath = "";

int numStops = 0;

ArrayList customers = new ArrayList();

while(true)

{

System.out.print("Enter number of stops the train has on its route (must be greater than 1): ");

numStops = keyboard.nextInt();

if(numStops < 1)

System.out.println("Invalid input, try again.");

else

break;

}

keyboard.nextLine();

while(true)

{

System.out.print(" Enter absolute path for data file or for default (C:/train/customer-data.txt) press Enter:");

filePath = keyboard.nextLine();

try {

if(filePath.equals(""))

fileReader = new Scanner(new File(FILE_PATH));

else fileReader = new Scanner(new File(filePath));

} catch (FileNotFoundException e) {

System.out.println("File not found, try again. ");

continue;

}

break;

}

while(fileReader.hasNextLine())

{

//assuming that the file contains customer information in one line separated by space

String line = fileReader.nextLine();

String tokens[] = line.split("\\s+");

customers.add(new Customer(Integer.valueOf(tokens[0]), Integer.valueOf(tokens[1]), Integer.valueOf(tokens[2]), Integer.valueOf(tokens[3])));

}

fileReader.close();

Train train = new Train(numStops, customers);

train.simulate();

train.displayStops();

}

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!