In Old School Runescape, players purchase weapons and armor so they can gear up and defeat monsters across the land of Gielinor. Imagine you are a programmer working on the game and need to build a small tool that processes equipment purchase orders submitted by players. The tool will read purchase requests from a file and determine whether the purchase succeeds or fails based on the player’s available money.
If the purchase is successful, the transaction will be recorded. If there is an error in the data or the player cannot afford the item, the program will record the problem so it can be reviewed later.
In this assignment you will work with four famous gear types from Old School Runescape:
| Name | Price |
|---|---|
| Scimitar | 30000 |
| Bow | 200000 |
| Armor | 500000 |
| Whip | 2500000 |
Your program will read purchase orders in the following format:
name amount typeWhere:
name is the name of the item the player wants to buyamount is how much money the player currently hastype is the type of currency the player is usingThere are two types of currency:
| Name | Note |
|---|---|
| Gold | Standard Unit of Money |
| Platinum | Equivalent to 1000 gold pieces |
For each order, your program must validate the input data and determine whether the player can afford the requested item. Valid purchases will be written to one output file, while errors will be written to another.
This assignment will help you practice several programming concepts used frequently in real software development:
Working with input and output files
Validating data read from files
Using selection statements (if / else if / else)
Handling input failures
Producing structured program output
All files needed for this assignment can be downloaded here
Write a program that processes purchase orders from an input file and determines whether each order succeeds or fails. You will need to:
Open two output files:
purchases.txt which will contain successful purchaseserrors.txt which will contain any errors encountered while processing the input fileRepeatedly prompt the user for an input filename until a valid file is entered. If an invalid file name is entered, output "Invalid filename\n" then read a new input file name until a valid one is entered.
Once a valid file is opened, read purchase orders from the file until the end of the file is reached.
For each line in the input file:
a. Read the weapon name, amount of money, and type of currency. If the monetary amount cannot be read (input failure) output to errors.txt "line X error: Invalid monetary amount\n", where X is the line number where the error occurred. Then move on to the next line of input.
b. Validate the type of currency. If the currency type is not gold or platinum output to errors.txt "line X error: Invalid monetary type\n", where X is the line number where the error occurred. Then move on to the next line of input.
c. If the currency type is platinum, convert it to gold by multiplying the amount by 1000.
d. Validate the weapon name. If the weapon name to purchase is not scimitar bow armor or whip output to errors.txt "line X error: Invalid weapon\n", where X is the line number where the error occurred. Then move on to the next line of input.
e. If the weapon is valid, determine whether the player has enough gold to purchase the item. If the player does not have enough money, output to errors.txt "line X error: not enough T to buy a I\n", where X is the line number where the error occurred, T is the currency type read from the file (gold or platinum), and I is the item name the player is trying to buy. Then move on to the next line of input.
f. If the player can afford the item first subtract the cost of the item from the player’s gold. Then, output to purchases.txt "purchasing I: A T remaining\n", where I is the item being purchased, A is the amount of money remaining after the purchase (if the player used platinum then the amount output should be divided by 1000), and T is the currency type for the purchase (gold/platinum). Then move on to the next line of input.
After all input has been processed, close the input and output files.
An example of an interaction with your program is shown below. When interacted with, your program must produce the exact same results.
as5_scimitars.txt
scimitar 30 platinum scimitar 30000 gold
Terminal
AS5 % ./a.out Filename: as5_scimitars.txt
purchases.txt
purchasing scimitar: 30 platinum remaining purchasing scimitar: 30000 gold remaining
errors.txt
as5_errors.txt
scimitar 29 platinum scimitar 29999 gold whip 2499 platinum whip 2499999 gold armor 499 platinum armor 499999 gold bow 199 platinum bow 199999 gold invalid 0 platinum scimitar invalid platinum scimitar 0 invalid
Terminal
AS5 % ./a.out Filename: invalid Invalid filename Filename: data/as5_errors Invalid filename Filename: coding is hard Invalid filename Filename: as5_errors.txt
purchases.txt
errors.txt
line 1 error: not enough platinum to buy a scimitar line 2 error: not enough gold to buy a scimitar line 3 error: not enough platinum to buy a whip line 4 error: not enough gold to buy a whip line 5 error: not enough platinum to buy armor line 6 error: not enough gold to buy armor line 7 error: not enough platinum to buy a bow line 8 error: not enough gold to buy a bow line 9 error: Invalid weapon line 10 error: Invalid monetary amount line 11 error: Invalid monetary type
as5_everything.txt
bow error platinum whip 37367859 gold armor 90124 platinum scimitar 1500 gp bow 299 platinum platelegs 12 platinum whip 1000 p scimitar 834672 gold armor invalid platinum bow 9999 gold scimitar 425635 gold armor 500001 gold whip 2500001 gold helmet 1000 platinum bow 947 platinum armor 12 invalid scimitar 342 platinum whip 98735 platinum armor 99999999 gold bow 1000 platinum scimitar inv platinum whip 8178376 gold boots 1500 gold bow 1247345617 gold scimitar 20 platinum armor 0 gold whip 3000 platinum platinum 9999 gold armor 3746 platinum bow 52 platinumpieces whip 100000 gold scimitar 23627474 gold armor 12 platinum scimitar 30001 gold bow 200 platinum armor 758473 gold whip 2501 platinum scimitar 55 platinum bow 327483948 gold armor zero gold cape 52 platinum whip oops gold armor 2375948 gold bow 999999 gold scimitar no gold gloves 0 gold whip 999999999 gold scimitar 20 plat armor 0 goldpieces scimitar 9001 platinum bow 52 platinum whip err platinum armor 501 platinum platebody 100000 gold whip 100000 g scimitar 1500 gold bow 9999 money whip 1000 platinum armor 900 platinum scimitar 134 platinum bow not gold bow 8273648 gold whip 154302 platinum sword 20 platinum
Terminal
Filename: data/as5 Invalid filename Filename: data/as5_everything Invalid filename Filename: data/as5_everything.tx Invalid filename Filename: data/as5_everything.txt
purchases.txt
purchasing whip: 37367859 gold remaining purchasing armor: 90124 platinum remaining purchasing bow: 299 platinum remaining purchasing scimitar: 834672 gold remaining purchasing scimitar: 425635 gold remaining purchasing armor: 500001 gold remaining purchasing whip: 2500001 gold remaining purchasing bow: 947 platinum remaining purchasing scimitar: 342 platinum remaining purchasing whip: 98735 platinum remaining purchasing armor: 99999999 gold remaining purchasing bow: 1000 platinum remaining purchasing whip: 8178376 gold remaining purchasing bow: 1247345617 gold remaining purchasing whip: 3000 platinum remaining purchasing armor: 3746 platinum remaining purchasing scimitar: 23627474 gold remaining purchasing scimitar: 30001 gold remaining purchasing bow: 200 platinum remaining purchasing armor: 758473 gold remaining purchasing whip: 2501 platinum remaining purchasing scimitar: 55 platinum remaining purchasing bow: 327483948 gold remaining purchasing armor: 2375948 gold remaining purchasing bow: 999999 gold remaining purchasing whip: 999999999 gold remaining purchasing scimitar: 9001 platinum remaining purchasing armor: 501 platinum remaining purchasing armor: 900 platinum remaining purchasing scimitar: 134 platinum remaining purchasing bow: 8273648 gold remaining purchasing whip: 154302 platinum remaining
errors.txt
line 1 error: Invalid monetary amount line 4 error: Invalid monetary type line 6 error: Invalid weapon line 7 error: Invalid monetary type line 9 error: Invalid monetary amount line 10 error: not enough gold to buy a bow line 14 error: Invalid weapon line 16 error: Invalid monetary type line 21 error: Invalid monetary amount line 23 error: Invalid weapon line 25 error: not enough platinum to buy a scimitar line 26 error: not enough gold to buy armor line 28 error: Invalid weapon line 30 error: Invalid monetary type line 31 error: not enough gold to buy a whip line 33 error: not enough platinum to buy armor line 40 error: Invalid monetary amount line 41 error: Invalid weapon line 42 error: Invalid monetary amount line 45 error: Invalid monetary amount line 46 error: Invalid weapon line 48 error: Invalid monetary type line 49 error: Invalid monetary type line 51 error: not enough platinum to buy a bow line 52 error: Invalid monetary amount line 54 error: Invalid weapon line 55 error: Invalid monetary type line 56 error: not enough gold to buy a scimitar line 57 error: Invalid monetary type line 58 error: not enough platinum to buy a whip line 61 error: Invalid monetary amount line 64 error: Invalid weapon
See CodeGrade for more examples.
Note: The line breaks after the inputs in the example output formatting above are the ones placed into the terminal by the user hitting enter on their keyboard to input. CodeGrade does not enter values with a keyboard, but rather supplies input via Linux redirection to programs. So in CodeGrade's auto tests there will not be line breaks after the inputs.
Save: Save your code as main.cpp. Do not ignore this step or save your file(s) with different names.
Submit: Your program source code must be submitted via CodeGrade as a properly named .cpp file prior to the deadline to receive full credit. Any submissions after the deadline will be subject to the class’ late policy.