tags:

views:

68

answers:

4

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

}
+1  A: 

You're trying to create an extra method within the constructor. That's not a good idea. You're also trying to index an array by an object, which won't work. Finally, you're trying to call getVerkaufspreis() on an Object, instead of a strongly-typed Artikel. Try this:

Warenkorb(String kunde, Artikel[] artikel){
    this.kunde = kunde;
    this.artikelliste = artikel;
    this.sessionid = s.nextInt();
    double summe = 0;
    for (Artikel preis : artikel) {
         summe += preis.getVerkaufspreis();
    }
}

Now by the end of the loop you'll have the sum - but what do you want to do with it? I suspect you want to create a field for it...

If you absolutely have to use an Object[] instead of an Artikel[] then you should cast on each iteration:

Warenkorb(String kunde, Object[] artikel){
    this.kunde = kunde;
    this.artikelliste = artikel;
    this.sessionid = s.nextInt();
    double summe = 0;
    for (Object preis : artikel) {
         summe += ((Artikel)preis).getVerkaufspreis();
    }
}
Jon Skeet
+1  A: 

First, you should have an array of Artikel, not Object. You can't do much with Objects.

Assuming you change artekelliste to Artikel[], the summation would go like this:

for(Artikel preis : artikelliste){
    summe += artikel.getVerkaufspreis();
}

When you use an enhanced-for loop, there is no need for explicit array indexing.

Also, you can't put a method inside a constructor. You need to pull gesamtpreis() out of the constructor, like this:

Warenkorb(String kunde, Artikel[] artikel){
    this.kunde = kunde;
    this.artikelliste = artikel;
    this.sessionid = s.nextInt();
}
public double gesamtpreis(){
    double summe = 0;
    for(Artikel preis : artikelliste){
        summe += artikel.getVerkaufspreis();
    }
    return summe;
}

As a side note, you can never use an Object as an array index; the equivalent standard-for loop would be

for(int i = 0; i < artikelliste.length; i++) {
    summe += artikelliste[i].getVerkaufspreis();
}
Michael Myers
A: 

Looking at your code, you are using a "for" instead of a "foreach" in your Warenkorb constructor.

ChrisBD
A: 

Move the summe variable out of the constructor, and do the loop without wrapping it in a function. Or, alternatively, move out the entire function gesamtpreis and call it from the constructor:

double summe;

Warenkorb(String kunde, Object[] artikel){
    this.kunde = kunde;
    this.artikelliste = artikel;
    this.sessionid = s.nextInt();
    this.summe = gesamtpreis();
}

public double gesamtpreis(){
    double _summe = 0;
    for(Object preis : artikelliste){
            _summe += artikelliste[preis].getVerkaufspreis();
    }
    return _summe;
}
Tomas Lycken