Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 221039
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T18:59:45+00:00 2026-05-11T18:59:45+00:00

I have two classes, one for articles and another for the cart. The cart

  • 0

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

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T18:59:45+00:00Added an answer on May 11, 2026 at 6:59 pm

    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();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two classes, one of which is a subclass of another, and differs
I have two classes one of which inherits from the other. Im trying to
I have two classes; one is a server, another one is a client, and
I have two classes, one which is hardware-dependent and one which is not (let's
I have two classes, one is the parent of the other. They both need
Simple Java generics question: I have two classes - one of which uses generics
I have two classes. One(Person) for getters and setters, and another(People) for compute the
I have two classes. One is a management class, which stores a bunch of
I have two classes, and want to include a static instance of one class
First I want to describe my situation briefly. I have two classes, one MainClass

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.