0

This is my home work. I am trying to write a method 'buy' that allows some shares of the stock to be bought at a given price. The method takes two arguments: a number of shares as an int, and a price per share as a double. For example:

Stock myStock = new Stock("FCS");
myStock.buy(20, 3.50); // buys 20 shares at $3.50
// myStock now has 20 shares at total cost $70.00
myStock.buy(10, 2.00); // buys 10 shares at $2.00
// myStock now has 30 shares at total cost $90.00

My Code :

public static void buy(int numBuyShares, double priceBuyShares )
{
    double tempTotalCost = ((double)numBuyShares * priceBuyShares);
  1. How do I write a proper code if I want to multiply Integer by Double? Am I doing this the right way?

  2. I would like to accumulate the cost and shares, so how do I write this? Because I need to use the shares and cost for a sell method.

Thank you all . Now I need to write a method sell that allows some shares of the stock to be sold at a given price. The method takes two arguments: a number of shares as an int, and a price per share as a double. The method returns a boolean value to indicate whether the sale was successful. For example:

 // myStock now has 30 shares at total cost $90.00
boolean success = myStock.sell(5, 4.00);
// sells 5 shares at $4.00 successfully
// myStock now has 25 shares at total cost $70.00
success = myStock.sell(2, 5.00);
// sells 2 shares at $5.00 successfully
// myStock now has 23 shares at total cost $60.00

1.) How do i use the previous shares to minus of the new price and the shares method?

  • Thanks. I am a newbie here. – newbie_Roy Feb 09 '14 at 15:26
  • 1
    Is the use of `double` for the price part of the assignment? Because it's really not ideal... – Jon Skeet Feb 09 '14 at 15:27
  • First thing I noticed: check your variable names. numBuyShares != numShares, priceBuyShares != priceShares – Chris Forrence Feb 09 '14 at 15:28
  • yes, the double price is part of the assignment. – newbie_Roy Feb 09 '14 at 15:28
  • 2
    `double` works as a representation of integers [up to 2^52](http://lua-users.org/wiki/FloatingPoint) and most integer math but you may run into ugly rounding errors: [Why not use Double or Float to represent currency?](http://stackoverflow.com/questions/3730019/why-not-use-double-or-float-to-represent-currency) – zapl Feb 09 '14 at 15:32
  • Actually , I am doing Multi Thread Java Code. I am writing Stocks program. I need to have buy , sell , profit method. First Buy stock, sell stock , finally profit. – newbie_Roy Feb 09 '14 at 15:32
  • Ya, i think it is ugly to do such code. – newbie_Roy Feb 09 '14 at 15:33

2 Answers2

0
  1. There no need for explicit casting of numShares to double. The implicit auto-boxing will take care of it, as there's already double variable priceShares - double arithmetic will be used.

  2. To save the number and price of your stock, you will have to use some kind of structure. I am thinking HashMap perhaps. But it's really not good to use double as a key there so, I would create some Stock class. So the key of your HashMap can the Stock object containing price and the int is the number of said stocks in you possession.

    HashMap<Stock, int> stocks = new HashMap<Stock, int>();

Kuba Spatny
  • 26,618
  • 9
  • 40
  • 63
  • Can I write this in my Stock classs :private Queue StockList = new LinkedList(); And can I write this code my in my buy method ? Stock newStock = new Stocks(numBuyShares,priceBuyShares); StockList.add(newStock); – newbie_Roy Feb 09 '14 at 15:44
  • well i think it would be better to use a map because you might want to buy 2 shares, then decide to buy 4 more. In a map you will try it already contains the stock you're buying, if it does then just increase the count in your map. – Kuba Spatny Feb 09 '14 at 15:47
0
  1. That way of multiplying a double and an integer will work fine.

  2. To accumulate total shares you need a variable in the Stock class to keep track. For example:

    public class Stock{
        private int noOfShares = 0;
    }
    

    Then in the buy method you need to add a line to add the number of shares just bought to it:

    noOfShares += numBuyShares;
    

    Following encapsulation principles, to access this variable from outside the class, you will need a get method, ie:

    public int getNoOfShares(){
        return noOfShares;
    }
    
Chris Forrence
  • 10,042
  • 11
  • 48
  • 64
MikeCon94
  • 463
  • 1
  • 6
  • 17