我已經編寫了一個程序,該程序從文件讀入並將信息存儲在我自己創建的集合類中。我的程序工作正常,但是我想知道是否有任何事情可以改善我的程序並通過繼承和其他Java功能防止重複的代碼。這是我的課程。我添加了評論來解釋每個班級的工作。通過繼承改進程序
abstract class Order { //superclass
private int quantity; //instance variables
public Order(int quantity) { //constructor
this.quantity = quantity;
}
public int getQuantity() { // instance method
return quantity;
}
public abstract double totalPrice();
public String toString() {
return "quantity: " + quantity;
}
} //super Class Order
class Coffee extends Order { //subclass
private String size; //instance variables
public Coffee (int quantity, String size) { //constructor
super(quantity);
this.size = size;
}
public double totalPrice() { //instance method to calculate price for the item
double priceSmall = 1.39;
double priceMed = 1.69;
double priceLar = 1.99;
double total = 0;
if (size.equals("small")) {
total = priceSmall * getQuantity();
} else {
if (size.equals("medium")) {
total = priceMed * getQuantity();
} else {
if(size.equals("large")) {
total = priceLar * getQuantity();
}
}
}
return total;
} //totalPrice
public String toString() {
return "Coffee ("+ size + "): " + super.toString() ;
}
} //coffee sub-class
class Donuts extends Order { //sub-class
private double price; //instance variables
private String flavour;
public Donuts(int quantity, double price, String flavour) { //constructor
super(quantity);
this.price = price;
this.flavour = flavour;
}
public double totalPrice() { //instance method to calculate price
double total = 0;
int quantity = getQuantity();
if(quantity < 6) {
total = (price * quantity);
double tax = 0.07 * total;
total += tax;
} else {
total = price * quantity;
}
return total;
} //totalPrice
public String toString() {
return "Donuts("+ flavour + "): " + super.toString() + ", price: " + price;
}
} //class Donuts
class Sandwich extends Order { //Sub-class
private double price; // instance variables
private String filling;
private String bread;
// constructor
public Sandwich (int quantity, double price, String filling, String bread) {
super(quantity);
this.price = price;
this.filling = filling;
this.bread = bread;
}
public double totalPrice() { //instance method
double total = 0;
int quantity = getQuantity();
total = (price * quantity);
double tax = 0.07 * total;
total += tax;
return total;
} //totalPrice
public String toString() {
return "Sandwich ("+ filling + ") (" + bread + "): "+ super.toString() +
", price: " + price ;
}
} // Sandwich class
class Pop extends Order { //sub-class
private String size;
private String brand;
public Pop(int quantity, String size, String brand) { //constructor
super(quantity);
this.size = size;
this.brand = brand;
}
public double totalPrice() { //instance method
double priceSmall = 1.79;
double priceMed = 2.09;
double priceLar = 2.49;
double total = 0;
if (size.equals("small")) {
total = priceSmall * getQuantity();
} else {
if (size.equals("medium")) {
total = priceMed * getQuantity();
} else {
if(size.equals("large")) {
total = priceLar * getQuantity();
}
}
}
return total;
} //totalPrice
public String toString() {
return "Pop ("+ brand + ") (" + size + "): " + super.toString() ;
}
} // class Pop
有四種產品,即咖啡,甜甜圈,三明治和砰砰聲的訂單我存儲,然後打印其總價格。
我正在讀的文件的示例是這樣的:
咖啡,3,介質
甜甜圈,7,0.89,巧克力
流行,5,大,圖示!可口可樂
三明治,1,3.89,神祕的肉,37粒全麥
我的計劃是一點點長,但我希望如果是這樣的社會可以幫助我提高我的程序。我正在尋求改進的是,我有totalPrice()
方法,我在每個班級壓倒一切。但是,如果仔細觀察coffee
類和pop
類的屬性有點相似。 donut
班和sandwiches
班同樣如此。有沒有辦法可以防止這些類中的代碼重複? 我希望一切都是不言自明的,如果有需要的解釋我願意提供。
根據您的回答判斷您是否暗示我沒有任何重要的代碼重複?儘管如此,我明白了你對StoreItem類的看法。 – Saad
嗯,沒有重複不是可擴展性的問題 - 要添加項目與您的解決方案,您需要添加類。儘管這種解決方案對於價格的大小如何影響價格,但確實存在重複,只需要擁有一個StoreItem類即可消除。 –