-4

I have finished most codes, but now I am stuck at how to use boolean to decide if the customer is qualified for a discount.

        System.out.print("Enter invoice number: ");
        int invoiceNum = console.nextInt();

        System.out.print("Enter first name: ");
        String firstName = console.next();

        System.out.print("Enter last name: ");
        String lastName = console.next();

        System.out.println("Is the customer qualified for a discount? (Y or N): ");
        String qualified = console.next();

        System.out.print("Enter discount rate ( ex. 12 for 12%): ");
        double discountRate = console.nextDouble();

        return new Service(invoiceNum, firstName,lastName, true/*???*/, discountRate);
 @Override
    public double calculateCost(){
        if(super.isIsQualifiedDiscount() == true){
            double total = serviceCharge * (1 - (super.getDiscountRate() / 100));
            System.out.printf("%nInspection Charge with Discount: %.2f", total);
            return total;
        }
        else{
        return serviceCharge;
        }
    }

    @Override
    public String toString() {
        return "Inspection Service Charge: " + serviceCharge;
    }

How should I connect these two parts so I can use Y or N to decide it?? below is my assignment picture:`` enter image description here

  • You say you need a boolean, so why is `qualified` a String? Shouldn't it be a `boolean` type? – virullius Feb 22 '20 at 07:53
  • Does this answer your question? [Is there a difference between single and double quotes in Java?](https://stackoverflow.com/questions/439485/is-there-a-difference-between-single-and-double-quotes-in-java) – Matsemann Feb 22 '20 at 07:54
  • Note: be aware of [Scanner is skipping nextLine() after using next() or nextFoo()?](https://stackoverflow.com/q/13102045/85421) in case a `nextLine()` is introduced – user85421 Feb 22 '20 at 08:39

2 Answers2

1

You can try

boolean isQualified = qualified.toLowerCase().equals("y")

Edit:

As suggested in the comment by @user85421, alternate and better approach would be

boolean isQualified = qualified.equalsIgnoreCase("y")
ashu
  • 1,363
  • 4
  • 17
  • 38
0

As per your problem statement, the service class should include getter and setter methods for your data members. The status of discount qualification should be a boolean variable in your service class. Thus, once you read the input from the console you can use the below code to set the value to your boolean variable.

String discountStatus = sc.next();
setQualified(status.equalsIgnoreCase("Y"));

Now in your calculateCost() method, you call your Base Class method isQualified() to evaluate condition.

    if(isQualified()){
      // business logic
    }