当然,以下是将代码分开为多个文件的示例。每个类都将放置在单独的文件中,以便更好地组织和管理代码。

'Product.java':

public class Product {
    private String name;
    private double price;
    private int availability;

    public Product(String name, double price, int availability) {
        this.name = name;
        this.price = price;
        this.availability = availability;
    }

    public String getName() {
        return name;
    }

    public double getPrice() {
        return price;
    }

    public int getAvailability() {
        return availability;
    }

    public void setAvailability(int availability) {
        this.availability = availability;
    }

    @Override
    public String toString() {
        return name + " ($" + price + " each) [" + availability + " items are available]";
    }
}

'Buyer.java':

import java.util.ArrayList;

public class Buyer {
    private String name;
    private String phoneNumber;
    private String emailAddress;
    private String homeAddress;
    private String postcode;
    private ArrayList<String> purchaseSummary;

    public Buyer(String name, String phoneNumber, String emailAddress, String homeAddress, String postcode) {
        this.name = name;
        this.phoneNumber = phoneNumber;
        this.emailAddress = emailAddress;
        this.homeAddress = homeAddress;
        this.postcode = postcode;
        this.purchaseSummary = new ArrayList<>();
    }

    public String getName() {
        return name;
    }

    public String getEmailAddress() {
        return emailAddress;
    }

    public void addPurchase(String purchase) {
        purchaseSummary.add(purchase);
    }

    public String getPurchaseSummary() {
        StringBuilder summary = new StringBuilder();
        summary.append("Customer: ").append(name).append("\n");
        summary.append("Phone number: ").append(phoneNumber).append("\n");
        summary.append("Email address: ").append(emailAddress).append("\n");
        summary.append("Home address: ").append(homeAddress).append("\n");
        summary.append("Postcode: ").append(postcode).append("\n");
        summary.append("Items:").append("\n");
        for (String purchase : purchaseSummary) {
            summary.append(purchase).append("\n");
        }
        summary.append("Total Cost: $").append(calculateTotalCost()).append("\n");
        return summary.toString();
    }

    private double calculateTotalCost() {
        double totalCost = 0;
        for (String purchase : purchaseSummary) {
            int startIndex = purchase.lastIndexOf("=" + 2);
            int endIndex = purchase.lastIndexOf(".");
            double cost = Double.parseDouble(purchase.substring(startIndex, endIndex).replace(",", ""));
            totalCost += cost;
        }
        return totalCost;
    }
}

'Sales.java':

import java.util.ArrayList;

public class Sales {
    private ArrayList<Product> productCatalogue;
    private ArrayList<Buyer> 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() {
        // Implementation of buyer assignment
    }

    public void displaySalesHistory() {
        // Implementation of sales history display
    }

    private double calculateTotalRevenue() {
        // Implementation of revenue calculation
    }
}

'ECommerceSalesManagementSoftware.java':

import java.util.Scanner;

public class ECommerceSalesManagementSoftware {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        Sales sales = new Sales();

        String choice = "";
        while (!choice.equals("5")) {
            System.out.println("Menu:");
            System.out.println("1. Add products to the sales catalogue");
            System.out.println("2. Get the list of all available products");
            System.out.println("3. Assign new buyers");
            System.out.println("4. Show the company's sales history and revenue");
            System.out.println("5. Exit");
            System.out.print("Enter your choice: ");
            choice = scanner.nextLine();

            switch (choice) {
                case "1":
                    System.out.print("Enter the product's name: ");
                    String productName = scanner.nextLine();
                    System.out.print("Enter the product's price: ");
                    double productPrice = Double.parseDouble(scanner.nextLine());
                    System.out.print("Enter the product's availability: ");
                    int productAvailability = Integer.parseInt(scanner.nextLine());
                    Product product = new Product(productName, productPrice, productAvailability);
                    sales.addProductToCatalogue(product);
                    break;
                case "2":
                    sales.displayProductCatalogue();
                    break;
                case "3":
                    sales.assignBuyer();
                    break;
                case "4":
                    sales.displaySalesHistory();
                    break;
                case "5":
                    System.out.println("Exiting the program...");
                    break;
                default:
                    System.out.println("Invalid choice. Please try again.");
            }
            System.out.println();
        }
    }
}

将每个类放在单独的文件中,可以更好地组织和维护代码。确保在相同的目录结构下编译和运行这些文件。希望这个样例可以满足你的需求。如果你有任何进一步的问题,请随时提问。

Java 代码组织:将多个类拆分为独立文件

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

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