Zum Inhalt springen
View in the app

A better way to browse. Learn more.

Fachinformatiker.de

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Problem mit einer Klasse als Vector in einen andren Klasse

Empfohlene Antworten

Hi!

ich hab ein Problem, ich habe ein Klasse Car diese Klasse befindet sich als Vector in der

Klasse Kaufvertrag. In einer 3 Klasse bestücke ich den Vector kaufvertragVector der die bestatndsteile der Klasse

Kaufvertrag beinhaltet .

Jetzt bin ich mir nicht sicher ob ich den Vector kaufvertragVector ( der den Vector car ( inhalt der Klasse Car) beinhaltet)

richtig fülle und dann auch anzeige.

ich habs mal versucht und es wird mir immer nur 1 Auto angezeigt wenn ich den Vector kaufvertragVector anzeige, obwohl

ich 2 Autos eingebe.

Könnt ihr mir dabei vielleicht weiterhefen?

Der 1 Code zeigt den code den ich zum füllen des Vectors kaufvertragVector benutze.

Der 2 Code zeigt wie ich den Inhalt des Vectors kaufvertragVector auf den screen wiedergee.

In einen von den 2 Codes ist min. 1 fehler bloss ich komm nicht drauf was ich falsch machen könnte.

MfG

gicio


        try

        {

            boolean repeat1 = true;

            while(repeat1) 

            {

                try

                {

                    System.out.print("Bitte geben Sie die ID des Auto ein.\n");

                    tastaturEingabe1 = eingabe.readLine();


                    for (e = autoVector.elements() ; e.hasMoreElements() ; )

                    {

                        car = (Car)e.nextElement();


                        if (tastaturEingabe1.equals("HM"))

                        {

                            display();

                        }

                        else if (tastaturEingabe1.length() == 0)					

                        {

                            throw new IOException();

                        }

                        else if (tastaturEingabe1.startsWith("-"))					

                        {

                            throw new IOException();

                        }

                        else if (new Integer(car.id()).toString().equals(tastaturEingabe1))

                        {

                            kaufver.addCar(car);

                            kaufver.setGesamtpreis(car.preis());


                            repeat1=false;

                        }

                    }

                }


                catch(IOException e)

                {

                    System.out.println("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nSie haben eine Falscheingabe gemacht!\n\n\n");

                }

            }         


            try

            {

                System.out.println("Moechten Sie ein 2 Auto Hinzufuegen?  (Ja = j, Nein = n):");

                tastaturEingabe1 = eingabe.readLine();

                tastaturEingabe1 = tastaturEingabe1.trim();


                if (tastaturEingabe1.equals("HM"))

                {

                    display();

                }

                else if (tastaturEingabe1.equals("j"))

                {

                    try

                    {

                        System.out.print("Bitte geben Sie die ID des 2 Auto ein.\n");

                        tastaturEingabe1 = eingabe.readLine();


                        for (e = autoVector.elements() ; e.hasMoreElements() ; )

                        {

                            Car car1 = new Car();

                            car1 = (Car)e.nextElement();


                            if (tastaturEingabe1.equals("HM"))

                            {

                                display();

                            }

                            else if (tastaturEingabe1.length() == 0)					

                            {

                                throw new IOException();

                            }

                            else if (tastaturEingabe1.startsWith("-"))					

                            {

                                throw new IOException();

                            }

                            else if (new Integer(car1.id()).toString().equals(tastaturEingabe1))

                            {


                                kaufver.addCar(car1) ;

                                //kaufver.setGesamtpreis(car.preis());

                            }

                        }

                    }

                    catch(IOException e)

                    {

                        System.out.println("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nSie haben eine Falscheingabe gemacht!\n\n\n");

                    }

                }

                else

                {

                    throw new Exception();

                }

            }

            catch(IOException e)

            {

                System.out.println("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nSie haben eine Falscheingabe gemacht!\n\n\n");

            }


        }

        catch(Exception e)

        {

            System.out.println("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nENDE DER BESTELLUNG!!\n\n\n\n\n");

        }

