Write a program that reads a price and a quantity from an input file, determines
ID: 3551030 • Letter: W
Question
- Write a program that reads a price and a quantity from an input file, determines the total bill (price * quantity) and prints the total bill to an output file.
- Hints and what program should include:
- Input file name should be called Lab9AInput.txt . Before beginning to write the program, using NotePad create a file by this name in the default Folder you would be creating the CodeBlocks project in. Enter the price 10.98 and the quantity 22 in Lab9AInput.txt, save and close the file.
- Output file name should be "bill.txt". Do not create this file in your Folder. Your program should create this file.
- Both input and output files should be stored in the Folder you created the project in when you started CodeBlocks, so no need to specify paths for the files.
- In your program check that the input file opened successfully before reading from it. Program should print out an error message if open not successful.
- When writing the total bill the output file, include a label and the bill should be formatted to show 2 decimal places.
- Before your program exits it should close the input and output files.
Explanation / Answer
import java.util.Scanner;
public class Test_class {
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.println("Enter a Integer");
int n=in.nextInt();
int sum=0;
for(int i=0;i<n;i++){
System.out.println("Enter a Amount");
sum+=in.nextInt();}
System.out.println("The Net Sum of Amount"+sum);
}
}