Alright, let's discuss the challenge I'm about to give you and I suspect you can guess what it is.
And after the challenge as always I'm going to show you my solution. So your challenge again is to fix
the project that the we've currently got because if you recall from the last video it's got some errors and
you want to make it run properly.
Now the steps mainly; create UI module, move the UI package currently in the commons module to
the new UI module.
Then you want to go ahead and fix all module info files and all three may need to be changed here.
And as a note, obviously, the project needs to run the same as it did before,
in other words without any exceptions or errors. Let's have a look at the image with a module structure here.
So this image represents the final module structure, so you can notice that some dependencies,
In other words the modules that we need in our modules. Now you can add more modules, you don't have to
match this image 100 percent.
And I will also add more modules that we need later in the next video, I'm then gonna discuss how to
use only modules that you actually need
and how to include other modules automatically without specifying the requires statement.
But in the challenge, for every module that the UI needs to use,
just use a requires statement. And just a small tip, the UI module will need the common module
not just db as per the image, so don't take this image as the 100 percent solution but you can use it
as guidance and we're going to clean up the code later in the next video also to match everything
so that it looks very much like you can see on this image. Alright, and let's just have a quick look
at an image with a final project structure in IntelliJ
so you can see there that shows you all the modules, packages and where each class needs to be.
And I've talked about this a number of times I should be fairly familiar with the various modules and
packages and so forth.
So I'm just going to bring back the challenge one more time now before I pause the video on screen.
So there's the challenge text there,
So pause the video and see how you go with the challenge and when you're ready come back and we'll go through
this step-by-step, so pause the video now.
Okay, so welcome back.
Hopefully you managed to get this working.
So let's actually go through the steps to get this challenge solved.
Now, we can use the same steps we've used previously for creating any modules.
Let's start with that. We come up to file, click on New > Module
click on next and the name is module of course is academy.learnprogramming.ui
And of course that's the same package name, or it's the same name as the package name, now we got ui classes
So there's our third module now. So, we're going to open that up and see the source folder
Let's create the module descriptor file, the module-info.java file, then the UI module.
I'm going to click on source, right click New, select module-info.java and there's our module info file
Next thing we need to do is start moving the UI package.
I'm going to start by creating a new package in the UI module.
It's kind of a new package, academy.learnprogramming. Let's enter there
Next, I'm going to drag the UI package from the common module into, and surprisingly, our ui module
I'm going to select ui, drag it, place on the academy.learnprogramming package in the UI module
click ok to move it, click on Refactor and you can see now we've got a UI package in the UI module with
controller, main and main.fxml in there.
Alright, so let's have a look because we've got some potential problems here we got Main with an error.
Let's look at controller first. We've got controller here and you can see, we've got quite a few errors here showing
at the moment.
Now, since ui module uses the classes Album, Artist ,data source and also some classes from
the Java fx module,
we obviously have to add the requires statements into our module-info.java file
And as always, we can fix the compilation errors by using the read bulb where you can just type in the module info file,
So I'll go ahead and type it in this time.
Making sure I've selected the academy.learnprogramming.ui module descriptor file
Alright, so we can say that we've got an empty file here at the moment.
It's just got the module declaration on line one.
Now we can copy some of the statements from the module info from the common module.
That's because some of them are not used anymore and won't be used anymore in common.
So let's actually go ahead and do that.
We're going to open our common module descriptor file, you can see that's now got some errors as well.
And surprisingly because we've moved the ui package. So since we are actually here that's actually clean
up some of this. So we can go ahead
now, we can leave java fx.base but we can't delete the graphics fxml controls
And also the java.sql because I don't want other packages go ahead delete that
And in terms of the exports, we're exporting the first line academy.learnprogramming.common.
However, we don't need the second exports anymore,
and obviously that's because we've moved the ui completely.
So let's move that.
And obviously the first one, the opens academy.learnprogramming.ui
now that we've moved to the user interface code, we can remove that as well.
So that's cleaned that up a lot, as you can see, now literally any three lines, there, so good.
Back to ui module descriptor file and we probably could have copied and pasted some of these
so I'm going to type it in again, requires javafx.base
And these are basically the ones from the commons module descriptor file
fx.graphics, we also want fxml, so javafx.fxml requires javafx.controls.
We also want to acquire both the other modules, the common and db, so requires academy.learnprogramming.common.
And also requires academy.learnprogramming.db, also exports and by the way you'll notice that the
common and db statements are in red and that's because we need to add two modules dependencies will
do that shortly.
exports academy.learnprogramming.ui to javafx.graphics. Then we want-- opens when
opens a UI to Java.
fx.fxml and we discussed in previous videos the reasoning behind this. So we're
just basically moving those from one module descriptor file to another, javafx.fxml over there
and that should be it.
So that should be everything.
But again you've got these two errors here.
And again that's because we have to add two modules, these two modules as dependencies to the UI module.
So while -- file project structure is probably the quickest way of doing that.
Select the UI module.
Then we can click on, there's a green button if you're on Windows to the right way down here. For Mac, plus, I want
to select module dependencies this time, module dependency and notice I've got two options to select, so control
click or shift click depending on your operating system.
Select both of them. Click OK.
Now edit both of those as dependencies to our module, click ok again, and you can see that those errors
have disappeared from our module descriptor file ui for our module.
So let's get back to our controller class and you can see that we've got a lot of these things fixed.
Now, but we still got this issue with datasource that's still showing in red and if we hover over,
you can see the problem there is that our db module doesn't export to module ui,
so in other words the db module was not exporting the package db so that other modules can use them
from the db package.
So at the moment I can't use them because we haven't export them.
So we just need to get our export statement, we'll add an export statement to our module info file for the db
module descriptor file, so let's go and dive in that, this went down here
So what we're going to do there is basically what three lines there, come down here and type exports
academy.learnprogramming.db
If you then go back to our controller, you could see that that fixes the error. Alright so I think we're good
to go, we actually fix everything now.
Let's get back to our main which now is in ui, right click and run that and just check that it's all working
it's looking good and up and going.
I'm going to click on an artists and show the albums. That's working nicely. All right so we managed
to now have our app working and we've migrated it from a single project structure to a three module structure.
So that's the challenge resolved and finished.
Now we've got some extra statements that we don't really need.
in module info, module descriptor files
But we're going to fix those in the next video and we're going to do that by understanding a way around
that using transitive dependencies.
So I'll see you in the next video.