Now we're ready to build the dealership application.
In this section, you learn to control how your code runs using false statements and switch statements.
The AFL statement uses conditions that return either true or false.
And the Switch statement compares a value to a list of cases.
Now, the apps we built so far in this section are static in the sense that they use predefined values.
If you want to take your application from static to dynamic values, need to come from the user, like
what you're seeing here.
And so the goal of the next three lessons is to build an interactive dealership application.
This application is going to test everything that you learned so far, so let's get started.
First thing I'll need you to do is create a new class by yourself inside the Section three folder.
Create a new file named Dealership de Java and make sure the class has a main method.
Let's do some review.
What is the scanner?
Scanner waits for the user to enter a value in the terminal for this app, we need to ask the user some
questions.
Then Skinner needs to wait for the user to enter some text.
We do that with scanned next line, and then the user must go to the terminal type text and press enter.
Once the user presses enter scan, the next line picks up the text and stores it in the variable.
We need to import Skinner in order to use it.
We'll start by making a new instance of scanner scanner scan.
Is equal to new scanner that can receive input from the system system.
And now this line of code should come with an error because remember, we have to import scanner before
we can use it.
And what's really convenient is that Visual Studio code as a cool auto import feature, let's assume
you want to use scanner, but you don't know where to import it from and you're too lazy to look it
up.
Well, just write scanner.
Wait for the scanner option to show up.
Click on it and VSCO knows to import it for you.
That's pretty cool.
If you're too lazy to look up or to import something from, then this feature has you covered.
All right, it's time to build the app at the Java dealership, clients can buy a car or Selwyn first,
your app should give the client a warm welcome.
So we're going to print.
Welcome to the Java dealership.
Then it should give the user the option to buy a car or sell one.
So first, I'm going to print.
Select option to buy a car.
Followed by another print where we say.
Select Option B to sell a car.
Let's run our code.
And we're off to a very good start.
The next thing we need to do is use scanner to get values from the user.
Remember that scan the next line waits for the user to enter a string.
Here, your application presents the user with a bunch of options, and we need the right string option
is equal to scan.
Next line, which is going to wait for the user to enter a string value.
Let's rerun our code.
And everything works as we expect.
Stand up next line waits for the user to enter their next string.
And the user went to the terminal and typed an option.
Now, before we use this value and move forward, you might have this burning question inside you that
I want to address.
Why didn't we use car?
Well, next line reads the input as a string value.
If you hover over the next line method, it says that it returns a string.
So no matter what the user writes, it will be interpreted as a string.
And you cannot store a string inside of a car variable.
There are ways to work around the situation, but it's honestly not worth the headache.
The input comes in as a string, so let's just treat it as such.
Now we can use which to compare the user's option against the list of options.
There are only two options A and B, so we're going to make a switch statement.
And it's going to compare the option.
Against Case A.
In case B.
And as always, we're going to add the usual default case.
Based on what the user chooses, we're going to print.
You chose option and will close the string and connect the option that they chose.
And if the user chooses something else, we're going to print.
Invalid option.
If you happen to be yelling at your screen and you forgot the brake keywords, then you're learning
fast.
In any case, when there's a case match switch runs every case that follows.
So don't forget that the break you were at at the end of each case.
Rerunning our code.
That looks a lot better.
Switch compares your value against each case.
It matches Case A Java runs case A and then it sees the break key keyword, which ends up breaking the
sweat statements.
Always remember to add the break you were at the end of each case.
Once you're done with scanner, you need to close it if you want to avoid a memory leak.
So we're going to close the scanner with scan dark cloak.
It's good practice.
And now we're all done.
In this lesson, you started building the dealership application in the next one, we're going to add
some logic for Case A buying a car.