Untitled
unknown
plain_text
10 months ago
16 kB
12
Indexable
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.Random;
import java.util.InputMismatchException;
import java.text.DecimalFormat;
class Customer {
private String customerName;
private String email;
private String password;
private String address;
private String contactNumber;
private String customerId;
public Customer(String customerName, String email, String password, String address, String contactNumber) {
this.customerName = customerName;
this.email = email;
this.password = password;
this.address = address;
this.contactNumber = contactNumber;
this.customerId = generateCustomerId();
}
private String generateCustomerId() {
Random random = new Random();
return String.format("%05d", random.nextInt(100000));
}
// Getters
public String getCustomerName() { return customerName; }
public String getEmail() { return email; }
public String getPassword() { return password; }
public String getAddress() { return address; }
public String getContactNumber() { return contactNumber; }
public String getCustomerId() { return customerId; }
// Setters
public void setCustomerName(String customerName) { this.customerName = customerName; }
public void setEmail(String email) { this.email = email; }
public void setPassword(String password) { this.password = password; }
public void setAddress(String address) { this.address = address; }
public void setContactNumber(String contactNumber) { this.contactNumber = contactNumber; }
@Override
public String toString() {
return "Customer ID: " + customerId +
"\nName: " + customerName +
"\nEmail: " + email +
"\nAddress: " + address +
"\nContact: " + contactNumber;
}
}
class Product {
private String productId;
private String productName;
private String productDescription;
private int availableQuantities;
private double price;
public Product(String productName, String productDescription, int availableQuantities, double price) {
this.productId = generateProductId();
this.productName = productName;
this.productDescription = productDescription;
this.availableQuantities = availableQuantities;
this.price = price;
}
private String generateProductId() {
Random random = new Random();
return String.format("(%d-%04d-%04d-%d)",
random.nextInt(10),
random.nextInt(10000),
random.nextInt(10000),
random.nextInt(10));
}
// Getters
public String getProductId() { return productId; }
public String getProductName() { return productName; }
public String getProductDescription() { return productDescription; }
public int getAvailableQuantities() { return availableQuantities; }
public double getPrice() { return price; }
// Setters
public void setProductName(String productName) { this.productName = productName; }
public void setProductDescription(String productDescription) { this.productDescription = productDescription; }
public void setAvailableQuantities(int availableQuantities) { this.availableQuantities = availableQuantities; }
public void setPrice(double price) { this.price = price; }
@Override
public String toString() {
DecimalFormat df = new DecimalFormat("#0.00");
return "Product ID: " + productId +
"\nName: " + productName +
"\nDescription: " + productDescription +
"\nQuantity: " + availableQuantities +
"\nPrice: $" + df.format(price);
}
}
public class CustomerProductManagementSystem {
private static List<Customer> customers = new ArrayList<>();
private static List<Product> products = new ArrayList<>();
private static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
while (true) {
displayMenu();
System.out.print("Enter your choice (1-7): ");
int choice = getValidChoice();
switch (choice) {
case 1:
registerCustomer();
break;
case 2:
updateCustomer();
break;
case 3:
addOrUpdateProduct();
break;
case 4:
findCustomerByEmail();
break;
case 5:
findHighestPricedProduct();
break;
case 6:
sortProductsByQuantity();
break;
case 7:
System.out.println("Thank you for using the Customer Product Management System!");
System.exit(0);
break;
default:
System.out.println("Invalid choice! Please try again.");
}
System.out.println("\nPress Enter to continue...");
scanner.nextLine();
}
}
private static void displayMenu() {
System.out.println("\n" + "=".repeat(50));
System.out.println(" CUSTOMER PRODUCT MANAGEMENT SYSTEM");
System.out.println("=".repeat(50));
System.out.println("1. Register New Customer");
System.out.println("2. Update Customer Details");
System.out.println("3. Add/Update Product");
System.out.println("4. Find Customer by Email");
System.out.println("5. Find Highest Priced Product");
System.out.println("6. Sort Products by Quantity");
System.out.println("7. Exit");
System.out.println("=".repeat(50));
}
private static int getValidChoice() {
try {
int choice = scanner.nextInt();
scanner.nextLine(); // consume newline
return choice;
} catch (InputMismatchException e) {
scanner.nextLine(); // consume invalid input
return -1;
}
}
// Task 1: Customer Registration
private static void registerCustomer() {
System.out.println("\n--- CUSTOMER REGISTRATION ---");
System.out.print("Enter Customer Name (max 50 characters): ");
String name = getValidString(50, "Customer Name");
System.out.print("Enter Email: ");
String email = getValidEmail();
System.out.print("Enter Password (6-12 characters): ");
String password = getValidPassword();
System.out.print("Enter Address (max 100 characters): ");
String address = getValidString(100, "Address");
System.out.print("Enter Contact Number (exactly 10 digits): ");
String contactNumber = getValidContactNumber();
Customer customer = new Customer(name, email, password, address, contactNumber);
customers.add(customer);
System.out.println("\nCustomer Registration is successful for <" + customer.getCustomerId() + ">");
}
// Task 2: Update Customer Details
private static void updateCustomer() {
System.out.println("\n--- UPDATE CUSTOMER DETAILS ---");
if (customers.isEmpty()) {
System.out.println("No customers registered yet!");
return;
}
System.out.print("Enter Customer Email to update: ");
String email = scanner.nextLine().trim();
Customer customer = findCustomerByEmailAddress(email);
if (customer == null) {
System.out.println("No customer found with email: " + email);
return;
}
System.out.println("Current Details:");
System.out.println(customer);
System.out.print("\nEnter new Customer Name (max 50 characters): ");
String name = getValidString(50, "Customer Name");
System.out.print("Enter new Email: ");
String newEmail = getValidEmail();
System.out.print("Enter new Password (6-12 characters): ");
String password = getValidPassword();
System.out.print("Enter new Address (max 100 characters): ");
String address = getValidString(100, "Address");
System.out.print("Enter new Contact Number (exactly 10 digits): ");
String contactNumber = getValidContactNumber();
customer.setCustomerName(name);
customer.setEmail(newEmail);
customer.setPassword(password);
customer.setAddress(address);
customer.setContactNumber(contactNumber);
System.out.println("\nYour Details updated successfully");
}
// Task 3: Add/Update Product
private static void addOrUpdateProduct() {
System.out.println("\n--- ADD/UPDATE PRODUCT ---");
System.out.print("Enter Product Name (max 50 characters): ");
String name = getValidString(50, "Product Name");
System.out.print("Enter Product Description (max 100 characters): ");
String description = getValidString(100, "Product Description");
System.out.print("Enter Available Quantities: ");
int quantity = getValidQuantity();
System.out.print("Enter Price (up to 2 decimal points): ");
double price = getValidPrice();
Product product = new Product(name, description, quantity, price);
products.add(product);
System.out.println("\nProduct added successfully");
}
// Task 4: Find Customer by Email
private static void findCustomerByEmail() {
System.out.println("\n--- FIND CUSTOMER BY EMAIL ---");
if (customers.isEmpty()) {
System.out.println("No customers registered yet!");
return;
}
System.out.print("Enter Customer Email: ");
String email = scanner.nextLine().trim();
Customer customer = findCustomerByEmailAddress(email);
if (customer != null) {
System.out.println("\nCustomer found:");
System.out.println(customer);
} else {
System.out.println("No Such Customer Exist with the Given Email");
}
}
// Task 5: Find Highest Priced Product
private static void findHighestPricedProduct() {
System.out.println("\n--- HIGHEST PRICED PRODUCT ---");
if (products.isEmpty()) {
System.out.println("Product List is Empty");
return;
}
Product highestPricedProduct = products.get(0);
for (Product product : products) {
if (product.getPrice() > highestPricedProduct.getPrice()) {
highestPricedProduct = product;
}
}
System.out.println("Highest Priced Product:");
System.out.println(highestPricedProduct);
}
// Task 6: Sort Products by Quantity
private static void sortProductsByQuantity() {
System.out.println("\n--- PRODUCTS SORTED BY QUANTITY ---");
if (products.isEmpty()) {
System.out.println("Product List is Empty");
return;
}
List<Product> sortedProducts = new ArrayList<>(products);
sortedProducts.sort((p1, p2) -> Integer.compare(p2.getAvailableQuantities(), p1.getAvailableQuantities()));
System.out.println("Products sorted by quantity (highest to lowest):");
for (int i = 0; i < sortedProducts.size(); i++) {
System.out.println("\n" + (i + 1) + ". " + sortedProducts.get(i));
if (i < sortedProducts.size() - 1) {
System.out.println("-".repeat(40));
}
}
}
// Helper methods
private static Customer findCustomerByEmailAddress(String email) {
for (Customer customer : customers) {
if (customer.getEmail().equalsIgnoreCase(email)) {
return customer;
}
}
return null;
}
private static String getValidString(int maxLength, String fieldName) {
while (true) {
String input = scanner.nextLine().trim();
if (input.length() > 0 && input.length() <= maxLength) {
return input;
}
System.out.print(fieldName + " must be between 1 and " + maxLength + " characters. Try again: ");
}
}
private static String getValidEmail() {
while (true) {
String email = scanner.nextLine().trim();
if (email.contains("@") && email.contains(".") && email.length() > 5) {
// Check for duplicate email
boolean isDuplicate = false;
for (Customer customer : customers) {
if (customer.getEmail().equalsIgnoreCase(email)) {
isDuplicate = true;
break;
}
}
if (!isDuplicate) {
return email;
} else {
System.out.print("Email already exists. Please enter a different email: ");
}
} else {
System.out.print("Please enter a valid email address: ");
}
}
}
private static String getValidPassword() {
while (true) {
String password = scanner.nextLine().trim();
if (password.length() >= 6 && password.length() <= 12) {
return password;
}
System.out.print("Password must be between 6 and 12 characters. Try again: ");
}
}
private static String getValidContactNumber() {
while (true) {
String contact = scanner.nextLine().trim();
if (contact.length() == 10 && contact.matches("\\d+")) {
return contact;
}
System.out.print("Contact number must be exactly 10 digits. Try again: ");
}
}
private static int getValidQuantity() {
while (true) {
try {
int quantity = scanner.nextInt();
scanner.nextLine(); // consume newline
if (quantity >= 0) {
return quantity;
}
System.out.print("Quantity must be a non-negative number. Try again: ");
} catch (InputMismatchException e) {
scanner.nextLine(); // consume invalid input
System.out.print("Please enter a valid number for quantity: ");
}
}
}
private static double getValidPrice() {
while (true) {
try {
double price = scanner.nextDouble();
scanner.nextLine(); // consume newline
if (price >= 0) {
return Math.round(price * 100.0) / 100.0; // Round to 2 decimal places
}
System.out.print("Price must be a non-negative number. Try again: ");
} catch (InputMismatchException e) {
scanner.nextLine(); // consume invalid input
System.out.print("Please enter a valid price (up to 2 decimal points): ");
}
}
}
}
Editor is loading...
Leave a Comment