You need to have Product.java and products.txt file ready. 1) Write a class, nam
ID: 3720053 • Letter: Y
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, 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 file’s 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;
}
}
Explanation / Answer
//ReadProduct.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import product.java.Product;
public class ReadProduct {
List<Product> listProduct;
Map<String,Product> mapCodeProduct;
/**
* @return the listProduct
*/
public List<Product> getListProduct() {
return listProduct;
}
/**
* @return the mapCodeProduct
*/
public Map<String, Product> getMapCodeProduct() {
return mapCodeProduct;
}
void readProductFile(){
listProduct = new ArrayList<Product>();
mapCodeProduct = new HashMap<String,Product>();
File inFile = new File("product.txt");
try {
Scanner keyboard = new Scanner(inFile);
while(keyboard.hasNext()) {
String line = keyboard.nextLine();
Product p = new Product();
String s[] = line.split(",");
p.setCode(s[0]);
p.setDescription(s[1]);
p.setPrice(Double.parseDouble(s[2]));
listProduct.add(p);
mapCodeProduct.put(p.getCode(), p);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//ProductApp.java
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import product.java.Product;
public class ProductApp {
public static void main(String[] args) {
ReadProduct rp = new ReadProduct();
rp.readProductFile();
List<Product> listProduct = rp.getListProduct();
Map<String, Product> mapCodeProduct = rp.getMapCodeProduct();
Iterator<Product> i = listProduct.iterator();
System.out.println("List of products:");
while (i.hasNext())
System.out.println(i.next());
System.out.println("Products Map:");
// Entry<String, Product> entrySet = mapCodeProduct.entrySet();
for (Map.Entry<String, Product> productMap : mapCodeProduct.entrySet()) {
System.out.println(productMap.getKey());
System.out.println(productMap.getValue());
}
}
}
//Product.java
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;
}
}
Assuming the product.txt file in the same folder as the ProductApp.java
Please use the above code for ReadProduct.java and ProductApp.java
Hope it helps
//ReadProduct.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import product.java.Product;
public class ReadProduct {
List<Product> listProduct;
Map<String,Product> mapCodeProduct;
/**
* @return the listProduct
*/
public List<Product> getListProduct() {
return listProduct;
}
/**
* @return the mapCodeProduct
*/
public Map<String, Product> getMapCodeProduct() {
return mapCodeProduct;
}
void readProductFile(){
listProduct = new ArrayList<Product>();
mapCodeProduct = new HashMap<String,Product>();
File inFile = new File("product.txt");
try {
Scanner keyboard = new Scanner(inFile);
while(keyboard.hasNext()) {
String line = keyboard.nextLine();
Product p = new Product();
String s[] = line.split(",");
p.setCode(s[0]);
p.setDescription(s[1]);
p.setPrice(Double.parseDouble(s[2]));
listProduct.add(p);
mapCodeProduct.put(p.getCode(), p);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//ProductApp.java
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import product.java.Product;
public class ProductApp {
public static void main(String[] args) {
ReadProduct rp = new ReadProduct();
rp.readProductFile();
List<Product> listProduct = rp.getListProduct();
Map<String, Product> mapCodeProduct = rp.getMapCodeProduct();
Iterator<Product> i = listProduct.iterator();
System.out.println("List of products:");
while (i.hasNext())
System.out.println(i.next());
System.out.println("Products Map:");
// Entry<String, Product> entrySet = mapCodeProduct.entrySet();
for (Map.Entry<String, Product> productMap : mapCodeProduct.entrySet()) {
System.out.println(productMap.getKey());
System.out.println(productMap.getValue());
}
}
}
//Product.java
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;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.