我试图制作程序,以便dicountPrice
等于相应的if语句。我不太确定
我的if声明或者我在课堂上遗漏了什么。我已经尝试过超级购买和购买这些商品。到目前为止,我已经难倒了。
示例输出:
名称:白雪公主
地址:111矮人巷
电话:555-0000
客户编号:200-A010
客户类型:首选
总购买量:2566.0
总欠款:2566.0
总折扣百分比:0.0
总欠款折扣:2566.0
但我需要折扣百分比为10
TIA
public class Customer extends Person {
protected String customerNumber, customerType;
protected double purchases;
Customer(){
super(DEFAULT_VALUE,DEFAULT_VALUE,DEFAULT_VALUE);
setCustomerType(DEFAULT_VALUE);
setPurchases(0.0);
}
Customer(String name, String address, String phone, String cusNum, String cusType, double purch){
super (name,address, phone);
customerNumber = cusNum;
customerType = cusType;
purchases = purch;
}
public void setCustomerNumber(String custNum){
customerNumber = custNum;
}
public String getCustomerNumber(){
return customerNumber;
}
public void setCustomerType(String cusType){
customerType = cusType;
}
public String getCustomerType(){
return customerType;
}
public void setPurchases(double purch){
purchases = purch;
}
public double getPurchases(){
return purchases;
}
public double getTotalOwed(){
return purchases;
}
public String toString(){
return super.toString() + "\nCustomer Number: " + customerNumber + "\nCustomer Type: " + customerType +
"\nTotal Purchases: " + purchases + "\nTotal Owed: " + purchases;
}
}
public class PreferredCustomer extends Customer {
private double discountPercent=0.0;
PreferredCustomer(){
super(DEFAULT_VALUE,DEFAULT_VALUE,DEFAULT_VALUE,DEFAULT_VALUE, DEFAULT_VALUE,0.0);
}
PreferredCustomer(String name, String address, String phone, String customerNumber, String customerType, double purchases){
super (name, address, phone, customerNumber, customerType, purchases);
}
public void setDiscountPercent(double dp){
discountPercent = dp;
}
public double getDiscountPercent(){
if (purchases >= 2000){
discountPercent = 100 * .10;
}
else if (purchases >= 1500 && purchases <2000){
discountPercent = 100 * .7;
}
else if (purchases >= 1000 && purchases <1500){
discountPercent = 100 * .6;
}
else if (purchases >=500 && purchases <1000){
discountPercent= 100 * .5;
}
else if(purchases<500){
discountPercent = 0;
}
return discountPercent ;
}
public double getTotalOwed(){
return purchases - discountPercent;
}
public String toString(){
return super.toString() + "\nTotal Discount Percent: " + discountPercent + "\nTotal Owed Minus Discount: " + this.getTotalOwed();
}
}
答案 0 :(得分:0)
从我在您的来源中看到的问题来自getDiscountPercent
;你在getter函数中进行计算和修改成员,这是一个坏主意。 getter应该只返回数据而不是修改类的内部状态。
解决方案是创建一个名为calculateDiscountPercent
的私有方法,并在修改会影响折扣百分比值的成员时在内部调用它。像这样:
private void calculateDiscountPercent(){
if (purchases >= 2000){
discountPercent = 100 * .10;
}
else if (purchases >= 1500 && purchases <2000){
discountPercent = 100 * .7;
}
else if (purchases >= 1000 && purchases <1500){
discountPercent = 100 * .6;
}
else if (purchases >=500 && purchases <1000){
discountPercent= 100 * .5;
}
else if(purchases<500){
discountPercent = 0;
}
}
从源代码中,我发现discountPercent
受其超级类purchases
成员的影响。因此,每次购买更改时,覆盖其setter函数以计算折扣。像这样:
@Override
public void setPurchases(double purch){
super.setPurchases(purch);
this.calculateDiscountPercent();
}
完整的课程看起来像:
public class PreferredCustomer extends Customer {
private double discountPercent=0.0;
PreferredCustomer(){
super(DEFAULT_VALUE,DEFAULT_VALUE,DEFAULT_VALUE,DEFAULT_VALUE, DEFAULT_VALUE,0.0);
this.calculateDiscountPercent();
}
PreferredCustomer(String name, String address, String phone, String customerNumber, String customerType, double purchases){
super (name, address, phone, customerNumber, customerType, purchases);
this.calculateDiscountPercent();
}
public void setDiscountPercent(double dp){
discountPercent = dp;
}
public double getDiscountPercent(){
return discountPercent ;
}
@Override
public void setPurchases(double purch){
super.setPurchases(purch);
this.calculateDiscountPercent();
}
private void calculateDiscountPercent(){
if (purchases >= 2000){
discountPercent = 100 * .10;
}
else if (purchases >= 1500 && purchases <2000){
discountPercent = 100 * .7;
}
else if (purchases >= 1000 && purchases <1500){
discountPercent = 100 * .6;
}
else if (purchases >=500 && purchases <1000){
discountPercent= 100 * .5;
}
else if(purchases<500){
discountPercent = 0;
}
}
public double getTotalOwed(){
return purchases - discountPercent;
}
public String toString(){
return super.toString() + "\nTotal Discount Percent: " + discountPercent + "\nTotal Owed Minus Discount: " + this.getTotalOwed();
}
}