Hi Leute,
ich muss ein Bestellprogramm programmieren, welches den Nutzer eine Bestellung erstellen lässt und dieser Bestellung soll dann Autos hinzugefügt werden, welche nur im Attribut 'farbe' variieren.
Das ganze funktioniert fast, doch aus irgendeinem Grund überschreibt der letze Eintrag meine vorherigen im Array.
1. Klasse: Bestellprogramm:
[Java]
package app;
import java.util.Scanner;
public class BestellProgramm {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner s=new Scanner(System.in);
//Bestellung-Informationen werden abgefragt
System.out.println("Bitte geben Sie das Autohaus ein: ");
String autohaus = s.next();
System.out.println("Bitte geben Sie die maximale Autoanzahl ein: ");
int maxAutos = s.nextInt();
//Neues Objekt 'autos' wird erstellt
Bestellung autos = new Bestellung(autohaus, maxAutos);
//Auto Farbe wird abgefragt
for(int i=0;i<=maxAutos-1;i++)
{
System.out.printf("\nAuto_%s\n", i+1);
System.out.println("Gebe Farbe ein: ");
String farbe=s.next();
//Neues Auto Objekt wird erstellt
Mercedes auto=new Mercedes("Benziner", 3000, 200, farbe);
//Wird Bestellung hinzugefügt
Bestellung.addAuto(auto);
}
//Ausgabe Autohausinformationen
System.out.printf("\nBestellung - Autohaus: %s\n", autohaus);
//Array zum 'überschreiben' des Arrays in Klasse 'Bestellung' wird erstellt
Mercedes[]autosprint=new Mercedes[maxAutos];
//Erstellte Auto-Objekte werden aus dem Array in der Klasse 'Bestellung' geholt und in das erstellte Array geschrieben
autosprint=autos.getAutos();
//Ausgabe des erstellten Arrays
for(int i=0;i<=maxAutos-1;i++){
System.out.println(autosprint.toString());
}
}
}
[/Java]
2. Klasse: Bestellung
[Java]
package app;
public class Bestellung {
private static int anzAutos=0;
private static int maxAutos=0;
private static Mercedes[] autos;
private static String autohaus;
private String farbe;
//Bestellung-Konstruktor
Bestellung(String autohaus, int maxAutos){
this.autohaus=autohaus;
this.maxAutos=maxAutos;
}
//Autos hinzufügen Methode
public static Mercedes[] addAuto(Mercedes auto)
{
//Falls anzAutos<1: Neues Array für Objekte wird erstellt
if(anzAutos<1){
//maxAutos wird über getter geholt
int maxAutos=Bestellung.getMaxAutos();
//Neues Array für Auto-Objekte in der Größe maxAutos wird erstellt
autos=new Mercedes[maxAutos];
}
//Auto-Objekt wird an Stelle i im Index des Arrays gespeichert
autos[anzAutos]=auto;
//Counter wird hochgezählt
anzAutos++;
return autos;
}
//Getters
public static Mercedes[] getAutos(){
return autos;
}
public static String getAutohaus() {
return autohaus;
}
public static int getMaxAutos() {
return maxAutos;
}
}
[/Java]
3.Klasse: Mercedes:
[Java]
package app;
public class Mercedes {
private static String motor;
private static int hubraum;
private static int leistung;
private static String farbe;
Mercedes(String motor, int hubraum, int leistung, String farbe){
this.motor=motor;
this.hubraum=hubraum;
this.leistung=leistung;
this.farbe=farbe;
}
public String toString(){
return String.format("%s - Motor: %s - Hubraum in ccm: %s - Leistung: %s - Farbe: %s \n\tCKlasse - Kombi: false\n-------------------------------------------------------------------------------------------------------------------",getClass() ,motor, hubraum,leistung, farbe);
}
private static int preisBerechnen;{
}
}
[/Java]
Hab keine Ahnung was das Problem ist
Wär für jede Hilfe dankbar!
ich muss ein Bestellprogramm programmieren, welches den Nutzer eine Bestellung erstellen lässt und dieser Bestellung soll dann Autos hinzugefügt werden, welche nur im Attribut 'farbe' variieren.
Das ganze funktioniert fast, doch aus irgendeinem Grund überschreibt der letze Eintrag meine vorherigen im Array.
1. Klasse: Bestellprogramm:
[Java]
package app;
import java.util.Scanner;
public class BestellProgramm {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner s=new Scanner(System.in);
//Bestellung-Informationen werden abgefragt
System.out.println("Bitte geben Sie das Autohaus ein: ");
String autohaus = s.next();
System.out.println("Bitte geben Sie die maximale Autoanzahl ein: ");
int maxAutos = s.nextInt();
//Neues Objekt 'autos' wird erstellt
Bestellung autos = new Bestellung(autohaus, maxAutos);
//Auto Farbe wird abgefragt
for(int i=0;i<=maxAutos-1;i++)
{
System.out.printf("\nAuto_%s\n", i+1);
System.out.println("Gebe Farbe ein: ");
String farbe=s.next();
//Neues Auto Objekt wird erstellt
Mercedes auto=new Mercedes("Benziner", 3000, 200, farbe);
//Wird Bestellung hinzugefügt
Bestellung.addAuto(auto);
}
//Ausgabe Autohausinformationen
System.out.printf("\nBestellung - Autohaus: %s\n", autohaus);
//Array zum 'überschreiben' des Arrays in Klasse 'Bestellung' wird erstellt
Mercedes[]autosprint=new Mercedes[maxAutos];
//Erstellte Auto-Objekte werden aus dem Array in der Klasse 'Bestellung' geholt und in das erstellte Array geschrieben
autosprint=autos.getAutos();
//Ausgabe des erstellten Arrays
for(int i=0;i<=maxAutos-1;i++){
System.out.println(autosprint.toString());
}
}
}
[/Java]
2. Klasse: Bestellung
[Java]
package app;
public class Bestellung {
private static int anzAutos=0;
private static int maxAutos=0;
private static Mercedes[] autos;
private static String autohaus;
private String farbe;
//Bestellung-Konstruktor
Bestellung(String autohaus, int maxAutos){
this.autohaus=autohaus;
this.maxAutos=maxAutos;
}
//Autos hinzufügen Methode
public static Mercedes[] addAuto(Mercedes auto)
{
//Falls anzAutos<1: Neues Array für Objekte wird erstellt
if(anzAutos<1){
//maxAutos wird über getter geholt
int maxAutos=Bestellung.getMaxAutos();
//Neues Array für Auto-Objekte in der Größe maxAutos wird erstellt
autos=new Mercedes[maxAutos];
}
//Auto-Objekt wird an Stelle i im Index des Arrays gespeichert
autos[anzAutos]=auto;
//Counter wird hochgezählt
anzAutos++;
return autos;
}
//Getters
public static Mercedes[] getAutos(){
return autos;
}
public static String getAutohaus() {
return autohaus;
}
public static int getMaxAutos() {
return maxAutos;
}
}
[/Java]
3.Klasse: Mercedes:
[Java]
package app;
public class Mercedes {
private static String motor;
private static int hubraum;
private static int leistung;
private static String farbe;
Mercedes(String motor, int hubraum, int leistung, String farbe){
this.motor=motor;
this.hubraum=hubraum;
this.leistung=leistung;
this.farbe=farbe;
}
public String toString(){
return String.format("%s - Motor: %s - Hubraum in ccm: %s - Leistung: %s - Farbe: %s \n\tCKlasse - Kombi: false\n-------------------------------------------------------------------------------------------------------------------",getClass() ,motor, hubraum,leistung, farbe);
}
private static int preisBerechnen;{
}
}
[/Java]
Hab keine Ahnung was das Problem ist
Wär für jede Hilfe dankbar!