Skip to content

3-part project focused on implementation of design patterns by the GOF, but in Java. Train-themed 🚂.

Notifications You must be signed in to change notification settings

CroAnna/design-patterns-GOF

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Railway Transport Management System - design patters

This project is mostly focused on different types of design patterns by GOF (Gang of Four) in Java.

About the project

It is separated in 3 different subprojects + data used to start them. Every one of them has following document that explains project requirements for it, how it should be implemented, what commands does it have etc. The second one is built upon the first one, and third one upon the second one, even though every one has new funcionalities and used design patterns.

🔨 Project #1

This Java project manages railway transport for passengers and goods, involving railway infrastructure, stations, and transport compositions. The program operates in a command-line environment, executing various commands for data retrieval and loads data from given CSV files.

Design patterns

Singleton:

  • Used for the ZeljeznickiSustav (Railway System) class because:
    • There must be exactly 1 instance of the system class
    • Global access point is required since it contains data used throughout the entire system (e.g., lists of loaded data)
    • Centralizes critical system data:
      • Lists of loaded vehicles, stations, and compositions
      • Total error count in the system
    • Ensures consistent access to system state from any component

Factory method:

  • Used for CSV reader classes because:
    • Different CSV files require different reading processes based on the file provided through arguments
    • Each CSV file has:
      • Different data structures and columns
      • Unique object creation processes
      • Distinct parsing requirements
    • Makes it easier to add new CSV readers later (since each CSV document has different data and reading process)

Builder:

  • Used for vehicle creation because:
    • Different types of railway vehicles exist:
      • Locomotives
      • Passenger wagons
      • Car transport wagons
      • Container freight wagons
    • All vehicle data is mandatory, vehicles of certain categories have some data (columns) filled with value 0
    • This means that data isn't relevant for that vehicle group
    • Allows multiple different construction processes for vehicles, each providing a different representation

Commands

  • IP: Display railway lines, showing each line's code, starting and ending stations, and total distance
  • ISP railwayLineCode order: List stations on a specific railway line. Use N for normal order and O for reverse order. (example: ISP M501 N)
  • ISI2S startStation - endStation: Show all stations between two specified stations on the same or different lines. (example: ISI2S Donji Kraljevec - ÄŒakovec)
  • IK compositionCode: Show transport vehicles in a specific composition, detailing each vehicle's role, type, and specifications. (example: IK 8001)
  • Q: Quit the program.

Demo

dz1

🔨 Project #2

Project #2 is an upgrade to the project #1. Two more CSV files were added (day marks and train schedule), as well as new commands and new problems. Now different types of trains exist (train is not equal to composition or vehicle) - normal, accelerated and fast (new columns were added to the train stations CSV file from project #1).

Design patterns

Old patterns changes:

Singleton:

  • Added several new lists, methods for them and command management

Factory Method:

  • Added new classes for new files:
    • CSV day markings
    • CSV timetable

New patterns:

Composite:

  • Timetable consists of multiple trains, trains have one or more stages with stations which can be implemented as tree structure
  • Defines class hierarchy:
    • VozniRedComponent (Timetable Component)
    • EtapaLeaf (Stage Leaf)
    • VlakComposite (Train Composite)
    • VozniRedComposite (Timetable Composite)

Visitor

  • Builds upon previously described Composite pattern to avoid overcrowding classes
  • Allows defining new operations without changing element classes, which is needed for tree structure with multiple required behaviors
  • Special print structure and different logic in composing that print for each command
  • Just need to add new concrete visitor class if new command appears in future homework

Observer

  • Used for functionality where subscribed users need to be notified about station/train they are following
  • When one object changes state, all dependents are notified and updated

Mediator

  • Used for lost & found office functionality
  • Instead of user who found/lost something questioning every user if they lost/found that item, mediator was introduced to handle this
  • Users can communicate through mediator

dijagram1 drawio

Commands

  • IV: Display all trains showing train number, starting station, ending station, departure time, arrival time, and total distance
  • IEV trainNumber: Display stages for a specific train, showing train number, railway line, stage's starting and ending stations, departure and arrival times, stage distance, and days of operation. (example: IEV 3609)
  • IEVD days: Show trains and their stages that operate on specified days of week. Days are specified using abbreviations: Po (Monday), U (Tuesday), Sr (Wednesday), ÄŒ (Thursday), Pe (Friday), Su (Saturday), N (Sunday). (example: IEVD PoSrPeN)
  • IVRV trainNumber: Display complete train timetable showing train number, railway line code, station names, departure times, and distance from starting station. (example: IVRV 3609)
  • IVI2S startStation - endStation - day - fromTime - toTime - format: Show trains (timetable) that can be used for travel between two stations on a specific day within given time period. Format parameter controls column display:
    • S: Station name
    • P: Railway line
    • K: Distance in kilometers
    • V: Train departure time
    Example: IVI2S Donji Kraljevec - ÄŒakovec - N - 0:00 - 23:59 - SPKV
  • DK firstName lastName: Register a new user. (example: DK Pero Kos)
  • PK: List all registered users
  • DPK firstName lastName - trainNumber [- station]: Add user subscription for train tracking:
    • Track entire train journey: DPK Pero Kos - 3301
    • Track specific station arrival: DPK Mato Medved - 3309 - Donji Kraljevec
  • SVV trainNumber - day - coefficient: Simulate train journey for specified day with time acceleration coefficient (1 simulation minute = 1/coefficient real seconds). Simulation runs until train reaches destination or user enters 'X'. Updates subscribed users about train progress. (example: SVV 3609 - Po - 60)
  • NAD firstName lastName item description: Report a found item to the lost & found office (example: NAD Marko Maric mobitel Samsung)
  • IZG firstName lastName item description: Report a lost item to the lost & found office (example: IZG Ana Anic mobitel Samsung)

