1 00:00:00,300 --> 00:00:01,180 ‫Welcome back. 2 00:00:01,200 --> 00:00:06,390 ‫And this video we are going to solve the challenge from last lecture. 3 00:00:06,390 --> 00:00:11,490 ‫And I hope you try to do it yourself and you fix it or you solve the problem. 4 00:00:11,490 --> 00:00:17,100 ‫So let's go ahead and create a login system and a well register system. 5 00:00:17,100 --> 00:00:20,100 ‫We need first of all to register before we can log in. 6 00:00:20,100 --> 00:00:22,440 ‫So let's create two methods to do that. 7 00:00:22,440 --> 00:00:29,820 ‫One of them is going to be public void or actually registration could go right. 8 00:00:30,150 --> 00:00:33,720 ‫It can be void and register. 9 00:00:34,410 --> 00:00:42,480 ‫And then in here we want to do all the registration, but as we want to use this method inside of our 10 00:00:42,480 --> 00:00:44,820 ‫main method, we need to make it static. 11 00:00:46,080 --> 00:00:49,620 ‫So in here we need to do certain things. 12 00:00:49,620 --> 00:00:55,020 ‫First of all, we need to check or we need to tell the user what to do. 13 00:00:55,050 --> 00:00:57,480 ‫Console right line. 14 00:00:58,410 --> 00:01:09,480 ‫Please enter your username and then the user can enter his username and we want to store that username 15 00:01:09,480 --> 00:01:12,150 ‫somewhere at least for the runtime. 16 00:01:12,150 --> 00:01:20,640 ‫So we need to create a new variable here, which I'm going to call static string username. 17 00:01:21,990 --> 00:01:27,060 ‫And in here we set the username as whatever the user has entered. 18 00:01:27,060 --> 00:01:30,120 ‫So console dot read line. 19 00:01:32,520 --> 00:01:39,780 ‫Next, I want to do the same thing with the password, so I'm going to copy the code and I'm going to 20 00:01:39,780 --> 00:01:44,850 ‫replace that with password and I'm going to replace this. 21 00:01:45,610 --> 00:01:47,460 ‫Username with password as well. 22 00:01:47,470 --> 00:01:50,680 ‫Therefore I will need a password variable. 23 00:01:50,680 --> 00:01:54,130 ‫So static string password. 24 00:01:55,330 --> 00:01:57,760 ‫And here of course we won't need the username anymore. 25 00:01:57,760 --> 00:01:59,230 ‫We will need the password. 26 00:01:59,230 --> 00:02:03,280 ‫So first we check for the username, then we check for the password. 27 00:02:03,830 --> 00:02:10,700 ‫Once that is done, once the user has registered, we want to tell him registration has worked. 28 00:02:10,910 --> 00:02:16,440 ‫So registration, complete it. 29 00:02:18,290 --> 00:02:27,590 ‫And then maybe even another line where we simply have a bunch of dashes just like that. 30 00:02:28,490 --> 00:02:28,910 ‫All right. 31 00:02:28,910 --> 00:02:31,330 ‫So that's how we can register the user. 32 00:02:31,340 --> 00:02:37,970 ‫And now let's create the login method, public static void login. 33 00:02:37,970 --> 00:02:42,080 ‫And that one will be quite similar to the register method. 34 00:02:42,080 --> 00:02:45,650 ‫So I'm just going to check for the username first. 35 00:02:46,250 --> 00:02:50,660 ‫And in here I have first ask for it and then I check if it actually works. 36 00:02:50,660 --> 00:02:57,320 ‫So if username is equal, so double equals sign whatever the user has entered. 37 00:02:57,530 --> 00:03:04,010 ‫If that's the case, then go ahead and run the following code. 38 00:03:04,010 --> 00:03:06,140 ‫Check for the password. 39 00:03:06,140 --> 00:03:19,010 ‫So then go ahead and ask for the password and check if password is equal to whatever the user has entered. 40 00:03:19,340 --> 00:03:24,500 ‫Now we can go ahead and write to the user that logging in has worked. 41 00:03:25,040 --> 00:03:34,430 ‫So red line login successful and if it didn't work, so if the user has not entered the correct value 42 00:03:34,430 --> 00:03:36,050 ‫then we'll tell him. 43 00:03:36,860 --> 00:03:38,570 ‫Log in failed. 44 00:03:39,200 --> 00:03:43,670 ‫Wrong password restart program. 45 00:03:46,060 --> 00:03:47,470 ‫And the same thing. 46 00:03:47,650 --> 00:03:53,590 ‫We're going to tell him if it didn't work to log in because he didn't enter the correct username. 47 00:03:54,460 --> 00:04:02,860 ‫So in here we simply say console right line login failed, wrong username restart program. 48 00:04:02,860 --> 00:04:09,790 ‫In our case, we don't have wild loops yet, so we don't have the capability of running while loops 49 00:04:09,790 --> 00:04:13,990 ‫so the user can log in again or try to log in multiple times. 50 00:04:14,200 --> 00:04:16,690 ‫So we're just going to solve it that way now. 51 00:04:16,690 --> 00:04:18,670 ‫So we say restart the program. 52 00:04:19,960 --> 00:04:22,690 ‫So we have an if statement where we check. 53 00:04:23,320 --> 00:04:24,940 ‫Did it work? 54 00:04:24,940 --> 00:04:27,280 ‫So did the user enter the correct username. 55 00:04:27,280 --> 00:04:29,890 ‫If he did, then we ask for his password. 56 00:04:29,890 --> 00:04:34,510 ‫If he entered the right password, we simply say, All right, cool, you're logged in. 57 00:04:34,540 --> 00:04:39,280 ‫If you didn't enter the right password, we say, login failed and the password was incorrect. 58 00:04:39,280 --> 00:04:45,910 ‫By the way, if he failed to enter the right username, we run into this LX statement and we simply 59 00:04:45,910 --> 00:04:48,370 ‫say, okay, failed to log in. 60 00:04:49,180 --> 00:04:59,020 ‫Now what we can do is we can go ahead and register and then we can log in. 61 00:05:01,300 --> 00:05:11,710 ‫And finally we need to do console, right, to do anything so that at least something is written onto 62 00:05:11,710 --> 00:05:12,580 ‫the console. 63 00:05:13,390 --> 00:05:14,110 ‫All right. 64 00:05:14,110 --> 00:05:19,360 ‫So we had to create global variables here, and we haven't covered that in too much detail yet. 65 00:05:19,360 --> 00:05:25,360 ‫But we need to create them globally because we need to use them in multiple methods. 66 00:05:26,110 --> 00:05:29,230 ‫That's something that is based on scope. 67 00:05:29,230 --> 00:05:34,900 ‫So we have covered scope and well, that's pretty much what it's really about. 68 00:05:34,900 --> 00:05:36,640 ‫So let's check it out. 69 00:05:36,640 --> 00:05:38,770 ‫Let's run the code and see if it works. 70 00:05:38,770 --> 00:05:40,720 ‫So please enter your username. 71 00:05:40,720 --> 00:05:43,480 ‫I'm going to enter Dennis and your password. 72 00:05:43,480 --> 00:05:48,340 ‫Password one highly secure password now registration complete. 73 00:05:48,340 --> 00:05:50,110 ‫Now please enter your username. 74 00:05:50,440 --> 00:05:56,710 ‫I'm going to enter Dennis and I'm going to enter a wrong password and it says login failed wrong password. 75 00:05:56,710 --> 00:06:01,330 ‫Restart the program so I'm going to close and start again. 76 00:06:03,490 --> 00:06:07,240 ‫Please enter username then is again password one. 77 00:06:07,520 --> 00:06:13,180 ‫Now I'm going to type in an incorrect username login failed wrong username reset program. 78 00:06:13,180 --> 00:06:14,530 ‫All right, I'm going to do that. 79 00:06:14,530 --> 00:06:16,510 ‫And now let's enter it correctly. 80 00:06:16,510 --> 00:06:23,950 ‫So then is password one then is password one. 81 00:06:24,370 --> 00:06:25,500 ‫Log in successful. 82 00:06:25,510 --> 00:06:25,840 ‫Great. 83 00:06:25,840 --> 00:06:27,220 ‫And now I can start hacking. 84 00:06:27,730 --> 00:06:34,030 ‫All right, so now you know how to create a very, very basic login screen or register and login screen. 85 00:06:34,030 --> 00:06:40,720 ‫Of course, we don't store the information yet, at least not longer than runtime, but we will see 86 00:06:40,720 --> 00:06:43,780 ‫how to store data permanently later on. 87 00:06:44,650 --> 00:06:45,040 ‫Great. 88 00:06:45,040 --> 00:06:49,240 ‫I hope you could manage to build something similar to that and could test it. 89 00:06:49,240 --> 00:06:51,940 ‫And well if not, no worries. 90 00:06:51,940 --> 00:06:58,330 ‫Try to shut this down or close this and just try to rebuild it tomorrow. 91 00:06:58,360 --> 00:07:04,840 ‫So give yourself some time and just try to do yourself based on memory and based on what you have learned. 92 00:07:04,840 --> 00:07:06,790 ‫So just try to do that tomorrow. 93 00:07:07,180 --> 00:07:07,570 ‫Great. 94 00:07:07,570 --> 00:07:11,110 ‫So I'd say that's a good start for a login screen. 95 00:07:11,260 --> 00:07:13,540 ‫And in the next video, we're going to go ahead. 96 00:07:13,540 --> 00:07:14,740 ‫So see you there.