Java实现商品管理系统代码
时间: 2023-08-14 11:07:08 浏览: 210
java实现商品管理系统
### 回答1:
以下是一个简单的Java商品管理系统代码示例:
```java
import java.util.Scanner;
public class ProductManagementSystem {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int choice;
boolean quit = false;
Product[] products = new Product[10];
int productCount = 0;
while (!quit) {
System.out.println("1. Add a new product");
System.out.println("2. List all products");
System.out.println("3. Search for a product");
System.out.println("4. Remove a product");
System.out.println("0. Quit");
System.out.print("Enter your choice: ");
choice = scanner.nextInt();
switch (choice) {
case 0:
quit = true;
break;
case 1:
if (productCount < products.length) {
System.out.print("Enter the product name: ");
String name = scanner.next();
System.out.print("Enter the product price: ");
double price = scanner.nextDouble();
Product product = new Product(name, price);
products[productCount] = product;
productCount++;
} else {
System.out.println("Product list is full.");
}
break;
case 2:
if (productCount == 0) {
System.out.println("No products found.");
} else {
for (int i = 0; i < productCount; i++) {
System.out.println(products[i]);
}
}
break;
case 3:
if (productCount == 0) {
System.out.println("No products found.");
} else {
System.out.print("Enter the product name: ");
String name = scanner.next();
boolean found = false;
for (int i = 0; i < productCount; i++) {
if (products[i].getName().equals(name)) {
System.out.println(products[i]);
found = true;
break;
}
}
if (!found) {
System.out.println("Product not found.");
}
}
break;
case 4:
if (productCount == 0) {
System.out.println("No products found.");
} else {
System.out.print("Enter the product name: ");
String name = scanner.next();
boolean found = false;
for (int i = 0; i < productCount; i++) {
if (products[i].getName().equals(name)) {
for (int j = i; j < productCount - 1; j++) {
products[j] = products[j + 1];
}
productCount--;
found = true;
break;
}
}
if (!found) {
System.out.println("Product not found.");
}
}
break;
default:
System.out.println("Invalid choice.");
break;
}
System.out.println();
}
}
}
class Product {
private String name;
private double price;
public Product(String name, double price) {
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public double getPrice() {
return price;
}
public String toString() {
return name + ", " + price;
}
}
```
该代码实现了简单的商品管理系统,可以添加商品、列出所有商品、搜索商品以及删除商品。通过Scanner类获取用户输入,使用数组存储商品信息。Product类表示商品,有名称和价格两个属性,并实现了toString方法以便于输出。
### 回答2:
Java实现商品管理系统代码需要以下几个关键步骤:
1. 设计商品类:
class Product {
private String productId;
private String name;
private double price;
// 其他属性和方法省略
}
2. 创建商品管理系统类:
import java.util.ArrayList;
import java.util.List;
public class ProductManagementSystem {
private List<Product> productList;
public ProductManagementSystem() {
productList = new ArrayList<>();
}
public void addProduct(Product product) {
productList.add(product);
}
public void removeProduct(Product product) {
productList.remove(product);
}
public Product getProductById(String productId) {
for (Product product : productList) {
if (product.getProductId().equals(productId)) {
return product;
}
}
return null;
}
// 其他操作方法省略
}
3. 创建测试类:
public class Test {
public static void main(String[] args) {
ProductManagementSystem system = new ProductManagementSystem();
// 添加商品
Product product1 = new Product("1", "商品1", 10.0);
system.addProduct(product1);
// 删除商品
system.removeProduct(product1);
// 获取商品
Product product2 = system.getProductById("2");
if (product2 != null) {
System.out.println(product2.getName());
} else {
System.out.println("商品不存在");
}
// 其他操作方法测试省略
}
}
以上就是使用Java实现商品管理系统的代码。商品类用于封装商品的属性和方法,商品管理系统类用于对商品进行增删改查等操作,并通过测试类进行验证。
### 回答3:
商品管理系统是一个用于管理商品信息的系统,可以进行商品的添加、删除、修改和查询等功能。下面是一个简单的Java实现商品管理系统的代码:
```java
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
class Product {
private String name;
private double price;
public Product(String name, double price) {
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public double getPrice() {
return price;
}
}
class ProductManager {
private List<Product> productList;
public ProductManager() {
productList = new ArrayList<>();
}
public void addProduct(Product product) {
productList.add(product);
}
public void removeProduct(Product product) {
productList.remove(product);
}
public void updateProduct(Product oldProduct, Product newProduct) {
int index = productList.indexOf(oldProduct);
productList.set(index, newProduct);
}
public List<Product> searchProduct(String keyword) {
List<Product> result = new ArrayList<>();
for (Product product : productList) {
if (product.getName().contains(keyword)) {
result.add(product);
}
}
return result;
}
public void displayAllProducts() {
for (Product product : productList) {
System.out.println(product.getName() + " - $" + product.getPrice());
}
}
}
public class Main {
public static void main(String[] args) {
ProductManager productManager = new ProductManager();
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("请选择操作:");
System.out.println("1. 添加商品");
System.out.println("2. 删除商品");
System.out.println("3. 修改商品");
System.out.println("4. 查询商品");
System.out.println("5. 显示所有商品");
int choice = Integer.parseInt(scanner.nextLine());
switch (choice) {
case 1:
System.out.println("请输入商品名称:");
String name = scanner.nextLine();
System.out.println("请输入商品价格:");
double price = Double.parseDouble(scanner.nextLine());
productManager.addProduct(new Product(name, price));
break;
case 2:
System.out.println("请输入要删除的商品名称:");
String deleteName = scanner.nextLine();
List<Product> deleteResult = productManager.searchProduct(deleteName);
if (deleteResult.size() == 0) {
System.out.println("找不到该商品!");
} else {
productManager.removeProduct(deleteResult.get(0));
System.out.println("删除成功!");
}
break;
case 3:
System.out.println("请输入要修改的商品名称:");
String updateName = scanner.nextLine();
List<Product> updateResult = productManager.searchProduct(updateName);
if (updateResult.size() == 0) {
System.out.println("找不到该商品!");
} else {
System.out.println("请输入新的商品名称:");
String newProductName = scanner.nextLine();
System.out.println("请输入新的商品价格:");
double newProductPrice = Double.parseDouble(scanner.nextLine());
productManager.updateProduct(updateResult.get(0), new Product(newProductName, newProductPrice));
System.out.println("修改成功!");
}
break;
case 4:
System.out.println("请输入要查询的商品名称:");
String searchName = scanner.nextLine();
List<Product> searchResult = productManager.searchProduct(searchName);
if (searchResult.size() == 0) {
System.out.println("找不到该商品!");
} else {
System.out.println("查询结果:");
for (Product product : searchResult) {
System.out.println(product.getName() + " - $" + product.getPrice());
}
}
break;
case 5:
productManager.displayAllProducts();
break;
default:
System.out.println("无效的操作!");
break;
}
}
}
}
```
以上是一个简单的Java实现的商品管理系统。可以通过选择操作的方式,实现商品的添加、删除、修改和查询等功能。
阅读全文