Question
For this project, youll create a series of pages that allow you to add, update, or delete a product thats available to the application. (
For this project, youll create a series of pages that allow you to add, update, or delete a product thats available to the application. (Prerequisites: chapters 1-8)
The Index page
The Products page
The Product page
The Confirm Delete page
Operation
When the application starts, it displays the Index page. This page contains a link that leads to the Products page that can be used to add, update, or delete products.
To add a new product, the user selects the Add Product button. This displays the Product page with all text fields empty. Then, the user can fill in the text fields and click on the Update Product button to add the product.
To edit an existing product, the user selects the Edit link for the product. This displays the Product page with all existing data for the product displayed. Then, the user can edit any entries and click on the Update Product button to update the data for the existing product.
To delete a product, the user selects the Delete link for the product. This displays the Confirm Delete page. Then, if the user confirms the deletion by selecting the Yes button, the product is deleted and the Products page is displayed to reflect the new data. If the user selects the No button, the Products page is displayed.
Specifications
Use a Product class like the one shown later in this document to store the product data.
Use a ProductIO class like the one shown later in this document to read and write the product data to a text file named products.txt in the WEB-INF directory.
Use a text file like the products.txt file shown later in this document as a starting point for the products that are available to the application.
Use server-side validation to validate all user entries. In particular, make sure the user enters a code, description, and price for each product. In addition, make sure the products price is a valid double value.
If possible, get the Product.java, ProductIO.java, and product.txt files from your instructor or trainer. Otherwise, you can import starting versions of these files from the book applications.
The Product class
package music.business;
import java.text.NumberFormat;
import java.io.Serializable;
public class Product implements Serializable
{
private String code;
private String description;
private double price;
public Product()
{
code = "";
description = "";
price = 0;
}
public void setCode(String code)
{
this.code = code;
}
public String getCode()
{
return code;
}
public void setDescription(String description)
{
this.description = description;
}
public String getDescription()
{
return description;
}
public void setPrice(double price)
{
this.price = price;
}
public double getPrice()
{
return price;
}
public String getPriceNumberFormat()
{
NumberFormat number = NumberFormat.getNumberInstance();
number.setMinimumFractionDigits(2);
if (price == 0)
return "";
else
return number.format(price);
}
public String getPriceCurrencyFormat()
{
NumberFormat currency = NumberFormat.getCurrencyInstance();
return currency.format(price);
}
}
The ProductIO class
package music.data;
import java.io.BufferedReader; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.List;
import music.models.Product;
public class ProductIO {
private static List
// Called once from the controller based on servlet context public static void init(String filePath) { ProductIO.filePath = filePath; products = new ArrayList
String line; while ((line = in.readLine()) != null) { String [] tok = line.split("\\|"); if (tok.length != 3) { continue; } String code = tok[0]; String desc = tok[1]; double price = Double.parseDouble(tok[2]); products.add(new Product(code, desc, price)); } in.close();
} catch (IOException e) { System.out.println(e); } }
public static List
public static Product getProduct(String productCode) { for (Product p : products) { if (productCode.equals(p.getCode())) { return p; } } return null; }
private static void saveProducts(List
for (Product p : products) { out.println(p.getCode() + "|" + p.getDescription() + "|" + p.getPrice()); }
out.close(); } catch (IOException e) { System.out.println(e); } }
public static void insertProduct(Product product) { products.add(product); saveProducts(products); }
public static void updateProduct(Product product) { for (int i = 0; i
public static void deleteProduct(Product product) { for (int i = 0; i
A product.txt file that contains four products
8601|86 (the band) - True Life Songs and Pictures|14.95
pf01|Paddlefoot - The first CD|12.95
pf02|Paddlefoot - The second CD|14.95
jr01|Joe Rut - Genuine Wood Grained Finish|14.95
a product Maintenance Microsoft Internet Explorer File Edit View Favorites Tools Help Back Search Address a http:/Mocalhost:8080/productMaint1/ Product Maintenance View Products Go Links a product Maintenance Microsoft Internet Explorer File Edit View Favorites Tools Help Back Search Address a http:/Mocalhost:8080/productMaint1/ Product Maintenance View Products Go LinksStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started