0

I would like to know if there is a way to pass a value from a getter setter. The code below will be the example I am using.


import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Sales {

    private int transaction;
    private int salesNum;
    private String name;
    private double amount;
    private double commission;

    Scanner input = new Scanner(System.in);
    Sales sales = new Sales();

    //default constructor
    public Sales() {
        transaction = 0;
        salesNum = 0;
        name = "";
        amount = 0.0;
        commission = 0.0;
    }

    //getter method
    public int getTransaction() {
        return transaction;
    }

    public int getSalesNum() {
        return salesNum;
    }

    public String getName() {
        return name;
    }

    public double getAmount() {
        return amount;
    }

    //setter method
    public void setTransaction() {
        do {
            System.out.println("Enter transaction number: ");
            try {
                transaction = Integer.parseInt(input.nextLine());
                break;
            } catch (NumberFormatException e) {
            }
        } while (true);
    }

    public void setSalesNum() {
        do {
            System.out.println("Enter sales number: ");
            try {
                salesNum = Integer.parseInt(input.nextLine());
                break;
            } catch (NumberFormatException e) {
            }
        } while (true);
    }

    public void setName() {
        do {
            System.out.println("Enter name: ");
            name = input.nextLine();
            Pattern pattern = Pattern.compile("[a-zA-Z]+");
            Matcher matcher = pattern.matcher(name);

            if (name.matches("[a-zA-Z]+")) {
                break;
            }
        } while (true);
    }

    public void setAmount() {
        do {
            System.out.println("Enter amount: ");
            try {
                amount = Double.parseDouble(input.nextLine());
                break;
            } catch (NumberFormatException e) {
            }
        } while (true);
    }
    
    public double commission(double commission) {
        this.commission= sales.getAmount();
        return this.commission;
    }
    
    //This was what I am trying to do
    /*  public void rate() {
        if(getAmount() >= 0 || getAmount() <= 999) {
            commission = 0;
        }
        
        else if(getAmount() >= 1000 || getAmount() <= 1999 ) {
            commission = 0.02;
        }
        
        else if(getAmount() >= 2000 || getAmount() <= 2999) {
            commission = 0.03;
        }
        
        else {
            commission = 0.05;
        }
    }*/
    
    @Override
    public String toString() {
        setTransaction();
        setSalesNum();
        setName();
        setAmount();
        System.out.print(this.commission);//I am using this to test if it will show the output of commission
        return String.format("%s, your transaction number and sales number are %d and %d respectively. Total amount is %.2f.\n", this.getName(), this.getTransaction(), this.getSalesNum(), this.getAmount());
    }
}

This will print out the amount when called in the driver class. However, I would like to pass the value of this.getAmount() to a parameter. Basically what I am trying to do is to get double value = this.getAmount() so I can put value into an if else statement to make it become if(value >= 0 && value <= 100) { percentage = 0.05; }. The desired output would be for me to enter 50 when it prompts to enter amount then it goes to check if it is within 0-100 then percentage would become 0.05 and so on according to the if else and the input. Then display the percentage.

I hope my explanation is understandable. Thank you in advance.

P.S i recently asked this question awhile ago but this ended up giving me StackOverflowError.

Tristan
  • 1
  • 1
  • What getters and setters for? Certainly to enable the principle of encapsulation. The write access to a variable makes it possible to execute code in the setter that depends on the change in the variable! That is why a typical setter also transfers a value! To read a value from the keyboard into a setter? Yes, that is possible, but it is definitely not the purpose of object-oriented programming. Just read the following article: https://stackoverflow.com/questions/1568091/why-use-getters-and-setters-accessors?rq=1 – QuinncyJones Jun 11 '21 at 17:16

0 Answers0