Click here to Skip to main content
15,887,416 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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; // Name of the customer
	String name; // Name of the requested item
	Integer count; // Count of the requested items
	
	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) {
		//  Auto-generated method stub
		
	}

	@Override
	public boolean hasNext() {
		//  Auto-generated method stub
		return false;
	}

	@Override
	public Order next() {
		//  Auto-generated method stub
		return null;
	}

	@Override
	public void remove() {
		//  Auto-generated method stub
		
	}
}
Posted
Updated 9-Aug-19 15:16pm

1 solution

You're trying to build an "in-memory database".

It's like reinventing the wheel when you could use SQL Lite, for example ... and make better progress.
 
Share this answer
 
Comments
Nabeel Munir 10-Aug-19 1:16am    
@Gerry Schmitz, I understand that. But, I am doing this task because I want to understand java's design pattern.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900