Project 2 (Assignment 2) in C#
Section II Project
Purpose of this project is to exercise the concepts learned in this chapter. It is not the purpose of this project to create realistic services and applications. We will create more realistic services and applications in the subsequent projects. In this project, you can use a console application or a simple GUI application to implement the user interface to your program. You do not need to create Web applications. You do not need to create web services. You can simply use classes in object-oriented paradigm.
Description: Find a parking space is not easy and not cheap when you travel in many large cities. You decide to create an e-business: a new parking space block booking system that involves parking space agents and parking structures (parking structure owners). The system consists of multiple parking space agents (clients) and multiple parking structures (servers). The parking space agents can buy in quantity (block) of parking spaces from the parking structures with lower (discounted) prices, and then resell the parking spaces to their customers (end users) at competitive prices. The required architecture and the major components of the system are shown in the diagram in Figure 1.
Figure 1 Architecture of a new parking space booking system in multithreading system
In this project, you will simulate both clients and servers in one system using multithreading and eventdriven programming. You will not implement this project in a web environment. You will further implement such systems in distributed web client and web server systems in the following projects in this course.
An Operation Scenario of the parking space block booking system is outlined as follows:
- A ParkingStructure uses a pricing model to calculate dynamically the parking space price for the parking space agents. The prices can go up and down from time to time. If the new price is lower than the previous price, it emits a (promotional) event and calls the event handlers in the parking space agents (clients) that have subscribed to the event.
- A ParkingAgent evaluates the needs based on the new price and other factors, generates an OrderObject (consisting of multiple values), and sends the order to the parking structure through a MultiCellBuffer.
- The ParkingAgent sends the OrderObject to the promoting parking structure through one of the free cells in the MultiCellBuffer.
- The ParkingStructure receives the OrderObject from the MultiCellBuffer.
- The ParkingStructure creates a new thread, an OrderProcessingThread, to process the order;
- The OrderProcessingThread processes the order, e.g., checks the credit card number and the maximum number allowed to purchase, etc., and calculates the total amount.
- The OrderProcessingThread sends a confirmation to the parking space agent and prints the order information (on screen). You will submit the output screenshots in a PDF or Word file. You can reference the case study sample output in lecture and in Text Section 2.6.3: Case Study, An Electronic Commerce Application in Event-Driven Approach, to select and organize your output file.
Components and tasks implementing the components in the diagram in Figure 1 are explained in details as follows, with their grading scores (points) allocation.
Assignment Tasks
- ParkingStructure1 through ParkingStructureK* are the objects of the same class on the server side: Each object has a method to be started as a thread by the Main method and will perform a number of functions. It uses a PricingModel to determine the block parking space prices. It defines a price-cut event that can emit an event and call the event handlers in the ParkingAgent objects if there is a price-cut according to the PricingModel. It receives the orders from the MultiCellBuffer. For each order, you must start a new thread (resulting in multiple threads for processing multiple orders) from OrderProcessing class (or method) to process the order based on the current price. There is a counter t in the ParkingStructure. After t (e.g., t = 20) price cuts have been made, a ParkingStructure thread will terminate. The parking space agents do not have to make an order after each price cut, but must make
some orders within your iterations of testing. [5 points]
In this project, you can set the number of parking structures K = 1. Note, you must define K as a variable, but you can set K = 1 when you submit your assignment.
- PricingModel: It can be a class or a method in the ParkingStructure class. It decides the price of parking spaces, which must be between 10 and 40. It can increase or decrease the price. You must define a mathematical model (formula) or use a random function that generate the numbers in the given range. You must make sure that your model will allow the price to go up sometimes and go down other times
within your iterations of testing. [5 points]
- OrderProcessing is a class or a method in a class on the server’s side. Whenever an order needs to be processed, a new thread is instantiated from this class (or method) to process the order. It will check the validity of the credit card number. You can define your credit card format, for example, the credit card number from the parking space agents must be a number registered to the ParkingStructure, or a number between two given numbers (e.g., between 5000 and 7000). Each OrderProcessing thread will calculate the total amount of charge, e.g., unitPrice*NoOfParkingspaces + Tax + LocationCharge. The tax and location charges must be represented as random values within predetermined ranges. For example, tax should be between 8% and 12%, and location charge should be $2 and $8. [5 points]
- ParkingAgent1 through ParkingAgentN. You must use a variable N, but you can set N = 5 in your implementation. Each parking space agent is a thread created by a different object instantiated from the same class. The parking space agent’s actions are event-driven. Each parking space agent contains a callback method (event handler) for the parking structures to call when a price-cut event occurs. The event handler will write the new price (reduced price) into a class variable that can be read by the parking space agent thread. The parking space agent will read the new price and calculate the number of parking spaces to order, for example, based on the need and the difference between the previous price and the current price. The thread will terminate after the ParkingStructure threads have terminated. Each order is an OrderClass object. Then, the parking space agent will send the order to the
MultiCellBuffer. [5 points]
- OrderClass is a class that contains at least the following private data members:
- senderId: the identity of the sender, you can use thread name or thread id.
- cardNo: an integer that represents a credit card number.
- receiverID: the identity of the receiver, you can use thread name or a unique name defined for a parking structure. If you are doing an individual project, you do not need this field.
- quantity: an integer that represents the number of parking spaces to order.
- unit price: a double that represents the unit price of the parking space received from the parking structure.
You must use public methods to set and get the private data members. You must decide if these methods need to be synchronized. The instances created from this class are of the OrderObject. [10 points]
- MultiCellBuffer is a class that is used for the communication between the parking space agents (clients) and the parking structures (servers): This class has three (3) data cells. Each cell is a reference to an OrderClass To write data into and to read data from one of the available cells, setOneCell and getOneCell methods can be defined. You must use a semaphore of value n (n = 3) to manage the availability of the cells. You must use an additional lock mechanism to provide read or write permissions for each cell. This lock mechanism can be one of the mechanisms offered through a library function, or a mechanism that you define by yourself. A cell can be used as long as it is available (unlocked). You cannot use a queue for the buffer, which is a different data structure and which does not allow you to lock the individual cells. The semaphore allows a parking space agent to see the availability of the cells, while the lock mechanism allows a parking space agent to gain the right to write into one of the buffer cells. The ParkingStructure can read buffer cells at the same time. To ensure the thread safety, synchronization through lock or monitor is required for read/write and write/write overlap. If your message generation is slow, e.g., you can use sleep function with a random sleeping time in the parking structure threads and in the agent threads. You need to choose proper sleeping time to avoid that the buffer cells are idle most of the time and the first cell is always used and the rest of the cells are never used. In this case, you need to reduce the sleep time in the parking structure and agent threads, and/or to add a sleep time in the buffer operation to make the cell to be
locked longer, so that all cells have a chance for being used and the usage being reflected in the
outputs/printouts. [10 points]
- Main: The Main thread will perform necessary preparation, create the buffer classes, instantiate the
objects, create threads, and start threads. [10 points]
Notes:
- It is the purpose of this course to enforce the knowledge of C# and Visual Studio, as we have multiple courses in our program that use Java. We encourage you to use C#. However, we allow the use of Java (on NetBeans) in this assignment. You must indicate the environment (e.g., 2022 NetBeans) that you use, so that the TA can use the same environment to grade the project. We taught multithreading in both Java and C#. However, we did not teach event-driven programming in Java. If you choose to do the project in Java, you need to study this part on your own. I suggest that you do the project in Java only if you already know how to program events. The event handling in Java is not as easy as in C# for this project, and you need to take extra effort to implement the required functions.
- You must follow what is defined in the assignment/project document. You have flexibility to choose your implementation details if they are not explicitly specified in the document. If you are not sure on any issue, ask the instructor or the TA by posting the question on the course discussion board.
- The program and each component of the program must be well commented.
- In this assignment, you may not need to have external input, but you must set your internal inputs in such a way that the program is properly tested, for example, each client must have completed at least one ordering process before the server terminates.








