Skip to content
Snippets Groups Projects
Commit 4e6de96a authored by Lars-Johan Larsen's avatar Lars-Johan Larsen :speech_balloon:
Browse files

Del 1: Begin setting up classes and making class diagram.

parent de004ecb
Branches
No related tags found
No related merge requests found
<mxfile host="Electron" modified="2023-11-15T14:42:32.213Z" agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) draw.io/22.0.3 Chrome/114.0.5735.289 Electron/25.8.4 Safari/537.36" etag="TATjxAjmso4DIjY_p-R2" version="22.0.3" type="device">
<diagram id="C5RBs43oDa-KdzZeNtuy" name="Page-1">
<mxGraphModel dx="1036" dy="606" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="827" pageHeight="1169" math="0" shadow="0">
<root>
<mxCell id="WIyWlLk6GJQsqaUBKTNV-0" />
<mxCell id="WIyWlLk6GJQsqaUBKTNV-1" parent="WIyWlLk6GJQsqaUBKTNV-0" />
<mxCell id="zkfFHV4jXpPFQw0GAbJ--0" value="TrainDispatchSystem" style="swimlane;fontStyle=2;align=center;verticalAlign=top;childLayout=stackLayout;horizontal=1;startSize=26;horizontalStack=0;resizeParent=1;resizeLast=0;collapsible=1;marginBottom=0;rounded=0;shadow=0;strokeWidth=1;" parent="WIyWlLk6GJQsqaUBKTNV-1" vertex="1">
<mxGeometry x="334" y="120" width="160" height="138" as="geometry">
<mxRectangle x="230" y="140" width="160" height="26" as="alternateBounds" />
</mxGeometry>
</mxCell>
<mxCell id="zkfFHV4jXpPFQw0GAbJ--1" value="Station" style="text;align=left;verticalAlign=top;spacingLeft=4;spacingRight=4;overflow=hidden;rotatable=0;points=[[0,0.5],[1,0.5]];portConstraint=eastwest;" parent="zkfFHV4jXpPFQw0GAbJ--0" vertex="1">
<mxGeometry y="26" width="160" height="26" as="geometry" />
</mxCell>
<mxCell id="zkfFHV4jXpPFQw0GAbJ--2" value="Menu" style="text;align=left;verticalAlign=top;spacingLeft=4;spacingRight=4;overflow=hidden;rotatable=0;points=[[0,0.5],[1,0.5]];portConstraint=eastwest;rounded=0;shadow=0;html=0;" parent="zkfFHV4jXpPFQw0GAbJ--0" vertex="1">
<mxGeometry y="52" width="160" height="26" as="geometry" />
</mxCell>
<mxCell id="zkfFHV4jXpPFQw0GAbJ--4" value="" style="line;html=1;strokeWidth=1;align=left;verticalAlign=middle;spacingTop=-1;spacingLeft=3;spacingRight=3;rotatable=0;labelPosition=right;points=[];portConstraint=eastwest;" parent="zkfFHV4jXpPFQw0GAbJ--0" vertex="1">
<mxGeometry y="78" width="160" height="8" as="geometry" />
</mxCell>
<mxCell id="A6uXA1JAniuv3VXLVRst-30" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;exitX=0.5;exitY=0;exitDx=0;exitDy=0;entryX=0;entryY=0.5;entryDx=0;entryDy=0;" edge="1" parent="WIyWlLk6GJQsqaUBKTNV-1" source="zkfFHV4jXpPFQw0GAbJ--6" target="zkfFHV4jXpPFQw0GAbJ--2">
<mxGeometry relative="1" as="geometry" />
</mxCell>
<mxCell id="zkfFHV4jXpPFQw0GAbJ--6" value="Menu" style="swimlane;fontStyle=0;align=center;verticalAlign=top;childLayout=stackLayout;horizontal=1;startSize=26;horizontalStack=0;resizeParent=1;resizeLast=0;collapsible=1;marginBottom=0;rounded=0;shadow=0;strokeWidth=1;" parent="WIyWlLk6GJQsqaUBKTNV-1" vertex="1">
<mxGeometry x="150" y="258" width="160" height="138" as="geometry">
<mxRectangle x="130" y="380" width="160" height="26" as="alternateBounds" />
</mxGeometry>
</mxCell>
<mxCell id="zkfFHV4jXpPFQw0GAbJ--7" value="MenuItems" style="text;align=left;verticalAlign=top;spacingLeft=4;spacingRight=4;overflow=hidden;rotatable=0;points=[[0,0.5],[1,0.5]];portConstraint=eastwest;" parent="zkfFHV4jXpPFQw0GAbJ--6" vertex="1">
<mxGeometry y="26" width="160" height="26" as="geometry" />
</mxCell>
<mxCell id="zkfFHV4jXpPFQw0GAbJ--9" value="" style="line;html=1;strokeWidth=1;align=left;verticalAlign=middle;spacingTop=-1;spacingLeft=3;spacingRight=3;rotatable=0;labelPosition=right;points=[];portConstraint=eastwest;" parent="zkfFHV4jXpPFQw0GAbJ--6" vertex="1">
<mxGeometry y="52" width="160" height="8" as="geometry" />
</mxCell>
<mxCell id="zkfFHV4jXpPFQw0GAbJ--11" value="Menu()" style="text;align=left;verticalAlign=top;spacingLeft=4;spacingRight=4;overflow=hidden;rotatable=0;points=[[0,0.5],[1,0.5]];portConstraint=eastwest;" parent="zkfFHV4jXpPFQw0GAbJ--6" vertex="1">
<mxGeometry y="60" width="160" height="26" as="geometry" />
</mxCell>
<mxCell id="A6uXA1JAniuv3VXLVRst-31" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;exitX=0.5;exitY=0;exitDx=0;exitDy=0;entryX=0;entryY=0.5;entryDx=0;entryDy=0;" edge="1" parent="WIyWlLk6GJQsqaUBKTNV-1" source="A6uXA1JAniuv3VXLVRst-0" target="zkfFHV4jXpPFQw0GAbJ--7">
<mxGeometry relative="1" as="geometry">
<Array as="points">
<mxPoint x="230" y="420" />
<mxPoint x="130" y="420" />
<mxPoint x="130" y="297" />
</Array>
</mxGeometry>
</mxCell>
<mxCell id="A6uXA1JAniuv3VXLVRst-0" value="MenuItem" style="swimlane;fontStyle=0;align=center;verticalAlign=top;childLayout=stackLayout;horizontal=1;startSize=26;horizontalStack=0;resizeParent=1;resizeLast=0;collapsible=1;marginBottom=0;rounded=0;shadow=0;strokeWidth=1;" vertex="1" parent="WIyWlLk6GJQsqaUBKTNV-1">
<mxGeometry x="150" y="440" width="160" height="138" as="geometry">
<mxRectangle x="130" y="380" width="160" height="26" as="alternateBounds" />
</mxGeometry>
</mxCell>
<mxCell id="A6uXA1JAniuv3VXLVRst-27" value="- label" style="text;align=left;verticalAlign=top;spacingLeft=4;spacingRight=4;overflow=hidden;rotatable=0;points=[[0,0.5],[1,0.5]];portConstraint=eastwest;" vertex="1" parent="A6uXA1JAniuv3VXLVRst-0">
<mxGeometry y="26" width="160" height="26" as="geometry" />
</mxCell>
<mxCell id="A6uXA1JAniuv3VXLVRst-1" value="- action" style="text;align=left;verticalAlign=top;spacingLeft=4;spacingRight=4;overflow=hidden;rotatable=0;points=[[0,0.5],[1,0.5]];portConstraint=eastwest;" vertex="1" parent="A6uXA1JAniuv3VXLVRst-0">
<mxGeometry y="52" width="160" height="26" as="geometry" />
</mxCell>
<mxCell id="A6uXA1JAniuv3VXLVRst-3" value="" style="line;html=1;strokeWidth=1;align=left;verticalAlign=middle;spacingTop=-1;spacingLeft=3;spacingRight=3;rotatable=0;labelPosition=right;points=[];portConstraint=eastwest;" vertex="1" parent="A6uXA1JAniuv3VXLVRst-0">
<mxGeometry y="78" width="160" height="8" as="geometry" />
</mxCell>
<mxCell id="A6uXA1JAniuv3VXLVRst-5" value="MenuItem()" style="text;align=left;verticalAlign=top;spacingLeft=4;spacingRight=4;overflow=hidden;rotatable=0;points=[[0,0.5],[1,0.5]];portConstraint=eastwest;" vertex="1" parent="A6uXA1JAniuv3VXLVRst-0">
<mxGeometry y="86" width="160" height="26" as="geometry" />
</mxCell>
<mxCell id="A6uXA1JAniuv3VXLVRst-29" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;exitX=0.5;exitY=0;exitDx=0;exitDy=0;entryX=1;entryY=0.5;entryDx=0;entryDy=0;" edge="1" parent="WIyWlLk6GJQsqaUBKTNV-1" source="A6uXA1JAniuv3VXLVRst-15" target="zkfFHV4jXpPFQw0GAbJ--1">
<mxGeometry relative="1" as="geometry" />
</mxCell>
<mxCell id="A6uXA1JAniuv3VXLVRst-15" value="Station" style="swimlane;fontStyle=0;align=center;verticalAlign=top;childLayout=stackLayout;horizontal=1;startSize=26;horizontalStack=0;resizeParent=1;resizeLast=0;collapsible=1;marginBottom=0;rounded=0;shadow=0;strokeWidth=1;" vertex="1" parent="WIyWlLk6GJQsqaUBKTNV-1">
<mxGeometry x="520" y="258" width="160" height="138" as="geometry">
<mxRectangle x="130" y="380" width="160" height="26" as="alternateBounds" />
</mxGeometry>
</mxCell>
<mxCell id="A6uXA1JAniuv3VXLVRst-16" value="Departures" style="text;align=left;verticalAlign=top;spacingLeft=4;spacingRight=4;overflow=hidden;rotatable=0;points=[[0,0.5],[1,0.5]];portConstraint=eastwest;" vertex="1" parent="A6uXA1JAniuv3VXLVRst-15">
<mxGeometry y="26" width="160" height="26" as="geometry" />
</mxCell>
<mxCell id="A6uXA1JAniuv3VXLVRst-18" value="" style="line;html=1;strokeWidth=1;align=left;verticalAlign=middle;spacingTop=-1;spacingLeft=3;spacingRight=3;rotatable=0;labelPosition=right;points=[];portConstraint=eastwest;" vertex="1" parent="A6uXA1JAniuv3VXLVRst-15">
<mxGeometry y="52" width="160" height="8" as="geometry" />
</mxCell>
<mxCell id="A6uXA1JAniuv3VXLVRst-20" value="Station()" style="text;align=left;verticalAlign=top;spacingLeft=4;spacingRight=4;overflow=hidden;rotatable=0;points=[[0,0.5],[1,0.5]];portConstraint=eastwest;" vertex="1" parent="A6uXA1JAniuv3VXLVRst-15">
<mxGeometry y="60" width="160" height="26" as="geometry" />
</mxCell>
<mxCell id="A6uXA1JAniuv3VXLVRst-32" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;exitX=0.5;exitY=0;exitDx=0;exitDy=0;" edge="1" parent="WIyWlLk6GJQsqaUBKTNV-1" source="A6uXA1JAniuv3VXLVRst-21" target="A6uXA1JAniuv3VXLVRst-16">
<mxGeometry relative="1" as="geometry">
<Array as="points">
<mxPoint x="600" y="420" />
<mxPoint x="700" y="420" />
<mxPoint x="700" y="297" />
</Array>
</mxGeometry>
</mxCell>
<mxCell id="A6uXA1JAniuv3VXLVRst-21" value="Departure" style="swimlane;fontStyle=0;align=center;verticalAlign=top;childLayout=stackLayout;horizontal=1;startSize=26;horizontalStack=0;resizeParent=1;resizeLast=0;collapsible=1;marginBottom=0;rounded=0;shadow=0;strokeWidth=1;" vertex="1" parent="WIyWlLk6GJQsqaUBKTNV-1">
<mxGeometry x="520" y="440" width="160" height="216" as="geometry">
<mxRectangle x="130" y="380" width="160" height="26" as="alternateBounds" />
</mxGeometry>
</mxCell>
<mxCell id="A6uXA1JAniuv3VXLVRst-22" value="- delay : Duration" style="text;align=left;verticalAlign=top;spacingLeft=4;spacingRight=4;overflow=hidden;rotatable=0;points=[[0,0.5],[1,0.5]];portConstraint=eastwest;" vertex="1" parent="A6uXA1JAniuv3VXLVRst-21">
<mxGeometry y="26" width="160" height="26" as="geometry" />
</mxCell>
<mxCell id="A6uXA1JAniuv3VXLVRst-33" value="- track : int" style="text;align=left;verticalAlign=top;spacingLeft=4;spacingRight=4;overflow=hidden;rotatable=0;points=[[0,0.5],[1,0.5]];portConstraint=eastwest;" vertex="1" parent="A6uXA1JAniuv3VXLVRst-21">
<mxGeometry y="52" width="160" height="26" as="geometry" />
</mxCell>
<mxCell id="A6uXA1JAniuv3VXLVRst-23" value="- originalDepartureTime : LocalTime" style="text;align=left;verticalAlign=top;spacingLeft=4;spacingRight=4;overflow=hidden;rotatable=0;points=[[0,0.5],[1,0.5]];portConstraint=eastwest;rounded=0;shadow=0;html=0;" vertex="1" parent="A6uXA1JAniuv3VXLVRst-21">
<mxGeometry y="78" width="160" height="26" as="geometry" />
</mxCell>
<mxCell id="A6uXA1JAniuv3VXLVRst-34" value="- destination : String" style="text;align=left;verticalAlign=top;spacingLeft=4;spacingRight=4;overflow=hidden;rotatable=0;points=[[0,0.5],[1,0.5]];portConstraint=eastwest;" vertex="1" parent="A6uXA1JAniuv3VXLVRst-21">
<mxGeometry y="104" width="160" height="26" as="geometry" />
</mxCell>
<mxCell id="A6uXA1JAniuv3VXLVRst-35" value="- line : String" style="text;align=left;verticalAlign=top;spacingLeft=4;spacingRight=4;overflow=hidden;rotatable=0;points=[[0,0.5],[1,0.5]];portConstraint=eastwest;" vertex="1" parent="A6uXA1JAniuv3VXLVRst-21">
<mxGeometry y="130" width="160" height="26" as="geometry" />
</mxCell>
<mxCell id="A6uXA1JAniuv3VXLVRst-36" value="- trainNumber: String" style="text;align=left;verticalAlign=top;spacingLeft=4;spacingRight=4;overflow=hidden;rotatable=0;points=[[0,0.5],[1,0.5]];portConstraint=eastwest;" vertex="1" parent="A6uXA1JAniuv3VXLVRst-21">
<mxGeometry y="156" width="160" height="26" as="geometry" />
</mxCell>
<mxCell id="A6uXA1JAniuv3VXLVRst-24" value="" style="line;html=1;strokeWidth=1;align=left;verticalAlign=middle;spacingTop=-1;spacingLeft=3;spacingRight=3;rotatable=0;labelPosition=right;points=[];portConstraint=eastwest;" vertex="1" parent="A6uXA1JAniuv3VXLVRst-21">
<mxGeometry y="182" width="160" height="8" as="geometry" />
</mxCell>
<mxCell id="A6uXA1JAniuv3VXLVRst-26" value="Departure()" style="text;align=left;verticalAlign=top;spacingLeft=4;spacingRight=4;overflow=hidden;rotatable=0;points=[[0,0.5],[1,0.5]];portConstraint=eastwest;" vertex="1" parent="A6uXA1JAniuv3VXLVRst-21">
<mxGeometry y="190" width="160" height="26" as="geometry" />
</mxCell>
</root>
</mxGraphModel>
</diagram>
</mxfile>
"Kravspek"
Train dispatch system
- Departure time - hh:mm, 24h display
- Line - A stretch of rails the train traverses, multiple trains can traverse the same line at different times.
- Train Number - Unique identification number(within a 24h period).
- Destination - Self explanatory?
- Track - Whole possitive number representing a track, -1 for trains that have not been designated a track yet.
- Delay - How much a train has been dilayed in hh:mm. Set to 00:00 if the train is not delayed.
"avgrensninger"
- The train dispatch system should only handle one station.
- The train dispatch system does not handle dates, only departures within the same day.
- The time is updated manually trough the user interface.
"Funksjonelle krav"
The application is to have a text-based user interface in the form of a menu.
From this menu the operator should be able to perform (at least) the following:
- Show/print a list of departures, sorted by current departure time.
- Add a new departure. It shouldn't be possible to add a duplicate of a train number.
- Assign a track to a departure, by first searching for a departure by the train number, and then add a track.
- Add a delay to a departure, by first searching for a departure by train number, and then adding a dellay in the format hh:mm.
- Search for departures by train number.
- Search for departures by destination.
- Update the current time by asking the user for a new time of day. This should not allow for setting the time of day at a earlier time of day than the current one. Train departures are to automatically be removed if the current time is set to after (departure time + delay).
- Exit the application.
the "information board" is to show the following information in this order:
- Departure time on the form hh:mm
- Line
- Train Number
- Destination (if there is no delay this should be epty, and not even display "00:00"
- Track (If no track is assigned, this should display nothing)
"Del 1"
Design av klasse, kodestil og dokumentasjon samt versjonskontroll og enhetstesting.
- Understand the project:
- Create the project as a Maven-project: OK
- Set up version controll like GitHub or GitLab: OK
- Implement the "entity class" representing a train departure:
- implement a test class to test the "entity class"
- Start writing the project report.
The documentation of the "entity class" should include
- What the class is responsible for
- which information the class contains, as well as which datatypes are used for each type of information with arguments for why this datatype has been chosen.
- An assessment of which information should only be assigned at the creation of an object, and which information should be possible to mutate.
- How the class responds to inproper or invalid data, and a description of the strategy used to achieve this.
Writing the project report:
- Get to know the report template
- Make the front page and write an introduction to the report.
- Make a UseCase-diagram showing the functionality the application is going to provide to a user.
- Start writing the chapters about theory and method.
- Start giving a short description of which classes you are going to make for the project in the "resultat" chapter, and make a class-diagram showing dependencies between those classes.
\ No newline at end of file
package edu.ntnu.stud;
public class Main {
}
package edu.ntnu.stud.menubuilder;
/** Dynamic menues.
*
*/
public class MenuBuilder {
}
package com.example.menu;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/** Represent a menu.
*
* @author Lars-Johan Larsen
* @version 1.0
* @since 1.0
*/
public class Menu {
private String title;
private List<MenuItem> entries;
private static String autoPromptNextLine(Scanner scanner, String prompt){
System.out.printf(prompt);
return scanner.nextLine();
}
private static int forceIntNextLine(Scanner scanner, String prompt, String inputError){
do {
try {
return Integer.parseInt(autoPromptNextLine(scanner, prompt));
} catch (NumberFormatException e){
System.out.printf("%s%n%n", inputError);
}
} while (true);
}
/** Lets the user select a menu item from the menu.
*
* @param scanner A Scanner that takes user input.
* @param maxChoice An int representing the number of MenuItems registered in the menu.
* @return An int representing the user's selected choice.
*/
private int getUserChoice(Scanner scanner, int maxChoice) {
int choice = -1;
do {
choice = forceIntNextLine(scanner,
"Enter your choice: ",
"Please enter a name from 0 to " + maxChoice);
} while (choice < 0 || choice > maxChoice);
return choice;
}
/** Makes a new Menu object.
*
* @param title A String representing the menu's title text.
*/
public Menu(String title) {
this.title = title;
this.entries = new ArrayList<>();
}
/** Makes a new MenuItem and tries to add it to the Menu.
*
* @param label A String representing a menu item's label.
* @param action A Runnable representing the action of a menu item.
*/
public void newEntry(String label, Runnable action) {
addEntry(new MenuItem(label, action));
}
/** Adds a MenuItem to the menu.
*
* @param myEntry A MenuItem representing an entry in a menu.
*/
public void addEntry(MenuItem myEntry) {
for (MenuItem entry : this.entries) {
if (entry.getLabel().equals(myEntry.getLabel())) {
throw new IllegalArgumentException(
"A MenuItem with this label already exists in this Menu");
}
if (entry.getAction().equals(myEntry.getAction())) {
throw new IllegalArgumentException(
"A MenuItem that does exactly this already exists in this Menu");
}
}
entries.add(myEntry);
}
/** Replaces a MenuItem in the Menu with a different MenuItem.
*
* @param toReplaceMenuItem A MenuItem representing an entry in a menu.
* @param replacemenMenuItem A MenuItem representing an entry in a menu.
*/
public void replaceEntry(MenuItem toReplaceMenuItem, MenuItem replacemenMenuItem) {
this.entries.set(this.entries.indexOf(toReplaceMenuItem), replacemenMenuItem);
}
/** Removes a MenuItem from the Menu.
*
* @param myentry A MenuItem representing an entry in a menu.
* @return A MenuItem representing the entry that has been removed.
*/
public MenuItem removeEntry(MenuItem myentry) {
MenuItem result = null;
for (MenuItem entry : this.entries) {
if (entry.equals(myentry)) {
this.entries.remove(entry);
result = entry;
break;
}
}
return result;
}
/** Gets the MenuItems from this Menu in the form of a List.
*
* @return A List of MenuItems representing the contents of a Menu.
*/
public List<MenuItem> getEntries() {
return this.entries;
}
/** Get a MenuItem from the Menu from the specified label.
*
* @param label A String representing a menu item's label.
* @return A MenuItem representing the entry that has been removed.
*/
public MenuItem getEntryByLabel(String label) {
MenuItem result = null;
for (MenuItem entry : this.entries) {
if (entry.getLabel().equals(label)) {
result = entry;
}
}
return result;
}
/** Get a MenuItem with the specified action from the Menu.
*
* @param action A Runnable representing the action of a menu item.
* @return A MenuItem representing an entry in a menu.
*/
public MenuItem getEntryByAction(Runnable action) {
MenuItem result = null;
for (MenuItem entry : this.entries) {
if (entry.getAction().equals(action.toString())) {
result = entry;
}
}
return result;
}
/** Get a MenuItem that has the specified position in the Menu.
*
* @param position An int representing the possition of a MenuItem in a Menu.
* @return A MenuItem representing an entry in a menu.
*/
public MenuItem getEntryByPosition(int position) {
MenuItem result = null;
if (((this.entries.size()) >= position) && (position >= 1)) {
this.entries.get(position);
}
return result;
}
/** Displays a menu object's menu entries and allows user to chose an item.
*
* @param exitOnChoice A boolean if a menu should exit when any choice is made.
*/
public void displayMenu(boolean exitOnChoice) {
int choice = 0;
do {
final Scanner scanner = new Scanner(System.in);
System.out.println("\n" + title);
int position = 1;
for (MenuItem entry : this.entries) {
System.out.println((position) + ". " + entry.getLabel());
position++;
}
System.out.println("0. Exit");
choice = getUserChoice(scanner, entries.size());
if (choice != 0) {
MenuItem selectedEntry = entries.get(choice - 1);
selectedEntry.executeAction();
if (exitOnChoice) {
break;
}
}
} while (choice != 0);
}
}
\ No newline at end of file
package com.example.menu;
/** Represent an item in a menu.
*
* @author Lars-Johan Larsen
* @version 1.0
* @since 1.0
*/
class MenuItem {
private String label;
private Runnable action;
public MenuItem(String label, Runnable action) {
this.label = label;
this.action = action;
}
/** gets an item's label.
*
* @return A String representing a menu item's label.
*/
public String getLabel() {
return label;
}
/** Sets an item's label.
*
* @param label A String representing a menu item's label.
*/
public void setLabel(String label) {
this.label = label;
}
/** Executes the action or set of actions specified for this menu item.
*/
public void executeAction() {
if (action != null) {
action.run();
}
}
/** Gets the action of this menu item.
*
* @return A String representing the action of a menu item.
*/
public String getAction() {
return action.toString();
}
/** Sets the action or set of items executed by this menu item.
*
* @param action A Runnable representing the action of a menu item.
*/
public void setAction(Runnable action) {
this.action = action;
}
}
\ No newline at end of file
package edu.ntnu.stud.menubuilder.trainz;
import java.time.LocalTime;
import java.time.Duration;
/** Represents a Departure.
* This class is responsible for keeping track og original departure time,
* destination, delay to departure, departure track, line, and train number.
*
*/
public class Departure {
private Duration delay;
private int track;
private final LocalTime originalDepartureTime;
private final String destination;
private final String line;
private final int trainNumber; //can be replaced with a reference to a train object?
// constructors
public Departure(int trainNumber, String line, String destination, LocalTime departureTime) {
this.track = -1;
this.delay = Duration.ZERO;
this.originalDepartureTime = departureTime;
this.destination = destination;
this.line = line;
this.trainNumber = trainNumber;
}
// delay methods
public void setDelay(Duration duration) {
this.delay = duration;
}
public void addDelay(Duration duration) {
this.delay = this.delay.plus(duration);
}
public void subDelay(Duration duration) {
this.delay = this.delay.minus(duration);
}
public Duration getDelay() {
return this.delay;
}
// track methods
public void setTrack(int trackNumber) {
if ((trackNumber <= 0) && (trackNumber != -1)) {
throw new IllegalArgumentException("""
Cannot assign non-existant track to a departure.
Please assign a track number higher than 0,
or -1 to set this departure to not have a departure assigned.""");
}
this.track = trackNumber;
}
public int getTrack() {
return this.track;
}
// departure methods
public LocalTime getCurrentDepartureTime() {
return this.originalDepartureTime.plus(this.delay);
}
public LocalTime getOriginalDepartureTime() {
return this.originalDepartureTime;
}
// destination methods
public String getDestination() {
return this.destination;
}
// line methods
public String getLine() {
return this.line;
}
// trainNumber methods
public int getTrainNumber() {
return this.trainNumber;
}
// information board
public String getInformationBoardEntry(int collumnWidth) {
StringBuilder stringBuilder = new StringBuilder("|");
String formString = "|s-" + collumnWidth + "s|";
stringBuilder.append(String.format(formString,
originalDepartureTime.plus(this.delay).toString()));
stringBuilder.append(String.format(formString, line));
stringBuilder.append(String.format(formString, trainNumber));
stringBuilder.append(String.format(formString, destination));
if (track != -1) {
stringBuilder.append(String.format(formString, track));
} else {
stringBuilder.append(String.format(formString, ""));
}
if (!delay.isZero()) {
stringBuilder.append(String.format(formString, delay));
} else {
stringBuilder.append(String.format(formString, ""));
}
stringBuilder.append("|");
return stringBuilder.toString();
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment