Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

Stock Portfolio

Marilyn de Queiroz
Sheriff
Posts: 9067
12

Can anyone help Lloyd?
[ July 16, 2003: Message edited by: Marilyn de Queiroz ]

Marilyn de Queiroz
Sheriff
Posts: 9067
12
Oops. The assignment got lost somewhere in the posting.

Stock Portfolio
Develop an application that allows users to "play the stock market." Users will be able to buy and sell stock, and find out the value of each stock as well as the value of their portfolio. Stock prices will fluctuate. This application will be developed in stages.

In stage I, you will create the Stock class that will have instance variables of name, purchase price, number of shares, and current price. You will need methods to gain access to the instance variables and method to randomly adjust stock prices up or down (generate a random number between -I and + I and multiply that value by the price per share and assign it to a variable called current price). A main() method can also be included for testing. There should be at least 9 methods for this class. The name of the class will be Stock.
In another class called PlayStock the main() method is where you should ask the user the name, purchase price and number of shares. Next, you will simulate the fluctuation of stock prices by increasing or decreasing the stock price randomly. After adjusting the price, print the stock name, purchase price, number of shares and current price. The main() method should also calculate and display the value of the stock by multiplying the number of shares by the current price, how much it was when purchased and the gain or loss (a positive number is a gain and a negative number is a loss).
To convert your stock names to upper case, use the toUpperCase() method of the String class. To round your decimal values, use the format() method of the DecimalFormat class.
[ July 16, 2003: Message edited by: Marilyn de Queiroz ]

Marilyn de Queiroz
Sheriff
Posts: 9067
12
In stage I, you will create the Stock class that will have instance variables of name, purchase price, number of shares, and current price. You will need methods to gain access to the instance variables and method to randomly adjust stock prices up or down (generate a random number between -I and + I and multiply that value by the price per share and assign it to a variable called current price). A main() method can also be included for testing. There should be at least 9 methods for this class. The name of the class will be Stock.

The class name is Stock. Good start, Lloyd.

I suggest that "name" should be a String, "purchasePrice" should be a double, "numberOfShares" should be an int (since you can't buy a fraction of a share), and "currentPrice" should be another double.

"methods to gain access to the instance variables" are usually called something like getName(), getPurchasePrice() etc. which return the variable. (That's already 4 methods) These types of methods are usually pretty straightforward.

You need a method to generate a random number. This one may take a little more research to fulfill the requirements stated above.

And your main method. So you only need three more methods.

Marilyn de Queiroz
Sheriff
Posts: 9067
12
In another class called PlayStock the main() method is where you should ask the user the name, purchase price and number of shares.

How are you going to get the input from the user?

Next, you will simulate the fluctuation of stock prices by increasing or decreasing the stock price randomly.

Here's the random stuff again.

After adjusting the price, print the stock name, purchase price, number of shares and current price.

Printing. That's pretty easy.

The main() method should also calculate and display the value of the stock by multiplying the number of shares by the current price, how much it was when purchased and the gain or loss (a positive number is a gain and a negative number is a loss).

Calculations. Also pretty easy. Multiplication, addition, subtraction. Is the result greater than or less than zero?