This is my first time asking on this site, and got my exam tomorrow so trying to make my code great! I will link the whole class (Even though its 3 others).
This is the method, and I cant manage to remove an object from lagerplass
, adding it to hk.addVarer
works tho. The idea behind all this is that lagerplass
is were the items is stored in a store(butikk), so when people buy an item from the store and put it in handlekurv
the items gets removed from the storage(lagerplass) :
public void leggTilHandlekurv(String varenavn)
{
for (Varer a : lagerplass)
{
if (a.getVarenavn().equals(varenavn))
{
hk.addVarer(a);
lagerplass.remove(varenavn);
}
}
}
Here is the kode for the whole Butikk class
public class Butikk
{
// instance variables - replace the example below with your own
public ArrayList<Varer> lagerplass;
Handlekurv hk = new Handlekurv();
/**
* Constructor for objects of class Butikk
*/
public Butikk()
{
// initialise instance variables
lagerplass = new ArrayList<Varer>();
}
/**
* An example of a method - replace this comment with your own
*
* @param y a sample parameter for a method
* @return the sum of x and y
*/
public void nyHeadset(String lyd, String vare,int pris, String varenavn )
{
// put your code here
Headset nyHeadset = new Headset ( lyd, vare, pris, varenavn);
lagerplass.add(nyHeadset);
}
public void nyMus(String vare, int pris, String varenavn, int dpi)
{
Mus nyMus = new Mus(vare, pris, varenavn, dpi);
lagerplass.add(nyMus);
}
public void printLagerplass()
{
for (Varer vare : lagerplass)
{
vare.printDetails();
}
}
public int lagerplassSize()
{
return lagerplass.size();
}
public void fjernHeleLagerplass()
{
lagerplass.clear();
}
public void leggTilHandlekurv(String varenavn)
{
Iterator<Varer> iterator = lagerplass.iterator();
while(iterator.hasNext()) {
Varer a = iterator.next();
if (a.getVarenavn().equals(varenavn)) {
iterator.remove();
hk.addVarer(a);
}
}
}
public void printHeleHandlekurven()
{
hk.printHandlekurv();
}
}enter code here
Here is the kode for the whole Varer class
enter code here public class Varer { // Representerer merke og pris til en vare. private String vare; private String merke; private int pris; private String varenavn;
/**
* Constructor for klassen Varer
*/
public Varer(int pris, String varenavn, String vare)
{
merke = "Razor";
this.pris = pris;
this.varenavn = varenavn;
this.vare = vare;
}
public String getMerke()
{
return merke;
}
public int getPris()
{
return pris;
}
public String getVarenavn()
{
return varenavn;
}
public String getVare()
{
return vare;
}
public String getDetails()
{
return vare + ", " + merke +" " + varenavn + ", "+pris + " kr.";
}
public void printDetails()
{
System.out.println(vare + ", " + merke +" "+ varenavn + ", "+pris + " kr.");
System.out.println();
}