I currently have the following code wrote:
import java.util.Scanner;
public class TicketPriceWithDiscount {
public static void main(String[] args) {
TicketPriceWithDiscount obj = new TicketPriceWithDiscount();
}
public TicketPriceWithDiscount(){
double regPrice = 9.25;
double otherPrice = 5.25; // ages under 12 or over 65
double discount = 2.00;
String person;
int pInput;
Scanner keyboard = new Scanner(System.in);
System.out.println("What is your age?");
pInput = keyboard.nextInt();
System.out.println("Do you have a coupon? Yes or no will do.");
person = keyboard.nextLine();
if(person == "yes" && pInput <= 12 || pInput >= 65){
otherPrice = otherPrice - discount;
System.out.println("Your ticket price is: " + otherPrice);
}
else if(person == "yes" && pInput >= 12 && pInput <= 65){
regPrice = regPrice - discount;
System.out.println("Your ticket price is: " + regPrice);
}
else if(pInput <= 12 || pInput >= 65){
System.out.println("Your ticket price is: " + otherPrice);
}
else if(pInput >= 12 || pInput <= 65) {
System.out.println("Your ticket price is: " + regPrice);
}
}
}
The program runs up to the point in which I ask for the person to input wither or not they have a coupon.
System.out.println("Do you have a coupon? Yes or no will do.");
person = keyboard.nextLine();
At this point, the program does not wait for an input and completes the if statement based on age input which it does wait for a reply from the user.
The output intended is for the user to input age, say yes or no to the coupon question and for the program to continue its run depending on those answers.
What I have tried:
- closing the scanner - keyboard.close();
- create a new scanner after closing the first instance of scanner.
Guidance is highly appreciated.