top of page

Python Assignment : Stock Price Simulator Solutions

Updated: Mar 19, 2021

Learning Objectives:

  • To understand the concept of Class and Object.

  • To identify classes and their fields/methods from a problem statement.

  • To understand overloading methods including class constructors.

  • To generate random numbers with the Random class.

  • To write your own testing cases to test your program.

  • To use debugger for debugging your Java program.

  • To use file output to write data to a CSV (Comma Separated Values) file


Problem Statement

You are required to write a stock price simulator, which simulates a price change of a stock. When the program runs, it should do the following:

 Create a Stock class which must include fields: name, symbol, currentPrice, nextPrice,

priceChange, and priceChangePercentage.

 The Stock class must have two constructor: a no-argument constructor and a constructor

with four parameters that correspond to the four fields.

o For the no-argument constructor, set the default value for each field such as:

 Name: Microsoft

 Symbol: MSFT

 currentPrice: 46.87

 nextPrice: 46.87

o For the argument constructor, set the value of four fields based on the arguments.

 The Stock class must have accessors and mutators for all of its fields.

 The setter methods must protect a user from setting the currentPrice/nextPrice into

negative number. You can set the values to 0 if a user tries to change them to negative

values.

 The Stock class should have a SimulatePrice() method, which increases or decreases the

currentPrice by 0 - 10% randomly, such as 0.56% and 9.65%.

 The main program will ask user to enter a name, symbol, current price of a stock. Then, it

simulates the prices for next 30 days. It displays the prices for the next 30 days on the

console and to a file named output.csv, instead of .txt. CSV (Comma Separated Values)

file format is very popular because it can be opened in Microsoft Excel. For more

information about CSV, you can read the following article online:

o https://www.computerhope.com/issues/ch001356.htm

 If a user enters “NONE”, “NA”, 0.0 for name, symbol, current price



Input

This program requires that you read in the following data values:

 A stock’s name, symbol, and current price.

o E.g., Microsoft Corporation, MSFT, 45.87.


You will use interactive I/O in this program. All of the input must be validated if it is needed. You can assume that for a numeric input value, the grader will enter a numeric value in the testing cases.


Output

Your program should display the stock’s name, symbol, current price, next

price priceChange priceChangePercentage for each day on the console and

write to a CSV file.

Comentarios


bottom of page