以下是一个简单的Android花店管理系统的代码示例,包括添加商品、删除商品、修改商品和查询商品的功能。
首先,创建一个名为`ShopManager`的类,用于管理花店的商品信息。
```java
public class ShopManager {
private List
public ShopManager() {
products = new ArrayList<>();
}
public void addProduct(Product product) {
products.add(product);
}
public void deleteProduct(int productId) {
for (int i = 0; i < products.size(); i++) {
if (products.get(i).getId() == productId) {
products.remove(i);
break;
}
}
}
public void updateProduct(int productId, Product updatedProduct) {
for (int i = 0; i < products.size(); i++) {
if (products.get(i).getId() == productId) {
products.set(i, updatedProduct);
break;
}
}
}
public Product getProduct(int productId) {
for (Product product : products) {
if (product.getId() == productId) {
return product;
}
}
return null;
}
public List
return products;
}
}
```
接下来,创建一个名为`Product`的类,用于表示花店的商品信息。
```java
public class Product {
private int id;
private String name;
private double price;
private boolean isSold;
public Product(int id, String name, double price, boolean isSold) {
this.id = id;
this.name = name;
this.price = price;
this.isSold = isSold;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public double getPrice() {
return price;
}
public boolean isSold() {
return isSold;
}
}
```
最后,在主Activity中实现添加商品、删除商品、修改商品和查询商品的功能。
```java
public class MainActivity extends AppCompatActivity {
private ShopManager shopManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
shopManager = new ShopManager();
}
public void addProduct(View view) {
EditText nameInput = findViewById(R.id.editTextName);
EditText priceInput = findViewById(R.id.editTextPrice);
EditText soldInput = findViewById(R.id.editTextSold);
String name = nameInput.getText().toString();
double price = Double.parseDouble(priceInput.getText().toString());
boolean isSold = Boolean.parseBoolean(soldInput.getText().toString());
shopManager.addProduct(new Product(0, name, price, isSold));
}
public void deleteProduct(View view) {
int productId = Integer.parseInt(findViewById(R.id.editTextProductId).getText().toString());
shopManager.deleteProduct(productId);
}
public void updateProduct(View view) {
int productId = Integer.parseInt(findViewById(R.id.editTextProductId).getText().toString());
String name = findViewById(R.id.editTextName).getText().toString();
double price = Double.parseDouble(findViewById(R.id.editTextPrice).getText().toString());
boolean isSold = Boolean.parseBoolean(findViewById(R.id.editTextSold).getText().toString());
shopManager.updateProduct(productId, new Product(productId, name, price, isSold));
}
public void queryProduct(View view) {
int productId = Integer.parseInt(findViewById(R.id.editTextProductId).getText().toString());
Product product = shopManager.getProduct(productId);
if (product != null) {
TextView nameTextView = findViewById(R.id.textViewName);
TextView priceTextView = findViewById(R.id.textViewPrice);
TextView soldTextView = findViewById(R.id.textViewSold);
nameTextView.setText(product.getName());
priceTextView.setText("¥" + product.getPrice());
soldTextView.setText("已售出");
} else {
Toast.makeText(this, "未找到该商品", Toast.LENGTH_SHORT).show();
}
}
}
```
在这个示例中,我们创建了一个`ShopManager`类来管理花店的商品信息,以及一个`Product`类来表示商品信息。在主Activity中,我们实现了添加商品、删除商品、修改商品和查询商品的功能。