62,615
社区成员
发帖
与我相关
我的任务
分享
package teacherhome.ex6;
public class CustomerDemo {
public static void main(String[] args) {
Customer customer[]=new Customer [3];
for (int i = 0; i < customer.length; i++) {
customer[i]=new Customer();
}
Production apple=new Production("apple", 15);
Production banana=new Production("banana",20);
Production tomato=new Production("tomato",10);
customer[0].buy(apple, 10);
customer[1].buy(banana, 333);
customer[2].buy(tomato, 666);
}
}
class Customer{
String name;
String address;
String cardNumber;
double avalueNumber=1000;
static double number;
void buy(Production production,double number){
this.number=number;
if(avalueNumber>=0){
avalueNumber=production.getSumPrice();
System.out.println("可用额度: "+avalueNumber);
System.out.println("打折: "+production.getDiscountPer());
System.out.println("优惠: "+production.getDiscount());
}
else{
System.out.println("可用额度: "+avalueNumber);
System.out.println("打折:0 ");
}
}
}
class Production{
String name;
double price;
//double number;
double sumPrice;
double discount;
Production(String name ,double price){
this.name=name;
this.price=price;
}
double getSumPrice(){
return price*Customer.number*this.getDiscountPer();
}
double getDiscountPer(){
if(this.getSumPrice()<=100)
return 0.9;
else if(this.getSumPrice()<=200)
return 0.8;
else
return 0.7;
}
double getDiscount(){
return price*Customer.number-this.getSumPrice();
}
}
double getSumPrice(){
return price*Customer.number*this.getDiscountPer();
}
double getDiscountPer(){
if(this.getSumPrice()<=100)
return 0.9;
else if(this.getSumPrice()<=200)
return 0.8;
else
return 0.7;
}
你在getSumPrice()方法里调用了getDiscountPer(),而getDiscountPer()方法里又调用了getSumPrice(),死循环了,会报栈溢出错误