So back in section nine, we looked at three of Java's list types.
That was the list interface, the array list, and also the linked list classes.
Now those form part of the Java collections framework, and
that framework also includes things like sets Maps, trees, and ques.
So at the top level of the collections framework is the collections class.
Now this exposes static methods that you can operate on collections
such as the sort method that we've used previously but
they also return collection objects such as list method.
Now the interfaces in the collection framework,
what they do is they allow the framework to be extended.
And they actually define methods for all the fundamental operations that
really are required for the various collection types.
Now one of the design goals of the collections framework was that there
should be good interoperability amongst various collections, so
not just the ones included in the framework.
But literally anything that may also be created in the future
that is a reasonable representation of a collection.
Now that also includes arrays.
Now a raise themselves couldn't be made part of the collection framework without
changing the Java language.
However, the framework does include methods That
enable connections to be move into arrays and vice versa, and
additionally methods to allow arrays to be viewed as collections.
So the main components, or
the core elements of the collections framework are interfaces.
Now these are the abstract, Types that represent collections,
including this list interface that we've already seen, as mentioned previously, and
we will be looking at the interface hierarchy of the collections framework.
Also, included is aggregate operations.
Now, although we've seen iterate in its earlier videos,
We'll cover them in much more detail here in this section.
In addition, another core element is implementations.
Now this is the concrete implementations of the interface,
including the ArrayList and Linked List classes.
That's two good examples of an implementation.
But also, algorithms.
Now the Java JDK provides a range of polymorphic algorithms
that work on collections objects.
Technically speaking objects that implement collection interface and
they provide reusable functionality.
So I'll be looking at all these elements in the section, but I'll do so
by looking at the framework, rather than producing a video on each element, so
what I'll start with, what I'm gonna do is review the array list and
link list on objects as well as the list interface that we've covered previously.
And consider them within the context of the Collections Framework.
What I will do is use a simple seatbooking system in theater to see how
these classes that we've used earlier fit into the Java Collections Framework.
So theater seats will be numbered with a row letter, and
then a seat number within each row.
I'll keep it simple and not have different numbers of seats, and that type of thing.
So I'm gonna start off by creating our theater class.
And that's gonna contain an inner sync class.
So I'm gonna go over as always, and come into our package,
right click it, new class, and we'll call this first one theater.
And we can start by creating a list for the seats.
So that's what we'll do first is let the names.
So it's gonna be private final String theatrename.
Then we're gonna create a private list of seat objects.
So lists.
Seat of seats, and seats we're gonna define shortly.
That's gonna be inner class, and make that a new array list like so.
And incidentally that error on the first line is gonna go away
once we initialize it, and if you are Paying attention and
watching the previous section where I talked about final and so forth.
You'll understand why that error's coming up and that it'll be fixed once we update
our constructor and we cut into this area here, most likely because this is weird.
I think it's a bug in IntelliJ.
For some reason, even though I've selected project structure,
project language level eight, in my default settings, it's set it to six.
So I'll go back and change it to eight.
The error should go away.
Obviously, we still need to define that in a class, which we'll do shortly.
So moving on, let's create a constructor,
and the constructor is going to consist of theater.
Once I do that, that should fix the other area you can see that area is gone away
now because we have initialized that object and
we're going to have a couple of parameters.
Let's also have the number of rows in this theater int numRows and int seatsPerRow.
And then what we're going to do is, we're going to allocate.
As I mentioned there’s going to be a seat number, consists of a row letter and
a sear number.
Theatre seats will be numbered with a row letter and a seat number, I should say.
So first of all, we're going to create the last row and do Ent LastRow =
A plus num rows type one.
Then what we're going to do is cycle through and create a seat for
each one of these theater seats.
So that will be four job row equals A.
A row is less or equal to last row,
row plus plus, so going through all the rows from A to Z effectively.
Okay, then for each, we've got the row, and for each row,
we now need to go through and allocate the seats for that section, so
we need to put 4 int Seat num
standing on one, because obviously we can't have a seat to starting with zero.
The seat num is to be less than or equal to seats per row which gave us pass to
the constructor and seat num plus plus to increment through seat in the row.
Then we're going to create seat object.
Flow plus string formats row, the row that we've defined,
that we're currently processing plus string.format.
That's going to be percent O2D, see now I'm just formatting it so
it looks correct.
And then we're going to add it to our array list that we've defined on line ten.
So it's seats.addseat.
There's a basic instructor, so you can see that's created now.
And before we fix this seat error we'll just create a few other things.
What we'll do is first create a getter for our theater name.
I'm just adding the getter there and actually I'll move that.
Below the constructor's probably a better spot for that.
Clean the code up a bit and put it there.
And the next one we want, what we'll do is we'll create a few more.
We'll create a reserve seat.
Where it does just as you think it would.
It reserves a particular seat for our theater.
And we'll also add a get seat where we can return, or print out the list of seats.
And we'll do the reserve seat first.
That's gonna be public.
Boolean reserve seat string seat number.
And we'll start off with seat,
requested seat is equally to null.
[SOUND] For Seat and seats.
[SOUND] And we go through all the seats and
were going to do a test to see whether we can find our particular one,
seat.getseatnumber Number.equals
seat number requested
seat equals seat.
Then we'll break out of it, cuz we've found the seat.
Then S out of that loop, we're going to put if Requested seat is equal to null.
And I'll put there is no seat,
seat num in case has been an invalid seat sentence method.
It returns false in that situation Because there's no other processing we
can do because we've got an invalid seat.
Otherwise what we're going to do is return, requested seat,
seat.reserve, and again, we haven't written the seat class yet,
and that will all be reviewed once we get to that, so that's how you go about
reserving a seat, and then the last method, we're going to, or the next method
we're going to create is the get seats, which is just really for testing purposes.
And it's going to be public void get seats.
And we're just going to go four seat seat seats.
And print them out.
So I'll do Seat.getseatnumber Like so.
Okay, so that's the basic code for the theater.
What we're doing now is we'll add our private class
which has got some of these other functions and
will fix up a lot of these errors that are showing on the screen.
So we're going to go ahead and do that, so private class seat,
And it's going to have A private final stream.
Seat number.
We'll get an error until we initialize that.
Private [INAUDIBLE] reserved.
It was false so I'm gonna save in one to indicate whether this particular seat
has been reserved or not.
So we'll do our constructor now, and we're just going to get the seat numbers past
this method, and you'll see when we've set that and fixed the area that
was showing on long 53, and that's all our constructor needs to do, and
next we need to fix up some of these, requested C should be, requested C.
So we need to basically import this error and
probably the easiest way of doing that would be to just that fixes that.
We could have done a and
just imported like that fixes that and some other things.
Just typos but method and the method that still need to be written.
In a class.
I'll do that.
First, we'll do the reserve.
So I'll go public, boolean reserve.
and the cover, it will be if not [INAUDIBLE]
.reserve equals true.
So obviously, if it's already reserved, we're not going to retry and
process it And if that's the case we were able to reserve it.
We just kinda put a message to the effect seat + seat number + reserved.
It returned true, our job is done in that case.
To get started here that's what we'll do is we'll put an else in here.
We'll put else.
Turn false, because obviously there was a problem reserving it because it was
already reserved.
You also want to cancel function.
We haven't used that code yet, but we'll want that, so public Berlin cancel.
And it does things the other way around.
It goes if stop reserve.
Somebody tries to cancel.
If the seat
number has already been reserved well this particular seat has been reserved.
So if stop reserved and we put if stop reserve.
Is equal to false.
And reserve it freed up so I can be allocated again.
"Reservation of seat " +
seatNumber + " cancelled".
Turn true because we successfully canceled those.
Return false.
We weren't able to cancel because the seat wasn't was already reserved.
And the last one to fix up is this get seat number.
Which is just a getter.
So I'm just gonna do getter for the seat number.
It should fix that and that should be all the errors that are now fixed
in this class in our first theater class with the inner class of being seat.
Again just as a recap looking back up here.
At the top.
The constructor takes a theater name together with the number of rows and
the number of seats per row.
And it uses these to create seats which it currently stores
as you can see in the array list on line 10 which is the seats fueled.
And you can see that I have defined a couple of other methods as well that
reserve seat.
And also we've created an inner class receipt and
got the ability to reserve or cancel A particular seat.
So let's go ahead now and create the main method.
And that's just gonna create a new theater and then attempt to reserve the same
seat twice just to confirm that things are working.
So we'll go back to our menu and here we're going to create a new reference.
So that's gonna be theater.
Equals new theater.
We'll call it the Olympian.
And again, the parameters here,
just to go back to confirm those, the parameters we're looking for
here is the number of rows in available in the theater and the sets per row.
So we're going to start with eight, so eight rows, with 12 seats per row.
And we'll do a theater.getseats.
We'll call that method just to print it out, see that it's working.
So let's try running that first.
And you can see that we've got our very seats there, starting from row A to 0,
1 to 0, 12 and we use that Formatting class, which I mentioned here,
String.format, to make sure they're all allocated with a leading 0 if necessary,
and we're going from row A to row H.
Of course that's a total of eight rows.
So that part's working okay, which is good.
Let's go back to our main.
So let's now try doing a reservation.
So we've got something like,
if(theatre.reserveSeat()) H11.
Please pay.
Seats been reserved.
Else sorry, seat is taken.
So first time you run this, it should work.
We run this, seat H11 reserve.
So you can see that's working fine.
There's an extra space there.
I should actually probably look at fixing that.
I must have added two spaces there, so I'll fix that.
And go back and run it again just to confirm.
Okay Seat H11 reserved.
Now if we do try and do the same thing again if I just copy and
paste the same code we should get an error the second time.
Because of course a previous call to it should have reserved the seat so
let's just try running that.
Sorry, seat is taken.
So the first time it reserved it and
the second time you can see that the seat's taken.
So that's good. That's working fine.
And the other thing we'll do now is we'll comment out line seven.
Because we can confirm that the code's working all right, and
the seats are being created correctly.
So I'll comment that out.
So I'm gonna pause the video now and in the next video we'll continue on and
we're going to look at changing our data structure,
which is defined in heatre.java on line ten, the seats using the ArrayList.
We're gonna start changing that to a few different formats, a list and
then also looking at turning it into a collection or making it more generic and
seeing how that all works.
So see you in that next video.