Please do it as Java program Part 1: Create a program that creates a text file f
ID: 3849198 • Letter: P
Question
Please do it as Java program
Part 1: 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.
Hammer
15.00
5
Drill
49.99
3
Screwdriver
3.00
6
Wrench
6.25
4
Pliers
4.00
2
Ratchet
12.50
1
Chisel
8.50
7
Prybar
7.25
4
Saw
14.50
6
Sander
27.99
2
Part 2: Create a program which reads the data file created in Part 1, and displays it as a table report with headings and a 4th calculated column.
Item
Price
Qty
Ext Price
Hammer
15.00
5
75
Drill
49.99
3
149.97
Screwdriver
3.00
6
18
Wrench
6.25
4
25
Pliers
4.00
2
8
Ratchet
12.50
1
12.5
Chisel
8.50
7
59.5
Prybar
7.25
4
29
Saw
14.50
6
87
Sander
27.99
2
55.98
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: $999.99
Average Single Item Price: $99.99
Count of all items Qty: 99
Hammer
15.00
5
Drill
49.99
3
Screwdriver
3.00
6
Wrench
6.25
4
Pliers
4.00
2
Ratchet
12.50
1
Chisel
8.50
7
Prybar
7.25
4
Saw
14.50
6
Sander
27.99
2
Explanation / Answer
CreateFileTools.java
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.Scanner;
public class CreateFileTools {
public static void main(String[] args) {
String toolName;
double toolPrice;
int toolQty;
//Scanner object is used to get the inputs entered by the user
Scanner sc=new Scanner(System.in);
PrintWriter pw = null;
BufferedWriter bw=null;
FileWriter fw=null;
File f=null;
int i=0;
while(true)
{
System.out.print("Enter the Tool Name ('q' or 'Q' for Quit)#"+(++i)+":");
toolName=sc.next();
if(toolName.equalsIgnoreCase("q"))
{
break;
}
else
{
System.out.print("Enter the Tool Price :");
toolPrice=sc.nextDouble();
System.out.print("Enter the Tool Quantity :");
toolQty=sc.nextInt();
try {
f=new File("D://tools.txt");
if(!f.exists())
{
f.createNewFile();
}
fw=new FileWriter(f,true);
bw=new BufferedWriter(fw);
pw=new PrintWriter(bw);
pw.print(toolName+" ");
pw.print(toolPrice+" ");
pw.print(toolQty);
pw.println(" ");
pw.close();
} catch (Exception e) {
System.out.println(e);
}
}
continue;
}
pw.close();
}
}
__________________
Output#1:
Enter the Tool Name ('q' or 'Q' for Quit)#1:Hammer
Enter the Tool Price :15.00
Enter the Tool Quantity :5
Enter the Tool Name ('q' or 'Q' for Quit)#2:Drill
Enter the Tool Price :49.99
Enter the Tool Quantity :3
Enter the Tool Name ('q' or 'Q' for Quit)#3:Screwdriver
Enter the Tool Price :3.00
Enter the Tool Quantity :6
Enter the Tool Name ('q' or 'Q' for Quit)#4:Wrench
Enter the Tool Price :6.25
Enter the Tool Quantity :4
Enter the Tool Name ('q' or 'Q' for Quit)#5:Pliers
Enter the Tool Price :4.00
Enter the Tool Quantity :2
Enter the Tool Name ('q' or 'Q' for Quit)#6:Ratchet
Enter the Tool Price :12.50
Enter the Tool Quantity :1
Enter the Tool Name ('q' or 'Q' for Quit)#7:Chisel
Enter the Tool Price :8.50
Enter the Tool Quantity :7
Enter the Tool Name ('q' or 'Q' for Quit)#8:Prybar
Enter the Tool Price :7.25
Enter the Tool Quantity :4
Enter the Tool Name ('q' or 'Q' for Quit)#9:Saw
Enter the Tool Price :14.50
Enter the Tool Quantity :6
Enter the Tool Name ('q' or 'Q' for Quit)#10:Sander
Enter the Tool Price :27.99
Enter the Tool Quantity :2
Enter the Tool Name ('q' or 'Q' for Quit)#11:q
______________
tools.txt ( (We can See this file under D Drive.As we specified the path of the output file as D:\Ex3-Results.txt) D://tools.txt)
Hammer 15.0 5
Drill 49.99 3
Screwdriver 3.0 6
Wrench 6.25 4
Pliers 4.0 2
Ratchet 12.5 1
Chisel 8.5 7
Prybar 7.25 4
Saw 14.5 6
Sander 27.99 2
________________
2)
tools.txt
Hammer 15.0 5
Drill 49.99 3
Screwdriver 3.0 6
Wrench 6.25 4
Pliers 4.0 2
Ratchet 12.5 1
Chisel 8.5 7
Prybar 7.25 4
Sqaw 14.5 6
Sander 27.99 2
_______________
ReadToolsInFile.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class ReadToolsInFile {
public static void main(String[] args) {
//Declaring variables
String toolName;
float price,extPrice,totExtPrice=0,avg;
int qty,totQty=0;
//Creating Scanner class reference
Scanner sc=null;
try {
//Opening the file
sc=new Scanner(new File("D://tools.txt"));
//Displaying the table
System.out.println(" Item Price Qty Ext Price");
System.out.println(" ---- ---- --- -------");
/* Reading the contents from the file and
* displaying them in the tabular format
*/
while(sc.hasNext())
{
toolName=sc.next();
price=sc.nextFloat();
qty=sc.nextInt();
System.out.printf("%10s %.2f %d %.2f ",toolName,price,qty,price*qty);
totExtPrice+=price*qty;
totQty+=qty;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
finally
{
//Closing the file
sc.close();
}
System.out.println("Total of Ext Price: $"+totExtPrice);
avg=(totExtPrice/totQty);
System.out.printf("Average Single Item Price: $ %.2f ",avg);
System.out.println("Count of all items Qty: "+totQty);
}
}
____________________
Output:
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
Sqaw 14.50 6 87.00
Sander 27.99 2 55.98
Total of Ext Price: $519.95
Average Single Item Price: $ 13.00
Count of all items Qty: 40
_____________Could you rate me well.Plz .Thank You