Почему это всегда возвращает false для логического значения? - PullRequest
0 голосов
/ 05 апреля 2020

Почему это всегда возвращает меня "не молотый кофе" или false для publi c логический молотый кофе? Спасибо впереди ...

Этот результат:

/*{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 ]

1 голос
/ 05 апреля 2020

Проблема в том, что вы не вызываете setGroundCoffee. Даже если вы предоставите конструктору 1, вам нужно вызвать setGroundCoffee. Элемент codeForIsGroundCoffee фактически не используется, поскольку setGroundCoffee скрывает это значение при его вызове.

Удалите codeForIsGroundCoffee и используйте только логическое значение. Затем просто установите true / false в конструкторе и методе setter.

       public Coffee(boolean groundCoffee, String name, double price, String expireDate) {
            super(name, price, expireDate);
            this.groundCoffee = groundCoffee;
        }
0 голосов
/ 10 апреля 2020

Итак, я должен признать, что вы помогли мне после всего оспаривания того факта, что у вас не было ответа на мой вопрос полностью. Код для классного кофе выглядит так:

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();
    }

}
...