Question: Calculate Stock Capital Gains/Losses using Java

Page 1 of 1

3 Replies - 2901 Views - Last Post: 08 October 2012 - 03:13 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=294684&amp;s=430cf805ff0733c784fb0bf58c05786c&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 tbtucker

Reputation: 0
• Posts: 2
• Joined: 07-October 12

Question: Calculate Stock Capital Gains/Losses using Java

Posted 07 October 2012 - 07:04 PM

Hello, I have an assignment to create a Java application that receives an input string such as "buy 100 share(s) at \$20 each;buy 50 share(s) at \$25 each;sell 135 share(s) at \$37 each". The code is supposed to return the capital gain/loss and is supposed to calculate it using a FIFO data structure. This would be calculated such as 100*17 + 35*10.

I have most of it coded, and everything is currently working up to the point of parsing each string (semi colon delimited) into the needed elements and adding them to a queue.

The problem I'm having is figuring out how to remove the elements in a way that calculates the capital gain/loss.

Does any one have any pointers of where to start with this algorithm?

Tara

Is This A Good Question/Topic? 0

Replies To: Question: Calculate Stock Capital Gains/Losses using Java

#2 Kinaces

Reputation: 78
• Posts: 230
• Joined: 04-October 12

Re: Question: Calculate Stock Capital Gains/Losses using Java

Posted 07 October 2012 - 08:18 PM

This program feels very unrealistic to me. The answer all depends on how you have your code setup.

#3 tbtucker

Reputation: 0
• Posts: 2
• Joined: 07-October 12

Re: Question: Calculate Stock Capital Gains/Losses using Java

Posted 07 October 2012 - 09:11 PM

Thank you for your reply, sorry if my post was confusing...I didn't do a very good job at explaining the problem/assignment.

I think I figured it out (I'm just getting an index error on my arraylist I'm using to track the buying and selling of shares).

If I can't figure out how to fix my code so I don't keep getting the error I will post another question.

Thanks again for taking the time to view and/or respond to my question.

Tara

#4 baavgai

• Dreaming Coder

Reputation: 6608
• Posts: 13,948
• Joined: 16-October 07

Re: Question: Calculate Stock Capital Gains/Losses using Java

Posted 08 October 2012 - 03:13 AM

So... it's just a queue.
```total = 0
for 100: equeue 20
for 50: equeue 25
for 135: total = total + ( 37 - dequeue )

```

Can't really offer more without code. Good luck.