Demo

2024-12-2120-10-22-ezgif com-video-to-gif-converter

🔨 Project #3

Project #3 builds upon project #2, adding new functionality for ticket management, railway status handling, and train controls.

Design patterns

Old patterns changes:

Singleton:

  • Added several new methods and needed objects for new commands

New patterns:

Command:

  • Implements train control functionality with actions: turn on the lights, turn on the AC, honk
  • Concrete commands: PotrubiCommand (honk), UpaliSvjetlaCommand (lights), UpaliKlimuCommand (AC)
  • Allows chaining multiple actions in sequence (e.g. honk then lights)

State:

  • Manages railway track segment states:
  • I = ispravna (working)
  • K = kvar (faulty)
  • Z = zatvorena (closed)
  • T = testiranje (testing)
  • Handles state transitions - railway can't directly change state from Z to I, it must be tested first (Z to T to I)

Strategy:

  • Implements ticket pricing strategies based on purchase method (web/mobile app, on-train or counter purchase)
  • Concrete strategies:
  • BlagajnaStrategy (counter purchase) = B - normal price
  • UVlakuStrategy (on-train purchase) = V - more expensive price
  • WebMobilnaStrategy (web/mobile purchase) - WM - cheaper price
  • Each calculates final price with applicable discounts/fees

Memento:

  • Stores ticket purchase history
  • Enables retrieving past tickets

uzdiz3 3 drawio

Commands

  • CVP price1 price2 price3 discountSuN discountWebMob increaseTrain: Set ticket base prices and discounts:
    • price1 - normal train price per km
    • price2 - accelerated train price per km
    • price3 - fast train price per km
    • discountSuN - weekend discount %
    • discountWebMob - web/mobile purchase discount %
    • increaseTrain - on-train purchase fee %
    Example: CVP 0,10 0,12 0,15 20,0 10,0 10,0
  • KKPV2S trainNumber - startStation - endStation - date - purchaseType: Purchase ticket
    • purchaseType: B (counter), WM (web/mobile), V (on-train)
    Example: KKPV2S 3301 - ÄŒakovec - Donji Kraljevec - 10.01.2025. - WM
  • IKKPV [n]: Display all purchased tickets or nth purchase
  • PSP2S lineCode - startStation - endStation - status: Change track segment status
    • status: I (working), K (faulty), T (testing), Z (closed)
    Example: PSP2S M501 - Donji Mihaljevec - ÄŒehovec - K
  • IRPS status [lineCode]: Show track segments with specified status, optionally filtered by line
  • CMD trainNumber - actions: Control train features
    • S - turn on lights
    • T - honk horn
    • K - activate climate control
    • Actions can be combined (e.g. KTS)
    Example: CMD 3001 - KTS

Demo

dz3-ezgif com-video-to-gif-converter

How to start

Prerequisites

  • Maven installed
  • Java 21 installed

To set up and run the Railway Transport Management System, follow these steps:

On Linux, open the terminal and navigate to the selected project

cd askarica20_zadaca_1 // or cd askarica20_zadaca_2 or cd askarica20_zadaca_3

Run the following command to compile and package the application:

mvn clean package

After building, go back to podaci folder and start the application using the following command:

cd ..
cd podaci/

Project #1:

java -jar /home/NWTiS_1/DZ_1/askarica20_zadaca_1/target/askarica20_zadaca_1-1.0.0.jar --zs DZ_1_stanice.csv --zps DZ_1_vozila.csv --zk DZ_1_kompozicije.csv

Project #2:

java -jar /home/NWTiS_1/DZ_1/askarica20_zadaca_2/target/askarica20_zadaca_2-1.0.0.jar --zs DZ_2_stanice.csv --zps DZ_2_vozila.csv --zk DZ_2_kompozicije.csv --zvr DZ_2_vozni_red.csv --zod DZ_2_oznake_dana.csv

Project #3:

java -jar /home/NWTiS_1/DZ_1/askarica20_zadaca_3/target/askarica20_zadaca_3-1.0.0.jar --zs DZ_3_stanice.csv --zps DZ_3_vozila.csv --zk DZ_3_kompozicije.csv --zvr DZ_3_vozni_red.csv --zod DZ_3_oznake_dana.csv

About

3-part project focused on implementation of design patterns by the GOF, but in Java. Train-themed 🚂.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages