java定义一个ArrsyList订单的集合
时间: 2024-05-10 20:21:41 浏览: 5
下面是一个Java ArrayList订单的集合的示例:
```java
import java.util.ArrayList;
public class Order {
private int orderId;
private String customerName;
private double orderTotal;
public Order(int orderId, String customerName, double orderTotal) {
this.orderId = orderId;
this.customerName = customerName;
this.orderTotal = orderTotal;
}
public int getOrderId() {
return orderId;
}
public void setOrderId(int orderId) {
this.orderId = orderId;
}
public String getCustomerName() {
return customerName;
}
public void setCustomerName(String customerName) {
this.customerName = customerName;
}
public double getOrderTotal() {
return orderTotal;
}
public void setOrderTotal(double orderTotal) {
this.orderTotal = orderTotal;
}
public static void main(String[] args) {
ArrayList<Order> orders = new ArrayList<Order>();
orders.add(new Order(1, "John", 100.00));
orders.add(new Order(2, "Mary", 50.00));
orders.add(new Order(3, "Tom", 75.00));
for (Order order : orders) {
System.out.println("Order ID: " + order.getOrderId() + ", Customer Name: " + order.getCustomerName() + ", Order Total: $" + order.getOrderTotal());
}
}
}
```
这个示例创建了一个Order类,并使用ArrayList<Order>来存储订单对象。在示例中,我们创建了三个订单对象,并将它们添加到ArrayList中。然后,我们使用一个for循环遍历ArrayList,并输出每个订单对象的属性。