java程序模拟一个水果店水果入库系统,包含登录界面和入库卷面。具备如下功能:
(1)店员在登录界面输入工号和密码。如果输入无路,则跳转入库界面,否则提示是输入工号错误还是密码错误,允许店员重新输入,直到输入正确或店员关闭登录窗口。可提前将工号和密码直接在代码中设定。
(2)店员可以在系统界面选择水果名称,输入水果重量,录入成本单价。例如可以选择苹果,系统自动显示苹果编号。入库时间,店员可手动输入苹果重量、成本单价,同样的也可以对葡萄等处理
(3)对库存中从来没有的水果,需手动添加编号、名称等信息
(4)入库界面应当显示当前入库明细,并实时更新系统库存、
(5)最后,店员确定入库明细无误,再确认,同时将最新库存自动存入文件fruitStorage.txt
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.swing.*;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
class Fruit {
private String fruitId;
private String fruitName;
private double fruitWeight;
private double fruitCostPrice;
public Fruit(String fruitId, String fruitName, double fruitWeight, double fruitCostPrice) {
this.fruitId = fruitId;
this.fruitName = fruitName;
this.fruitWeight = fruitWeight;
this.fruitCostPrice = fruitCostPrice;
}
public String getFruitId() {
return fruitId;
}
public String getFruitName() {
return fruitName;
}
public double getFruitWeight() {
return fruitWeight;
}
public double getFruitCostPrice() {
return fruitCostPrice;
}
@Override
public String toString() {
return "水果编号:" + fruitId +
",水果名称:" + fruitName +
",水果重量:" + fruitWeight +
"kg,水果成本价:" + fruitCostPrice +
"元/kg";
}
}
class FruitInventorySystem {
private static final String USERNAME = "12345"; // 预设用户名
private static final String PASSWORD = "1234567"; // 预设密码
private static final String STORAGE_FILE_PATH = "fruitStorage.txt"; // 存储库存信息的文件路径
private static Map<String, Fruit> fruitInventory = new HashMap<>(); // 水果库存信息
private static JFrame frame;
private static JTextField usernameField;
private static JPasswordField passwordField;
private static JTextArea outputArea;
private static JTextField fruitNameField;
private static JTextField fruitIdField;
private static JTextField fruitWeightField;
private static JTextField fruitCostPriceField;
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGUI());
}
private static void createAndShowGUI() {
frame = new JFrame("水果店入库系统");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel loginPanel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.WEST;
gbc.insets = new Insets(5, 5, 5, 5);
JLabel usernameLabel = new JLabel("工号:");
loginPanel.add(usernameLabel, gbc);
usernameField = new JTextField(15);
gbc.gridx = 1;
loginPanel.add(usernameField, gbc);
JLabel passwordLabel = new JLabel("密码:");
gbc.gridx = 0;
gbc.gridy = 1;
loginPanel.add(passwordLabel, gbc);
passwordField = new JPasswordField(15);
gbc.gridx = 1;
loginPanel.add(passwordField, gbc);
JButton loginButton = new JButton("登录");
gbc.gridx = 0;
gbc.gridy = 2;
gbc.gridwidth = 2;
gbc.anchor = GridBagConstraints.CENTER;
loginButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String username = usernameField.getText();
String password = String.valueOf(passwordField.getPassword());
if (username.equals(USERNAME) && password.equals(PASSWORD)) {
showInventoryScreen();
} else {
JOptionPane.showMessageDialog(frame, "工号或密码错误,请重新输入!", "登录失败", JOptionPane.ERROR_MESSAGE);
}
}
});
loginPanel.add(loginButton, gbc);
frame.getContentPane().add(loginPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private static void showInventoryScreen() {
frame.getContentPane().removeAll();
frame.setTitle("水果库存管理");
frame.setLayout(new BorderLayout());
JPanel inputPanel = new JPanel(new GridLayout(5, 2, 5, 5));
inputPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
JLabel fruitIdLabel = new JLabel("水果编号:");
inputPanel.add(fruitIdLabel);
fruitIdField = new JTextField();
inputPanel.add(fruitIdField);
JLabel fruitNameLabel = new JLabel("水果名称:");
inputPanel.add(fruitNameLabel);
fruitNameField = new JTextField();
inputPanel.add(fruitNameField);
JLabel fruitWeightLabel = new JLabel("水果重量(kg):");
inputPanel.add(fruitWeightLabel);
fruitWeightField = new JTextField();
inputPanel.add(fruitWeightField);
JLabel fruitCostPriceLabel = new JLabel("水果成本价(元/kg):");
inputPanel.add(fruitCostPriceLabel);
fruitCostPriceField = new JTextField();
inputPanel.add(fruitCostPriceField);
JButton addButton = new JButton("添加水果");
addButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String fruitId = fruitIdField.getText();
String fruitName = fruitNameField.getText();
double fruitWeight = Double.parseDouble(fruitWeightField.getText());
double fruitCostPrice = Double.parseDouble(fruitCostPriceField.getText());
Fruit fruit = new Fruit(fruitId, fruitName, fruitWeight, fruitCostPrice);
fruitInventory.put(fruitId, fruit);
outputArea.append("水果已添加至库存:" + fruit.toString() + "\n");
clearInputFields();
saveInventoryToFile();
}
});
inputPanel.add(addButton);
JButton viewButton = new JButton("查看库存");
viewButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
outputArea.setText("当前库存信息:\n");
for (Fruit fruit : fruitInventory.values()) {
outputArea.append(fruit.toString() + "\n");
}
}
});
inputPanel.add(viewButton);
frame.add(inputPanel, BorderLayout.NORTH);
outputArea = new JTextArea();
outputArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(outputArea);
frame.add(scrollPane, BorderLayout.CENTER);
JButton logoutButton = new JButton("退出登录");
logoutButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
fruitInventory.clear();
frame.getContentPane().removeAll();
frame.setTitle("水果店入库系统");
createAndShowGUI();
}
});
frame.add(logoutButton, BorderLayout.SOUTH);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private static void clearInputFields() {
fruitIdField.setText("");
fruitNameField.setText("");
fruitWeightField.setText("");
fruitCostPriceField.setText("");
}
private static void saveInventoryToFile() {
try (BufferedWriter writer = new BufferedWriter(new FileWriter(STORAGE_FILE_PATH))) {
for (Fruit fruit : fruitInventory.values()) {
writer.write(fruit.getFruitId() + "," +
fruit.getFruitName() + "," +
//fruit.getF() + "," +
fruit.getFruitWeight() + "," +
fruit.getFruitCostPrice());
writer.newLine();
}
writer.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
private static void loadInventoryFromFile() {
try (BufferedReader reader = new BufferedReader(new FileReader(STORAGE_FILE_PATH))) {
String line;
while ((line = reader.readLine()) != null) {
String[] fields = line.split(",");
if (fields.length == 5) {
String fruitId = fields[0];
String fruitName = fields[1];
double fruitWeight = Double.parseDouble(fields[2]);
double fruitCostPrice = Double.parseDouble(fields[3]);
Fruit fruit = new Fruit(fruitId, fruitName, fruitWeight, fruitCostPrice);
fruitInventory.put(fruitId, fruit);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
private static class Fruit {
private String fruitId;
private String fruitName;
private double fruitWeight;
private double fruitCostPrice;
public Fruit(String fruitId, String fruitName, double fruitWeight, double fruitCostPrice) {
this.fruitId = fruitId;
this.fruitName = fruitName;
this.fruitWeight = fruitWeight;
this.fruitCostPrice = fruitCostPrice;
}
public String getFruitId() {
return fruitId;
}
public String getFruitName() {
return fruitName;
}
public double getFruitWeight() {
return fruitWeight;
}
public double getFruitCostPrice() {
return fruitCostPrice;
}
@Override
public String toString() {
return "编号:" + fruitId + ",名称:" + fruitName + ",重量:" + fruitWeight + "kg,成本价:" + fruitCostPrice + "元/kg";
}
}
}