Question: You need to have Product.java and products.txt file ready. 1) Write a class, named ReadProduct.java. It has the following two attributes: an ArrayList object, listProduct,
You need to have Product.java and products.txt file ready. 1) Write a class, named ReadProduct.java. It has the following two attributes: an ArrayList object, listProduct, which will contain Product objects and a HashMap object, mapCodeProduct, in which the key is product code, and the value is the product object. 2) The class has a method, readProductFile(), that reads products.txt line by line(refer to chapter 18 for the text files format and sample content). Using information from each line, it creates a Product object and stores the object in the listProduct ArrayList. This class contains another method, getListProduct(), which returns the listProduct ArrayList object. 3) When reading each line, it stores the product code and the product object as a record to the mapCodeProduct HashMap object. It provides another method, getMapCodeProduct(), which returns the mapCodeProduct HashMap object. 4) Write another class, ProductApp.java, which has a main method to test the ReadProduct class.
product.txt paperback,fight club,20.25 hardcover,the alchemist,15.75 hardcover,java programming,30.00
package product.java;/** * * @author babyd */ import java.text.NumberFormat; public class Product { // the instance variables private String code; private String description; private double price; protected static int count = 0; // the constructor public Product(){ code = ""; description = ""; price = 0; } // the set and get methods for the code varibale public void setCode (String code ) { this.code = code; } public String getCode () { return code; } // the set and get methods for the description variable public void setDescription (String description) { this.description = description; } public String getDescription () { return description; } //the set and get methods for the price variable public void setPrice (double price) { this.price = price; } public double getPrice() { return price; } //a custom get method for the price variable public String getFormattedPrice() { NumberFormat currency = NumberFormat.getCurrencyInstance(); return currency.format (price); } @Override public String toString () { return "Code: " + code + " " + "Description:" + description + " " + "Price :" + this.getFormattedPrice() + " "; } public static int getCount() { return count; } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
