# Array List implementation(Buying and selling)

Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last
• September 7th, 2013, 11:04 PM
ss1w
Array List implementation(Buying and selling)
I have this question on buying and selling of equipment.

I am implementing the purchasing part with 2 array list. One to store the amount of equipment bought, the other to store the buying price.
However i am having some problem understanding the logic behind the selling part whereby i have to calculate the capital gain. Any sample coding or explanation would be greatly appreciated.

Example:
I have purchased the following equipment
300 equipment - \$10
200 equipment - \$20
100 equipment - \$30

But i might be selling them at
150 equipment - \$50
Attachment 2306

This is my buying implementation.

public void buy(int x, int y)
//purchase = x*y;
numStocks = numStocks + x;
• September 7th, 2013, 11:23 PM
jps
Re: Array List implementation(Buying and selling)
Quote:

Originally Posted by ss1w
However i am having some problem understanding the logic behind the selling part whereby i have to calculate the capital gain.

Use a class to store the item and the price.
What is it you are having problems with? working out the math or writing the code that performs the math?
• September 7th, 2013, 11:42 PM
ss1w
Re: Array List implementation(Buying and selling)
I could do the maths on paper so I guess it is more of writing the code that perform the maths :(
• September 7th, 2013, 11:43 PM
jps
Re: Array List implementation(Buying and selling)
Write out the math here and I will help you translate it into code
• September 8th, 2013, 12:08 AM
ss1w
Re: Array List implementation(Buying and selling)
Base on the example i have set i have worked out the following amount. The value purchased and sold off varies.

I have purchased the following equipment
300 equipment - \$10
200 equipment - \$20
100 equipment - \$30

i might be selling them at
150 equipment - \$50

------------------------------------
Purchased price = (100 * \$30)+(50*\$20)
selling price = 150* \$50
Capital gain = selling price - purchase price (7500-4000)
= \$3500
------------------------------------
After which i might decide to sell off another batch of equipments
200 equipment - \$50

Purchased price = (150 * \$20)+(50*\$10)
selling price = 200*\$50
Capital gain = selling price - purchase price
-------------------------------------
• September 8th, 2013, 12:15 AM
jps
Re: Array List implementation(Buying and selling)
...and the code you have so far?
..so we have a starting point, I can not just write the code for you :)
• September 8th, 2013, 12:21 AM
ss1w
Re: Array List implementation(Buying and selling)
The code i have so far is the buying implementation.
Declaration

*
Code Java:

```public void buy(int NumItems, int price) // as far as i know, my junit test pass this stage Stocks.add(NumItems); StockPrice.add(price); //purchase = item*price; numStocks = numStocks + NumItems;```

The coding part which i am stuck at. I am thinking along the link of a sublist. However it seems abit weird when it comes to implementation
*
Code Java:

```public int sell (int NumItems, int price);   sale = (int)items.subList(0,NumItems).size(); total_amount_sold = sale * price; capital_gain = total_amount_sold - purchase;```
• September 8th, 2013, 12:46 AM
jps
Re: Array List implementation(Buying and selling)
Be sure to use code tags when posting code, help can be found on the announcements page
public int sell(int x, int y)
x and y are poor variable names, they should describe the value they represent like quantity and price or something

Trying to maintain two array lists in sync is just problems waiting to happen, I would suggest a different approach

The sell method should be more generic. For example, take as parameters qtySold, sellPrice, buyPrice.. Then the method has all of the information it needs regardless of the object(s) in question. Then the method name "sell" does not seem to fit the functionality of the method. It seems to be more of a getCapitalGain type method since that is what is the information returned, and it really does not sell anything.

Post the code in a more complete (SSCCE) manner and not so much a snippet at a time
• September 8th, 2013, 01:08 AM
ss1w
Re: Array List implementation(Buying and selling)
Hi Jps, i have made the necessary correct. Wasn't aware of the code tag function. Yap. i couldnt figure out how could i keep both array list in syn, even on paper it requires a little work.
I dont really understand your suggestion on parameters. Are you suggesting that i create an int qtySold, int sellPrice and int buyPrice to store all my data?

