如何用JAVA写超市管理系统?
时间: 2024-01-01 19:03:13 浏览: 24
超市管理系统一般包含以下模块:商品管理、库存管理、销售管理、用户管理、权限管理、报表统计等。
以下是一个简单的超市管理系统的JAVA代码框架:
```
//商品类
public class Product {
private String name; //商品名称
private double price; //商品价格
private int count; //商品库存数量
//构造函数
public Product(String name, double price, int count) {
this.name = name;
this.price = price;
this.count = count;
}
//getter和setter方法
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
}
//库存管理类
public class Inventory {
private List<Product> products; //商品列表
//构造函数
public Inventory() {
this.products = new ArrayList<Product>();
}
//添加商品
public void addProduct(Product product) {
products.add(product);
}
//根据名称查找商品
public Product findProductByName(String name) {
for (Product product : products) {
if (product.getName().equals(name)) {
return product;
}
}
return null;
}
//更新商品库存数量
public void updateProductCount(String name, int count) {
Product product = findProductByName(name);
if (product != null) {
product.setCount(count);
}
}
}
//销售管理类
public class Sales {
private List<Order> orders; //订单列表
//构造函数
public Sales() {
this.orders = new ArrayList<Order>();
}
//添加订单
public void addOrder(Order order) {
orders.add(order);
}
//计算总销售额
public double getTotalSales() {
double totalSales = 0;
for (Order order : orders) {
totalSales += order.getTotalPrice();
}
return totalSales;
}
}
//订单类
public class Order {
private List<OrderItem> items; //订单项列表
//构造函数
public Order() {
this.items = new ArrayList<OrderItem>();
}
//添加订单项
public void addOrderItem(OrderItem item) {
items.add(item);
}
//计算订单总价
public double getTotalPrice() {
double totalPrice = 0;
for (OrderItem item : items) {
totalPrice += item.getTotalPrice();
}
return totalPrice;
}
}
//订单项类
public class OrderItem {
private Product product; //商品
private int count; //数量
//构造函数
public OrderItem(Product product, int count) {
this.product = product;
this.count = count;
}
//计算订单项总价
public double getTotalPrice() {
return product.getPrice() * count;
}
}
//用户类
public class User {
private String username; //用户名
private String password; //密码
private int role; //角色
//构造函数
public User(String username, String password, int role) {
this.username = username;
this.password = password;
this.role = role;
}
//getter和setter方法
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getRole() {
return role;
}
public void setRole(int role) {
this.role = role;
}
}
//权限管理类
public class Authorization {
private Map<String, Integer> roles; //角色列表
//构造函数
public Authorization() {
this.roles = new HashMap<String, Integer>();
}
//添加角色
public void addRole(String username, int role) {
roles.put(username, role);
}
//根据用户名查找角色
public int findRoleByUsername(String username) {
if (roles.containsKey(username)) {
return roles.get(username);
}
return -1;
}
}
//主函数
public class Main {
public static void main(String[] args) {
//初始化商品列表
Inventory inventory = new Inventory();
Product product1 = new Product("可乐", 3.0, 100);
Product product2 = new Product("薯片", 5.0, 50);
inventory.addProduct(product1);
inventory.addProduct(product2);
//创建订单
Order order = new Order();
OrderItem item1 = new OrderItem(product1, 2);
OrderItem item2 = new OrderItem(product2, 3);
order.addOrderItem(item1);
order.addOrderItem(item2);
//添加订单到销售列表
Sales sales = new Sales();
sales.addOrder(order);
//输出销售总额
System.out.println("总销售额: " + sales.getTotalSales());
}
}
```
以上代码只是一个简单的框架,实际开发中还需要考虑更多的细节问题。