1

I'm trying to write a stock market simulation. Sorry that I wrote the code in german, but that was easier for me... So for understanding: Aktie==stock, Boerse== stock market

In the class "Boerse" I have a HashMap with ArrayLists inside, in these ArrayLists are objects of the class "Aktie".

Now I want to access the method "getOwner()" and "setOwner()" of an object "Aktie" in the method "buy()" in this ArrayList in the HashMap, but that isn't possible. :( But why??

Here ist the Code of the class "Boerse":

import java.util.*;
public class Boerse {

    HashMap<String,ArrayList> inventory = new HashMap<String, ArrayList>(); 
    HashMap<String,User> users = new HashMap<String, User>();
    Aktie[] Apple = new Aktie[10];

    /**
     * Main Methode der Klasse Boerse
     */
    public static void main(String[] args)
    {
        Boerse b = new Boerse();
    }

    /**
     * Erzeugt ein Objekt der Klasse Boerse
     */
    public Boerse()
    {
        FillInventory();
        System.out.println(inventory.get("Apple").toString());
        //buy("Apple", "Jonas");
        System.out.println(inventory.get("Apple").toString());
    }

    /**
     * Diese Methode fuegt dem Inventar die verfuegbaren Aktien zu
     */
    public void FillInventory()
    {
        /*
        *   //Sample
        *   ArrayList<Aktie> Sample = new ArrayList<Aktie>();
        *   int nSample = 0;
        *   double vSample = 0;
        *   Aktie aSample = new Aktie("Sample", vSample);
        *   for(int i=0;i<nSample;i++)
        *   {
        *       Sample.add(aSample);
        *   }
        *   inventory.put("Sample", Sample);
        */



        //Apple
        ArrayList<Aktie> Apple = new ArrayList<Aktie>();
        int nApple = 10;
        double vApple = 50.0;
        Aktie aApple = new Aktie("Apple", vApple);
        for(int i=0;i<nApple;i++)
        {
            Apple.add(aApple);
        }
        inventory.put("Apple", Apple);



        //Google
        ArrayList<Aktie> Google = new ArrayList<Aktie>();
        int nGoogle = 15;
        double vGoogle = 64.5;
        Aktie aGoogle = new Aktie("Google", vGoogle);
        for(int i=0;i<nGoogle;i++)
        {
            Google.add(aGoogle);
        }
        inventory.put("Google", Google);

        System.out.println(inventory.toString());
    }

    private void buy(String userName, String userPassword, String aktie,String pOwner)
    {
        for(int i=0;i<inventory.get(aktie).size();i++)
        {
            if(inventory.get(aktie).get(i).getOwner()==null) //here is the problem
            {
                inventory.get(aktie).get(i).setOwner(pOwner); //here is the problem
                break;
            }
        }

    }

}

And the class "Aktie":

public class Aktie {
    public double value;
    public String name;
    public String owner = null;

    /**
     * Erzeugt ein Objekt der Klasse Aktie
     * @param pName Name der Aktie
     * @param pValue Wert der Aktie
     */
    public Aktie(String pName, double pValue)
    {
        name=pName;
        value=pValue;
    }

    /**
     * Aendert den Besitzer der Aktie
     * @param pOwner neuer Besitzer der Aktie
     */
    public void setOwner(String pOwner)
    {
        owner = pOwner;
    }

    /**
     * Gibt den Namen des Besitzers aus
     * @return
     */
    public String getOwner()
    {
        return owner;
    }
}
Jonas G
  • 23
  • 5
  • 3
    `HashMap inventory = new HashMap()` should be `Map> inventory = new HashMap<>()` – JB Nizet Oct 31 '15 at 23:00
  • 1
    try as JB Nizet specified or use type cast as below. Try using IDE's like Eclipse which gives you suggestions. It is easy way to find the options you have.. – RamPrakash Oct 31 '15 at 23:11

1 Answers1

1

try as JB Nizet specified or use type cast as below

private void buy(String userName, String userPassword, String aktie,String pOwner)
    {
        for(int i=0;i<inventory.get(aktie).size();i++)
        {
            if(((Aktie) inventory.get(aktie).get(i)).getOwner()==null) //typecast here
            {
                ((Aktie) inventory.get(aktie).get(i)).setOwner(pOwner); //typecast here
                break;
            }
        }

    }
RamPrakash
  • 1,687
  • 3
  • 20
  • 25