Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Iterator;
- import java.util.Random;
- public class Bag extends Container {
- public Bag(String name, double weight, double capacity) {
- super(name, weight, capacity);
- }
- public Bag(String name, double weight, String properties, double capacity) {
- super(name, weight, properties, capacity);
- }
- @Override
- public Item takeItem() throws ItemStoreException {
- try {
- Random rnd = new Random();
- int takableItem = rnd.nextInt(content.size());
- this.setCurrentWeight(this.getCurrentWeight() - content.get(takableItem).getWeight());
- Item buffer = content.get(takableItem);
- System.out.println("Предмет " + buffer.getName() + " удален из контейнера " + this.getName());
- content.remove(takableItem);
- return buffer;
- } catch (NullPointerException e) {
- e.getMessage();
- }
- throw new EmptyBagException();
- }
- public Item found(String name) throws ItemStoreException {
- Iterator<Item> iterator = content.iterator();
- while (iterator.hasNext()) {
- Item i = iterator.next();
- if (i.getName().equals(name)) {
- System.out.println("Предмет " + name + " найден");
- return i;
- }
- }
- System.out.println("Предмета " + name + " нет в мешке");
- throw new NotFoundItemException();
- }
- public static void main(String[] args) {
- try {
- Bag bag = new Bag("Сумка", 0.1, 10);
- bag.put(new Item("предмет1", 4));
- bag.put(new Item("предмет2", 1));
- bag.printContainerInfo();
- Item takeItem = bag.takeItem();
- bag.printContainerInfo();
- Item foundItem = bag.found("предмет2");
- } catch (ItemStoreException e) {
- e.getMessage();
- }
- }
- }
Add Comment
Please, Sign In to add comment