import java.util.ArrayList; import java.util.Scanner;

public class Sales { private ArrayList productCatalogue; private ArrayList salesHistory;

public Sales() {
    this.productCatalogue = new ArrayList<>();
    this.salesHistory = new ArrayList<>();
}

public void addProductToCatalogue(Product product) {
    if (productCatalogue.size() < 10) {
        productCatalogue.add(product);
        System.out.println('Product added to the catalogue.');
    } else {
        System.out.println('There are already 10 products in the system.');
    }
}

public void displayProductCatalogue() {
    if (productCatalogue.isEmpty()) {
        System.out.println('There are no products currently available in the catalogue.');
    } else {
        System.out.println('The following is the list of products in the catalogue:');
        for (int i = 0; i < productCatalogue.size(); i++) {
            System.out.println((i + 1) + '. ' + productCatalogue.get(i));
        }
    }
}

public void assignBuyer() {
    Scanner scanner = new Scanner(System.in);
    System.out.print('Please enter the name of the customer: ');
    String name = scanner.nextLine();
    System.out.print('What is the customer's phone number: ');
    String phoneNumber = scanner.nextLine();
    System.out.print('What is the customer's email address: ');
    String emailAddress = scanner.nextLine();
    System.out.print('What is the customer's home address: ');
    String homeAddress = scanner.nextLine();
    System.out.print('What is the postcode: ');
    String postcode = scanner.nextLine();

    Buyer buyer = new Buyer(name, phoneNumber, emailAddress, homeAddress, postcode);
    salesHistory.add(buyer);

    System.out.print('How many different products are being purchased: ');
    int numProducts = Integer.parseInt(scanner.nextLine());

    System.out.println('The following is the list of products in the catalogue:');
    displayProductCatalogue();
    System.out.println((productCatalogue.size() + 1) + '. Skip Purchase');

    for (int i = 0; i < numProducts; i++) {
        System.out.print('What type of product would you like to add to the purchase: ');
        int productIndex = Integer.parseInt(scanner.nextLine()) - 1;

        if (productIndex < productCatalogue.size()) {
            Product product = productCatalogue.get(productIndex);
            System.out.println('You selected ' + product.getName() + '.');
            System.out.print('How many of that do you want to purchase: ');
            int quantity = Integer.parseInt(scanner.nextLine());

            if (quantity > product.getAvailability()) {
                System.out.println('The purchase cannot be completed due to the unavailability of the product.');
                System.out.println('Please select another product.');
                i--;
            } else {
                product.setAvailability(product.getAvailability() - quantity);
                double totalCost = quantity * product.getPrice();
                String purchase = quantity + ' x ' + product.getName() + ' @ $' + product.getPrice() + ' = $' + totalCost;
                buyer.addPurchase(purchase);
                System.out.println('All items are now added to the customer’s purchase. The purchase is now complete.');
            }
        } else {
            System.out.println('Skip Purchase');
        }
    }
}

public void displaySalesHistory() {
    if (salesHistory.isEmpty()) {
        System.out.println('There is no sales history available.');
    } else {
        System.out.println('The following is the sales history:');
        for (int i = 0; i < salesHistory.size(); i++) {
            Buyer buyer = salesHistory.get(i);
            System.out.println((i + 1) + '. ' + buyer.getName() + ' (email: ' + buyer.getEmailAddress() + ') [total cost: $' + buyer.calculateTotalCost() + ']');
        }
        System.out.println('Total revenue from the sales so far: $' + calculateTotalRevenue());
    }
}

private double calculateTotalRevenue() {
    double totalRevenue = 0;
    for (Buyer buyer : salesHistory) {
        totalRevenue += buyer.calculateTotalCost();
    }
    return totalRevenue;
}

}

Java Sales Management System: Track Products, Customers, and Revenue

原文地址: https://www.cveoy.top/t/topic/b4xR 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录