0

I got such a problem. I have ArrayList of Passengers in a Car(Class).

 public ArrayList<Man> passengers = new ArrayList<Man>(capacity);

There is a limited capacity for this Car, so I want to create an exception, when the user wants to add passengers, he gets an exception, that car is full and to remove excess object from ArrayList.

try {
    mercedes.passengers.add(vova);
    mercedes.passengers.add(vova);
    mercedes.passengers.add(vova);
    mercedes.passengers.add(vova);
    mercedes.passengers.add(vova);
    mercedes.passengers.add(vova);
    mercedes.passengers.add(vova);
    if (mercedes.passengers.size() > mercedes.get_capacity()) {
        mercedes.passengers.remove(mercedes.get_capacity());
        throw new Exception("There cannot be more passengers, than allowed");
    }
}
catch(Exception ex){
    System.out.println(ex.getMessage());
}

The problem is, that it works only once, even if I try to use for cycle. What do i suppose to do?

Han4o
  • 27
  • 5
  • You already have an own types (here with the variable `mercedes`), so why do you allow direct access to the `passengers` list instead of providing an own `add()` method in your class where you can _easily_ check if there is enough space in the car. – Tom Oct 15 '19 at 09:24
  • So after throwing the exception what functionality are you expecting? – Sujay Mohan Oct 15 '19 at 09:24
  • I’ve never seen this get_capacity method in java. where did you get that from? i don’t even see it in the java docs: https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html – angryip Oct 15 '19 at 09:26
  • Possible duplicate of [How to get the capacity of the ArrayList in Java?](https://stackoverflow.com/questions/2497063/how-to-get-the-capacity-of-the-arraylist-in-java) – angryip Oct 15 '19 at 09:28
  • 1
    @angryip OP doesn't use `get_capacity` on `passengers`, but on `mercedes`. – f1sh Oct 15 '19 at 09:28

5 Answers5

3

Create an addPassenger method to the Car class which does the validation for you:

public void addPassenger(Man m) {
  if(passengers.size() >= get_capacity()) {
    throw new Exception("There cannot be more passengers than allowed");
  }
  passengers.add(m);
}

Now use this method instead of the mercedes.passengers.add(vova); lines.

f1sh
  • 11,489
  • 3
  • 25
  • 51
  • A nice thing about this solution is how it adds relevant domain language to the code. "Car" and "Passenger" etc as opposed to "List" and "Element". It keeps things very readable. – byxor Oct 15 '19 at 09:43
  • Thank you.I also used `passengers.remove(m);` method for removing excess object from ArrayList and also used try-catch expression for printing Error message. – Han4o Oct 15 '19 at 10:05
1

You have to step back here.

Thing is: you either use a standard list, then you don't have validation like that in the right place. (Like your current example).

You see, the OOP thing to do: you want that the call to add() throws that exception!

You can achieve that by creating your own implementation of the List interface, for example by extending ArrayList. And then you simply ensure that your add() method checks the current size before adding a new member.

You definitely want to avoid to decouple adding elements and validation like you do in your example.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
1

Try something like this

public class ValidationList<T> extends ArrayList<T> {

    private final Integer maxCapacity;

    public ValidationList (Integer maxCapacity) {
        super(maxCapacity);
        this.maxCapacity = maxCapacity;
    }

    @Override
    public boolean add (T t) {
        if (size() == maxCapacity) {
            throw new RuntimeException("There cannot be more passengers than allowed");
        }
        return super.add(t);
    }
}

In this case you override the ArrayList implementation and before each add it will check, whether your car is full, or not.

Joel
  • 138
  • 10
0

Thank you f1sh for answer, i just edited it for myself, so it works now as i suggested.

public void addPassenger(Man m) {
            if(passengers.size() >= get_capacity()) {
                try {
                    passengers.remove(m);
                    throw new Exception("Too much passengers");
                }
                catch (Exception e) {
                    System.out.println("There cannot be more passengers than allowed");
                }
            }
            passengers.add(m);
        }
Han4o
  • 27
  • 5
0

@Han4o

Please check below block :

public void addPassenger(Man m) {
    if(passengers.size() == get_capacity()) {
        throw new Exception("There cannot be more passengers than allowed");
    } else {
        passengers.add(m);
    }
}

Now if you want to print the exception message, use below in the catch block of code from where you called this method addPassenger:

System.out.println(e.getMessage());
abhy
  • 1
  • 2