SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,kenrick Mock
Chapter:
Console Input And Output
Exercise:
Programming Projects
Question:10 | ISBN:9780132830317 | Edition: 5

Question

Write a program that inputs the name, quantity, and price of three items. The name may contain spaces. Output a bill with a tax rate of 6.25%. All prices should be output to two decimal places. The bill should be formatted in columns with 30 characters for the name, 10 characters for the quantity, 10 characters for the price, and 10 characters for the total. Sample input and output is shown as follows:


Input name of item 1:

lollipops

Input quantity of item 1:

10

Input price of item 1:

0.50


Input name of item 2:

diet soda

Input quantity of item 2:

3

Input price of item 2:

1.25


Input name of item 3:

chocolate bar

Input quantity of item 3:

20

Input price of item 3:

0.75


Your bill:


Item Quantity Price Total

lollipops 10 0.50 5.00

diet soda 3 1.25 3.75

chocolate bar 20 0.75 15.00

Subtotal 23.75

6.25% sales tax 1.48

Total 25.23

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

import java.util.Scanner;
import java.text.DecimalFormat;

public class StroreBill 
{
    public static void main(String[] args) 
    {
        Scanner keyboard = new Scanner(System.in);
        
        String item = "Item";
        String quantity = "Quantity";
        String price = "Price";
        String total = "Total";
        String subTotal = "Subtotal";
        String salesTax = "6.25% sales tax";
        
        
        
        System.out.println("Input name of item 1: ");
        String nameItem1 = keyboard.nextLine();
        System.out.println("Input quantity of item 1: ");
        int quantityItem1 = keyboard.nextInt();
        System.out.println("Input price of item 1: ");
        double priceItem1 = keyboard.nextDouble();
        
        double totalPriceItem1 = priceItem1 * quantityItem1;
        keyboard.nextLine();
        System.out.println("Input name of item 2: ");
        String nameItem2 = keyboard.nextLine();
        System.out.println("Input quantity of item 2: ");
        int quantityItem2 = keyboard.nextInt();
        System.out.println("Input price of item 2: ");
        double priceItem2 = keyboard.nextDouble();
        
        double totalPriceItem2 = priceItem2 * quantityItem2;
        keyboard.nextLine();
        System.out.println("Input name of item 3: ");
        String nameItem3 = keyboard.nextLine();
        System.out.println("Input quantity of item 3: ");
        int quantityItem3 = keyboard.nextInt();
        System.out.println("Input price of item 3: ");
        double priceItem3 = keyboard.nextDouble();
        
        double totalPriceItem3 = priceItem3 * quantityItem3;
        
        double totalPrice = totalPriceItem1 + totalPriceItem2 + totalPriceItem3;
        double totalSalesTax = (totalPrice * 6.25) / 100;
        double totalPriceWithTax = totalPrice + totalSalesTax;
        
        System.out.println("Your bill: ");
        System.out.printf("%-20s %-10s %-10s %-10s\n", item, quantity, price, total );
        System.out.printf("%-20s %-10d %-10.2f %5.2f\n", nameItem1, 
               quantityItem1, priceItem1, totalPriceItem1 );
        System.out.printf("%-20s %-10d %-10.2f %5.2f\n", nameItem2, 
               quantityItem2, priceItem2, totalPriceItem2 );
        System.out.printf("%-20s %-10d %-10.2f %-10.2f\n", nameItem3, 
               quantityItem3, priceItem3, totalPriceItem3 );
        System.out.printf("%-20s %-10s %-10s %-10.2f\n", subTotal, 
               "         ", "         ", totalPrice );
        System.out.printf("%-20s %-10s %-10s %5.2f\n", salesTax, 
               "         ", "         ", totalSalesTax );
        System.out.printf("%-20s %-10s %-10s %-10.2f\n", total, 
               "         ", "         ", totalPriceWithTax );        
    }
}

 

1 0

Discussions

Post the discussion to improve the above solution.