1

So for school I am making a program where we are creating a booking system where people can book a ticket for a movie that has a capacity of 10 people. People are allowed to change the time of the booking to the next day as long as the theater is not full for that day.

An array will be no good in this situation as I need to be able to remove an object from the array and make another free space in said array, and then add the removed object to a different Array for the different day. This part is suitable for an ArrayList but it has no size limit so I'm stuck with what the best solution is. Any ideas that I can look into?

Thanks

John
  • 45
  • 5

3 Answers3

0

You can try the below, start from an array and convert it to list via Arrays.asList. Just note you could only use the set() method on the List, and not the add/remove methods as these would modify its size :

    String[] array = {"a1","b2","c3"};
    List<String> fixed = Arrays.asList(array);
    fixed.set(0, "new_string"); // OK
    fixed.add("aNewString"); // This will throw an exception
nullPointer
  • 4,188
  • 1
  • 14
  • 27
0

You can extend a class which already has the functionality you need and only override the methods required to implement new functionality (i.e. enforce a size limit). Consider the following solution:

public class CappedList<T extends Object> extends ArrayList<T> {

    private final int maxSize;

    public CappedList(int maxSize) {
        this.maxSize = maxSize;
    }

    @Override
    public boolean add(T e) {
        if (this.size() == this.maxSize) {
            //Already reached max size, abort adding
            throw new IllegalStateException("List is maxed out");
        } else {
            return super.add(e);
        }
    }

}

For completeness, you need to also override all add and addAll overloaded methods. You can then use this CappedList class instead of a simple ArrayList. A utility function isMaxedOut would be handy, in order to avoid exception handling:


public boolean isMaxedOut() {
        return this.size() == this.maxSize;
    }
Tasos P.
  • 3,521
  • 2
  • 21
  • 38
0

It all depends how far you are in understanding of the language.

Let's say, first of all as a base logic that you might consider is, that you should find what is unique for 10 tickets. Obviously it's a group, which has own unique identifier and that's a date, so you have to concentrate on binding your tickets to a date groups, then you should control the amount what you are populating per date, you might need advanced control logic, rather than an usual variable that might do the job for you.

First of all, I would not store tickets in a different variables per day.

Let's go into details.

If you are obsessed by using only one specific property and that's ArrayList, I found nice answer here

But to have more precise population and access to the list later, for example to filter out specific date and see those tickets that are sold that day you should do it with a bit more structured logic, rather than a simple ArrayList(), may be you should even use different Type of variable that you should store that data in.

If you are on a bit advanced programming course, from the brief observation, for a simple task I might say that there is the way to use getter and setter methods to implement limitations and you could use any type of object to store that data, beside ArrayList.

Or you could write own functions that can control adding and removing elements from a list.

Or in more advanced way, if you have been introduced to a class concept, you could use class to define a ticket and then you could construct any logic behind it. This last one is the best way to go - to write easily readable, understandable and reusable code, or store specific data and control it the way you want.

Irakli
  • 370
  • 3
  • 13