Ich kann nicht herausfinden, warum mein Code nicht registriert, dass es eine maximale Größe für das Objekt gibt. Ich dachte, vielleicht war es aufgrund der Tatsache, dass die unteren Variablen der Minimal- und Maximalwerte können außer Kraft setzen, aber es nichtErstellen eines Schuhobjekts
public class Shoes {
private static final int MIN_SIZE = 1;
private static final int MAX_SIZE = 15;
private String brand;
private double price;
private int size;
public Shoes(String brand, double price, int size) {
this.brand = brand;
this.price = price;
this.size = size;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
if (price < 0) {
System.out.println("Price Must be greater than zero!\n");
return;
}
this.price = price;
}
public int getSize() {
return size;
}
public void setSize(int size) {
if (size > MAX_SIZE && size < MIN_SIZE) {
System.out.println("Invalid Size!\n");
}
}
@Override
public String toString() {
return "Shoe [brand = " + brand + ", price = " + price + ", size = " + size + "]";
}
public static void main(String[] args) {
Shoes myShoes = new Shoes("J.F.", 45.99, 10);
Shoes otherShoes = new Shoes("Addidas", 65.99, 16);
System.out.println("The shoes: ");
System.out.println(myShoes.toString());
System.out.println("Other Shoes: ");
System.out.println(otherShoes.toString());
}
}
Die othershoes als eine ungültige Größe registrieren soll zu helfen scheint. aber es läuft nur der Code als normal und gibt den ungültigen Text überhaupt nicht aus, ich verstehe nicht warum.
Sie haben keine Größenprüfung im Konstruktor, nur in der 'setSize()' Methode, die Sie nicht verwenden. Zusätzlich sollte '||' (oder) nicht '&&' (und) verwendet werden. – Kayaman