파일에서 읽고 자신의 만든 컬렉션 클래스에 정보를 저장하는 프로그램을 작성했습니다. 그러나 내 프로그램을 개선하고 상속 및 기타 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 곡물 전체 밀
내 프로그램은 약간 긴하지만, SO 커뮤니티 내 프로그램을 개선하는 데 도움이 할 수 있다면 내가 기대하고있다. 내가 개선하기 위해 찾고있는 것은, 내가 각 클래스에서 무시하고있는 totalPrice()
메서드가 있다는 것입니다. 그러나 당신이 가깝게 보면 coffee
클래스와 pop
클래스는 그 속성이 다소 비슷합니다. donut
클래스와 sandwiches
클래스도 동일합니다. 해당 클래스에서 코드의 중복을 막을 수있는 방법이 있습니까? 설명이 필요하다면 모든 것을 설명 할 수 있기를 바랍니다. 나는 그것을 기꺼이 제공 할 의향이 있습니다.
당신의 대답으로 판단하면 어떤 중요한 코드 중복이 없다는 것을 암시하고 있습니까? 당신이'StoreItem' 클래스에 대해 말한 것을 가지고 있습니다. – Saad
복제본은 확장성에 별다른 문제가 없습니다. 솔루션에 항목을 추가하려면 클래스를 추가해야합니다. 하지만이 솔루션으로 가격이 가격에 미치는 영향에 관해서는 복제본을 가지고 있습니다. 단 하나의 StoreItem 클래스 만 있으면이를 제거 할 수 있습니다. –