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.
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.
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);
}
}
public class ProductIO {
private static Listproducts = 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 ListselectProducts() {
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(Listproducts) {
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);
}
}
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