myself

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;
import javax.swing.*;

class Fruit {
    private String id;
    private String name;
    private double weight;
    private double cost;

    public Fruit(String id, String name, double weight, double cost) {
        this.id = id;
        this.name = name;
        this.weight = weight;
        this.cost = cost;
    }

    public String getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public double getWeight() {
        return weight;
    }

    public double getCost() {
        return cost;
    }
}

class Login extends JFrame implements ActionListener {
    private JTextField usernameField;
    private JPasswordField passwordField;

    public Login() {
        super("登录");
        setLayout(new GridLayout(3, 2));
        add(new JLabel("工号:"));
        usernameField = new JTextField(10);
        add(usernameField);
        add(new JLabel("密码:"));
        passwordField = new JPasswordField(10);
        add(passwordField);
        JButton loginButton = new JButton("登录");
        loginButton.addActionListener(this);
        add(loginButton);
        pack();
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        String username = usernameField.getText();
        String password = new String(passwordField.getPassword());
        if (username.equals("admin") && password.equals("password")) {
            new InStorageFrame();
            dispose();
        } else {
            JOptionPane.showMessageDialog(this, "工号或密码错误", "错误", JOptionPane.ERROR_MESSAGE);
            usernameField.setText("");
            passwordField.setText("");
            usernameField.requestFocus();
        }
    }
}

class InStorageFrame extends JFrame implements ActionListener {
    private JComboBox<String> fruitComboBox;
    private JTextField weightField;
    private JTextField costField;
    private JTextArea detailArea;
    private ArrayList<Fruit> fruits;

    public InStorageFrame() {
        super("入库");
        fruits = new ArrayList<>();
        setLayout(new BorderLayout());
        JPanel topPanel = new JPanel(new GridLayout(3, 2));
        topPanel.add(new JLabel("水果名称:"));
        fruitComboBox = new JComboBox<>(new String[]{"苹果", "葡萄"});
        topPanel.add(fruitComboBox);
        topPanel.add(new JLabel("重量(kg):"));
        weightField = new JTextField(10);
        topPanel.add(weightField);
        topPanel.add(new JLabel("成本单价:"));
        costField = new JTextField(10);
        topPanel.add(costField);
        add(topPanel, BorderLayout.NORTH);
        JPanel centerPanel = new JPanel(new BorderLayout());
        centerPanel.add(new JLabel("入库明细:"), BorderLayout.NORTH);
        detailArea = new JTextArea(10, 30);
        detailArea.setEditable(false);
        centerPanel.add(new JScrollPane(detailArea), BorderLayout.CENTER);
        add(centerPanel, BorderLayout.CENTER);
        JPanel bottomPanel = new JPanel();
        JButton addButton = new JButton("添加");
        addButton.addActionListener(this);
        bottomPanel.add(addButton);
        JButton confirmButton = new JButton("确认");
        confirmButton.addActionListener(this);
        bottomPanel.add(confirmButton);
        add(bottomPanel, BorderLayout.SOUTH);
        pack();
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        String command = e.getActionCommand();
        if (command.equals("添加")) {
            String name = (String) fruitComboBox.getSelectedItem();
            double weight = Double.parseDouble(weightField.getText());
            double cost = Double.parseDouble(costField.getText());
            String id = UUID.randomUUID().toString();
            Fruit fruit = new Fruit(id, name, weight, cost);
            fruits.add(fruit);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String time = sdf.format(new Date());
            detailArea.append(String.format("%s %s %s %.2fkg %.2f元/kg\n", time, id, name, weight, cost));
            weightField.setText("");
            costField.setText("");
            weightField.requestFocus();
        } else if (command.equals("确认")) {
            try (PrintWriter pw = new PrintWriter(new FileWriter("fruitStorage.txt"))) {
                for (Fruit fruit : fruits) {
                    pw.println(fruit.getId() + " " + fruit.getName() + " " + fruit.getWeight() + " " + fruit.getCost());
                }
                JOptionPane.showMessageDialog(this, "入库成功", "成功", JOptionPane.INFORMATION_MESSAGE);
                detailArea.setText("");
                fruits.clear();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
}

public class exf {
    public static void main(String[] args) {
        new Login();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值