Create a program that creates a text file from user input with the following dat
ID: 3849144 • Letter: C
Question
Create a program that creates a text file from user input with the following data, which is a list of tools, prices and quantities. There should be 3 columns of data, including 1 String for toolName, 1 double for toolPrice, and 1 int for toolQty. Create a program which reads the data file created in Part 1, and displays it as a table report with headings and a 4^th calculated column. Also, after the table is printed, include a sum of the Ext. Price at the end, an average of the Price column, and a count of the Qy of items. Sample output: Total of Ext Price: exist999.99 Average Single Item Price: exist99.99 Count of all items Qty: 99Explanation / Answer
Here is the code for creating and reading the file. Sample output attached. Please don't forget to rate the answer if it helped. Thank you very much.
part 1
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
//program to create a tools file where each line in the file is of the format
//toolname price quantity
public class ToolFileWriter {
public static void main(String[] args) {
String filename;
String answer="y";
String toolname;
double toolprice;
int quantity;
Scanner input = new Scanner(System.in);
System.out.print(" Enter the file name to be created: ");
filename = input.nextLine().trim();
try {
PrintWriter writer = new PrintWriter(new File(filename));
while(answer.equalsIgnoreCase("y"))
{
System.out.print(" Enter tool name: ");
toolname = input.next();
System.out.print(" Enter tool price:");
toolprice = input.nextDouble();
System.out.print(" Enter tool quantity: ");
quantity = input.nextInt();
writer.print(toolname + " " + toolprice + " " + quantity +" ");
System.out.print(" Enter another tool? y/n :");
answer = input.next();
}
writer.close();
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
}
}
}
part 2:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class ToolFileReader {
public static void main(String[] args) {
String filename;
Scanner input = new Scanner(System.in);
System.out.print(" Enter filename to be read: ");
filename = input.next();
try {
Scanner infile = new Scanner(new File(filename));
int quantity, totalQuantity = 0, count = 0;
double averagePrice = 0;
double totalPrice = 0;
double extPrice, cost;
String toolname;
System.out.printf(" %-25s %10s %10s %10s","Item", "Price", "Qty","Ext Price");
while(infile.hasNext())
{
count ++;
toolname = infile.next();
cost = infile.nextDouble();
quantity = infile.nextInt();
extPrice = cost * quantity;
totalPrice +=extPrice;
totalQuantity += quantity;
System.out.printf(" %-25s %10.2f %10d %10.2f",toolname, cost, quantity, extPrice);
}
averagePrice = totalPrice / count;
System.out.printf(" Total of Ext. Price: $%.2f",totalPrice);
System.out.printf(" Average single item price: $%.2f",averagePrice);
System.out.print(" Count of all item Qty " + totalQuantity);
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
}
}
}
output
Enter filename to be read: tools.txt
Item Price Qty Ext Price
Hammer 15.00 5 75.00
Drill 49.99 3 149.97
Screwdriver 3.00 6 18.00
Wrench 6.25 4 25.00
Pliers 4.00 2 8.00
Ratchet 12.50 1 12.50
Chisel 8.50 7 59.50
Prybar 7.25 4 29.00
Saw 14.50 6 87.00
Sander 27.99 2 55.98
Total of Ext. Price: $519.95
Average single item price: $52.00
Count of all item Qty 40
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.