我编写了一个程序,它从文件中读取并将信息存储在我自己制作的集合类中。我的程序工作正常,但是我想知道是否可以做些什么来改进我的程序并通过继承和其他 Java 功能防止重复代码。这是我的类(class)。我添加了注释来解释每个类的作用。
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
有四种产品,即咖啡、 donut 、三明治和汽水,我正在存储它们的订单,然后打印它们的总价格。
我正在阅读的文件示例如下:
Coffee,3,medium
Donut,7,0.89,chocolate
Pop,5,large,Splat! Cola
Sandwich,1,3.89,mystery meat,37-grain whole wheat
我的程序有点长,但我希望社区可以帮助我改进我的程序。我希望改进的是,我有 totalPrice()
方法,我在每个类中重写该方法。但如果仔细观察,coffee
类和 pop
类的属性有些相似。 donut
类和 sandwiches
类也是如此。有什么办法可以防止这些类中的代码重复吗?
我希望一切都是不言自明的,如果需要解释,我愿意提供。
最佳答案
继承有时在面向对象系统中被过度使用。一般来说,组合是一种更好的技术 - 阅读“继承与组合”。
对于这种情况,您尝试将商店中的库存商品视为订单很奇怪,而且可能没有帮助。订单具有与其关联的项目,但这些项目本身并不是真正的订单。
在这方面,您可以拥有一个具有名称和价格的类 StoreItem。您还可以允许该类具有影响价格的可选大小属性。因此,对于商店商品,您可以调用 item.getName() 和 item.getPrice()。当您构建商店商品时,您可以仅使用 namd 和价格来初始化它,或者使用具有尺寸的商品的名称、尺寸和价格来初始化它。
然后你可以有一个 Store 类,并且商店有一个商品库存 - 可用商品的列表。订单是针对一系列商品发出的,您的成本计算可能会在订单类别中进行一次。它只是循环遍历其项目列表并询问每个项目的价格。
使用此解决方案,您最终会在某处得到 Item、Store、Order 和主程序,但要将问题扩展到包括更多项目,您根本不需要添加任何新类。
关于java - 通过继承改进程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40688422/