Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that prompts the user to enter the number of books that they are

ID: 3812918 • Letter: W

Question

Write a program that prompts the user to enter the number of books that they are purchasing. Prompt the user to enter their budgeted amount (real number) for their book purchase. Use a for loop to prompt the user to enter the price (real number) of each book. Accumulate the total of the books. Add 6% sales tax. Display the total of the books, the tax amount, and the total for the sale - include the "$" symbol in the display for all 3 amounts. Use a decision structure to determine if the total for the sale is within the user's budget. If it is, display a message (in your own words) that the user is within their budget. If not, display a message(in your own words) that they have gone over their budget.

Explanation / Answer

Please find the required program and output below: Please find the comments against each line for the description:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


class Test {
  
   public static void main(String args[]) throws NumberFormatException, IOException {

       BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
       System.out.println("Enter the no:of books wants to purchase : ");
       int n = Integer.parseInt(br.readLine());   //read no:of books need to purchase
       System.out.println("Enter the budget : ");
       double budget = Double.parseDouble(br.readLine());   //read the user budget
       System.out.println("Enter the price of each book : ");
       double price[] = new double[n];  
       double totalPrice = 0;
       for(int i=0; i<n; i++){   //iterate n no:of times to read the price for each book
           price[i] = Double.parseDouble(br.readLine());
           totalPrice = totalPrice + price[i];   //add each book price to get the total price
       }
      
       double tax = 0.06 * totalPrice;   //find the tax amount
      
       double totalWithTax = totalPrice + tax; //total with tax
      
       System.out.println("Total price of books = $"+totalPrice);   //print the results
       System.out.println("Tax amount = $"+tax);
       System.out.println("Total sale amount = $"+totalWithTax);  
      
       if(totalWithTax<=budget)   //if the total amount with tax is within the budget
           System.out.println("Total amount is within the budget");
       else   //if not
           System.out.println("Total amount is not within the budget");
   }
  
}

----------------------------------------------------------------------------------

OUTPUT:

Enter the no:of books wants to purchase :
5
Enter the budget :
650
Enter the price of each book :
120
75.5
100
50
35
Total price of books = $380.5
Tax amount = $22.83
Total sale amount = $403.33
Total amount is within the budget

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote