Pass the object as argument

Asked

Viewed 445 times

1

There is the +fazUmaCompra method in the Customer class that calculates the discount and the customer’s points, and at the end of the method, an object of type Purchase should be instantiated, but I cannot instantiate the object purchased.

Attributes and Constructor of 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; 
    }

Class method "Client":

public Compra fazUmaCompra(double p){   
    double pf = 0;
    double ds = 0;
        if (p > 300){
            if (pontos > 2000){
                pf = p - (p * 0.05);
                ds = pf - p;
                pontos = 0;
            }    
            else if (pontos > 1000){
                 pf = p - (p * 0.03);
                 ds = pf - p;
                 pontos = pontos - (int) p * 1;
            }    
            else if (pontos > 500){
                pf = p - (p * 0.02);
                ds = pf - p;
                pontos = pontos - (int) p * 1;
            }
            else
                pf = p;

    }
    pontos = pontos + (int) p * 1;
    Compra c = new Compra(p, ds, pf, this.Cliente);
    return c;

When compiling, Bluej marks "this.Client" as error. " Cannot find Symbol - variable Client. Thank you

  • 1

    Instead of this.Cliente write only this

  • Thank @ramaral , Beginner Idiot Hahah error

  • put points = points + (int) p * 1; Purchase c = new Purchase(p, ds, pf, this.Customer); Return c;

  • @asousajose actually the keys are from if, it is that it is badly identado

2 answers

4


this refers to esse objeto, as you are within a Customer class method you should pass only the this as an argument, because then you will be passing the current object, which is of the Client type, as a parameter for the constructor of Compra. Correct like this:

Compra c = new Compra(p, ds, pf, this);

-3

Attention message, friend, "Cannot find Symbol - variable Customer". Java is case sensitive, thus the created variable cliente cannot be called Cliente. Another thing: in your constructor, when passing the values to the member variables you should use this.cliente, for example, due to the scope of variables. Using only cliente = ... you are trying to use a variable not created in this scope, that is, within the constructor.

  • 1
    1. in the Client class there is no variable cliente, then put this.cliente the error will remain, the correct is to put only this; 2) there is no problem in doing cliente = ... inside the constructor, because the constructor can see the class variable
  • 2

    Crazy Caraca... customer is an attribute of the class! Pq ñ would be visible in the builder?!!?!?!?

Browser other questions tagged

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