//Method to automatically create the 3 trains given in the pdf for the demonstration private void createDemonstrationTrains() { //creates the 3 train and set all the given properties ExpressFactory factory1 = new ExpressFactory("1S45"); Train train1 = factory1.GetTrainType(); StoppingFactory factory2 = new StoppingFactory("1E05"); Train train2 = factory2.GetTrainType(); SleeperFactory factory3 = new SleeperFactory("1E99"); Train train3 = factory3.GetTrainType(); train1.Departure = "London (Kings Cross)"; train1.Destination = "Edinburgh (Waverley)"; train1.Type = "Express"; train1.Hour = "10"; train1.Minute = "00"; train1.Day = "01/11/18"; train1.Berth = false; train1.FirstClass = true; saveTrains.add(train1); train2.Departure = "Edinburgh (Waverley)"; train2.Destination = "London (Kings Cross)"; train2.Type = "Stopping"; train2.IntermediateYK = "York"; train2.IntermediateDG = "Darlington"; train2.IntermediatePB = "Peterborough"; train2.Hour = "12"; train2.Minute = "00"; train2.Day = "01/11/18"; train2.Berth = false; train2.FirstClass = true; saveTrains.add(train2); train3.Departure = "Edinburgh (Waverley)"; train3.Destination = "London (Kings Cross)"; train3.Type = "Sleeper"; train3.Hour = "21"; train3.Minute = "30"; train3.Day = "01/11/18"; train3.Berth = true; train3.FirstClass = false; saveTrains.add(train3); //run the duplTrain method to check if the trains are already in the list saveTrains.duplTrain(file); }
//runs the creation of the default trains just to check if everything works public void TestMethod1() { ExpressFactory factory1 = new ExpressFactory("1S45"); Train train1 = factory1.GetTrainType(); StoppingFactory factory2 = new StoppingFactory("1E05"); Train train2 = factory2.GetTrainType(); SleeperFactory factory3 = new SleeperFactory("1E99"); Train train3 = factory3.GetTrainType(); train1.Departure = "London (Kings Cross)"; train1.Destination = "Edinburgh (Waverley)"; train1.Type = "Express"; train1.Hour = "10"; train1.Minute = "00"; train1.Day = "01/11/18"; train1.Berth = false; train1.FirstClass = true; MainWindow.saveTrains.add(train1); train2.Departure = "Edinburgh (Waverley)"; train2.Destination = "London (Kings Cross)"; train2.Type = "Stopping"; train2.IntermediateYK = "York"; train2.IntermediateDG = "Darlington"; train2.IntermediatePB = "Peterborough"; train2.Hour = "12"; train2.Minute = "00"; train2.Day = "01/11/18"; train2.Berth = false; train2.FirstClass = true; MainWindow._saveTrains.add(train2); train3.Departure = "Edinburgh (Waverley)"; train3.Destination = "London (Kings Cross)"; train3.Type = "Sleeper"; train3.Hour = "21"; train3.Minute = "30"; train3.Day = "01/11/18"; train3.Berth = true; train3.FirstClass = false; MainWindow._saveTrains.add(train3); MainWindow._saveTrains.duplTrain(file); }
//method that creates an object of the type train for the validated user entry private void addTrain() { //declaring a new TrainFactory object and set it as null TrainFactory factory = null; /*Switch case to determine which object of which abstract Train inherited * class should be created with the type choosen by the user*/ switch (typeChoice) { case "Express": factory = new ExpressFactory(typeChoice); break; case "Stopping": factory = new StoppingFactory(typeChoice); break; case "Sleeper": factory = new SleeperFactory(typeChoice); break; default: break; } //Try/Catch method to validate if the train can be created try { //if the train id is already in the file if (duplId(file) == true) { //set the validation bool to false isEntryValid = false; //Throw argument exception to prompt the user with the error that has been compiled throw new ArgumentException("The Train ID is already present, must be unique", "Train ID"); } //creating an empty train object based on the method in the factory class Train train = factory.GetTrainType(); //Assign the ID and departure train.ID = idInput; train.Departure = depInput; //Based on the departure that the user has chosen, run the getDeparture method depInput = train.getDeparture(); //Assign the destination train.Destination = destInput; //Based on the destination that the user has chosen, run the getDestination method destInput = train.getDestination(); //Assign the type train.Type = typeInput; //Based on the type that the user has chosen, run the getType method typeInput = train.getType(); //Assign the stops train.IntermediateNC = ctNC; train.IntermediateYK = ctYK; train.IntermediateDG = ctDT; train.IntermediatePB = ctPB; //Assign the hour train.Hour = hourInput; //Based on the hour that the user has chosen, run the getHour method hourInput = train.getHour(); //Assign the minute train.Minute = minuteInput; //Based on the minute that the user has chosen, run the getMinute method minuteInput = train.getMinute(); //Assign the minute train.Day = dateInput; //Assign the cabin train.Berth = isBerth; //Assign the firstclass train.FirstClass = isFirstClass; //add the train to the trainlist saveTrains.add(train); //add the train that has just been added to the listbox in the form _lstAllTrains.Items.Add(train.ID + ", " + train.Departure + ", " + train.Destination + ", " + train.Type + ", " + train.IntermediateNC + ", " + train.IntermediateYK + ", " + train.IntermediateDG + ", " + train.IntermediatePB + ", " + train.Hour + ":" + train.Minute + ", " + train.Day + ", has cabin " + train.Berth + ", has first " + train.FirstClass); } //Setting the message box containing the error message catch (Exception execMsg) { MessageBox.Show(execMsg.Message); } }