Hello,
I have an order class that has 3 variables, "customer(name of customer)",
"name(name of a requested item)","count(total count of requested item)".
I want to impelement a method in another class that has the method Set<order> getItemSet() that returns a set and joins all requests for the same item from different customers in following way,
for example:
if there are two requests:
- ItemN: Customer1: 3
- ItemN: Customer2: 1
Set of orders should be:
- ItemN: Customer1,Customer2: 4
I was thinking to store orders in a list and iterate over them to check how many requests an item has and then store them in a set for that particular item. But, I am not sure to be honest. I am kind of lost. Any kind of hint or help with implementation would be great.
What I have tried:
public class Order implements Comparable<Order> {
String customer;
String name;
Integer count;
public Order(String customer, String name, Integer count) {
super();
this.customer = customer;
this.name = name;
this.count = count;
}
@Override
public int compareTo(Order arg0) {
int status;
status = this.name.compareTo(arg0.name);
if (status == 0){
status = this.customer.compareTo(arg0.customer);
if (status == 0){
status = this.count - (arg0.count);
}
}
if (status < 0){
status = -1;
}
if (status > 0){
status = 1;
}
return status;
}
public boolean equals(Object obj){
if (this == obj){
return true;
}
if (obj == null){
return false;
}
if (getClass() != obj.getClass()){
return false;
}
Order arg0 = (Order) obj;
if (count != arg0.count){
return false;
}
if (customer == null){
if (arg0.customer != null)
return false;
} else if (!customer.equals(arg0.customer))
return false;
if (name == null){
return false;
}else if (!name.equals(arg0.name)){
return false;
}
return true;
}
@Override
public String toString() {
return "ItemName: "+ name + "OrdererName: "+ customer + " count: " + count ;
}
@Override
public int hashCode(){
return (name + customer + count).hashCode();
}
}
This is the second class where I want to implement the method that returns set.
public class Orders implements Iterator<Order>{
int index = 0;
List<Order> orders
;
public Orders(){
index = -1;
orders = new ArrayList<>();
}
public void add(Order item){
orders.add(item);
}
public List<Order> getItemsList(){
return orders;
}
<pre>public Set<Order> getItemsSet()
{
Set<Order> orderSet = new HashSet<>();
return orderSet;
}
<pre>@Override
public void forEachRemaining(Consumer<? super Order> arg0) {
}
@Override
public boolean hasNext() {
return false;
}
@Override
public Order next() {
return null;
}
@Override
public void remove() {
}
}