1 00:00:00,390 --> 00:00:02,610 Now we're ready to build the dealership application. 2 00:00:03,910 --> 00:00:09,040 In this section, you learn to control how your code runs using false statements and switch statements. 3 00:00:11,260 --> 00:00:15,140 The AFL statement uses conditions that return either true or false. 4 00:00:15,790 --> 00:00:18,970 And the Switch statement compares a value to a list of cases. 5 00:00:20,030 --> 00:00:25,610 Now, the apps we built so far in this section are static in the sense that they use predefined values. 6 00:00:26,270 --> 00:00:30,830 If you want to take your application from static to dynamic values, need to come from the user, like 7 00:00:30,830 --> 00:00:31,850 what you're seeing here. 8 00:00:36,250 --> 00:00:40,300 And so the goal of the next three lessons is to build an interactive dealership application. 9 00:00:40,540 --> 00:00:44,380 This application is going to test everything that you learned so far, so let's get started. 10 00:00:46,310 --> 00:00:51,020 First thing I'll need you to do is create a new class by yourself inside the Section three folder. 11 00:00:51,440 --> 00:00:55,850 Create a new file named Dealership de Java and make sure the class has a main method. 12 00:01:01,330 --> 00:01:02,300 Let's do some review. 13 00:01:02,320 --> 00:01:03,340 What is the scanner? 14 00:01:03,880 --> 00:01:09,010 Scanner waits for the user to enter a value in the terminal for this app, we need to ask the user some 15 00:01:09,010 --> 00:01:09,850 questions. 16 00:01:12,500 --> 00:01:15,110 Then Skinner needs to wait for the user to enter some text. 17 00:01:15,320 --> 00:01:21,980 We do that with scanned next line, and then the user must go to the terminal type text and press enter. 18 00:01:22,700 --> 00:01:27,650 Once the user presses enter scan, the next line picks up the text and stores it in the variable. 19 00:01:31,010 --> 00:01:33,500 We need to import Skinner in order to use it. 20 00:01:34,010 --> 00:01:38,300 We'll start by making a new instance of scanner scanner scan. 21 00:01:40,020 --> 00:01:45,760 Is equal to new scanner that can receive input from the system system. 22 00:01:47,200 --> 00:01:51,870 And now this line of code should come with an error because remember, we have to import scanner before 23 00:01:51,870 --> 00:01:52,680 we can use it. 24 00:01:53,370 --> 00:01:58,770 And what's really convenient is that Visual Studio code as a cool auto import feature, let's assume 25 00:01:58,770 --> 00:02:02,430 you want to use scanner, but you don't know where to import it from and you're too lazy to look it 26 00:02:02,430 --> 00:02:02,640 up. 27 00:02:03,360 --> 00:02:04,800 Well, just write scanner. 28 00:02:05,280 --> 00:02:07,230 Wait for the scanner option to show up. 29 00:02:07,530 --> 00:02:11,160 Click on it and VSCO knows to import it for you. 30 00:02:12,020 --> 00:02:12,930 That's pretty cool. 31 00:02:12,930 --> 00:02:17,250 If you're too lazy to look up or to import something from, then this feature has you covered. 32 00:02:20,820 --> 00:02:27,210 All right, it's time to build the app at the Java dealership, clients can buy a car or Selwyn first, 33 00:02:27,210 --> 00:02:29,250 your app should give the client a warm welcome. 34 00:02:29,850 --> 00:02:30,870 So we're going to print. 35 00:02:31,290 --> 00:02:34,560 Welcome to the Java dealership. 36 00:02:39,870 --> 00:02:43,290 Then it should give the user the option to buy a car or sell one. 37 00:02:44,160 --> 00:02:45,900 So first, I'm going to print. 38 00:02:49,270 --> 00:02:51,580 Select option to buy a car. 39 00:02:59,680 --> 00:03:02,140 Followed by another print where we say. 40 00:03:06,050 --> 00:03:08,630 Select Option B to sell a car. 41 00:03:15,770 --> 00:03:16,760 Let's run our code. 42 00:03:22,760 --> 00:03:24,890 And we're off to a very good start. 43 00:03:25,610 --> 00:03:29,180 The next thing we need to do is use scanner to get values from the user. 44 00:03:29,750 --> 00:03:33,140 Remember that scan the next line waits for the user to enter a string. 45 00:03:33,890 --> 00:03:40,670 Here, your application presents the user with a bunch of options, and we need the right string option 46 00:03:41,210 --> 00:03:42,350 is equal to scan. 47 00:03:42,560 --> 00:03:46,250 Next line, which is going to wait for the user to enter a string value. 48 00:03:47,310 --> 00:03:48,510 Let's rerun our code. 49 00:03:57,140 --> 00:03:59,000 And everything works as we expect. 50 00:04:00,360 --> 00:04:03,570 Stand up next line waits for the user to enter their next string. 51 00:04:03,990 --> 00:04:06,720 And the user went to the terminal and typed an option. 52 00:04:07,910 --> 00:04:12,800 Now, before we use this value and move forward, you might have this burning question inside you that 53 00:04:12,800 --> 00:04:13,760 I want to address. 54 00:04:14,420 --> 00:04:16,010 Why didn't we use car? 55 00:04:17,010 --> 00:04:20,100 Well, next line reads the input as a string value. 56 00:04:20,790 --> 00:04:24,180 If you hover over the next line method, it says that it returns a string. 57 00:04:24,600 --> 00:04:28,080 So no matter what the user writes, it will be interpreted as a string. 58 00:04:28,740 --> 00:04:31,800 And you cannot store a string inside of a car variable. 59 00:04:32,370 --> 00:04:35,850 There are ways to work around the situation, but it's honestly not worth the headache. 60 00:04:36,180 --> 00:04:38,940 The input comes in as a string, so let's just treat it as such. 61 00:04:41,760 --> 00:04:45,690 Now we can use which to compare the user's option against the list of options. 62 00:04:46,320 --> 00:04:50,880 There are only two options A and B, so we're going to make a switch statement. 63 00:04:54,910 --> 00:04:56,530 And it's going to compare the option. 64 00:04:58,840 --> 00:05:00,160 Against Case A. 65 00:05:03,450 --> 00:05:04,380 In case B. 66 00:05:07,410 --> 00:05:10,320 And as always, we're going to add the usual default case. 67 00:05:11,730 --> 00:05:14,130 Based on what the user chooses, we're going to print. 68 00:05:16,420 --> 00:05:22,750 You chose option and will close the string and connect the option that they chose. 69 00:05:27,430 --> 00:05:29,950 And if the user chooses something else, we're going to print. 70 00:05:32,070 --> 00:05:33,180 Invalid option. 71 00:05:35,370 --> 00:05:39,510 If you happen to be yelling at your screen and you forgot the brake keywords, then you're learning 72 00:05:39,510 --> 00:05:40,110 fast. 73 00:05:40,800 --> 00:05:44,940 In any case, when there's a case match switch runs every case that follows. 74 00:05:45,420 --> 00:05:48,270 So don't forget that the break you were at at the end of each case. 75 00:05:54,200 --> 00:05:55,340 Rerunning our code. 76 00:06:02,130 --> 00:06:03,780 That looks a lot better. 77 00:06:05,570 --> 00:06:07,880 Switch compares your value against each case. 78 00:06:08,000 --> 00:06:14,720 It matches Case A Java runs case A and then it sees the break key keyword, which ends up breaking the 79 00:06:14,720 --> 00:06:15,770 sweat statements. 80 00:06:16,580 --> 00:06:19,670 Always remember to add the break you were at the end of each case. 81 00:06:22,930 --> 00:06:26,980 Once you're done with scanner, you need to close it if you want to avoid a memory leak. 82 00:06:27,250 --> 00:06:29,940 So we're going to close the scanner with scan dark cloak. 83 00:06:30,100 --> 00:06:31,300 It's good practice. 84 00:06:32,590 --> 00:06:33,550 And now we're all done. 85 00:06:35,980 --> 00:06:40,600 In this lesson, you started building the dealership application in the next one, we're going to add 86 00:06:40,600 --> 00:06:43,270 some logic for Case A buying a car.