Question

I have two classes, one for articles and another for the cart. The cart consists of an object-array, which contains article objects.

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).

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

}
Was it helpful?

Solution

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

OTHER TIPS

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

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

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;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top