写出在 Java 类文件中实现菜品列表的初始化、购物车的添加和删除、结算金额的计算等功能的代码
时间: 2024-02-11 09:06:45 浏览: 87
好的,以下是一个简单的 Java 类文件,包含了菜品列表的初始化、购物车的添加和删除、结算金额的计算等功能的代码:
```java
public class MainActivity extends AppCompatActivity {
private List<Dish> menuList; // 菜品列表
private List<Dish> cartList; // 购物车列表
private ListView menuListView; // 菜品列表控件
private ListView cartListView; // 购物车列表控件
private TextView totalPriceTextView; // 结算金额控件
private Button checkoutButton; // 结算按钮控件
private DishAdapter menuAdapter; // 菜品列表适配器
private CartAdapter cartAdapter; // 购物车列表适配器
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 获取布局文件中的控件
menuListView = findViewById(R.id.lv_menu);
cartListView = findViewById(R.id.lv_cart);
totalPriceTextView = findViewById(R.id.tv_total_price);
checkoutButton = findViewById(R.id.btn_checkout);
// 初始化菜品列表和购物车列表
initMenu();
initCart();
// 设置菜品列表的点击事件,将菜品添加到购物车列表中
menuListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Dish dish = menuList.get(position);
cartList.add(dish);
cartAdapter.notifyDataSetChanged();
updateTotalPrice();
}
});
// 设置购物车列表的长按事件,从购物车列表中删除该项
cartListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
cartList.remove(position);
cartAdapter.notifyDataSetChanged();
updateTotalPrice();
return true;
}
});
// 设置结算按钮的点击事件,计算购物车中所有菜品的价格总和
checkoutButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int totalPrice = 0;
for (Dish dish : cartList) {
totalPrice += dish.getPrice();
}
Toast.makeText(MainActivity.this, "总价:" + totalPrice + "元", Toast.LENGTH_SHORT).show();
}
});
}
// 初始化菜品列表
private void initMenu() {
menuList = new ArrayList<>();
menuList.add(new Dish("宫保鸡丁", 20));
menuList.add(new Dish("鱼香肉丝", 18));
menuList.add(new Dish("辣子鸡", 22));
menuAdapter = new DishAdapter(this, R.layout.dish_item, menuList);
menuListView.setAdapter(menuAdapter);
}
// 初始化购物车列表
private void initCart() {
cartList = new ArrayList<>();
cartAdapter = new CartAdapter(this, R.layout.cart_item, cartList);
cartListView.setAdapter(cartAdapter);
}
// 更新结算金额
private void updateTotalPrice() {
int totalPrice = 0;
for (Dish dish : cartList) {
totalPrice += dish.getPrice();
}
totalPriceTextView.setText("总价:" + totalPrice + "元");
}
// 菜品类
private class Dish {
private String name; // 名称
private int price; // 价格
public Dish(String name, int price) {
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public int getPrice() {
return price;
}
}
// 菜品适配器类
private class DishAdapter extends ArrayAdapter<Dish> {
private int resourceId;
public DishAdapter(Context context, int resourceId, List<Dish> objects) {
super(context, resourceId, objects);
this.resourceId = resourceId;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Dish dish = getItem(position);
View view = LayoutInflater.from(getContext()).inflate(resourceId, parent, false);
TextView nameTextView = view.findViewById(R.id.tv_dish_name);
TextView priceTextView = view.findViewById(R.id.tv_dish_price);
nameTextView.setText(dish.getName());
priceTextView.setText(dish.getPrice() + "元");
return view;
}
}
// 购物车适配器类
private class CartAdapter extends ArrayAdapter<Dish> {
private int resourceId;
public CartAdapter(Context context, int resourceId, List<Dish> objects) {
super(context, resourceId, objects);
this.resourceId = resourceId;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Dish dish = getItem(position);
View view = LayoutInflater.from(getContext()).inflate(resourceId, parent, false);
TextView nameTextView = view.findViewById(R.id.tv_cart_name);
TextView priceTextView = view.findViewById(R.id.tv_cart_price);
nameTextView.setText(dish.getName());
priceTextView.setText(dish.getPrice() + "元");
return view;
}
}
}
```
以上代码实现了菜品列表的初始化、购物车的添加和删除、结算金额的计算等功能。其中,Dish 类表示一个菜品,DishAdapter 类和 CartAdapter 类分别是菜品列表和购物车列表的适配器类,用于将菜品数据显示在 ListView 控件上。在 onCreate 方法中,我们初始化了菜品列表和购物车列表,并设置了菜品列表的点击事件、购物车列表的长按事件和结算按钮的点击事件,以实现购物车的添加和删除、结算金额的计算等功能。
阅读全文