I have two classes, one for articles and another for the cart. the cart consists of an object-array, filled with articles.
I need to sum up the prices of the articles with a foreach-loop within the constructor. When i write the method (that is probably wrong) within the constructor than its type is not accepted as double. How can i sum up fields of objects within an object-array and how do i do this inside a constructor (<- this looks like a bad design decision, but it is part of may class work). I use eclipse.
Here are my classes:
package org.teubler.sucks.aufgaben;
public class Artikel {
public enum Warengruppe{
A, B, C, S
}
String name;
double verkaufspreis;
Warengruppe Art;
Artikel(String name, double preis){
this.name = name;
this.verkaufspreis = preis;
this.Art = Warengruppe.S;
}
public double getVerkaufspreis() {
return verkaufspreis;
}
public void setWarengruppe(Warengruppe Art) {
switch(Art){
case A:Art = Warengruppe.A;
case B:Art = Warengruppe.B;
case C:Art = Warengruppe.C;
default: Art = Warengruppe.S;
}
}
}
second class
package org.teubler.sucks.aufgaben;
import java.util.Random;
public class Warenkorb {
String kunde;
Artikel artikelliste[];
int sessionid;
Random s = new Random();
Warenkorb(String kunde, Artikel[] artikel){
this.kunde = kunde;
this.artikelliste = artikel;
this.sessionid = s.nextInt();
public double gesamtpreis(){
double summe = 0;
for(Artikel preis : artikel){
summe += artikel.getVerkaufspreis();
}
return summe;
}
}
}