Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 1 of 2 12 LastLast
Results 1 to 25 of 26

Thread: Array List implementation(Buying and selling)

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    25
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default 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
    Ptm3j.png

    This is my buying implementation.

    public void buy(int x, int y)
    Stocks.add(x);
    StockPrice.add(y);
    //purchase = x*y;
    numStocks = numStocks + x;


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Array List implementation(Buying and selling)

    Quote Originally Posted by ss1w View Post
    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?

  3. #3
    Junior Member
    Join Date
    Sep 2013
    Posts
    25
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default 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

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Array List implementation(Buying and selling)

    Write out the math here and I will help you translate it into code

  5. #5
    Junior Member
    Join Date
    Sep 2013
    Posts
    25
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default 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
    -------------------------------------
    Last edited by ss1w; September 8th, 2013 at 12:10 AM. Reason: added more explanation

  6. #6
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default 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

  7. #7
    Junior Member
    Join Date
    Sep 2013
    Posts
    25
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Array List implementation(Buying and selling)

    The code i have so far is the buying implementation.
    Declaration

    *
    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
    *
    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;

  8. #8
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default 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

  9. #9
    Junior Member
    Join Date
    Sep 2013
    Posts
    25
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default 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.

  10. #10
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default 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)?

  11. #11
    Junior Member
    Join Date
    Sep 2013
    Posts
    25
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default 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.
    *
    	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());
    	}

  12. #12
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Array List implementation(Buying and selling)

    Can you post the instructions, I want to be sure I fully understand what should be done

  13. #13
    Junior Member
    Join Date
    Sep 2013
    Posts
    25
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default 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.
    */

  14. #14
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default 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.

  15. #15
    Junior Member
    Join Date
    Sep 2013
    Posts
    25
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default 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 ?

  16. #16
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Array List implementation(Buying and selling)

    Quote Originally Posted by ss1w View Post
    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)

  17. #17
    Junior Member
    Join Date
    Sep 2013
    Posts
    25
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default 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)
    {


    }

  18. #18
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default 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.

  19. #19
    Junior Member
    Join Date
    Sep 2013
    Posts
    25
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Array List implementation(Buying and selling)

    That would be like my current implementation or i have reach a new level of clueless.
    *
    public void buy(int stocks, int pice) {
                    Stocks.add(stock);
    		StockPrice.add(price);
    		numStocks = numStocks + stock;

  20. #20
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Array List implementation(Buying and selling)

    In the current design you have two array lists.
    Stocks.add(stock);
    StockPrice.add(price);

    In the proposed setup, you have only one list, and add items as such:
    Stocks.add(new Transaction(qtyPurchased, pricePaidEach));

    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.

  21. #21
    Junior Member
    Join Date
    Sep 2013
    Posts
    25
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default 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.
    *
    public in sell(int qtyPurchased, int pircePardEach){
     
    Stocks.remove(part2.Transaction(qtyPurchased, pircePardEach));
     
    }

  22. #22
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default 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

  23. #23
    Junior Member
    Join Date
    Sep 2013
    Posts
    25
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default 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 ?

    *
    public void buy(int qtyPurchased, int pricePaidEach) {
    class Transaction {
    		public void xxxx()	
    			Stocks.add(void buy(qtyPurchased, pricePaidEach))
    		}
                            numStocks = numStocks + qtyPurchased;
    		}

  24. #24
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default 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

  25. #25
    Junior Member
    Join Date
    Sep 2013
    Posts
    25
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default 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 ?

Page 1 of 2 12 LastLast

Similar Threads

  1. Simple Adjacent List implementation
    By IHeartProgramming in forum Collections and Generics
    Replies: 2
    Last Post: October 1st, 2012, 04:06 PM
  2. [SOLVED] Help with Min Heap Implementation (with an array)
    By arsparfven in forum What's Wrong With My Code?
    Replies: 10
    Last Post: April 27th, 2012, 05:57 AM
  3. Array List of Array Lists working for first item but not for second.
    By javapenguin in forum Collections and Generics
    Replies: 6
    Last Post: February 15th, 2012, 05:12 PM
  4. Implementation Stack Using Array
    By rainbow9 in forum Java Programming Tutorials
    Replies: 1
    Last Post: August 20th, 2011, 09:48 AM

Tags for this Thread