import java.util.*;
// 定义水果类
class Fruit {
private String name;
private double price;
private int stock;
public Fruit(String name, double price, int stock) {
this.name = name;
this.price = price;
this.stock = stock;
}
public String getName() {
return name;
}
public double getPrice() {
return price;
}
public int getStock() {
return stock;
}
public void setStock(int stock) {
this.stock = stock;
}
}
// 定义水果管理系统类
class FruitManager {
private Map<String, Fruit> fruits;
public FruitManager() {
fruits = new HashMap<>();
}
// 添加水果信息
public void addFruit(Fruit fruit) {
fruits.put(fruit.getName(), fruit);
}
// 查询水果信息
public Fruit getFruit(String name) {
return fruits.get(name);
}
// 更新水果库存
public void updateStock(String name, int stock) {
Fruit fruit = fruits.get(name);
if (fruit != null) {
fruit.setStock(stock);
}
}
// 获取水果销售统计
public Map<String, Integer> getSalesStatistics() {
Map<String, Integer> statistics = new HashMap<>();
for (Fruit fruit : fruits.values()) {
int sales = fruits.get(fruit.getName()).getStock();
statistics.put(fruit.getName(), sales);
}
return statistics;
}
}
// 测试水果管理系统类
public class Main {
public static void main(String[] args) {
FruitManager fruitManager = new FruitManager();
// 添加水果信息
fruitManager.addFruit(new Fruit("Apple", 3.0, 10));
fruitManager.addFruit(new Fruit("Banana", 2.0, 5));
fruitManager.addFruit(new Fruit("Orange", 4.0, 8));
// 查询水果信息
Fruit apple = fruitManager.getFruit("Apple");
System.out.println("Apple price: " + apple.getPrice());
System.out.println("Apple stock: " + apple.getStock());
// 更新水果库存
fruitManager.updateStock("Apple", 5);
// 获取水果销售统计
Map<String, Integer> statistics = fruitManager.getSalesStatistics();
System.out.println("Sales statistics: " + statistics);
}
}
上述UI主页
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class FruitStore {
public static void main(String[] args) {
JFrame frame = new JFrame("Fruit Store");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(new Dimension(1200, 800));
JPanel panel = new JPanel();
frame.add(panel);
panel.setLayout(null);
// Navbar
JPanel navbar = new JPanel();
navbar.setBackground(Color.GRAY);
navbar.setBounds(0, 0, 1200, 30);
panel.add(navbar);
JButton homeButton = new JButton("Home");
homeButton.setBounds(20, 5, 80, 25);
navbar.add(homeButton);
JButton productButton = new JButton("Products");
productButton.setBounds(110, 5, 80, 25);
navbar.add(productButton);
JButton serviceButton = new JButton("Services");
serviceButton.setBounds(200, 5, 80, 25);
navbar.add(serviceButton);
JButton aboutButton = new JButton("About Us");
aboutButton.setBounds(310, 5, 80, 25);
navbar.add(aboutButton);
// Carousel
JPanel carousel = new JPanel();
carousel.setBounds(0, 35, 1200, 450);
panel.add(carousel);
carousel.setLayout(new java.awt.FlowLayout());
JLabel appleLabel = new JLabel("Apple - $3.00");
appleLabel.setIcon(new javax.swing.ImageIcon("apple.jpg"));
carousel.add(appleLabel);
JLabel bananaLabel = new JLabel("Banana - $2.00");
bananaLabel.setIcon(new javax.swing.ImageIcon("banana.jpg"));
carousel.add(bananaLabel);
JLabel orangeLabel = new JLabel("Orange - $4.00");
orangeLabel.setIcon(new javax.swing.ImageIcon("orange.jpg"));
carousel.add(orangeLabel);
// Carousel end
// Product
JPanel product = new JPanel();
product.setBounds(0, 520, 1200, 280);
panel.add(product);
product.setLayout(new java.awt.FlowLayout());
JLabel appleProduct = new JLabel("Apple - $3.00");
appleProduct.setIcon(new javax.swing.ImageIcon("apple.jpg"));
product.add(appleProduct); // Product end // 产品名称和价格可根据实际需求进行更改。 在上面的代码中,我们使用Java语言创建了一个简单的水果超市管理系统的前端页面,并且包含图形用户界面。该页面包括一个导航栏、轮播图、特色产品展示区域以及页脚。你可以根据实际需求修改代码中的标记和样式,以适应你的项目。请确保将实际的图片文件命名为apple.jpg、banana .jpg等,并将路径指定为正确的位置。这样,代码中的图像标记才能正确引用到对应的图片资源。如果你还有其他需求或需要进一步的帮助,请随时告诉我。祝你的项目取得成功! // 下面的代码演示了如何为产品按钮添加点击事件监听器,以显示产品详情。 // 在实际项目中,你可以根据需要添加更多功能和交互。 // Product end 43 } // Carousel end 39 } // navbar end panel = new JPanel();frame=new JFrame ("Fruit Store Home Page ");frame .setDefaultCloseOperation (JFrame .EXIT_ON
标签:水果,管理系统,JPanel,超市,add,Fruit,new,public,stock
From: https://www.cnblogs.com/asd1235/p/17771724.html