In Java, Implement List Of Inventory

import java.util.List;
import java.util.LinkedList;

public class InventoryListImpl implements InventoryList{

	private List<Inventory> list;

	public InventoryListImpl(){
		list = new LinkedList<Inventory>();
	}

	public int getSize(){
		return list.size();
	}

	public boolean add(Inventory item){
		int size = getSize(), cmp;
		if(MAX_SIZE == size) return false;
		for(int i = 0; size > i; i ++){
			cmp = list.get(i).getName().compareTo(item.getName());
			if(0 == cmp){
				return false; 
			} else if(0 < cmp){ 
				list.add(i, item); 
				i = size;
				return true;
			}
		}
		list.add(item); 
		return true;
	}

	public Inventory remove(Inventory item){
		int size = getSize();
		if(0 == size) return null;
		for(int i = 0; size > i; i ++){
			if(0 == list.get(i).getName().compareTo(item.getName())){
				return list.remove(i);
			}
		}
		return null;
	}

	public boolean contains(Inventory item){
		int size = getSize();
		if(0 == size) return false;
		for(int i = 0; size > i; i ++){
			if(0 == list.get(i).getName().compareTo(item.getName())){
				return true;
			}
		}
		return false;
	}

	public boolean isEmpty(){
		return list.isEmpty();
	}

	public String findByItemCostLess(double cost){
		StringBuilder names = new StringBuilder();
		String sp = "";
		for(Inventory item: list){
			if(item.getCost() < cost)continue;
			names.append(sp);
			names.append(item.getName());
			sp = ", ";
		}
		return names.toString();
	}

	@Override
	public String toString(){
		return list.toString();
	}
}
Download

Comments

Popular posts from this blog