首页 > 解决方案 > 为什么这总是为布尔返回false?

问题描述

为什么这总是返回我“不是地面咖啡”或公共布尔地面咖啡的错误?前面谢谢...

这个结果:

/*{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);
        }

    }


标签: javaboolean

解决方案


问题是您没有调用 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;
        }

推荐阅读