ArrayList中对象类型的出现次数

huangapple go评论78阅读模式
英文:

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 orders) {
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&lt;Integer, Integer&gt; numberOfOrdersOnHold(List&lt;Orders&gt; orders) {
    final Map&lt;Integer, Integer&gt; map = new HashMap&lt;&gt;();
    for (Orders o : orders) {
        int currentNumber = o.getCustomerNumber();

        // if map doesn&#39;t contain the key to the corresponding customer, were gonna add it
        if (!map.containsKey(currentNumber)) {
            map.put(currentNumber, 0);
        }

        // if the status is &quot;On hold&quot;, increment the counter to the current customer
        if (&quot;On hold&quot;.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 个订单等待处理

备注:

  1. 使用 enum 来表示状态值,这样有助于编写和维护代码。请查阅 此链接 了解更多关于 Java enum 的信息。
  2. 遵循 Java 命名规范,例如 NumberOfOrdersOnHold 应该被命名为 numberOfOrdersOnHold
  3. 函数 numberOfOrdersOnHold 需要提供客户编号,以便查找其订单的状态。
英文:

Do it as follows:

import java.util.List;
enum OrderStatus {
ONHOLD(&quot;On Hold&quot;), APPROVED(&quot;Approved&quot;), DELIVERED(&quot;Delivered&quot;);
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 &quot;Order [status=&quot; + status + &quot;, customerNumber=&quot; + customerNumber + &quot;]&quot;;
}
}
public class Main {
public static void main(String[] args) {
List&lt;Order&gt; 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(&quot;Customer#1234 has &quot; + numberOfOrdersOnHold(orders, 1234) + &quot; order(s) on hold&quot;);
}
static int numberOfOrdersOnHold(List&lt;Order&gt; orders, int customerNumber) {
int count = 0;
for (Order order : orders) {
if (order.getCustomerNumber() == customerNumber &amp;&amp; order.getStatus() == OrderStatus.ONHOLD) {
count++;
}
}
return count;
}
}

Output:

Customer#1234 has 2 order(s) on hold

Notes:

  1. Use enum for status values as it makes easy for you to write and maintain the code. Check this to learn more about Java enum.
  2. Follow Java naming conventions e.g. NumberOfOrdersOnHold should be named as numberOfOrdersOnHold.
  3. The function, numberOfOrdersOnHold also requires the customer number whose order status has to be found.

huangapple
  • 本文由 发表于 2020年4月8日 00:44:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/61085056.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定