Problem

Develop a Java web application that allows the user of the application to add, update, or delete a product data from a list of products thats available in a file.

Application View

The application view consists of 4 pages

1.Index page
2.Products page
3.A product page
4.Confirm-Delete page

The Index page see image.

The Producs Page see image.

The Product page see image.

The Confirm Delete page see image.

Application Operations

  • When the application starts, it displays the Index page. The page contains a link that leads to the Products page. The Products page displays a list of products and can be used to add, update, or delete a product.
  • To add a new product, the user clicks the Add Product button. This displays the Product page with all text fields empty. The user can fill in the text fields and click on the Update Product List button to add the product to the list.
  • To edit an existing product, the user selects the Edit link for the product from the Products page. This displays the Product page with all existing data for the product displayed. Then, the user can edit any entry and click on the Update Product button to update the data for the existing product. A click on the View Products button takes the user to the Products page.
  • 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 updated to reflect the deletion of the product. If the user selects the No/Cancel button, the "Products" page is displayed with the product.
  • User should be able to go to the "Products" page from any page any time.

Application Specifications

  • Use a Product class like the one shown in this document to model the product data.
  • Use a ProductIO class like the one shown 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 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. Generate appropriate messages for invalid data.
  • Product.java, ProductIO.java, and product.txt files are given for reference. You have to customize as you consider necessary for your design.

The Product class

public class Product implements Serializable {

private Long productId;
private String code;
private String description;
private double price;

public Product() {}

public Long getId() {
return productId;
}

public void setId(Long productId) {
this.productId = productId;
}

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 getPriceCurrencyFormat() {
NumberFormat currency = NumberFormat.getCurrencyInstance();
return currency.format(price);
}
}

The ProductIO class

public class ProductIO {

private static List products = null;
private static String filePath = null;

// Called once from the controller based on servlet context
public static void init(String filePath) {
ProductIO.filePath = filePath;
}

public static List selectProducts() {
products = new ArrayList();
File file = new File(filePath);
try {
BufferedReader in = new BufferedReader(new FileReader(file));
String line = in.readLine();
while (line != null) {
StringTokenizer t = new StringTokenizer(line, "|");
if (t.countTokens() >= 3) {
String code = t.nextToken();
String description = t.nextToken();
String priceAsString = t.nextToken();
double price = Double.parseDouble(priceAsString);

Product p = new Product();
p.setCode(code);
p.setDescription(description);
p.setPrice(price);

products.add(p);
}
line = in.readLine();
}
in.close();
return products;
} catch (IOException e) {
System.out.println(e);
return null;
}
}

public static Product selectProduct(String productCode) {
products = selectProducts();
for (Product p : products) {
if (productCode != null
&& productCode.equalsIgnoreCase(p.getCode())) {
return p;
}
}
return null;
}

public static boolean exists(String productCode) {
Product p = selectProduct(productCode);
if (p != null) return true;
else return false;
}

private static void saveProducts(List products) {
try {
File file = new File(filePath);
PrintWriter out = new PrintWriter(new FileWriter(file));

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 = selectProducts();
products.add(product);
saveProducts(products);
}

public static void updateProduct(Product product) {
products = selectProducts();
for (int i = 0; i < products.size(); i++) {
Product p = products.get(i);
if (product.getCode() != null
&& product.getCode().equalsIgnoreCase(p.getCode())) {
products.set(i, product);
}
}
saveProducts(products);
}

public static void deleteProduct(Product product) {
products = selectProducts();
for (int i = 0; i < products.size(); i++) {
Product p = products.get(i);
if (product != null
&& product.getCode().equalsIgnoreCase(p.getCode())) {
products.remove(i);
}
}
saveProducts(products);
}
}

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
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.