You are viewing our Forum Archives. To view or take place in current topics click here.

Need help with logic of projectPosted:

Status: Offline
Joined: Mar 23, 20107 Year Member
Posts: 531
Reputation Power: 20
I'm creating a project for my IT class where you "invest" a certain amount of money in stocks and you have certain things like the currentPrice, initialPrice, currentShares and things along that line that need to be kept track of. I'm a little lost on how to follow some of the logic that goes with it (mainly aggregation). I'm pretty new to programming and any help will be greatly appreciated. If you would like to help, I can send you the document with further instructions. It would be beyond greatly appreciated! (JAVA PROGRAMMING)
#2. Posted:
ip
  • 2 Million
Status: Offline
Joined: Dec 30, 20124 Year Member
Posts: 3,710
Reputation Power: 426
It depends on what the document wants you to do. Does it want to ask the user for the amount of shares they have in a certain stock? etc. The things you listed: currentPrice, initialPrice, currentShares are just variables that you can keep track of using something like this.
float currentPrice;
float initialPrice;
int currentShares;


Doing this will allow you to continue to add on when the user enters an input.
#3. Posted:
Status: Offline
Joined: Mar 23, 20107 Year Member
Posts: 531
Reputation Power: 20
ip wroteIt depends on what the document wants you to do. Does it want to ask the user for the amount of shares they have in a certain stock? etc. The things you listed: currentPrice, initialPrice, currentShares are just variables that you can keep track of using something like this.
float currentPrice;
float initialPrice;
int currentShares;


Doing this will allow you to continue to add on when the user enters an input.

can I PM you and ask you some questions?
#4. Posted:
ip
  • Gold Gifter
Status: Offline
Joined: Dec 30, 20124 Year Member
Posts: 3,710
Reputation Power: 426
Yea, no problem.
#5. Posted:
-Deano
  • Rated Awesome
Status: Offline
Joined: Aug 19, 20107 Year Member
Posts: 4,881
Reputation Power: 423
Motto: Thanks to Prodigy for the gold! | PCMR
Typically, you would have a Stock class for each individual stock object.


public class Stock {
        // Attributes
   private float initialPrice;
   private float price;
   private int currentShares;
   
        // Constructor
   public Stock(float initial, int shares) {
      initialPrice = initial;
      price = initialPrice;
      currentShares = shares;
   }
   
   // Get current price
   public float price() {
      return currentPrice;
   }
   
   // Get initial price
   public float initialPrice() {
      return initialPrice;
   }
   
   // Get current shares
   public int shares() {
      return currentShares();
   }
   
   // Set current price
   public void setPrice(float priceIn) {
      price = priceIn;
   }
   
   // Adjust current price by an amount (i.e. send in 1.78 to increase price by 1.78, send in -3.20 to decrease current price by 3.20)
   public void adjustPrice(float amount) {
      price += amount;
   }
   
   // Set current shares
   public void setShares(int amount) {
      currentShares = amount;
   }
}

public class MyStocksApp {
   public static void main(String args[]) {
      Stock stock1 = new Stock(10.00, 100);
      
      stock1.price(); // Will return 10.00
      stock1.shares(); // Will return 100
      stock1.adjustPrice(2.5); // Price will now be 12.50
   }
}


I would advise looking into ArrayLists if you are up for learning ahead of your course. They would be very helpful in an exercise like this.
Jump to:
You are viewing our Forum Archives. To view or take place in current topics click here.