Bad Operand types for binary operator

-1

I have to do an exercise for the sales-related faculty and in the client class I need to apply a + do aCommerce method.

Detail, I have done so far only part "a", put everything in case it helps in solving the problem.

code in the Client class:

public void fazUmaCompra(Compra preco){
    if (preco > 300){
        if (pontos > 2000){
            preco = preco - (preco * 0.05);
            pontos = 0;
        }    
        else if (pontos > 1000){
            preco = preco - (preco * 0.03);
            pontos = pontos -preco / 1;
        }    
        else if (pontos > 500){
            preco = preco - (preco * 0.02);
            pontos = pontos -preco / 1;
        }
        else
            preco = preco;  
}
}

EDIT: CLASS PURCHASE

public class Compra{
    private String numero;
    private Cliente cliente;
    private Vendedor vendedor;
    private double precoOriginal;
    private double desconto;
    private double preco;

    public Compra(double pO, double ds, double pF, Cliente client){        
        cliente = client;
        String s = cliente.getNome();
        String sub = s.substring(0, 3);
        String num = String.valueOf((int)(Math.random()*10000+100));
        numero = num + sub;
        precoOriginal = pO;
        desconto = ds;
        preco = pF; 
    }
    public Compra(double prO, double dst, double prF, Cliente client, Vendedor vend){
        cliente = client;
        String s = cliente.getNome();
        String sub = s.substring(0, 3);
        String num = String.valueOf((int)(Math.random()*10000+100));
        numero = num + sub;
        precoOriginal = prO;
        desconto = dst;
        preco = prF;
        vendedor = vend;
    }
    public void impoeDesconto(double desct){

    }
    public void setVendedor(Vendedor vend){
        vendedor = vend;
    }
    public void emiteNotaFiscal(){
        System.out.println("Número da Compra: " + numero);
        System.out.println("Nome do Cliente: " + this.cliente.getNome());
        if (vendedor = null)
            vendedor = vendedor;
        else    
        System.out.println("Nome do Vendedor: " + this.vendedor.getNome());
        System.out.println("Preço Inicial: " + precoOriginal);
        System.out.println("Valor do desconto: " + desconto);
        System.out.println("Preço Pago: " + preco);
    }
    public double getPreco(){
        return preco;
    }
}

EDIT: CLIENT CLASS

   private String nome;
    private Endereco endereco;
    private int pontos;

    public Cliente(String nm, Endereco ed){
        nome = nm;
        endereco = ed;
        pontos = 0;
    }
    public Cliente(String nm){
        this.nome = nm;
        this.endereco = new Endereco();
    }
    // alteraEndereco funcionamento errado
    public void alteraEndereco(){
        this.endereco.altera();
    }
    public void fazUmaCompra(Compra preco){
        if (preco.getPreco() > 300){
            if (pontos > 2000){
                preco.getPreco() = preco.getPreco() - (preco.getPreco() * 0.05);
                pontos = 0;
            }    
            else if (pontos > 1000){
                preco.getPreco() = preco.getPreco() - (preco.getPreco() * 0.03);
                pontos = pontos -preco / 1;
            }    
            else if (pontos > 500){
                preco.getPreco() = preco.getPreco() - (preco.getPreco() * 0.02);
                pontos = pontos -preco / 1;
            }
            else
                preco = preco;  
    }
    }
    public String getNome(){
        return nome;
    } 
}

The error occore on line:

if (preco > 300){

bad operand types for binary operator '>'

    
asked by anonymous 13.04.2014 / 17:21

2 answers

2

Compra is a class, and preco is a reference variable that references an object of class Compra . You can not compare an object to a value, like this:

public void fazUmaCompra(Compra preco){
    if (preco > 300){ //ERRADO

You should be wanting to get some attribute of class Compra to then compare to the values you want, I believe that the attribute you want to compare is preco , so your code would look like:

public void fazUmaCompra(Compra preco){
    if (preco.getPreco() > 300){ //CERTO

You should replace all your comparisons of preco with preco.getPreco() in your code, since the compiler stopped at the first error, however as soon as you fix the error the compiler will begin to show the other errors.

EDITION

Reading your code better, I saw that your getPreco() method is wrong, because it returns a int whereas its preco is double.

public int getPreco(){
    return preco;
}

Fix your get to look like this:

public double getPreco(){
    return preco;
}
    
13.04.2014 / 17:31
0

Class example:

public class Compra {
    /*
     * Construtor
     */
    public Compra() {
    }    
    /*
     * Privates
     */
    private Double preco;    
    /*
     * Get/Set
     */
    public Double getPreco() {
        return preco;
    }

    public void setPreco(Double preco) {
        this.preco = preco;
    }    
}

Note that private Double preco is only visible in its scope, by creating your Get / Set

getPreco and setPreco ), you can access your private .

Use:

Compra c = new Compra();
c.setPreco(150D);
if (c.getPreco()> 300){

}
    
13.04.2014 / 17:39