-4

Sorry, i need to clarify detailly.

I'm using Java to write simple array recording 3 times stock buying; every new car initiated is presented as new transaction made:

~~ 1 share purchased at time $100/share.

2 shares are purchased at time $200/share.

3 shares are purchased at time $300/share.

Total 6 shares in hand. ~~

How to calculate the average buying price per share ? Which continually run by keep adding new car.

package javaex;

import java.util.ArrayList;
import java.util.function.Predicate;

    public class javaExstockprice
        public static void main(String[] args){
         ArrayList<Car> al= new ArrayList();
        al.add(new Car(1,100));
        al.add(new Car(2,200));
        al.add(new Car(3,300));

        System.out.println("showsharesbuyingmethod-alltransaction = shares : buying");
        al.forEach(c->c.showsharesbuying() );
        System.out.println();
        al.removeIf(c->c.shares>1);
        System.out.print("transcation amount 1 share / below");
        System.out.println();
         al.forEach(c->c.showsharesbuying() );
        System.out.println();
    }







    class Car
    float shares;
    float buying;
    Car (float a, float b) {
        shares = a;
        buying = b;
    }

    void showsharesbuying() {
        System.out.println("showsharesbuying " + shares+ " : " + buying);
    }
Makyen
  • 31,849
  • 12
  • 86
  • 121
sally tam
  • 1
  • 4
  • What is the actual question? `How to calculate the average buying price per share ?` or how to call calculate when the list changes size – Kenneth Clark Dec 06 '17 at 06:07
  • How to add code for counting those 3transaction averge share price at first. – sally tam Dec 06 '17 at 06:15
  • I'm still not sure what the question is, but if you want to calculate something that's outside the scope of single car, I'd advise you to add you cars in something more than a simple List, which would keep track of all the 'transactions' and calculate whatever you want calculated. – Bruno Medeiros Dec 06 '17 at 06:20
  • @BrunoJCM heres the changes, hope you could help. – sally tam Dec 07 '17 at 02:47
  • Why would you want to exclude transactions with only one item? – daniu Dec 07 '17 at 15:00
  • @daniu i just want the average buying price/ share of those transactions and compare to existing stock price,see if loss or gain at moment. I dun know if thats you want to know but thanks. – sally tam Dec 08 '17 at 02:27
  • Can anyone help – sally tam Apr 01 '18 at 05:03
  • Editing Questions to improve them (e.g. clarification, adding additional information, etc.) *is encouraged*. However, editing a Question to change it into a different question which results in invalidating an Answer, is against policy on Stack Overflow. Your edit here did so. The policy is that other users should proactively revert such changes. I have done so here. You *are encouraged to [ask a new Question](//stackoverflow.com/questions/ask)*, perhaps with a link to this one for additional context. We want to help, but your new/additional issue needs to be a new Question. – Makyen Jul 01 '18 at 09:11
  • [Here is a link to the Markdown source for the question as you had edited it](https://stackoverflow.com/revisions/12ccc07a-813c-4053-b1d5-744d9dd9d56d/view-source) and [this is what it looked like](https://stackoverflow.com/revisions/47667074/11). This information may help when you [ask a new question](https://stackoverflow.com/questions/ask). – Makyen Jul 01 '18 at 09:14

1 Answers1

0

Well, trying to guess what you want, here is what comes to my mind:

import java.util.List;
import java.util.ArrayList;
import java.util.function.Predicate;

public class JavaExStockPrice {

    public static void main(String[] args){

        List<Car> al= new ArrayList();
        al.add(new Car(1,100));
        al.add(new Car(2,200));
        al.add(new Car(3,300));

        System.out.println("showsharesbuyingmethod-alltransaction = shares : buying");
        al.forEach(c->c.showSharesBuying() );
        System.out.println("Avg: " + priceAvg(al));
        System.out.println();
        al.removeIf(c->c.shares>1);
        System.out.print("transcation amount 1 share / below");
        System.out.println();
        al.forEach(c->c.showSharesBuying() );
        System.out.println("Avg: " + priceAvg(al));
        System.out.println();
    }

    static double priceAvg(List<Car> l) {
        return l.stream().mapToDouble(c -> c.buying * c.shares).average().getAsDouble() / 
                    l.stream().mapToDouble(c -> c.shares).average().getAsDouble();
    }

}



class Car {
    float shares;
    float buying;

    Car (float a, float b) {
        shares = a;
        buying = b;
    }

    void showSharesBuying() {
        System.out.println("showsharesbuying " + shares+ " : " + buying);
    }
}

Having logic on static methods like this is not a good idea, but I'm not sure if that's what you want to know.

Bruno Medeiros
  • 2,251
  • 21
  • 34