From the course: Build Spring Boot Apps with the Kotlin Programming Language

Explaining the challenge

- [Instructor] Hello and welcome again. We've reached chapter 11 now and I think it's time for another exercise. In this chapter we're going to be writing some Kotlin code and this is going to be a bit challenging to complete but by the end of this chapter we'll have some rather nice looking production standard Kotlin code, and actually this is going to be the start of our main case study. In a few chapters time we're going to introduce Spring Boot and we'll be building a fully functional system in Spring Boot over the remaining chapters of the course. Now although we're not there just yet, we're actually going to be starting on this case study now and we'll be able to use the code that we write here later on in our project. In this exercise we're going to be working with a small theater. We've been asked to build the system that will be used to manage booking seats for their performances. In this chapter we're just going to be modeling the seats, so the idea is that this theater has 15 rows of seats, each with 36 seats in a row. Each seat can be identified by a combination of its seat number and its row number, and this row number and seat number are also used to determine the price of each seat and its description. For example, seat number seven in row four, which would be somewhere 'round about here I think, well that would be described as a standard seat and it would cost 18. I'm not worrying about currencies here so you can think of this as being $18 or £18, whatever currency you normally work with. And as another example, if somebody wanted to book seat number 12 on row two, that would be somewhere around about here, well that is going to be a best view seat costing 21. Okay, so I've got the business requirements on screen here and when I say, pause the video and give this a go, I will leave this screen visible, but you're not going to start this exercise completely from scratch, I've written a little bit of code that I want to use as the starting point, so follow along with me and let's create a new project which we'll call Exercise2. Once again we'll pick Kotlin for the JVM as the libraries that we need, we'll call this Exercise2, and then in the starting workspace for this chapter there is a file called Exercise2.kt, so we'd like to copy that file and place it in our workspace for the new Exercise2 project that we've just created. Here's my general workspace, Exercise2 into the src folder and we'll paste it in there. Then we can hopefully expand it, find it in IntelliJ, and open it up. Okay, now the first thing is that this class uses the BigDecimal which we'll need to import, so just put your mouse somewhere in there, and press Alt and Enter on the keyboard to import BigDecimal, so this now looks okay. Let's talk through what we've got here. The first thing is, I've created a data class called seat which contains the parameters of each seat, that's a parameter called row which is an integer, a parameter called num, that's the seat number which is also an integer, a price which is a BigDecimal, and the description which is a String. All of these are vals so the idea is that seat is going to be an immutable class. We'll create a seat by passing the data into the constructor but we can't then edit any of those values, and part of the requirements for this exercise is you may not edit this seat class. As you can see, I've overridden the toString method, and actually I've included a dollars here for the price, but you can use whatever symbol you prefer, if you want to change that to a pound, or to a euro, go ahead, that's fine. So you're not allowed to edit the seat class, what you do need to do is in this class here called theater, is create the variable that I've called seats, and this needs to be an immutable list of these seat objects, so the plan is then within theater, you need to create each of these different seat objects with different rows and numbs, representing the row and the seat number, and based on the relevant row or seat number, a price and a description. Now, you can probably guess we can create some of that with loops and ranges, and obviously we're going to be creating collections here, and just to help you out I've also put here a reminder for the requirements on how to determine the seat price and the seat description. If you've got that working, when you run the main method what we should be able to do is print out a list of all the seats in the back two rows, that's what should get matched. I know we've not looked at the filter method yet, we're going to look at that probably later on, but for now just accept that all we're doing here is saying, let's just get a list of those seats that cost 14.50, and then we'll print those out to the console. Now there is something extra that you do need to know before you can undertake this challenge. So far when we've been creating immutable lists we've used the listOf function to do it. The problem with listOf is that you need to then provide a range of elements to form your immutable list. We can't create an immutable list by doing some kind of loop, that just simply won't work, so how are we going to do this? How are we going to make seats an immutable list? Well the answer is, is that we can take a mutable list and convert it or create an immutable list out of it. So for example, if I have a mutable list, and in fact that could be say, an Array list of Strings, then I can create an immutable list from it by doing my mutableList.toList, that will create an immutable version of any mutable list, so this will help us get there. I'm going to remove those and put this back to how it was at the start, so this should then match the file that you've loaded up in the starting workspace for this chapter, and I want you to now pause the video and give this a go. Don't forget, you're not allowed to edit the seat class, and what we need is an immutable list. I'm going to suggest that even if you get this working, do have a look at the code in the closing workspace or watch the walk through, which will be what the rest of this video is, because I'm going to talk as I do the walk through about the design choices that I'm making and it might be helpful for you to hear what I've got to say, but do pause the video now and give this a go.

Contents