Home > database >  How can I correct this code, so it can work?
How can I correct this code, so it can work?

Time:10-30

import java.util.Scanner; // Needed to make Scanner available

public static int age()
{
    int age;
    Scanner scanner = new Scanner(System.in);
    System.out.println("How old are you? ");
    age = Integer.parseInt(scanner.nextLine());
    return age;
}

age();

public static void yesornodisability()
{
    String disabled;
    Scanner scanner = new Scanner(System.in);
    System.out.println("Are you registered disabled(Yes / No)? ");
    disabled = scanner.nextLine();
    return;
}

yesornodisability();

public static int total()
{
    int total;
    total = 10;
    return total;
}

total();

public static void swimmingprice()
{
    if (age()<=18);{
    total() = total()/2};
    
    else if (age()>=65);{
        total()=total()-3};
    
    else if (yesornodisability().equals("Yes");{
        total() = total()-4};
    
    System.out.println("The swimming price for you is " total() " pounds.");
}

I am asking two sets of questions the first question is asking there age, second question is asking if they are registered disabled. Then using both results I put them in a if statement. As if they are younger than 18 they get 50% discount, if they are registered disable they get 4 pounds discount. My inputs are working, but when I put them in the if statement they are class it expected and else without if error.

CodePudding user response:

First problem, don't write these isolated statements in the class body yesornodisability();, age(); and total(); . I doubt they compile.

Second store intermediate values. Since age() asks to input the age each time, it will be very inconvenient. Plus the user might enter different values.

int userAge = age();
int totalCost = total();
boolean disability = yesornodisability();

I also made a variable for total() because you want to update the value, and a variable yesornodisability() so the question gets asked immediately.

Your if statements have valid but incorrect syntax. You need to remove the semi-colons after them otherwise they won't have a body and the statement following will always be executed.

CodePudding user response:

Consider calling your methods from within the main method:

import java.math.BigDecimal;
import java.util.Scanner;

public class AnonymosSwimmingPriceCalculator {

    private static final int MAX_YOUTH_AGE = 18;
    private static final BigDecimal YOUTH_DISCOUNT_PERCENT = BigDecimal.valueOf(0.5); // 50%.
    private static final BigDecimal DISABLED_DISCOUNT_DOLLARS = BigDecimal.valueOf(4);  // $4.

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int age = getPositiveIntegerInput("How old are you? ", scanner);
        boolean isDisabled = getYesOrNoInput("Are you registered disabled (Yes/No)? ", scanner);
        BigDecimal baseSwimmingPrice = new BigDecimal(10);
        BigDecimal actualSwimmingPrice = getSwimmingPrice(baseSwimmingPrice, age, isDisabled);
        System.out.printf("The swimming price for you is %.2f pounds.%n", actualSwimmingPrice);
    }

    private static BigDecimal getSwimmingPrice(BigDecimal baseSwimmingPrice, int age, boolean isDisabled) {
        BigDecimal actualSwimmingPrice = baseSwimmingPrice;
        if (age <= MAX_YOUTH_AGE) {
            actualSwimmingPrice = actualSwimmingPrice.multiply(YOUTH_DISCOUNT_PERCENT);
        }
        if (isDisabled) {
            actualSwimmingPrice = actualSwimmingPrice.subtract(DISABLED_DISCOUNT_DOLLARS);
        }
        return actualSwimmingPrice;
    }

    public static int getIntegerInput(String prompt, Scanner scanner) {
        System.out.print(prompt);
        int validInteger = -1;
        while (scanner.hasNext()) {
            if (scanner.hasNextInt()) {
                validInteger = scanner.nextInt();
                break;
            } else {
                System.out.println("Error: Invalid input, must be integer, try again...");
                System.out.print(prompt);
                scanner.next();
            }
        }
        return validInteger;
    }

    public static int getPositiveIntegerInput(String prompt, Scanner scanner) {
        int num = getIntegerInput(prompt, scanner);
        while (num <= 0) {
            System.out.println("Error: Integer must be positive.");
            num = getIntegerInput(prompt, scanner);
        }
        return num;
    }

    public static boolean getYesOrNoInput(String prompt, Scanner scanner) {
        System.out.print(prompt);
        String input = scanner.nextLine().toLowerCase();
        while (!input.equals("yes") && !input.equals("no")) {
            input = scanner.nextLine().toLowerCase();
        }
        return input.equals("yes");
    }

}

Example Usage 1:

How old are you? 25
Are you registered disabled (Yes/No)? No
The swimming price for you is 10.00 pounds.

Example Usage 2:

How old are you? 18
Are you registered disabled (Yes/No)? Yes
The swimming price for you is 1.00 pounds.
  • Related