Design and implement a program that reads data from a file and displays content
ID: 3573047 • Letter: D
Question
Design and implement a program that reads data from a file and displays content on the screen. The file contains product information with the following attributes:
1) Product ID (database integer ID)
2) Product Name (Maximum 200 character string)
3) Product Price (decimal)
4) Company Name (Maximum 200 character string)
The file is a tab-delimited and contains 10 rows of product information. The data may contain errors during the load so the program must do the following when loading the data file:
5) If an issue occurs with the data file load, the program presents a "data loading error" message
If the program encounters any of these or other exceptions, the following must occur:
6) The program reports the error to the user during the load
7) The program ignores the data record and continues with loading the rest of the file
When all records are loaded, a report of the information loaded is presented:
8) Show the counts of records successfully and unsuccessfully loaded
9) Show the average product price of all the products successfully loaded
Technical requirements: You must use exception handling during the data load. You must also load the product information into a Product class and create an array or ArrayList of Products that capture successfully loaded product information
The emphasis on this assignment is on the design of the file loading, Product class and use of exception handling. Output to the screen is not a priority so you don't have to worry about cool looking output.
Explanation / Answer
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
public class Product {
// Instance variables
private int produceID;
private String productName;
private double productPrice;
private String companyName;
// Getters and setters to access instance variables from outside
public int getProduceID() {
return produceID;
}
public void setProduceID(int produceID) {
this.produceID = produceID;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public double getProductPrice() {
return productPrice;
}
public void setProductPrice(double productPrice) {
this.productPrice = productPrice;
}
public String getCompanyName() {
return companyName;
}
public void setCompanyName(String companyName) {
this.companyName = companyName;
}
public String getProductString(){
return "Product ID : "+this.getProduceID()+", Prodcut name: "+this.getProductName()+", Price : "+this.getProductPrice()+", Company name : "+this.getCompanyName();
}
// Main function to read the data
public static void main(String[] args) {
try {
BufferedReader brInput = new BufferedReader(new FileReader("data.txt"));
String line;
Product product;
int totalCount = 0;
ArrayList<Product> products = new ArrayList<Product>();
while((line=brInput.readLine()) != null){
totalCount++;
product = convertToProduct(line);
if(product != null){
products.add(product);
}
}
System.out.println("Total products - "+totalCount+", Successfully loaded - "+products.size()+", Unsuccessful loads : "+(totalCount-products.size()));
System.out.println();
double totalPrice = 0;
for(int i=0;i<products.size();i++){
product = products.get(i);
totalPrice += product.getProductPrice();
System.out.println(product.getProductString());
}
System.out.println();
System.out.println("Average product price : "+(totalPrice)/products.size());
brInput.close();
}
catch(FileNotFoundException e){
System.out.println("File not found.");
}
catch(IOException e){
System.out.println("data loading error.");
}
}
// Converts line to Product object
private static Product convertToProduct(String line){
Product product = null;
if(line != null){
String[] args = line.split(" ");
StringBuffer products = new StringBuffer();
for(int i=0;i<args.length;i++){
if(args[i] != null && !args[i].isEmpty()){
products.append(args[i]+",");
}
}
args = products.toString().split(",");
try {
product = new Product();
product.setProduceID(Integer.parseInt(args[0]));
product.setProductName(args[1]);
product.setProductPrice(Double.parseDouble(args[2]));
product.setCompanyName(args[3]);
}
catch(NumberFormatException | ArrayIndexOutOfBoundsException e){
product = null;
}
}
return product;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.