-1

I'm working on a program which states:

Create a Java Stock class to represent information about a stock that is bought and sold....Whenever the stock price is changed, the change in price per share is also updated. Omit the setter for the change in price per share. The change in price per share will only be set when price per share changes. Do not include a parameter for change in price in the constructor. Initialize the change to zero.

The only way I figured I could calculate the change in share price was to create a fourth variable (originalPrice), equate it to sharePrice, and subtract them. However, this is not working, and I am not sure how to create a "deep" copy for variables. Is there a way to make originalPrice a copy of sharePrice which can then be updated separately from sharePrice? Thank you for your help!

Stock class:

public class Stock {
    private String name;
    private double sharePrice;
    private double originalPrice;
    private double changeSharePrice = 0;

    /**
     * @param name
     * @param sharePrice
     * @param changeSharePrice
     */
    public Stock(String stockName, double sharePrice) {
        setName(stockName);
        setSharePrice(sharePrice);
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String stockName) {
        this.name = stockName;
    }

    /**
     * @return the sharePrice
     */
    public double getSharePrice() {
        return sharePrice;
    }

    /**
     * @param sharePrice the sharePrice to set
     */
    public void setSharePrice(double sharePrice) {
        this.sharePrice = sharePrice;
        originalPrice = sharePrice;
        changeSharePrice = sharePrice - originalPrice;
    }

    /**
     * @return the changeSharePrice
     */
    public double getChangeSharePrice() {
        return changeSharePrice;
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return "Stock Name: " + name + "/n Price Per Share: " + sharePrice + "/n Change in Price Per Share: "
            + changeSharePrice;
    }

    /*
     * Copy Constructor
     */
    public Stock(Stock other) {
        name = other.name;
        sharePrice = other.sharePrice;
        changeSharePrice = other.changeSharePrice;
    }

    /*
     * Deep copy method
     */
    public Stock copy() {
        return new Stock(name, sharePrice);
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Object#equals(java.lang.Object) Equals Method
     */
    public boolean equals(Object obj) {
        Stock other = (Stock) obj;
        if (name == other.name)
            return true;
        else
            return false;
    }
}

Here is my main:

public class tester {

    public static void main(String[] args) {
        // An object is created for Nomad Foods Limited
        Stock one = new Stock("NOMD", 10.46);
        //An object is created for Treehouse Foods
        Stock two = new Stock("THS", 69.18);        
        one.setSharePrice(4.00);
        System.out.print(one.getChangeSharePrice());
    }
}
dave
  • 11,641
  • 5
  • 47
  • 65

2 Answers2

0

Your overall logic looks sounds, but you have a problem in:

public void setSharePrice(double sharePrice) {
    this.sharePrice = sharePrice;
    originalPrice = sharePrice;
    changeSharePrice = sharePrice - originalPrice;
}

You set the originalPrice equal to the sharePrice and then you take the difference of the two. Which is of course zero.

If you re-order your statements, you should get the result you want:

public void setSharePrice(double sharePrice) {
    changeSharePrice = sharePrice - this.sharePrice;
    this.sharePrice = sharePrice;
}

This even allows you to do away with originalPrice.

PS: It's unrelated to your main issue, but your equals() method has a bug. Do not use == to compare strings, use String.equals() instead. Thus name == other.name should be name.equals(other.name).

dave
  • 11,641
  • 5
  • 47
  • 65
  • Should not the `changeSharePrice` be from the original value? (hence the name of the variable) – Scary Wombat Nov 29 '16 at 02:45
  • @ScaryWombat I'm not sure. I think the requirements are ambiguous. I understood it to be the change in price at each update, but you could also read it as the change since the original. – dave Nov 29 '16 at 02:48
  • If you are right, then your answer is the **Bees Knees** – Scary Wombat Nov 29 '16 at 02:49
0

Change your constructor to

public Stock(String stockName, double sharePrice) {
    setName(stockName);
    this.sharePrice = sharePrice;
    this.originalPrice = sharePrice;

}

Assuming that you want the changeSharePrice value to be the change from the original value, then change your setter to

public void setSharePrice(double sharePrice) {
    this.sharePrice = sharePrice;
    changeSharePrice = sharePrice - originalPrice;

}
Scary Wombat
  • 44,617
  • 6
  • 35
  • 64