I'm working on an assignment where we are to use a deque to manage a "stock portfolio". Basically, commands are read in from a file where an operation is given, a value per share, and a total number of shares are given. We have to obviously read in the stock information and save it onto the deque. However, the catch is that I cannot use the deque as a queue (which would obviously make this program very simple). We have to come up with some sort of system to store the information and access it again.
My current thought process on this, which I know is full of flaws, but is my best attempt so far is this:
The input is read to alternate ends of the deque, when one of the objects is 'depleted' of stocks, it is removed from the deque. I guess a visualization of what I WANT to do is as follows...
1 3 5 .... 6 4 2
then when '1' becomes 'depleted...
3 5 ... 6 4 2
This is what I WANT to happen, but I don't think it's possible without sorting (and this would be a pain in the butt to program a sorter to do that...)
However, I think the way it's actually going to happen is...
5 3 1 .... 2 4 6
Can anyone give me some advice on a better design? I have been tinkering around with this idea for several hours now trying to come up with a better idea, but this is honestly the only use I can think of. I can see how deques would be useful if you wanted to cancel an operation before it was executed, or if you wanted to place priority on an item and have it executed before anything else, but I really don't see how this would be implemented in a simple first in, first out implementation that we have to do essentially...
Now, going through all the transactions we can state that we currently have 7 A and that net cash flow has been -15. This, however, means that the transactions are stored and getting current status requires iterating over the whole list.
On the other hand one could only keep track of the count and cash so that there is only one entry for stock A that gets updated:
1 2 3 4
That sounds more like a std::map<stockname, pair<count,cash>>, but of course a list or deque could do (bit better than a vector for "depletions").
Sorry, I should have been more specific last night with the input (it was my last post before heading to bed). My question was more about using a deque in a unique way rather than actually implementing this program, I should have made that more clear.
The file input comes in this form...
1 2 3
BUY 0.25 100
BUY 1.25 25
SELL 2.25 110
This is supposed to result in output saying something like...
1 2 3
Bought 100 shares at $0.25
Bought 25 shares at $1.25
Sold 110 shares at $2.25 for a profit of $210.00
The way I'm planning on storing this information is just making a struct called "Stock" with two variables: numShares, valShares. (This will store the number of stocks bought and the original value of each stock). When a "BUY" function is read in, the data will be saved to a struct and then put on the deque. When a "SELL" function is read in, the information will be read to a "operator" object which will perform math on the appropriate deque object, removing it if necessary and finishing the operation on the next object(s).
I have the logic already figured out for this program, it should be fairly straight forward, I just can't think of any other way to use the deque other than the obvious use of "first in, first out".
So selling the 110 will use all 100 from the first buy (removing that entry) and 10 from the second buy (modifying that entry), leaving 15 shares at $1.25 as the only entry in the deque? Calculating profit/loss would deduct the purchase cost(s) from the sale (first bought, first sold principle).
When all transactions have been processed, the deque contains remaining shares (in one or more entries).
Furthermore, a sale that would sell more shares from the portfolio than there is, would be flagged as an error?