超市商品管理系统java源代码_代码示例

xl1407

温馨提示:这篇文章已超过245天没有更新,请注意相关的内容是否还可用!

超市商品管理系统java源代码_代码示例

超市商品管理系统是一个用于管理超市商品信息的系统。通过该系统,可以对商品进行添加、删除、修改和查询等操作,实现对商品信息的全面管理。

下面是一个简单的超市商品管理系统的Java源代码示例:

import java.util.ArrayList;

import java.util.List;

public class SupermarketProductManagementSystem {

private List<Product> products;

public SupermarketProductManagementSystem() {

products = new ArrayList<>();

}

public void addProduct(Product product) {

products.add(product);

}

public void removeProduct(Product product) {

products.remove(product);

}

public void updateProduct(Product product) {

for (int i = 0; i < products.size(); i++) {

if (products.get(i).getId() == product.getId()) {

products.set(i, product);

break;

}

}

}

public Product getProductById(int id) {

for (Product product : products) {

if (product.getId() == id) {

return product;

}

}

return null;

}

public List<Product> getAllProducts() {

return products;

}

public static void main(String[] args) {

SupermarketProductManagementSystem system = new SupermarketProductManagementSystem();

// 添加商品

Product product1 = new Product(1, "苹果", 5.0);

system.addProduct(product1);

Product product2 = new Product(2, "香蕉", 3.0);

system.addProduct(product2);

// 查询商品

Product queriedProduct = system.getProductById(1);

System.out.println("查询到的商品信息:" + queriedProduct);

// 更新商品

Product updatedProduct = new Product(1, "橙子", 4.0);

system.updateProduct(updatedProduct);

// 删除商品

system.removeProduct(product2);

}

}

class Product {

private int id;

private String name;

private double price;

public Product(int id, String name, double price) {

this.id = id;

this.name = name;

this.price = price;

}

public int getId() {

return id;

}

public String getName() {

return name;

}

public double getPrice() {

return price;

}

@Override

public String toString() {

return "Product{" +

"id=" + id +

", name='" + name + '\'' +

", price=" + price +

'}';

}

}

以上示例代码演示了一个简单的超市商品管理系统的实现。通过`SupermarketProductManagementSystem`类,可以进行商品的添加、删除、更新和查询等操作。商品信息通过`Product`类进行封装,其中包括商品的id、名称和价格等属性。通过调用相应的方法,可以实现对商品信息的管理。

文章版权声明:除非注明,否则均为莫宇前端原创文章,转载或复制请以超链接形式并注明出处。

取消
微信二维码
微信二维码
支付宝二维码