为什么这总是为布尔值返回false?

时间:2020-04-04 21:10:01

标签: java boolean

为什么这总是向我返回“非研磨咖啡”或对公共布尔型GroundCoffee来说为假? 在此先感谢...

此结果:

/*{Nescafe, Minas}
------------------------PRODUCTS------------------------
Coffe{ not groundCoffee Product{name=Nescafe, price=780.89, expireDate=20/06/2019}
Coffe{ not groundCoffee Product{name=Minas, price=360.19, expireDate=02/12/2018}
Nescafe, 843.36
Minas, 389.01

This is code:
*/

        public class Product {

        protected String name;
        protected double price;
        protected String expireDate;

        public Product(String name, double price, String expireDate) {
            this.name = name;
            this.price = price;
            this.expireDate = expireDate;
        }

        public double pricePlusTaxes(){
            return price;
        }

        public String getname() {
            return name;
        }

        public void setIme(String ime) {
            this.name = name;
        }

        public double getCena() {
            return price;
        }

        public void setCena(double cena) {
            this.price = price;
        }

        public String getRokTrajanja() {
            return expireDate;
        }

        public void setRokTrajanja(String rokTrajanja) {
            this.expireDate = rokTrajanja;
        }



        @Override
        public String toString() {
            return "Product{" + "name=" + name + ", price=" + price + ", expireDate=" + expireDate + '}';
        }


    }

    public class Coffee extends Product {
        public int codeForIsGroundCoffee;
        public boolean groundCoffee;

        public Coffee(int codeForIsGroundCoffee,String name, double price, String expireDate) {
            super(name, price, expireDate);
            this.codeForIsGroundCoffee = codeForIsGroundCoffee;
        }
    @Override
    public double pricePlusTaxes(){
        return price * 1.08;
    }

        public boolean isgroundCoffee() {
            return groundCoffee;
        }

        public void setGroungCoffee(int codeForIsGroundCoffee) {

            if (codeForIsGroundCoffee == 1){
                groundCoffee = true;
            }else{
                groundCoffee = false;
            }

        }

        @Override
        public String toString() {
            if(groundCoffee){
                return "Coffe{" + " ground Coffee " + super.toString();
            }
            return "Coffe{" + " not groundCoffee " + super.toString();
        }

    }

    public class Runner2 {

        private static List<Product> list = new ArrayList<>();

        public static void addProduct(Product p) {
            list.add(p);
        }

        public static void DeleteProduct(Product p) {
            list.remove(p);
        }

         public static void PrintProductName(List<Product> list) {
            System.out.print("{");
            for (int i = 0; i < list.size() - 1; i++) {
                System.out.print(list.get(i).getname()+ ", ");
            }
            System.out.println(list.get(list.size() - 1).name + "}");
        }

         public static void printListOfProducts(List<Product> lista){
             System.out.println("------------------------PROIZVODI------------------------");
             for(Product p : lista){
                 System.out.println(p);
             }
         }
         public static void printProductPrice(List<Product> lista){
             for(Product p : lista){

             System.out.printf("%s, %.2f\n", p.name, p.pricePlusTaxes());
         }
         }

        public static void main(String[] args) {

            Coffee nes = new Coffee(1, "Nescafe", 780.89, "20/06/2019");
            Coffee minas = new Coffee(2, "Minas", 360.19, "02/12/2018");



            addProduct(nes);
            addProduct(minas);


            PrintProductName(list);
            printListOfProducts(list);
            printProductPrice(list);
        }

    }


2 个答案:

答案 0 :(得分:1)

问题是您没有调用setGroundCoffee。即使为构造函数提供1,也需要调用setGroundCoffee。实际上,并未使用成员codeForIsGroundCoffee,因为调用时setGroundCoffee会隐藏该值。

删除codeForIsGroundCoffee并仅使用布尔值。然后只需在构造函数和setter方法中设置true / false。

       public Coffee(boolean groundCoffee, String name, double price, String expireDate) {
            super(name, price, expireDate);
            this.groundCoffee = groundCoffee;
        }

答案 1 :(得分:0)

因此,我必须承认,您毕竟还没有完全回答我的问题,对您有所帮助。上课咖啡的代码是这样的。

public class Coffee extends Product {
    public int codeForIsGroundCoffee;
     public int value;
     boolean groundCoffee ; 

    public Coffee(int value,String name, double price, String expireDate) {
        super(name, price, expireDate);
        this.value = value;
        this.groundCoffee = (value==1);

    }
@Override
public double pricePlusTaxes(){
    return price * 1.08;
}





    @Override
    public String toString() {
        if(groundCoffee){
            return "Coffe is {" + " ground Coffee " + super.toString();
        }
        return "Coffe{" + " not groundCoffee " + super.toString();
    }

}

或者类似这样的东西:

public class Coffee extends Product {
    public int codeForIsGroundCoffee;
     public int value;
     boolean groundCoffee  ; 

    public Coffee(int value,String name, double price, String expireDate) {
        super(name, price, expireDate);
        this.value = value;
        this.groundCoffee = gc(value);

    }

    public boolean gc(int value){
        if (value == 1){
            return true;
        }else
            return false;
    }
@Override
public double pricePlusTaxes(){
    return price * 1.08;
}





    @Override
    public String toString() {
        if(groundCoffee){
            return "Coffe is {" + " ground Coffee " + super.toString();
        }
        return "Coffe{" + " not groundCoffee " + super.toString();
    }

}