OOP另一种方法,如果不工作为什么?

时间:2015-06-24 12:05:31

标签: java oop if-statement

我不知道为什么if语句在程序中不起作用。 当订单大于批量订单时,它仍然返回0。 您认为我编写的代码存在问题,如下所示?

public class ShoeStoreOrder{ //ShoeStoreOrder .java
    // private method
     private String typeofshoe ;
     private String season ;
     private double cost ;
     private int bulkOrderQuantity;
     private int discount;
     private int order;
     //constructor
     public ShoeStoreOrder(String t,String s,double c,int b,int d,int o){
     typeofshoe = t;
     season = s;
     cost = c;
     bulkOrderQuantity = b;
     discount = d;
     order = o;
     }
     //get Method
     public String gettypeofshoe(){
     return typeofshoe;
     }
     public String getseason(){
     return season;
     }
     public double getcost(){
     return cost;
     }
     public int getbulkOrderQuantity(){
     return bulkOrderQuantity;
     }
     public int getdiscount(){
     return discount;
     }
     public int getorder(){
     return order;
     }

     //set method 
     public void settypeofshoe(String t){
     typeofshoe=t;
     }
     public void setseason(String s){
     season=s;
     }
     public void setcost(double c){
     cost=c;
     }
     public void setbulkOrderQuantity(int b){
     bulkOrderQuantity=b;
     }
     public void setorder(int o){
     order=o;
     }
     //other method


     //overload method 
    public double gettotaldiscount(){
      if(order()>bulkOrderQuantity()){
      return order*cost*(discount/100);
      }
      else{
      return 0;
      }
      }
     public double gettotalamount(){
     return order*cost-gettotaldiscount();
     }
    }

2 个答案:

答案 0 :(得分:2)

orderbulkOrderQuantity是实例变量而不是方法。调用方法时使用(),而不是引用变量时使用{<1}}:

  if(order()>bulkOrderQuantity()){

将其更改为

  if(order>bulkOrderQuantity){

答案 1 :(得分:0)

当订单值等于bulkOrderQuantity:

时,您跳过了大小写
public double gettotaldiscount() {
    if (order >= bulkOrderQuantity) {
        return order * cost * discount / 100;
    } else {
        return 0;
    }
}

//检查输入和结果:

public static void main(String[] args) {
    ShoeStoreOrder sso;
    System.out.println("No discount");
    sso = new ShoeStoreOrder("vertical", "winter", 20, 5, 10, 1);
    System.out.println(sso.gettotalamount());

    System.out.println("After discount");
    sso = new ShoeStoreOrder("vertical", "winter", 20, 5, 10, 5);
    System.out.println(sso.gettotalamount());

    sso = new ShoeStoreOrder("vertical", "winter", 20, 5, 10, 50);
    System.out.println(sso.gettotalamount());
}

输出:

  • 没有折扣
  • 20.0
  • 折扣后
  • 90.0
  • 900.0