以下是一个简单的Java书籍销售程序示例,该程序使用数组存储书籍信息和订单明细信息:

import java.util.Scanner;

public class BookSalesProgram {
    public static void main(String[] args) {
        String[] bookCodes = {"B001", "B002", "B003", "B004", "B005"};
        String[] bookNames = {"书籍1", "书籍2", "书籍3", "书籍4", "书籍5"};
        double[] bookPrices = {10.0, 15.0, 20.0, 25.0, 30.0};

        int[] orderQuantities = new int[bookCodes.length];
        double[] orderAmounts = new double[bookCodes.length];

        Scanner scanner = new Scanner(System.in);
        boolean continueShopping = true;

        while (continueShopping) {
            System.out.println("请输入书籍序列号进行购买(输入其他字符退出购买):");
            String bookCode = scanner.nextLine();

            int bookIndex = -1;
            for (int i = 0; i < bookCodes.length; i++) {
                if (bookCodes[i].equals(bookCode)) {
                    bookIndex = i;
                    break;
                }
            }

            if (bookIndex == -1) {
                System.out.println("书籍序列号不存在,请重新输入。");
                continue;
            }

            System.out.print("请输入购买数量: ");
            int quantity = scanner.nextInt();
            scanner.nextLine();

            orderQuantities[bookIndex] += quantity;
            orderAmounts[bookIndex] += bookPrices[bookIndex] * quantity;

            System.out.println("是否继续购买?(输入 Y/y 继续购买,其他字符结束购买)");
            String continueInput = scanner.nextLine();

            if (!continueInput.equalsIgnoreCase("Y")) {
                continueShopping = false;
            }
        }

        System.out.println("订单信息:");
        System.out.println("书籍序列号	书籍名称	购买数量	总金额");
        for (int i = 0; i < bookCodes.length; i++) {
            if (orderQuantities[i] > 0) {
                System.out.println(bookCodes[i] + "\t" + bookNames[i] + "\t" + orderQuantities[i] + "\t\t" + orderAmounts[i]);
            }
        }
    }
}

在上述代码中,我们定义了一个 BookSalesProgram 类。程序中的书籍信息包括书籍序列号、书籍名称和书籍价格,分别存储在 bookCodesbookNamesbookPrices 数组中。

程序使用 orderQuantitiesorderAmounts 数组分别存储订单中每本书籍的购买数量和总金额。

在主程序的 while 循环中,用户被要求输入书籍序列号和购买数量,然后将购买数量和金额更新到订单数组中。

程序会询问用户是否继续购买,如果输入的是 'Y' 或 'y',则继续购买;否则,结束购买。

在结束购买后,程序会打印出订单信息,包括书籍序列号、书籍名称、购买数量和总金额。

请根据实际需求添加和修改代码,以满足特定的需求。希望能对你有所帮助!

Java书籍销售程序:使用数组实现简单购物流程

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

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