java programming Question: Your program will ask the user for information about
ID: 3577044 • Letter: J
Question
java programming Question:
Your program will ask the user for information about products on an order, and write information to a text file. It should do the following:
Open a text file in your project directory (e.g. using the Formatter).
Ask the user for a product name.
Ask the user whether it is taxable.
Ask the user for a price; verify that it is a valid price.
Ask the user for a quantity; verify that it is a valid quantity
Calculate the total for that product (price*quantity), calculate the tax (assume 6%, if taxable)
Write a line to a text file with product name, price, quantity, total, tax amount.
Continue to ask the user for product information until a blank is entered for product name.
When a blank is entered, calculate the total for the order and total tax and write these to the text file, then terminate the program.
Explanation / Answer
Here is the code for you:
import java.io.*;
import java.util.*;
class ProductsOrder
{
public static void main(String[] args) throws FileNotFoundException
{
//Open a text file in your project directory (e.g. using the Formatter).
File fout = new File("ProductsOrder.txt");
PrintWriter pw = new PrintWriter(fout);
//Ask the user for a product name.
Scanner sc = new Scanner(System.in);
System.out.print("Enter the product name: ");
double grandTotal = 0.0;
double totalTax = 0.0;
String productName = sc.next();
while(productName != null)
{
//Ask the user whether it is taxable.
System.out.print("Is the product taxable (Y: Yes, N: No): ");
char taxable = sc.next().charAt(0);
//Ask the user for a price; verify that it is a valid price.
System.out.print("Enter the price: ");
double price = sc.nextDouble();
while(price <= 0)
{
System.out.print("Invalid price. Please enter a valid price: ");
price = sc.nextDouble();
}
//Ask the user for a quantity; verify that it is a valid quantity
System.out.print("Enter quantity: ");
int quantity = sc.nextInt();
while(quantity <= 0)
{
System.out.print("Invalid quantity. Please enter a valid quantity: ");
quantity = sc.nextInt();
}
//Calculate the total for that product (price*quantity), calculate the tax (assume 6%, if taxable)
double totalPrice = price * quantity;
double tax = 0.0;
if(taxable == 'Y' || taxable == 'y')
tax = totalPrice * 0.06;
//Write a line to a text file with product name, price, quantity, total, tax amount.
pw.write(productName + " " + price + " " + quantity + " " + totalPrice + " " + tax + " ");
//Continue to ask the user for product information until a blank is entered for product name.
System.out.print("Enter another product name: ");
productName = sc.next();
grandTotal += totalPrice;
totalTax += tax;
}
//When a blank is entered, calculate the total for the order and total tax and write
//these to the text file, then terminate the program.
pw.write("Total price of the order: " + grandTotal + " ");
pw.write("Total tax amount: " + totalTax + " ");
pw.close();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.