Question: Need help in java: You are a fundraising distributor who needs to to pre-sell a limited number of doughnut coupon books. Each buyer can buy

Need help in java: You are a fundraising distributor who needs to to pre-sell a limited number of doughnut coupon books. Each buyer can buy as many as 4 coupon books. No more than 100 coupon books can be sold. Implement a program called Fundraiser that prompts the user for the desired number of coupon books and then displays the number of remaining coupon books. Repeat until all coupon books have been sold, and then display the total number of buyers.

My code is working for the most part, I am having trouble when I enter a number over 4 my code recognizes that it is an invalid request but still calculates it into the remaining number of books left, (for example, if I try to request 5 books it prints invalid, but will minus 5 books from the remaining and I don't know how to get it to not calculate a request over 4). I also have another error handling my coupon book but I'm not sure where. This is my code so far:

public static void main(String[] args) {

// TODO Auto-generated method stub

//presell 100 coupon books

//prompt user for desired number of books

// display how many coupon books are left

// total number of buyers

final int MAX_BOOKS = 100;

int availableCouponBooks = MAX_BOOKS;

int buyers = 0, requested = 0;

Scanner console = new Scanner(System.in);

while (availableCouponBooks > 0)

{

System.out.print("Requested Coupon Books: ");

requested = console.nextInt();

if (requested >= 1 && requested <= availableCouponBooks && requested <= 4);

{

buyers ++;

availableCouponBooks = availableCouponBooks-requested;

System.out.println("Available Coupon Books: " + availableCouponBooks);

System.out.println("Number of buyers: " + buyers);

} if (requested > 4)

System.out.println("Invalid request, Try Again!");

{

}

}

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!