Zdravim, riesim akurat problem ze nefunguju mi metody pop a push na vkladanie a vyberanie poloziek do pola v batohu...
mohli by ste mi niekto pomoct???
public class BackPack {
private static final BackPack backPack = new BackPack();
private static final int NUMBER_OF_ITEMS = 4;
private PortableItem[] items;
private BackPack() {
items = new PortableItem [NUMBER_OF_ITEMS];
}
public static BackPack getBackPack() {
return backPack;
}
public PortableItem[] getItems() {
return items;
}
public PortableItem pop(String name) throws WrongItemExistenceException {
PortableItem variable;
for (PortableItem i : items) {
if (i != null) {
System.out.println("pop rozny od null");
if (i.getName().equals(name)) {
variable = i;
i = null;
return variable;
}
}
}
throw new WrongItemExistenceException("Predmet " + name + " sa v batohu nenachádza ...");
}
public void push(Item item) throws WrongItemUsageException {
if (item instanceof NonPortableItem)
throw new WrongItemUsageException("Predmet " + item.getName() + " sa nedá vložiť do batohu ...");
else {
for (PortableItem i : items) {
if (i == null) {
System.out.println("push rozny od null");
i = (PortableItem) item;
return;
}
}
throw new WrongItemUsageException("V batohu nieje miesto na uloženie dalšieho predmetu ...");
}
}
}
pricom PortableItem je trieda, ktora ma 2 atributy - name a description a nim prisluchajuce gettery...