Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

COMP1117A Computer Programming

Assignment 4

Due date: 7 December, 2022 23:59

Overview

In this assignment, you are going to build a virtual vending machine for selling train tickets. Please read this assignment sheet carefully before starting your work.

Reminders

This assignment involves a lot of console input/output. You are reminded that the VPL system on       HKU Moodle evaluates your program with a full score under the condition that your program output is the EXACT MATCH of the expected output. In other words, any additional or missing space             character, newline character, etc. will be treated as errors during the evaluation of your program.      Also, you are suggested to make more test cases on your own for testing your program.

Assignment Specification and Requirement

Consider a vending machine, which sells train tickets, that accepts coins only. The vending machine allows customers to select a destination, insert coins, and buy tickets.

Load initial status from file

The number of tickets the machine contains and the fare chart is defined in a text (.txt) file. Upon      running the program, the program should ask the user to input the name of the text file and then      loads the data into the system. Please refer to the sample input file and sample output below for the exact input and output format.

The text file has the following format:

•   The first line gives the number of tickets the machine contains. You can assume the number is larger than 0.

•    Beginning from the second line, each line gives the station name that the customer can  travel to and the ticket fare, separated by a space. You can assume the station name is a single word and the fare is an integer larger than 0. You can also assume the fare chart   contains at least one row.

For example, a text file (init1.txt) contains the following:

3

LoWu 20

FoTan 8

ShaTin 6

TaiWai 4

MongKok 2

This file defines 5 stations and the vending machine has 3 tickets for selling.

Below is the program output after loading data from the input file (Bold red texts are user input):

File to initialize the vending machine:

init1.txt

Station(s): LoWu, FoTan, ShaTin, TaiWai, MongKok.

Please choose a destination or enter 'Exit':

•   The above program has NOT terminated yet.

•   There is NO space character at the end of each line.

•    Assume the input file and your program are placed in the same folder. You only need to

input the filename instead of the full path of the file.

Note that the above program has not terminated yet.

Main menu

As shown in the above sample output, after loading data from the input file, the system needs to       display the main menu that lists all the stations and ask the user to select a destination. The user can choose the destination by inputting the name of the station.

•   The listing of stations names follows the order in the input file

Exit – terminate the program

If the user enters “Exit”, the program prints “Bye” and then terminates.

File to initialize the vending machine:

init1.txt

Station(s): LoWu, FoTan, ShaTin, TaiWai, MongKok.

Please choose a destination or enter 'Exit':

Exit

Bye

Out of ticket

The number of tickets is finite and is given by the input file. When the machine runs out of the ticket, the machine should print “Out of Service. Please enter 'Exit':” . Please refer to the sample output in    the last section for the exact output format.

•    You can assume the user will only input “Exit” when the machine runs out of ticket.

Number of tickets

When the system receives a station name, the system will then ask how many tickets the customer wants to buy. If the machine has enough tickets, the system will enter the payment process. Please refer to the next section for the details of the payment process. If the customer tries to buy more   tickets than the machine available, prints “Error: Cannot handle your request.” and then goes back to the main menu.

Below is a case when the customer tries to buy more tickets than the available in the machine.

File to initialize the vending machine:

init1.txt

Station(s): LoWu, FoTan, ShaTin, TaiWai, MongKok.

Please choose a destination or enter 'Exit':

FoTan

Please enter the number of tickets:

5

Error: Cannot handle your request.

Station(s): LoWu, FoTan, ShaTin, TaiWai, MongKok.

Please choose a destination or enter 'Exit':

Exit

Bye

Note that 3 tickets is initially defined by the input file init1.txt. The customer tried to buy 5 tickets at once, which is more than available. The system should prints Error: Cannot handle your request.” .

Payment process

In the payment process, the system continuously displays the price and the sum of the inserted         credit, and asks the user to insert a coin until the sum of the inserted credit is larger than or equal to the price. The user can insert a coin at a time by inputting the face value. When inserted credit is       enough, the system drops ticket(s) and gives change. Please refer to the sample output below for      the exact format.

•    You can simply calculate the change by subtracting the inserted credit by the price. After a successful transaction, the system goes back to the main menu.

Below is a case of a successful transaction.

File to initialize the vending machine:

init1.txt

Station(s): LoWu, FoTan, ShaTin, TaiWai, MongKok.

Please choose a destination or enter 'Exit':

FoTan

Please enter the number of tickets:

2

Destination: FoTan, Quantity: 2, Price: $16, Inserted: $0.

Please insert a coin or enter 'Cancel':

2

Destination: FoTan, Quantity: 2, Price: $16, Inserted: $2.

Please insert a coin or enter 'Cancel':

2

Destination: FoTan, Quantity: 2, Price: $16, Inserted: $4.

Please insert a coin or enter 'Cancel':

5

Destination: FoTan, Quantity: 2, Price: $16, Inserted: $9.

Please insert a coin or enter 'Cancel':

10

Destination: FoTan, Quantity: 2, Price: $16, Inserted: $19. Dropped ticket(s). Your change: $3.

Station(s): LoWu, FoTan, ShaTin, TaiWai, MongKok.

Please choose a destination or enter 'Exit':

Exit

Bye

Note that as defined in the input file init1.txt, a single ticket to FoTan costs $8. Thus, two tickets cost $16.

Cancel a Payment Process

During the payment process, the customer can cancel it by inputting “Cancel” instead of inserting a

coin. When the process is cancelled, inserted coins are returned to the customer.

•    You can assume customers would not cancel the process when asked to input the number of tickets.

•   The listing of coins should be sorted by the face value ascendingly.

•    If there is no coin to be returned, print Returned no coin.” .

After the cancellation, the system goes back to the main menu.

File to initialize the vending machine:

init1.txt

Station(s): LoWu, FoTan, ShaTin, TaiWai, MongKok.

Please choose a destination or enter 'Exit':

LoWu

Please enter the number of tickets:

1

Destination: LoWu, Quantity: 1, Price: $20, Inserted: $0.

Please insert a coin or enter 'Cancel':

2

Destination: LoWu, Quantity: 1, Price: $20, Inserted: $2.

Please insert a coin or enter 'Cancel':

1

Destination: LoWu, Quantity: 1, Price: $20, Inserted: $3.

Please insert a coin or enter 'Cancel':

5

Destination: LoWu, Quantity: 1, Price: $20, Inserted: $8.

Please insert a coin or enter 'Cancel':

Cancel

Cancelled. Returned coin(s): $1, $2, $5.

Station(s): LoWu, FoTan, ShaTin, TaiWai, MongKok.

Please choose a destination or enter 'Exit':

LoWu

Please enter the number of tickets:

1

Destination: LoWu, Quantity: 1, Price: $20, Inserted: $0.

Please insert a coin or enter 'Cancel':

Cancel

Cancelled. Returned no coin.

Station(s): LoWu, FoTan, ShaTin, TaiWai, MongKok.

Please choose a destination or enter 'Exit':

Exit

Bye