[java新手]我的程序那里有错误?是static不对吗?

only_one_moment 2015-10-24 09:38:32
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();
}
}

实验内容
编程实现一个模拟的用户消费信用卡的示例,在其中用户Customer类具有用户名、地址、卡号、消费额度、可以享受的打折度等多种个人信息;而CustomerDemo类中建立3个消费者,对其分别进行操作,模拟其行为。


实验目的
在前几节介绍了面向对象的基本概念,包括类、对象、属性以及方法。在这个基础上,用两个面向对象的简单程序,继续让读者充分体会面向对象编程的方法。


实现思路
简单模拟了一个商店客户折扣卡的功能,自定义Customer类用来保存在某个商店中的客户的折扣卡信息。在主类CustomerDemo中,创建Customer类的一个数组对象,该数组中包含了三个Customer的对象,用来保存三个不同的消费者各自持有的折扣卡信息。通过这三个对象,可以根据用户消费的金额来改变用户在本店中所能享受的折扣价格。
...全文
165 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
应该customer类里有个card类,就相当于人持有1个信用卡 customer类还要有个付款的功能(方法),这个方法应该有个参数(购买的物品的价格),付款的时候就要调用信用卡,查看信用卡的信息,对其额度、折扣进行调整,最后要返回这次消费的实际要付款的金额,或则打印出这次消费的信息。
  • 打赏
  • 举报
回复
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(),死循环了,会报栈溢出错误
dreamhyz 2015-10-24
  • 打赏
  • 举报
回复
首先你要把错误点说出来,这样才方便我们去看
only_one_moment 2015-10-24
  • 打赏
  • 举报
回复
引用 3 楼 lonrence 的回复:
应该customer类里有个card类,就相当于人持有1个信用卡 customer类还要有个付款的功能(方法),这个方法应该有个参数(购买的物品的价格),付款的时候就要调用信用卡,查看信用卡的信息,对其额度、折扣进行调整,最后要返回这次消费的实际要付款的金额,或则打印出这次消费的信息。
谢谢你我改好了
only_one_moment 2015-10-24
  • 打赏
  • 举报
回复
引用 1 楼 dreamhyz 的回复:
首先你要把错误点说出来,这样才方便我们去看
我就是不知道哪里错了才问的,,,
only_one_moment 2015-10-24
  • 打赏
  • 举报
回复
引用 3 楼 lonrence 的回复:
应该customer类里有个card类,就相当于人持有1个信用卡 customer类还要有个付款的功能(方法),这个方法应该有个参数(购买的物品的价格),付款的时候就要调用信用卡,查看信用卡的信息,对其额度、折扣进行调整,最后要返回这次消费的实际要付款的金额,或则打印出这次消费的信息。
好的,谢谢

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