I get what you mean on the "sell" method. But that is the way i was given and i dont really think the method name affect the result.
• September 8th, 2013, 01:23 AM
jps
Re: Array List implementation(Buying and selling)
No the method name does not change the result, it is just convention to have variable names describe the value they hold, and method names describe their purpose. It is in an attempt to make the code easier for people to read.

My suggestion on the parameters is because I am looking at the samples in post #5, where 200 items were sold, it looks like they were purchased at two different prices [Purchased price = (150 * \$20)+(50*\$10) ]. To make the method fit the case where the purchase prices vary, it should be an additional parameter of the method.
So the method would have three parameters instead of two. This way the method will fit any sale, no matter what the purchase price, sell price, and qty sold. It is hard to tell exactly because the code provided so far has just been snippets, the driver/test part is missing.

Again on the double array lists, come up with a new approach. I do not know what data structures you have studied so far, and there are many possible ways to handle it in an object oriented manner. It seems the data structure may be the point of this assignment, so likely something brought up recently (just my guess)?
• September 8th, 2013, 01:37 AM
ss1w
Re: Array List implementation(Buying and selling)
We have cover linked list, array list, doubly linked list, stacks and queues. For this assignment we were told that we can use any linear data structure in java.util, thus i choose array list (queue) as it seems to be the easiest to be implemented.

