英文:
ArrayList occurance of object type
问题
public class Orders {
private String status = null;
private int customerNumber = 0;
public Orders(String status, int customerNumber) {
super();
this.status = status;
this.customerNumber = customerNumber;
}
}
public int NumberOfOrdersOnHold(List
int sum = 0;
for (Orders o : orders) {
if (o.getStatus().contentEquals("On Hold"));
}
}
英文:
I have a class of orders and I want to see how many times a unique customerNumber will get mapped to a status reading "On Hold"
public class Orders {
private String status = null;
private int customerNumber = 0;
public Orders(String status, int customerNumber) {
super();
this.status = status;
this.customerNumber = customerNumber;
}
}
public int NumberOfOrdersOnHold(List<Orders> orders) {
int sum = 0;
for (Orders o : orders) {
if(o.getStatus().contentEquals("On Hold"));
}
}
I was thinking a counter method would work but it wouldnt. how will i achieve this method.I need numbers of orders in hold for each customerNumber not the total number.
答案1
得分: 0
你可以使用一个 Map!尝试像这样操作:
public Map<Integer, Integer> numberOfOrdersOnHold(List<Orders> orders) {
final Map<Integer, Integer> map = new HashMap<>();
for (Orders o : orders) {
int currentNumber = o.getCustomerNumber();
// 如果映射不包含对应客户的键,我们将添加它
if (!map.containsKey(currentNumber)) {
map.put(currentNumber, 0);
}
// 如果状态是 "On hold",增加当前客户的计数器
if ("On hold".equals(o.getStatus())) {
map.put(currentNumber, map.get(currentNumber) + 1);
}
}
return map;
}
英文:
You can use a Map! Try something like this:
public Map<Integer, Integer> numberOfOrdersOnHold(List<Orders> orders) {
final Map<Integer, Integer> map = new HashMap<>();
for (Orders o : orders) {
int currentNumber = o.getCustomerNumber();
// if map doesn't contain the key to the corresponding customer, were gonna add it
if (!map.containsKey(currentNumber)) {
map.put(currentNumber, 0);
}
// if the status is "On hold", increment the counter to the current customer
if ("On hold".equals(o.getStatus())) {
map.put(currentNumber, map.get(currentNumber) + 1);
}
}
return map;
}
答案2
得分: 0
import java.util.List;
enum OrderStatus {
ONHOLD("等待处理"), APPROVED("已批准"), DELIVERED("已送达");
private final String statusCode;
private OrderStatus(String statusCode) {
this.statusCode = statusCode;
}
}
class Order {
private OrderStatus status = null;
private int customerNumber = 0;
public Order(OrderStatus status, int customerNumber) {
this.status = status;
this.customerNumber = customerNumber;
}
public OrderStatus getStatus() {
return status;
}
public void setStatus(OrderStatus status) {
this.status = status;
}
public int getCustomerNumber() {
return customerNumber;
}
public void setCustomerNumber(int customerNumber) {
this.customerNumber = customerNumber;
}
@Override
public String toString() {
return "订单 [状态=" + status + ", 客户编号=" + customerNumber + "]";
}
}
public class Main {
public static void main(String[] args) {
List<Order> orders = List.of(
new Order(OrderStatus.ONHOLD, 1234),
new Order(OrderStatus.APPROVED, 1234),
new Order(OrderStatus.ONHOLD, 2345),
new Order(OrderStatus.APPROVED, 1234),
new Order(OrderStatus.ONHOLD, 1234),
new Order(OrderStatus.ONHOLD, 2345));
System.out.println("客户#1234 有 " + numberOfOrdersOnHold(orders, 1234) + " 个订单等待处理");
}
static int numberOfOrdersOnHold(List<Order> orders, int customerNumber) {
int count = 0;
for (Order order : orders) {
if (order.getCustomerNumber() == customerNumber && order.getStatus() == OrderStatus.ONHOLD) {
count++;
}
}
return count;
}
}
输出:
客户#1234 有 2 个订单等待处理
备注:
- 使用
enum
来表示状态值,这样有助于编写和维护代码。请查阅 此链接 了解更多关于 Javaenum
的信息。 - 遵循 Java 命名规范,例如
NumberOfOrdersOnHold
应该被命名为numberOfOrdersOnHold
。 - 函数
numberOfOrdersOnHold
需要提供客户编号,以便查找其订单的状态。
英文:
Do it as follows:
import java.util.List;
enum OrderStatus {
ONHOLD("On Hold"), APPROVED("Approved"), DELIVERED("Delivered");
private final String statusCode;
private OrderStatus(String statusCode) {
this.statusCode = statusCode;
}
}
class Order {
private OrderStatus status = null;
private int customerNumber = 0;
public Order(OrderStatus status, int customerNumber) {
this.status = status;
this.customerNumber = customerNumber;
}
public OrderStatus getStatus() {
return status;
}
public void setStatus(OrderStatus status) {
this.status = status;
}
public int getCustomerNumber() {
return customerNumber;
}
public void setCustomerNumber(int customerNumber) {
this.customerNumber = customerNumber;
}
@Override
public String toString() {
return "Order [status=" + status + ", customerNumber=" + customerNumber + "]";
}
}
public class Main {
public static void main(String[] args) {
List<Order> orders = List.of(new Order(OrderStatus.ONHOLD, 1234), new Order(OrderStatus.APPROVED, 1234),
new Order(OrderStatus.ONHOLD, 2345), new Order(OrderStatus.APPROVED, 1234),
new Order(OrderStatus.ONHOLD, 1234), new Order(OrderStatus.ONHOLD, 2345));
System.out.println("Customer#1234 has " + numberOfOrdersOnHold(orders, 1234) + " order(s) on hold");
}
static int numberOfOrdersOnHold(List<Order> orders, int customerNumber) {
int count = 0;
for (Order order : orders) {
if (order.getCustomerNumber() == customerNumber && order.getStatus() == OrderStatus.ONHOLD) {
count++;
}
}
return count;
}
}
Output:
Customer#1234 has 2 order(s) on hold
Notes:
- Use
enum
for status values as it makes easy for you to write and maintain the code. Check this to learn more about Javaenum
. - Follow Java naming conventions e.g.
NumberOfOrdersOnHold
should be named asnumberOfOrdersOnHold
. - The function,
numberOfOrdersOnHold
also requires the customer number whose order status has to be found.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论