购物清单 商品单价 金额 商品名称 购买数量 1 1900 1900 鳄鱼夹克 2 400 800 lipodMP4
时间: 2024-11-30 10:29:41 浏览: 9
看起来你正在描述一个简单的购物清单结构,其中包含了商品的相关信息。在Java中,我们可以使用一个类(ShoppingItem)来表示这样的数据,包含商品单价(price)、总金额(totalAmount)以及商品名称(itemName)和购买数量(quantity)。这里是一个基本的示例:
```java
public class ShoppingItem {
private String itemName;
private double price;
private int quantity;
public ShoppingItem(String itemName, double price, int quantity) {
this.itemName = itemName;
this.price = price;
this.quantity = quantity;
}
// Getter and Setter methods
public String getItemName() {
return itemName;
}
public void setItemName(String itemName) {
this.itemName = itemName;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
// Calculate the total amount for the item
public double getTotalAmount() {
return price * quantity;
}
}
// Creating shopping list items
ShoppingItem crocodileJacket = new ShoppingItem("鳄鱼夹克", 1900, 2);
ShoppingItem lipodMP4 = new ShoppingItem("lipod MP4", 400, 1);
// Printing the details
System.out.println("商品名称: " + crocodileJacket.getItemName());
System.out.println("单价: " + crocodileJacket.getPrice() + "元");
System.out.println("购买数量: " + crocodileJacket.getQuantity());
System.out.println("金额: " + crocodileJacket.getTotalAmount() + "元");
System.out.println("\n商品名称: " + lipodMP4.getItemName());
System.out.println("单价: " + lipodMP4.getPrice() + "元");
System.out.println("购买数量: " + lipodMP4.getQuantity());
System.out.println("金额: " + lipodMP4.getTotalAmount() + "元");
```
这个例子定义了一个`ShoppingItem`类,然后创建了两个实例,分别代表鳄鱼夹克和lipod MP4。你可以根据需求扩展这个类或添加更多的功能。
阅读全文