2016-05-08 2 views
0

Если количество не положительное, оно должно быть установлено равным 0. Если цена за товар не является положительной, она должна быть установлена ​​в 0.0. Когда я ввожу отрицательное число, он продолжает устанавливать мою переменную отрицательную, а не Zero.Моя проверка класса Java не работает, почему?

Это мой пакет классов com.company;

/** 
* Created by juliodiaz on 5/7/16. 
*/ 
public class Invoice { 

    private String partNumber; 
    private String partDescription; 
    private int partQuantity; 
    private double partPrice; 

    public Invoice(String partNumber, String partDescription, int partQuantity, double partPrice) { 
     this.partNumber = partNumber; 
     this.partDescription = partDescription; 
     this.partQuantity = partQuantity; 
     this.partPrice = partPrice; 
    } 

    public String getPartNumber() { 
     return partNumber; 
    } 

    public void setPartNumber(String partNumber) { 
     this.partNumber = partNumber; 
    } 

    public String getPartDescription() { 
     return partDescription; 
    } 

    public void setPartDescription(String partDescription) { 
     this.partDescription = partDescription; 
    } 

    public int getPartQuantity() { 
     return partQuantity; 
    } 

    public void setPartQuantity(int partQuantity) { 
     this.partQuantity = partQuantity; 
    } 

    public double getPartPrice() { 
     return partPrice; 
    } 

    public void setPartPrice(double partPrice) { 
     this.partPrice = partPrice; 
    } 

    public double invoiceAmountMethod(double partPrice, int partQuantity) { 
     if (partQuantity < 0 || partPrice < 0.0) { 
      this.partQuantity = 0; 
      return partPrice * partQuantity; 
     } 
     else 
      return partPrice * partQuantity; 
    } 
} 

//Main method 
package com.company; 

public class Main { 

    public static void main(String[] args) { 
     // write your code here 

     Invoice myTruck = new Invoice("A101", "Wheels", -2, 100.00); 

     System.out.println(myTruck.getPartDescription()); 
     System.out.println(myTruck.getPartNumber()); 
     System.out.println(myTruck.getPartQuantity()); 
     System.out.println(myTruck.getPartPrice()); 

     double price = myTruck.getPartPrice(); 
     int quantity = myTruck.getPartQuantity(); 

     System.out.println("The total cost is " + myTruck.invoiceAmountMethod(price, quantity)); 

    } 
} 

ВЫВОД

Wheels 
A101 
-2 
100.0 
The total cost is -200.0 
+1

Вы поставили точки останова в своем методе? – ryekayo

ответ

1

Вы назначая:

this.partQuantity = 0; 

но вы возвращаете:

return partPrice * partQuantity; 

(параметры, которые были переданы в метамфетамин О.Д.).

вы можете это исправить, вернув:

return this.partPrice * this.partQuantity; 

и вы действительно не должны проходить какие-либо параметры этого метода.

0

Вот что вам нужно.

public double invoiceAmountMethod(double partPrice, int partQuantity) { 

    return (partQuantity < 0 || partPrice < 0.0)?0.0:partPrice * partQuantity;   

} 
Смежные вопросы