Code 2:

    public void AnzeigeKaufvertraegen()

    {

        Kaufvertrag kaufver = new Kaufvertrag();


        try

        {

            BufferedReader eingabe = new BufferedReader(new InputStreamReader (System.in) );



            if(kaufvertragVector.isEmpty()) 

            {

                System.out.println("\n\n\t\t\tDIE KAUFVERTRAEGEDATENBANK IST LEER!!!");

                tastaturEingabe1 = eingabe.readLine();

                InhaltsAnzeige();

            }

        }

        catch (IOException es)

        {

            System.out.println("Sie haben eine Falscheingabe gemacht");

        }


        for (e = kaufvertragVector.elements() ; e.hasMoreElements() ; )

        {

            kaufver = (Kaufvertrag)e.nextElement();

            System.out.println("Name                            : " + kaufver.pers().name());

            System.out.println("Strasse                         : " + kaufver.pers().address().street() + " " + kaufver.pers().address().streetnr());

            System.out.println("Ort                             : " + kaufver.pers().address().plz() + " " + kaufver.pers().address().city());

            System.out.println("Telefonnummer                   : " + kaufver.pers().tel());

            System.out.println("Alter                           : " + kaufver.pers().age());


            for ( e = kaufver.autos().elements(); e.hasMoreElements() ;   )

            {

                Car a = new Car();

                a = (Car)e.nextElement();


                System.out.println("Auto - Hersteler              : " + a.hersteler() );

                System.out.println("Auto - Model                  : " + a.model());

                System.out.println("Auto - Hubraum                : " + a.hubraum());

                System.out.println("Auto - Farbe                  : " + a.farbe());

                System.out.println("Auto - Ps - Zahl              : " + a.ps());

                System.out.println("Auto - Preis                  : " + a.preis() + " DM");

                System.out.println("Auto - Auto ID                : " + a.id());

            }

        }




Ps.: bin für jede hilfe dankbar!!! thx!!!


:WD

hier ist noch die Klasse Kaufvertrag:


// Datei Kaufvertrag.java


import java.util.*;

import java.text.*;



public class Kaufvertrag

{

    private Vector autos;

    private Person pers;

    private int id;

    private int gesamtpreis;

    private Calendar datum;


    public Kaufvertrag()     

    {

        super();

        autos       = new Vector();

    }


    public Kaufvertrag(Person p_pers, int p_id, int p_gesamtpreis, Calendar p_datum)

    {

        super();

        pers        = p_pers;

        id          = p_id;

        gesamtpreis = p_gesamtpreis;

        datum       = p_datum;

        autos       = new Vector();

    }

    // getter und setter


    public void addCar(Car c)

    {

        autos.addElement(c);

    }


    public void removeCar(Car c)

    {

        autos.remove(c);

    }


    public Vector autos()

    {

        return autos;

    }


    public void setPers(Person p_pers)

    {

        pers = p_pers;

    }


    public Person pers()

    {

        return pers;

    }


    public void setId(int p_id)

    {

        id = p_id;

    }


    public int id()

    {

        return id;

    }


    public void setGesamtpreis(int p_gesamtpreis)

    {

        gesamtpreis = p_gesamtpreis;

    }


    public int gesamtpreis()

    {

        return gesamtpreis;

    }


    public void setDatum(Calendar p_datum)

    {

        datum = p_datum;

    }


    public Calendar datum()

    {

	return datum;

    }


}

Sei mir nicht böse, aber das ist mir alles ein wenig zu unübersichtlich. Was machst du denn, wenn du irgendwann mal 3 Autos in einem Kaufvertrag haben willst?

Diese Eingabe könntest du vielleicht noch in eine Schleife packen, statt den kompletten Teil nochmal zu kopieren.

Ansonsten habe ich so auf den ersten Blick (bzw. man braucht schon zwei, um da durchzublicken) keinen offensichtlichen Logikfehler gefunden.

DANKE!!

das mit der schleiffe kommt später.

ich will das erst testten bei max. 2 Autos.

hat sonst noch einer einen tip?

thx

gicio

welches der beiden wird denn ausgegeben? Das erste, oder das zweite ???

nur das erste.

bin immer noch nicht weiter.......

:confused: :confused: :confused:

hat einer ne idee?

thx

gicio

Archiv

Dieses Thema wurde archiviert und kann nicht mehr beantwortet werden.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.