lundi 8 avril 2019

How do i find out if an item exists in a Hashset of Arrays of size 2 when the items are stored in Arr[0]?

I am trying to create a Locker class and a Long Term Storage class for homework at uni. Both of them should be able to store an Item object that was already made by our teachers that has a volume and a type. (while each Locker has a limited amount of storage units and the long term storage has a set amount of 1000 units.) I am trying to decide how to implement the storage in the best way. I thought about just creating an Array, because i know generally speaking a 1000 items tops isn't much, but i want to write the best and most efficient code i can, and there is no importance for their order. We just learned about HashSets and so i thought about maybe creating a HashSet, which makes the runtime of my program WAY BETTER. The issue is that i need to keep count of the amount of items stored of each kind and sets do not allow duplicate. And so i thought about maybe creating a HashSet of Arrays with length of 2 that keep track of a type of item and it's amount in the storage.

I am not really sure what is the best way to implement such storage and maybe im going with the absolute wrong way. It just seems to me like simple Arrays are not very efficient when i want to find a specific kind of item out of a 1000. (in case a 1000 different items of a different type and volume of 1 are stored in the long term storage).

An extra question: in this exercise im supposed to work with the TDD method (Test Driven Development) and im not sure how to implement the testing code properly. help would be appreciated. (Supposed to work with JUnit and Asserts).

An example of the start of my code:


import oop.ex3.spaceship.*;

import java.lang.reflect.Array;
import java.util.*;

public class Locker {

    private static final int TOO_MANY_ITEMS = -1;
    private static final int WORKED_WELL = 0;
    private static final int MOVED_TO_LONG_TERM = 1;

    private final int capacity;
    private int currentLoad;
    private HashSet<Item> itemsStored;

    public Locker(int capacity){
        this.capacity = capacity;
        this.currentLoad = 0;
        itemsStored = new HashSet<>();
    }

    public int addItem(Item item, int n){
        if (currentLoad + n*item.getVolume() < capacity){


        }
        else{
            return this.TOO_MANY_ITEMS;
        }
        return 0;
    }

Aucun commentaire:

Enregistrer un commentaire