Project 1
package com.HELLOJAVA;
import java.io.*;
import java.util.*;
class Product {
private int productId;
private String productName;
private String productCategory;
private String productSubcategory;
private double productPrice;
private int productQuantity;
public Product(int productId, String productName, String productCategory, String productSubcategory, double productPrice, int productQuantity) {
this.productId = productId;
this.productName = productName;
this.productCategory = productCategory;
this.productSubcategory = productSubcategory;
this.productPrice = productPrice;
this.productQuantity = productQuantity;
}
public int getProductId() {
return productId;
}
public String getProductName() {
return productName;
}
public String getProductCategory() {
return productCategory;
}
public String getProductSubcategory() {
return productSubcategory;
}
public double getProductPrice() {
return productPrice;
}
public int getProductQuantity() {
return productQuantity;
}
}
class Catalog {
private ArrayList<Product> productList;
public Catalog() {
productList = new ArrayList<>();
}
public void addProduct(Product product) {
productList.add(product);
}
public ArrayList<Product> getProductList() {
return productList;
}
public ArrayList<Product> getProductListByCategory(String category) {
ArrayList<Product> productsByCategory = new ArrayList<>();
for (Product product : productList) {
if (product.getProductCategory().equals(category)) {
productsByCategory.add(product);
}
}
return productsByCategory;
}
public ArrayList<Product> getProductListBySubcategory(String subcategory) {
ArrayList<Product> productsBySubcategory = new ArrayList<>();
for (Product product : productList) {
if (product.getProductSubcategory().equals(subcategory)) {
productsBySubcategory.add(product);
}
}
return productsBySubcategory;
}
}
class Cart {
private ArrayList<Product> cartItems;
public Cart() {
cartItems = new ArrayList<>();
}
public void addItem(Product product) {
cartItems.add(product);
}
public void removeItem(int productId) {
for (Product product : cartItems) {
if (product.getProductId() == productId) {
cartItems.remove(product);
break;
}
}
}
public void checkout(int customerId) {
String fileName = customerId + ".txt";
try {
FileWriter fileWriter = new FileWriter(fileName);
for (Product product : cartItems) {
fileWriter.write(product.getProductId() + ", " + product.getProductName() + ", " + product.getProductCategory() + ", " + product.getProductSubcategory() + ", " + product.getProductPrice() + ", " + product.getProductQuantity() + "\n");
}
fileWriter.close();
System.out.println("Order details written to file: " + fileName);
} catch (IOException e) {
System.out.println("Error occurred while writing order details to file.");
e.printStackTrace();
}
}
}
class User {
private String userId;
private String password;
public User(String userId, String password) {
this.userId = userId;
this.password = password;
}
public String getUserId() {
return userId;
}
public String getPassword() {
return password;
}
}
class Seller extends User {
public Seller(String userId, String password) {
super(userId, password);
}
public void addProductTo