Skip to main content

Initial Digital Model of London Underground Stations

 

Scenario:

You are required to design a class to hold details of a metro station, including the number of tickets sold.  The station should have the following properties:

Property Name

Examples

Validation

Public Accessors

Name

“Aldgate”

“Barbican”

Not “”

Get

Set

Line

“District”

“Central”

Not “”

Get

Set

TicketsSold

1234

0 to 999999

Get

 

Examples of names and lines can be found at https://en.wikipedia.org/wiki/List_of_London_Underground_stations  .

 

The Station class should have a public  method sellTicket()  which adds 1 to the TicketsSold property.


Task 1:

Draw a class diagram showing the Station class – you might want to use draw.io to produce the diagram, paste your diagram into the box below:




Task 2: Implement your Station class in c# and paste the source code below:


using System;

using MetroStation;

 

namespace MetroStation

{  

     // Here we are Defining Station class which include properties,

    //   Name, Line and TicketsSold.

   

    class Station

    {

        private string station_name = "" ;

        private string station_line = "";

        private int TicketSold = 0; // Ticket Sold counter.

        

        // Property to get or set the station_name.

        public string Name

        {

            get

            {

                return station_name;

            }

            set

            {   // Checking if the provided value is null or empty.

                if (string.IsNullOrEmpty(value))

                 {

                 // If it’s, throw ArgumentException with Specific                          Message and   

                //  parameter name.

                throw new ArgumentException("Station Name cannot be                                                                                                                  Blank!!!","Name");               

                 }

                station_name = value;

            }

        }

 

        // Property to get or set station_line.

        public string Line

        {

            get

            {

                return station_line;

            }

            set

            {   // Checking if the provided value is null or empty.

                if (string.IsNullOrEmpty(value))

                {

              // If it’s, throw ArgumentException with Specific Message

     

              // parameter name.

              throw new ArgumentException("Station Line cannot be                                                 Blank!!!", "Line");

                }

                station_line = value;

            }

        }          

        // Property to get number of tickets sold.

 

        public int TicketsSold

        {

            get

            {

                return TicketSold;

            }

        }

       

        // Method for approximating the sale of ticket.

        public void SellTicket()

        {   

            // Keeps track of number of tickets sold

            TicketSold++;

        }

    }

       

  Task 3 (Advanced)

Add a property ‘ID’ to your station class, which can be get and set.   The ID property takes the form of a number in the range 1000 to 2000.

 

Add the following code to your Station class:

...

private String _id;

public string ID   // property

{

get {

return _id;

     }  

 

set {

    if (value == ""){

      throw new ArgumentException("ID cannot be blank!","ID");

}

      _id = value;

 } 

}

...

The following code can be used in a console application to test your Station class:

//Test for blank

try

{

    myStation.ID = "";

}

catch (Exception ex)

{  

Console.WriteLine(ex.Message);

}

Modify the set() method of Station.ID to throw an error if the ID is not a number or is out of the 1000 ro 2000 range.      

 

 

// So here we are Introducing new property id in Station Class

 

...

 

private string id= “”;

 

 

 public string ID

 {

 

     get

    

     {

         return id;

     }

    

 

     set

   

     {    

           // So here we are checking if the number is numeric or not,

          // If the value (Station ID) falls between 1000 and 2000 or not,

 

        if (!IsNumericString(value) || int.Parse(value) <= 1000 || int.Parse(value) >= 2000)

          {

          throw new ArgumentException("Station ID should be Numeric value but also between  

                                       1000 and 2000.", "ID");      

         }

        

          id = value;

     }

 

 

         

 }

 

 

// IsNumericString method for checking if a value is numeric or not

// Got an idea on, how to use IsNumeric method from

// Identify if a string is numeric in C# | Techie Delight

 

 

 private static bool IsNumericString(string value)

 

{

     return int.TryParse(value, out _);

}

. . .   


Paste your main method (console) here:

 class Program

{

    static void Main(string[] args)

    {

        // Create an instance of the Station class

        Station station = new ();

     

        try

        {

            // Set properties

            // Here we check for the ID if it is numeric or not

            station.ID = "ABC";

            station.Name = "Preston";

            station.Line = "District";

 

            station.SellTicket();

 

            Console.WriteLine(" Here are your ticket Details ");

            Console.WriteLine("Station Id: " + station.ID);

            Console.WriteLine("Station Name: " + station.Name);

            Console.WriteLine("Station Line: " + station.Line);

            Console.WriteLine("Tickets Sold: " + station.TicketsSold);

 

        }

        catch (Exception ex)

        {

            Console.WriteLine(ex.Message);

 

        }

         

           // Just for a gap

           Console.WriteLine("");

       

       

        try

        {   // Here we are checking if the provided id is within the range or not.

            station.ID = "2999";

            station.Name = "London";

            station.Line = "Central";

 

            station.SellTicket();

 

            Console.WriteLine(" Here are your ticket Details ");

            Console.WriteLine("Station Id: " + station.ID);

            Console.WriteLine("Station Name: " + station.Name);

            Console.WriteLine("Station Line: " + station.Line);

            Console.WriteLine("Tickets Sold: " + station.TicketsSold);

   

        }

        catch (Exception ex)

        {

            Console.WriteLine(ex.Message);

        }

            // Just for a gap

               Console.WriteLine("");  

        try

        {   // Thus, this will show the valid output,

           // if everything is provided correctly.

           

            station.ID = "1222" ;

            station.Name = "Edinburgh";

            station.Line = "West";

 

            station.SellTicket();

 

            Console.WriteLine(" Here are your ticket Details ");

            Console.WriteLine("Station Id: " + station.ID);

            Console.WriteLine("Station Name: " + station.Name);

            Console.WriteLine("Station Line: " + station.Line);

            Console.WriteLine("Tickets Sold: " + station.TicketsSold);

 

        }

        catch (Exception ex)

        {

            Console.WriteLine(ex.Message);

        }

 

    }

}

 Paste the output from your console app here:

 

 

Station ID should be Numeric value but also between 1000 and 2000. (Parameter 'ID')

 

Station ID should be Numeric value but also between 1000 and 2000. (Parameter 'ID')

 

 Here are your ticket Details

Station Id: 1222

Station Name: Edinburgh

Station Line: West

Tickets Sold: 1

 

C:\Users\40612246\OneDrive - Edinburgh Napier University\Coursework1 OOP\Coursework1 OOP\bin\Debug\net6.0\Coursework1 OOP.exe (process 23852) exited with code 0.

To automatically close the console when debugging stops, enable Tools->Options->Debugging->Automatically close the console when debugging stops.

Press any key to close this window . . .  











Comments

Popular posts from this blog

Sudoku Game Using PyGame

Click to Download and Play I created a Sudoku game in Python using the Pygame library for the graphical interface as part of my coursework project on algorithms and data structures. Below are the instructions on how to compile and execute the provided code. ### Prerequisites: - Latest version of Python installed on your system - Pygame library installed (install via pip:                  `pip install pygame`) ### Instructions: 1. **Download the Code**: Save the provided Python script named "sudoku.py" to your local machine. 2. **Install Python**: Ensure you have Python installed on your system. Download it from the official website: [Python Downloads](https://www.python.org/downloads/). 3. **Install Pygame**: Open your terminal or command prompt and run:        ``    pip install pygame    `’ 4. **Navigate to the Script**: Use the terminal or command prompt to navigate to the directory where you saved "s...