This commit is contained in:
louiscklaw
2025-01-31 19:15:17 +08:00
parent 09adae8c8e
commit 6c60a73f30
1546 changed files with 286918 additions and 0 deletions

View File

@@ -0,0 +1,22 @@
public class Coffee extends Drink {
protected boolean isSweet;
public Coffee(String name, int price, int volume, boolean isSweet) {
super(name, price, volume);
this.isSweet = isSweet;
}
public String toString() {
if (isSweet)
return super.toString() + ", sweet";
else
return super.toString() + ", not sweet";
}
public void setPrice(int price) {
if (price < 10)
this.price = 10;
else
super.setPrice(price);
}
}

View File

@@ -0,0 +1,19 @@
public class Drink extends Food {
protected int volume;
public Drink(String name, int price, int volume) {
super(name, price);
this.volume = volume;
}
public void setPrice(int price) {
if (price < 5)
this.price = 5;
else
this.price = price;
}
public String toString() {
return super.toString() + ", volume=" + volume;
}
}

View File

@@ -0,0 +1,25 @@
public class Food {
protected String name;
protected int price;
public Food() {
name = null;
price = 0;
}
public Food(String name, int price) {
this.name = name;
setPrice(price);
}
public String toString() {
return "name=" + name + ", price=" + price;
}
public void setPrice(int price) {
if (price >= 0)
this.price = price;
else
this.price = 0;
}
}

View File

@@ -0,0 +1,12 @@
public class TestFood {
public static void main(String s[]) {
Food f[] = new Food[4];
f[0] = new Drink("Pepsi", 7, 250);
f[1] = new Coffee("Cappuccino", 13, 200, true);
f[2] = new Drink("Orange Juice", -10, 180);
f[3] = new Coffee("Ireland", -11, 200, false);
for (int i = 0; i < f.length; i++) {
System.out.println("Food " + i + ": " + f[i]);
}
}
}