If its just simply buying and selling the same amount, i guess it would have gone well. I didnt expect it to ask me to return the capital gain :(

This is the test part.
*
Code Java:

``` public void basicTest() { Portfolio p = new Portfolio(); Assert.assertEquals("Number of stocks incorrect.", 0, p.stocksHeld()); p.buy(100, 20); p.buy(20, 24); p.buy(200, 36); Assert.assertEquals("Number of stocks incorrect.", 320, p.stocksHeld()); Assert.assertEquals("Capital gain incorrect", 940, p.sell(150, 30)); Assert.assertEquals("Number of stocks incorrect.", 170, p.stocksHeld()); Assert.assertEquals("Capital gain incorrect", 400, p.sell(100, 40)); Assert.assertEquals("Number of stocks incorrect.", 70, p.stocksHeld()); }```
• September 8th, 2013, 01:51 AM
jps
Re: Array List implementation(Buying and selling)
Can you post the instructions, I want to be sure I fully understand what should be done
• September 8th, 2013, 01:57 AM
ss1w
Re: Array List implementation(Buying and selling)
This is all the instruction that is given.

Your job is to implement the class skeleton efficiently according to its
specification in that file.
You may use the linear data structures in java.util from the Java 7 SE API in your
implementation (e.g. ArrayList, LinkedList, ArrayDeque etc.), but no other libraries should be
used. (It is not necessary and makes marking hard.)
Any extra classes that you write should be included as
private nested classes. Any additional class methods should also be private.

/**
* This class is used to represent a portfolio of company stocks. It keeps track
* of the purchase price of each individual stock held, and the order in which
* they were purchased. Stocks may be purchased and added to the portfolio via
* the buy operation, and removed from the portfolio using the sell operation.
* The sell operation calculates the capital gain (or loss) from the sale.
*/
• September 8th, 2013, 02:34 AM
jps
Re: Array List implementation(Buying and selling)
The instructions are not clear that the first purchased are to be the first sold or not, so that could be up for debate.
So it seems to me I would make a class to represent a transaction, and store these transactions in the linear data structure of your choice.
For example, qty was purchased at purchasePrice, create an object that holds that data, add that data to the set.
Then (by which ever means you choose) decide which to sell first. It seems this part was purposely left open so you could implement a stack or queue etc...
How ever you pick what to sell first, grab that transaction (front or back of the set) and get the purchasePrice from that object, and if you need to sell more items, grab the next one in line to use the correct purchase price for them.

I would start with a clear definition of how you will implement this first, and worry about the code later.
• September 8th, 2013, 02:53 AM
ss1w
Re: Array List implementation(Buying and selling)
No. The selling part is not debatable.
Oh dear. I left out a small footnote at the bottom. I have to implement it in a queue and not as a stack.

@post: The oldest q stocks held are each sold for price p, and the
* capital gain (or loss) from the sale is returned. The capital gain
* (or loss) is calculated to be the total selling price of the
* stocks (q*p) minus the total value of the stocks being sold when
* they were originally purchased.

I was working base on a array list queue idea thus the first stock in would be the first stock sold. Are you suggesting that i am to create an ArrayList storing both the number and the price together instead of 2 ArrayList ? Or are we exploring other data structure options ?
• September 8th, 2013, 04:41 AM
jps
Re: Array List implementation(Buying and selling)
Quote:

Originally Posted by ss1w
Are you suggesting that i am to create an ArrayList storing both the number and the price together instead of 2 ArrayList ? Or are we exploring other data structure options ?

Creating a data type (new class) to store the data is much better practice than trying to maintain two different array lists in sync. What would happen if one list did not get updated?...Both lists become trash. Encapsulate the data in a type, as those pieces of information go hand in hand (qty purchased & price paid each).
I would do it a totally different way, but the instructions prevent that specific format, you have to use what you were allowed to use, and I think the most OO approach here is to make that class. So yes you can call it "other data structure options" which will store both the qty and price in an ArrayList... the ArrayList holds items of type (what ever class you make to hold the qty & price, called "Transaction" in my example, call it what you want)
• September 8th, 2013, 05:45 AM
ss1w
Re: Array List implementation(Buying and selling)
Ok you lost me there on the implementation part. Are you talking about this ?

Private class Transaction(Int Stocks, Int Price)
{

}
• September 8th, 2013, 05:54 AM
jps
Re: Array List implementation(Buying and selling)
Something like that should work, ya. This way you end up with a single unit to store in the queue, and an easy way to get the purchasePrice associated with each purchase on an individual basis, and only one list to maintain.
• September 8th, 2013, 06:03 AM
ss1w
Re: Array List implementation(Buying and selling)
That would be like my current implementation or i have reach a new level of clueless.
*
Code Java:

```public void buy(int stocks, int pice) { Stocks.add(stock); StockPrice.add(price); numStocks = numStocks + stock;```
• September 8th, 2013, 06:31 AM
jps
Re: Array List implementation(Buying and selling)
In the current design you have two array lists.

In the proposed setup, you have only one list, and add items as such:

This cuts out the issue of keeping two lists in sync, encapsulates the data in a neat little package (the Transaction class, or what ever you end up calling it) providing easy access when it comes time to do the math, and is a more object oriented approach.
• September 8th, 2013, 07:18 AM
ss1w
Re: Array List implementation(Buying and selling)
Haha. Alright that does clear up the confusion. Great, so now i have created a empty transaction class, linked it to my current document and test document. Also i have replaced 2 arraylist with 1 instead.

Also please bear with me, i am not familiar with calling a class from somewhere else.
After which i have proceed with the selling portion.
*
Code Java:

```public in sell(int qtyPurchased, int pircePardEach){   Stocks.remove(part2.Transaction(qtyPurchased, pircePardEach));   }```
• September 8th, 2013, 07:51 AM
jps
Re: Array List implementation(Buying and selling)
The instructions said:
Any extra classes that you write should be included as
private nested classes. Any additional class methods should also be private.
So be sure to follow that guideline
• September 8th, 2013, 08:15 AM
ss1w
Re: Array List implementation(Buying and selling)
I am really sorry to keep bother you like this, this is my first semester for java programming

I tried googling and i came up with such inner class. i have nested my function within the private class which is in the public void buy function. Does it look right to you ?

*
Code Java:

```public void buy(int qtyPurchased, int pricePaidEach) { class Transaction { public void xxxx() Stocks.add(void buy(qtyPurchased, pricePaidEach)) } numStocks = numStocks + qtyPurchased; }```
• September 8th, 2013, 10:02 AM
jps
Re: Array List implementation(Buying and selling)
How will the new version of the sell method look then?
Here is some reading on nested classes
• September 8th, 2013, 11:48 AM
ss1w
Re: Array List implementation(Buying and selling)
That is really great. May i check with you the maths logic behind the selling for the captial gain ?
Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last