You are on page 1of 5

Bài TH 1: Product Maintenance

Trong bài TH này, bạn sẽ tạo 1 số trang Web cho phép thêm, sửa, xóa 1 sản phẩm hàng hóa.
Dưới đây là các màn hình của ứng dụng và mã gợi ý của lớp Product và lớp ProductIO. Bạn cần
hoàn thiện các mã nguồn còn lại và tích hợp/triển khai ứng dụng.
The Index page

The Products page

The Product page


The Confirm Delete page

Hoạt động của hệ thống


• Khi hệ thống khởi tạo, trang Index sẽ được hiển thị. Trang này chứa 1 liên kết tới trang
Products chứa danh sách các sản phẩm cho phép người dùng xem và có thể thêm, sửa, xóa sản
phẩm.
• Để thêm mới 1 sản phẩm, người dùng chọn nút Add Product. Trang Product sẽ hiển thị với các
trường nhập dữ liệu trống. Người dùng có thể nhập thông tin vào và bấm nút Update Product
để thêm sản phẩm.
• Để sửa sản phẩm, người dùng chọn liên kết Edit. Trang Product sẽ hiển thị cùng với các thông
tin hiện tại trên các ô nhập liệu. Người dùng có thể sửa thông tin và bấm nút Update Product để
cập nhật sửa thông tin.
• Để xóa sản phẩm, người dùng chọn liên kết Delete tương ứng. Trang Confirm Delete sẽ hiển
thị. Người dùng có thể xác nhận xóa bằng cách bấm nút Yes và sản phẩm sẽ bị xóa. Nếu người
dùng chọn No, sản phẩm sẽ không bị xóa và hệ thống quay về trang Products.

Yêu cầu
• Sử dụng lớp Product bên dưới để chưa dữ liệu về các sản phẩm.
• Sử dụng lớp ProductIO để thực hiện các thao tác đọc, ghi, xóa sản phẩm từ 1 file có tên
products.txt trong thư mực WEB-INF.
• Sử dụng file text products.txt như mô tả bên dưới để lưu thông tin sản phẩm cho ứng dụng.
• Thực hiện việc validate thông tin người dùng nhập vào từ server. Cụ thể, kiểm tra việc người
dùng đưa các thông tin về code, description, và price cho mỗi sản phẩm. Kiểm tra giá sản phẩm
là số.
• Sử dụng JSP EL và JSTL để thay thế cho các code Java trong trang JSP.
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;
}

The ProductIO class


package music.data;

import java.io.*;
import java.util.*;

import music.business.*;

public class ProductIO


{
private static ArrayList<Product> products = null;

public static ArrayList<Product> getProducts(String path)


{

}
public static Product getProduct(String productCode, String path)
{

public static boolean exists(String productCode, String path)


{
products = getProducts(path);
for (Product p : products)
{
if (productCode != null &&
productCode.equalsIgnoreCase(p.getCode()))
{
return true;
}
}
return false;
}

private static void saveProducts(ArrayList<Product> products,


String path)
{
try
{
File file = new File(path);
PrintWriter out =
new PrintWriter(
new FileWriter(file));

for (Product p : products)


{
out.println(p.getCode() + "|"
+ p.getDescription() + "|"
+ p.getPrice());
}

out.close();
}
catch(IOException e)
{
e.printStackTrace();
}
}

public static void insert(Product product, String path)


{
products = getProducts(path);
products.add(product);
saveProducts(products, path);
}
public static void update(Product product, String path)
{
products = getProducts(path);
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, path);
}

public static void delete(Product product, String path)


{
products = getProducts(path);
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, path);
}
}

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

You might also like