Bad Operand types for Binary Operator

Asked

Viewed 975 times

-1

I have to do an exercise for the college related to sales and in the customer class I need to apply a +fazUmaCompra method.

Detail, I performed so far only part "a", I put everything in case help in solving the problem.

code in the Customer 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 occore error on the line:

if (preco > 300){

bad operand types for Binary Operator '>'

  • Where Bad Operand types for binary operator gets into your doubt?

  • sorry, I forgot to put this information in the post, I will edit.

  • Friend, what is your question? after all we will not do the exercise just help where is the problem.

  • Could put your class Compra for us?

  • Buying price is a correct class, so must have price.[here the field you will know if and > 300]. Example price.getPreco() > 300, if you compare the class will give error even !!!

  • Preco is an object and not a variable! should do something like preco->getValor().

  • Fulvio, how could I do that if the price attribute of the Purchase class should be private? a get method maybe?

  • @user3511983, use get/set in private who wishes to exhibit outside the class !!!

  • 1

    OK Fulvio!! thank you very much

  • points = price points / 1; missed here ó

Show 5 more comments

2 answers

2


Compra is a class, and preco is a reference variable referencing an object of the class Compra. You can’t compare an object to a value like this:

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

You must be wanting to pick up some class attribute Compra to then compare with the values you want, I believe the attribute you want to compare is the preco, so your code would be:

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

You must replace all your comparisons of preco for preco.getPreco() in your code, because the compiler stopped at the first error, however as soon as you fix the error the compiler will start showing the other errors.

EDITION

Reading your code better I saw your method getPreco() is wrong because it returns a int meanwhile his preco is double.

public int getPreco(){
    return preco;
}

Fix your get to look like this:

public double getPreco(){
    return preco;
}
  • Strange hmmm... the compiler is pointing the parentheses of "getPreco()" as Unexpected type, required: variable, found: value

  • @user3511983 see my edition

  • had already located this error and made the correction, but the error persists

  • @user will play in the IDE so, you could update your code on the question to make sure I’m using the most current?

  • of course, I’ll edit it now

  • @user3511983 Here in the IDE I could not reproduce the error you quoted. The class that has the method fazUmaCompra can see the class Compra?

  • yes, the customer class sees the purchase class, so much so that there is the +emit method

Show 2 more comments

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

Realize that the private Double preco is only seen in its scope, creating its Get/Set (getPreco and setPreco), you can access for them your private price.

Use:

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

}
  • Fulvio, what would be the meaning of that "150D"?

  • 150D @user3511983, so he knows it’s double ... if you put a fixed number he asks it... the value is 150 !!!

  • @user3511983, look at this link from datatypes: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

  • look, thanks!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.