入库登记同时库存也要更新,代码
时间: 2024-04-05 20:30:40 浏览: 21
好的,以下是一个示例代码,它实现了物品入库登记和库存更新:
```
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class InventoryManagementSystem {
private JFrame mainFrame;
private JLabel headerLabel;
private JPanel controlPanel;
private int inventory = 0; // 初始库存为0
public InventoryManagementSystem(){
prepareGUI();
}
public static void main(String[] args){
InventoryManagementSystem inventoryManagementSystem = new InventoryManagementSystem();
inventoryManagementSystem.showEventDemo();
}
private void prepareGUI(){
mainFrame = new JFrame("Inventory Management System");
mainFrame.setSize(400,400);
mainFrame.setLayout(new GridLayout(3, 1));
mainFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent){
System.exit(0);
}
});
headerLabel = new JLabel("", JLabel.CENTER);
controlPanel = new JPanel();
controlPanel.setLayout(new FlowLayout());
mainFrame.add(headerLabel);
mainFrame.add(controlPanel);
mainFrame.setVisible(true);
}
private void showEventDemo(){
headerLabel.setText("Inventory Management System");
JLabel nameLabel= new JLabel("Name: ", JLabel.RIGHT);
final JTextField nameText = new JTextField(6);
JLabel quantityLabel = new JLabel("Quantity: ", JLabel.RIGHT);
final JTextField quantityText = new JTextField(6);
JLabel priceLabel = new JLabel("Price: ", JLabel.RIGHT);
final JTextField priceText = new JTextField(6);
JButton submitButton = new JButton("Submit");
submitButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String name = nameText.getText();
int quantity = Integer.parseInt(quantityText.getText());
double price = Double.parseDouble(priceText.getText());
// 将物品信息添加到数据结构中
// 假设数据结构是一个Map,key为物品名称,value为物品信息对象
Map<String, Item> inventoryMap = new HashMap<String, Item>();
Item item = new Item(name, quantity, price);
inventoryMap.put(name, item);
// 更新库存
inventory += quantity;
JOptionPane.showMessageDialog(mainFrame, "Item added successfully");
}
});
controlPanel.add(nameLabel);
controlPanel.add(nameText);
controlPanel.add(quantityLabel);
controlPanel.add(quantityText);
controlPanel.add(priceLabel);
controlPanel.add(priceText);
controlPanel.add(submitButton);
mainFrame.setVisible(true);
}
class Item {
private String name;
private int quantity;
private double price;
public Item(String name, int quantity, double price) {
this.name = name;
this.quantity = quantity;
this.price = price;
}
public String getName() {
return name;
}
public int getQuantity() {
return quantity;
}
public double getPrice() {
return price;
}
}
}
```
这段代码与之前的示例代码相似,不同之处在于它使用了一个Map来存储物品信息,同时在添加物品时更新了库存。请注意,这只是一个示例,实际的实现可能涉及到更多的细节和逻辑。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)