java水果店水果入库系统

java程序模拟一个水果店水果入库系统,包含登录界面和入库卷面。具备如下功能:
(1)店员在登录界面输入工号和密码。如果输入无路,则跳转入库界面,否则提示是输入工号错误还是密码错误,允许店员重新输入,直到输入正确或店员关闭登录窗口。可提前将工号和密码直接在代码中设定。
(2)店员可以在系统界面选择水果名称,输入水果重量,录入成本单价。例如可以选择苹果,系统自动显示苹果编号。入库时间,店员可手动输入苹果重量、成本单价,同样的也可以对葡萄等处理
(3)对库存中从来没有的水果,需手动添加编号、名称等信息
(4)入库界面应当显示当前入库明细,并实时更新系统库存、
(5)最后,店员确定入库明细无误,再确认,同时将最新库存自动存入文件fruitStorage.txt

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class FruitStoreInventorySystem {
    private static final String FRUIT_STORAGE_FILE = "fruitStorage.txt"; // 库存文件名
    private static final String LOGIN_ID = "123456"; // 预设的工号
    private static final String PASSWORD = "password"; // 预设的密码

    private Map<String, Fruit> fruitInventory; // 水果库存

    private Scanner scanner; // 用于接收用户输入的Scanner对象

    public FruitStoreInventorySystem() {
        fruitInventory = new HashMap<>();
        scanner = new Scanner(System.in);
    }

    // 登录界面
    public void login() {
        System.out.println("欢迎使用水果店入库系统!");
        System.out.print("请输入工号:");
        String id = scanner.nextLine();
        System.out.print("请输入密码:");
        String password = scanner.nextLine();

        if (id.equals(LOGIN_ID) && password.equals(PASSWORD)) {
            System.out.println("登录成功!");
            showInventory();
        } else {
            System.out.println("工号或密码错误,请重新输入!");
            login();
        }
    }

    // 入库界面
    public void showInventory() {
        while (true) {
            System.out.println("-----------");
            System.out.println("水果库存:");
            for (String fruitId : fruitInventory.keySet()) {
                Fruit fruit = fruitInventory.get(fruitId);
                System.out.println("编号:" + fruitId + ",名称:" + fruit.getName() + ",重量:" + fruit.getWeight()
                        + ",成本单价:" + fruit.getCostPrice());
            }
            System.out.println("-----------");
            System.out.println("请选择操作:");
            System.out.println("1. 录入水果入库");
            System.out.println("2. 手动添加水果信息");
            System.out.println("3. 确认入库明细并保存");
            System.out.println("4. 退出系统");

            int choice = getChoice(1, 4);
            switch (choice) {
                case 1:
                    enterFruitInventory();
                    break;
                case 2:
                    addFruitManually();
                    break;
                case 3:
                    confirmAndSave();
                    break;
                case 4:
                    System.out.println("谢谢使用,再见!");
                    System.exit(0);
            }
        }
    }

    // 录入水果入库
    public void enterFruitInventory() {
        System.out.println("请选择水果名称:");
        for (String fruitId : fruitInventory.keySet()) {
            Fruit fruit = fruitInventory.get(fruitId);
            System.out.println(fruitId + ". " + fruit.getName());
        }
        String fruitId = scanner.nextLine();
        if (fruitInventory.containsKey(fruitId)) {
            Fruit fruit = fruitInventory.get(fruitId);
            System.out.print("请输入水果重量:");
            double weight = getDoubleInput();
            System.out.print("请输入成本单价:");
            double costPrice = getDoubleInput();

            LocalDateTime now = LocalDateTime.now();
            String formattedDateTime = now.format(DateTimeFormatter.ofPattern("yyyy



        fruit.setWeight(weight);
        fruit.setCostPrice(costPrice);
        fruit.setEntryTime(formattedDateTime);

        System.out.println("成功录入水果入库信息:");
        System.out.println("编号:" + fruitId);
        System.out.println("名称:" + fruit.getName());
        System.out.println("重量:" + fruit.getWeight());
        System.out.println("成本单价:" + fruit.getCostPrice());
        System.out.println("入库时间:" + fruit.getEntryTime());

    } else {
        System.out.println("水果编号不存在,请选择手动添加水果信息选项进行添加。");
    }
}

// 手动添加水果信息
public void addFruitManually() {
    System.out.println("请输入新水果的编号:");
    String fruitId = scanner.nextLine();
    if (fruitInventory.containsKey(fruitId)) {
        System.out.println("编号已存在,请重新输入。");
        return;
    }
    System.out.println("请输入新水果的名称:");
    String fruitName = scanner.nextLine();
    System.out.println("请输入新水果的重量:");
    double weight = getDoubleInput();
    System.out.println("请输入新水果的成本单价:");
    double costPrice = getDoubleInput();

    Fruit fruit = new Fruit(fruitName, weight, costPrice);
    fruitInventory.put(fruitId, fruit);

    System.out.println("成功添加新水果信息:");
    System.out.println("编号:" + fruitId);
    System.out.println("名称:" + fruit.getName());
    System.out.println("重量:" + fruit.getWeight());
    System.out.println("成本单价:" + fruit.getCostPrice());
}

// 确认入库明细并保存
public void confirmAndSave() {
    System.out.println("确认入库明细并保存吗?(Y/N)");
    String choice = scanner.nextLine();
    if (choice.equalsIgnoreCase("Y")) {
        saveFruitStorageToFile();
        System.out.println("入库明细已保存至" + FRUIT_STORAGE_FILE + "文件。");
    }
}

// 保存水果库存至文件
public void saveFruitStorageToFile() {
    try (BufferedWriter writer = new BufferedWriter(new FileWriter(FRUIT_STORAGE_FILE))) {
        for (String fruitId : fruitInventory.keySet()) {
            Fruit fruit = fruitInventory.get(fruitId);
            writer.write(fruitId + "," + fruit.getName() + "," + fruit.getWeight() + "," + fruit.getCostPrice()
                    + "," + fruit.getEntryTime());
            writer.newLine();
        }
    } catch (IOException e) {
        System.out.println("保存文件失败:" + e.getMessage());
    }
}

// 获取用户输入的整数选项
public int getChoice(int min, int max) {
    int choice;
    while (true) {
        try {
            choice = Integer.parseInt(scanner.nextLine());
            if (choice >= min && choice <= max) {
                break;
            } else {
                System.out.println("请输入有效选项(" + min + " - " + max + "):");
            }
        } catch (NumberFormatException e) {
            System.out.println("请输入有效选项(" + min + " - " + max + "):");
        }
    }
    return choice;
}

// 获取用户输入的浮点数
public double getDoubleInput() {
    double value;
    while (true) {
        try {
            value= Double.parseDouble(scanner.nextLine());
break;
} catch (NumberFormatException e) {
System.out.println("请输入有效数字:");
}
}
return value;
}

typescript
Copy code
// 主程序入口
public static void main(String[] args) {
    FruitStore fruitStore = new FruitStore();
    fruitStore.initialize();
    fruitStore.login();
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

孤儿屯鼠鼠之友

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值