#C12708. Stock Market Simulator
Stock Market Simulator
Stock Market Simulator
You are required to implement a simple stock market simulator. The simulator supports the following operations:
- add: Add a new company with its share price and available shares.
- buy: Buy a certain number of shares from a company. If the company does not have enough shares, output an error message.
- sell: Sell a certain number of shares, which increases the available shares of the company.
- display: Display the current market status in a table format.
The table should have three columns: the company name, share price, and available shares. The header must be formatted with fixed-width fields as shown in the sample output. Use the following latex style for any formula if needed: for example a share update can be given by \( \text{New Shares} = \text{Old Shares} - \text{Bought Shares} + \text{Sold Shares} \).
The commands will be provided through standard input. Your program must process all the commands in the given order and output the final result of the display operation.
inputFormat
The first line contains an integer n, representing the number of commands. Each of the next n lines contains a command in one of the following formats:
add <company_name> <share_price> <available_shares>
buy <company_name> <shares>
sell <company_name> <shares>
display
— this command outputs the market table.
Note: share_price
is a floating point number and available_shares
/shares
are integers.
outputFormat
When the command display
is encountered, print the current status of the market on standard output in the following format:
Company Name Share Price Available Shares ================================================== <company1> <price1> <shares1> <company2> <price2> <shares2> ...
Each field must be left-aligned with fixed widths (20 for the company name, 15 for the share price) as shown above.
## sample4
add TechCorp 100 1000
buy TechCorp 50
sell TechCorp 20
display
Company Name Share Price Available Shares
==================================================
TechCorp 100 970
</p>