Part 1: Create a program that creates a text file from user input with the follo
ID: 3847277 • Letter: P
Question
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 4thcalculated 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
Part1)
package com.praveen.code;
import java.io.BufferedInputStream;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Scanner;
public class ToolsCollection {
public static void main(String args[]) throws IOException {
// Please provide the input in the following format
// Toolname toolprice tool Quantity
Scanner noOfTools = new Scanner(System.in);
Scanner input = new Scanner(new BufferedInputStream(System.in));
int range = noOfTools.nextInt();
String toolArray[] = new String[range];
int i = 0;
while (i < range) {
toolArray[i] = input.nextLine();
readToFile(toolArray[i]);
i++;
}
for (int j = 0; j < toolArray.length; j++) {
System.out.println(toolArray[j]);
}
}
private static void readToFile(String tool) throws IOException {
String[] toolDetails = tool.split(" ");
Tool toolObj = new Tool();
if (toolDetails != null) {
toolObj.setItem(toolDetails[0]);
toolObj.setPrice(Double.parseDouble(toolDetails[1]));
toolObj.setToolQty(Integer.parseInt(toolDetails[2]));
}
File fout = new File("readTools.txt");
FileOutputStream fos = new FileOutputStream(fout,true);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));
bw.write(toolObj.toString());
bw.newLine();
//bw.close();
}
}
Supporitng POJO class
package com.praveen.code;
public class Tool {
String Item;
double Price;
int ToolQty;
double ToolExtPrice;
public String getItem() {
return Item;
}
public void setItem(String item) {
Item = item;
}
public double getPrice() {
return Price;
}
public void setPrice(double price) {
Price = price;
}
public int getToolQty() {
return ToolQty;
}
public void setToolQty(int toolQty) {
ToolQty = toolQty;
}
public double getToolExtPrice() {
return ToolExtPrice;
}
public void setToolExtPrice(double toolExtPrice) {
ToolExtPrice = toolExtPrice;
}
}