Transcription
[Music] hello everyone. I'm Landis Chung, and today we're going to learn bash and SQL by building a bike rental shop. This is a 210 lesson course, so 78 lessons longer than this one right here. Uh, will probably take quite a while, so let's get started, and let's do this course. I'm going to run with code Ally. I'm sure it's going to pop up here. Yeah, and then we should be good to go. All right, here we go. We can start this tutorial. The first thing we have to do is start the terminal, and we're doing that with control backtick. And now we have to do Echo hello terminal to move on. All right, Echo hello terminal. Here we go, and then Ctrl enter to move on. And looks like we have to connect to psql or postgres, so we do psql -username=freecodecamp and DB name=postgres. Here we go. List the databases with backslash l, and we just have the default ones right now, and we need to create a new database named bikes, so let's do that. Do that with create database bikes, and there we go. Those databases again to make sure it's there, and it is there. It is. Connect to it so you can start building a structure. You have a bulb in C, so you have to do backslash c x, and then we are now connected to bikes as user freecodecamp. We work, we were connected to the postgres database. Now we're connected to the bikes database. All right. Your database needs three tables: one for your bike inventory, one for your customers, and one for the bikes that are rented out. Create a table named bikes in your database. All right, so we're gonna do create table bikes. So we have a bikes database and a bikes table. Looks like, and display the tables. We can do that with backslash D. There we go. Like the table will have a few columns for bike information. First is a unique ID column. Add a column to the bike stable name bike ID. We can do that with alter table bikes add column, and we want to add bike ID, which is going to be a serial serial, and it's also going to be a primary key, and then make sure to close that off with a semicolon. And now we can display the bikes again. Bikes table backslash D bikes, but you do see the sequence in there as well. That's for the serial. First Column. Set at a column name type for the type of bike. Okay, so we're gonna do alter table bikes add a column, and we have to add this type, which is going to be a varchar(50) and it's going to be not null. Yeah, there we go. Let's play the details of the bikes table again. Backslash D bikes. There we go. The first two columns look good. Add a column name size to the bike stable that is an INT and has a not null constraint. All right, so we're going to do the same thing basically. Alter table bikes add column. This is going to be size, which is going to be a type of int, size, just an INT, and it's also going to be not null, so that should be good. Yep. Another column named available, make a Boolean. All right, and also not null. Give it a default value of true. Okay, so available, so by default they're going to be available, Boolean, and not null, and then we can add defaults at the end here. Uh, defaults of true, like so. Yeah, cool. Let's play the details of the bikes table again. Backslash D bikes. Okay. Can we see the defaults of true there? That's cool. That table is done for now. Create another table named customers. All right, create table customers, and there we go. Add a customer ID column. At alter alter table customers add a column, and we have to add the customer ID. It's going to be type serial and primary key, primary key meaning it's Unique and it's what you use to connect the two other tables. Display the details of the customers table. That backslash D customers. There it is. Add a column name phone for customers phone numbers. All right, so we're gonna do alter table customers add column. This file is going to be the phone call. Make it a varchar, looks like, and a max length of 15, and then also make sure it can't be null and be unique. All right, so just do not null unique. Uh, no unique. There we go. Cool. And last column, call it name. All right, so this is going to be a name, which is also going to be a varchar(40) and not null, except it can be the same as other as other rows. All right. Display the details of the customers table. Backslash D customers. There they are. Tables finished. Lastly, you need a table to store which bikes are rented and who has rented them. Create a new table named rentals. Create table rentals. There we go. Add a rental ID column. Alter table rentals add column, and this uh rental ID column. Yeah, serial and primary key. Display the details. Backslash D Rentals. Next, you need a column for the customer who's renting a bike. Add a column named customer ID. This will have an ID of a customer, and then make comment int and not null. All right, so we're going to do the same thing. Alter table rentals add column. This time it's going to be the customer ID, customer ID, and it's going to be int and not null, and then we're going to add a constraint to it. I guarantee it. Yep. All right. Make the column you just added a foreign key. Yep, so we're going to do alter table rentals add foreign key. The column name is this customer ID column, customer ID, and it references, right, for princess the customers table. Yeah, customers, and then a customer is customer ID column. Yeah, customer ID. There we go. Should be good. Awesome. Let's play the details of the rentals table. Make sure your key is set. Backslash D Rentals. Foreign key is set. You need another column, so you know what like a customer is renting. Add a column named bike ID. Hibernates. So basically the same thing, I think. Um, this time it's going to be bike ID, and then we're going to kind of constraint to that as well for the bikes. Yeah. I had a column for a foreign key. This time it's going to reference. So just change customer to bike everywhere. Uh, bike references the bike stable and a bike ID column of the bike stable. All right, bike ID. Suite. Display the details. XSD Rentals. Moving along. You want to know when a customer rents a bike and when it gets returned. Add a column to your rental stable name date rented that's a type of date. Make sure it can't be null and give it a default value of now. All right, so we need to alter table the rentals, and then we're going to add a column of date rented. It's going to be a type of date, and it's going to be not null and default to now, like so. Here we go. Play the details of the rentals table again. Backslash D Rentals. Okay, we got everything in there. Looks good. Lastly, you need a column for when a customer returns a bike. Add a column named date return that's a type of date. All right, and then I'm guessing it can be nullable. It's just going to be a type of date and a return date. [Music] All right. Yeah, move on. Okay. Included. Okay. Just okay. Whatever. The tables are all finished. Display all the tables so you can see what you ended up with. Backslash D to view them all. You have nine bikes in your inventory. Add the first one to your bikes table. Um, yeah, it has a type of mountain and a size of 27. Make sure to put your varchar values in single quotes. All right, so you have to do an insert statement here. Insert into bikes values, and I insert into bikes, and then inside here we have to specify what columns they are. It's going to be type and size, type, size, and the values are going to be, not 10 and 27. Okay, so we're going to go Mountain, and I think yeah, single quotes, and then 27 for the size. All right, sweet. View all the columns with the select, so we can do select all from bikes. Who looks like it's all working. The bike ID and available columns are filled in automatically. Insert another bike. Give it a type of mountain and a size of 28s. So we have to insert something. Yeah, insert another bike, another type of mountain, but size of 28. All right, so we can do that pretty easily. Just change 27 to 28. Another mountain bike, make it a 29 inch bike. Getting pretty big now. All right. At a 27 inch uh road bike, 27 inch Road. All right, cool. Select to view all the data and like stable again. Select all from bikes. There we go. Yeah, the two bikes to your inventory. They are 29, 28, 29 inch road bikes. Try to add them both, one with Once man. Um, really, I guess I can uh Road 28, and then I can add another one. Do I do that with like another currency here? I like forgot how to do this. I think it's like this, and then we can just add another thing here and have another road and 29. I think this will work. Never mind. Yeah, I kind of forgot how to do that. Oh, it is like this, isn't it? Yeah, except I I guess I what did I do? Oh, no extra parentheses, I guess. So get rid of these, and that should work. Okay, cool. There's three more bikes. Add 19, 20, and 21 inch BMX bikes to a table. Add them with one command. Uh, man, can we I don't want to though. Um, but they're making me do it. BMX, at least I think they're BMX, 20, 19, BMX 20, and BMX 21. BMX 21. There we go. You're all the dating your bikes table again. Select all from bikes. All right. Your current inventory is all added. For the rest of the project, I recommend leaving it that terminal open and connected, and that you should split the terminal so you have a second one. It reads, so we're gonna add another terminal just like that. I'll do that. Yeah, whatever. After you have opened it, use the touch command to create a file named bikeshop.sh in your project folder. All right, so we're going to go touch BikeShop.sh, and we are in the project folder, so we should be good. All right. Did this file will be the program of your rental shop. Let's get this thing here. Throw it up. Come on. There we go, and then we need to add this shebang at the top, and that is for running it in bash the whole time in the file. Use Echo with the e-flag to print this. All right, so I'm going to copy this bad boy to the new line at the beginning and end. I've done this quite a few times before. Now Echo -e, and now we have to paste this in here and a backslash n at the back and got the fronts for the new lines. All right. Use the terminal and the chmod command to make it executable. So we do that with chmod +x and then they like shop the sh file type. bikeshop, and then we run the script. There we go. In the script, create an empty function named main menu. This will have a few options to enter when the script runs. All right, so how do we do a function? I think it's just like main menu, and then you do parentheses or something, not really sure. Need a hint. Okay. Yeah, parentheses, and then uh make sure to close those off. There we go. In the function, Echo the text how may I help you. All right, let's copy that. Echo how may I help you. So there's a greeting. All your main menu at the bottom of the file. Um, so the function runs when you start the script. All right, so we're going to call this function main menu. I think we have to call it like that. I think yeah. Okay. On the file and terminal again, so you can see what it's outputting. Unexpected end of file. It's coming along. Add another Echo command in the function below the other one. Make it output text that looks like this. Okay, and note that there's an empty line at the start. Uh-huh. Another Echo command in the function below the other one and make it look like this. All right, below this one. Echo. I think we have to do like the attach e and then paste and then I have to do like new lines in these. I think that's what they want. Backslash n and then backslash n here, and then we also need a backslash n at the start. I think this will work. Make the output look like this. Echo -e. Do I need another backslash n at the back? Run. Um, it should display the correct main menu options. Okay, it's not displaying anything. Echo -e \n. Not really sure what's wrong, so I'm gonna get a hint. Yeah, I need a space at the end. Why is there? Um, there's a syntax error. Oh, I might have a main menu along, maybe just like that. Maybe that's why. Yeah, I actually had that wrong, but it's the previous one anyway. From the file to make sure it worked, and there we go. Okay, that makes more sense. You have some options displaying. Next, you need to get input from whoever is using the program. Use the read command to read input into a variable called main menu selection below the options. All right, so we're going to read main menu selection. So let's copy this, and right here we're going to go read and paste, and then whatever we input it will read that in. When an option gets entered, you need to take a user to one of those other menus and an empty rent menu function below the main menu function. Google it's below this main menu function. We need to be a rent menu function. Here we go. For the time being, just Echo rent menu in the function so you can know it's working. Echo rent menu. Add an empty return menu function below the rent menu function for when a user enters the option to return a bike. Okay, so we need a return menu function. Return menu. Pretty good. You could use a good print return menu. Let go return menu. Add an empty exit function below the return menu function. All right, so let's just copy this down. This has to be exit. sbmt2. Darn it. There we go, and then Echo thank you for stopping in this time. Okay, let's copy that. Go paste uh with a new line at the beginning and end. Okay, so we have to add new lines. We have to add this -e in there, and then we can do backslash n. That shouldn't be good. Yep. When a user enters an option at the main menu, you want to take them to the appropriate Sub menu. You can use a case statement for this. Here's an example. Okay. Uh, the expression you want is the main menu selection variable. You're expecting it to be a one, two, or three for your various menus. Add a case statement that takes users to the corresponding menus. The star is for when anything else is entered. Take users to the main menu when the variable isn't a one, two, or three. Okay, let's copy this first, and let's throw this is in the main menu selection variable. Um, and yeah, that's for this right, and then so I have to paste that here. Foreign is main menu selection variable. You were expecting it to be one, two, or three for the various menus. Yeah. Okay. Rent a bike, return bike, or exit. That makes more sense. Add a case statement. Uh, the stars from when anything else is entered. Pick users to the main menu when the variable isn't on one, two, or three. So that's for the star. The pattern is one, two, or three. One, two, three. The expression is this main menu selection, and then star is going to be a main menu. I think does this work? It's running. Um, and I'll put the correct text. Oh, yeah. Okay. I also need to change these up, so the first one is rent a bike. So rent bike or rent menu. All right, menu. We have return menu. Return menu, and then we have exit here. Exit. I think that'll work. Come on, baby. I need dollar sign in front of here as well. Ah, yes. Let's go. I mean, 30 of the way. Okay. Run the script a few times and try out the different menus. Be sure to enter something other than one of the options to go to the main menu. All right, let's enter to earn bike. Aha. Return menu and argument to where you call main menu in the case statement. It should be please enter a valid option. The Next Step will adjust the function so the message is printed when a user enters an invalid option. All right. Add an argument to where you call main menu in the case statement. Should be this. An argument to where you call main menu in the case statement. An argument. An argument to it. Hadn't argument. Oh, like like this. Um, is that what they want? I can be because that's an argument for this main menu function. Run. I don't know what you want function call. Oh, I see that. No parentheses for this. It's just that comes after, and then that will be the argument. All right. At the top of the main menu function and if condition that checks if there's an argument. Dollar sign one has to the function. If there is, print the message with a new line in front of it. All right, so we need a dollar sign one here, and then we have to say if um if then if my if we have an argument passed to the function. So if we think we can do that with like -z or something, dollar sign one, not Tyler sure, but I think that's what it is, and then print the message with a new line in front of it. For the message, what this thing or how may I help you? I'll print the message which is dollar sign one. Aha. So we can do Echo -e um \n dollar sign one. Maybe. Come on, baby. I don't know. If condition. Uh, what condition do I need? Oh, just like if dollar sign one, maybe. Uh, no, this. Yeah, dollar sign one. Echo. Yeah. Uh-huh. Be sure to put at the top of the main menu function. It is. Um, what am I doing wrong here? Echo -e. If I then if I run it, what do I get? Unexpected token near dollar sign one. What I do? What does my parameter and mean? B. I don't need if there's an argument. Okay, so I think I need these to have spaces here. No. Yes. No. I mean, menu or send one. It should just be one or just nothing. Maybe nothing in here. Okay. I yeah, I'm thinking too much. Like is it's a regular programming language, but it's apparently not. Okay. Run the script and enter an invalid option to see the message. All right, so let's run this, and let's go to return menu. Oh, I should have done the invalid option. I can do that with a five. Yeah, and then it re-asks me. All right. Looks good. Delete the echo rent menu from the rent menu function so you can start adding the ability to rent a bike from the database. All right, so let's get rid of this rent menu Echo function or Echo. You can do that with Ctrl shift key to delete it in one false move. And the rents menu function at three single line comments. All right, so let's grab this, and then do three single line comments inside here. At least we're going to say get available bikes. Um, if nobody's available and send to main menu. Control / There we go. To get the bikes available, you need to query the database from your script. Below the shebang, add a psql variable that looks like this. You will then be able to use it to create the database like that. All right, so underneath this in bash, we're going to connect where that's how we connect. Uh, below the get of like below the get available bikes comment. Create an available bikes variable that gets the bike ID, type, and size columns from bikes table for the bikes that are available. Order the results by the bike ID column. Here's an example. Okay. Well, let's copy that, then below the get available bikes comment. All right, so right here, paste and then get available bikes. Select all from bikes where available equals one, which is true or true, like that. I think. Um, if it gets the bike ID, type, and size, bike ID, type, and size, so I can work. Order the results by their bike ID column. Order by bike ID. Will that work? Yeah, there we go. Cool. Below the new variable, use Echo to print it. Place it in double quotes so it prints any new lines. All right, so we're gonna now Echo uh available bikes. Dollar sign avail
X okay, cool. Run your script and go to the rent menu to see if the available bikes are being printed all right. To rent a bike. Oh, cool, awesome.
In the psql prompt, set the available column to false for all the bikes so you can see what it prints when there's no bikes available. Set the available column to false in the psql prompt. Um, set available column to false for all bikes. All right, so we do that with the update bikes set available equal to false like so. There we go. Run your script and go to the menu to see the output, and now they should be all unavailable. Rent a bike, and nothing shows up. Okay, great. All right.
So there's no bike available variable will be empty in the script below the if no bikes available comments and if condition that checks that their variable is empty. These dollars or use -z to check if it's empty. Place the send to main menu comment in its statements area. In its statements area. Okay, I don't know what you mean by statements, but right below if no bike's available, so we'll go if -z and we're going to say available bikes, uh, dollar sign available bikes. Available bikes is empty, then we have to Echo this thing here: sent to main menu, and then that's all right here. Um, comment. Oh, I see, just a comment this out: sent to main menu [Music] This quote in this one that was a good okay, cool. Very particular bro, what the comments should say below the comment in the if you just added: send users to the main menu and give them the message: sorry we don't have any bikes available right now.
All right, so let's copy that, and we can send them to the main menu by doing this function right, so we can just type in main menu, main menu, menu, and then passing in this uh this argument for the message. All right, run the script and go to the rent menu to see the message. Uh, here to do rent and sorry we don't have any bikes available right now. How may I help you? Let me exit. All right, cool. If no bikes are available, you'll get that message, and else to the if condition for when there is bikes available in it, add four single line comments. Okay, let's grab this. So we need anything else in here? I think we can just do it else like this, and then I don't think we need it. Then I'm not completely certain on that though, uh, and then let's paste yeah, these comments, the control slash and send to main menu. Okay, cool. Below the display available bikes comment you just added, use Echo to print: here are the bikes we have available with a new line in front of the message. Great. Copy uh here are the bikes available right here. Echo -e and \n we need an uh backslash n and paste: here are the bikes we have available. All right. Move the echo command that prints all the available bikes below the message you just added. Uh, to choose this one, let's move this. We can do that with holding alt and then arrow down, and there we go.
Right in the psql prompt, set all the bikes except for the BMX bikes back to true so you can see the list. All right, except for the BMX bikes, so we can do that with update bike set available true where the type does not equal BMX and then we can do yeah BMX and it has to be in single quotes. Okay, there we go. That should be good. All right, all right. Run is this thing gonna work? Select the column from lights true true true except for those ones are false should work. Run it like um it uh it paused on me. I have to reset now. Run or is this thing update six again? Set all the bikes except for being okay, there we go. Yeah, just uh taking a while there. Run the script and go to the rent menu to see the list of bikes available. To run the script, come on. Uh, it's it's paused. Come on, man. Nice. Seriously. Reset again. Unscript and go to the rent menu. Getting ready to run the script. Seriously, can I not? It's not letting me type. Oh, disconnected. Attempting to reconnect. This thing is not doing so hot. Hopefully it saved on the script and then go to the right menu to see the list of bikes available and one. Okay, cool. Hit saved. I just had to like refresh the page and restart it. All right, let's move on.
Instead of directly printing the list, pipe the output into a while loop that reads each line. Here's how that looks. All right, it will read the first line. Okay, so instead of directly printing the list, we're going to do this. Instead of directly printing the list, so right here paste and then we need to do while read part one. Okay, the variables are like ID type and size right here, like ID type and size. Another one in the statements area, use Echo to print them. Oh, and R I could eat bar type bar. Oh, interesting. I suppose there is a bar there. Okay, and then we have to Echo these: Echo bike ID. Do they want us to do somehow in that order? Okay, so just like this and dollar sign. Hopefully this is what they want. Oh my God. Hmm. Sorry, no pipe and R sign size. Hopefully that works. Okay, cool. Run the script and go to the right menu to again see if it's working. Right menu one. All right, here's the bikes available. It's working. Adjust the echo command that prints the bike info so that first line printed would look like this one and then that the rest would look the same but with their bike info. Make sure to escape any characters you need. Come on the pool. So the first one looks like that. Adjust the echo command so the first line printed looks like that one. One like that. The rest would look the same. I don't know what you want. Uh, 27 inch. I see. Uh, let's go backslash a year one 27 backslash quote and then actually that's size dollar signs and then dollar sign type. Okay, I don't know. I don't know. One near Road 29 Road. Okay, so remove this one then. Yeah, I need help. Echo Bike ID. Oh, bike ID. Oh, I see the r sign bike ID and size type bike type by not not real. Come on, man. Really type bike. I really thought that worked. Make the suggested line look like this. It does on that looks pretty good to me. The whole loop should look at that. Yeah, it does. Run. Come on. How's this not working? No -e in here, huh? That's what you want. No -e. Okay, great. Put on the script and go to the rent menu again. One. There we go. That's better.
Below the ask for a bike to rent comments, print: which one would you like to rent with a new line in front of it. All right, below the ask a bike for rent I'll print this: ask for a bike to rent. Echo - make sure there's a new line. Paste just below that. Add a comment to read input into a variable name bike I need to rent. All right. Read paste here we go. Next you want to find out how to test if the user input is a number and the terminal enter this: see if a is a number. All right, the conditional expression will run and Echo $question mark will print the xcode of it. Shoot, I can't oh, that's annoying. I can't you know copy into my terminal. It's annoying. Uh, tilde zero through nine and then Echo $question mark. There we go. One. It printed one for false. Yeah, one means there was uh so weird. It's backwards. All right, meaning that a did not match a pattern or a did not contain a number from zero through nine. Enter the same commands but check if A1 matches the pattern. All right, and do the same command but check if A1 matches pattern. Uh, it does. All right, A1 does contain a number from zero through nine. Enter the same command but change a pattern to this that signifies the start of the pattern and dollar sign means the end. All right, so we need a start, so we do that with a carrots and a dollar sign for the end r sign. All right, wanted for false. A1 does not match the pattern. Using the same syntax, check if one matches a pattern. It should, and yes it does. One does match pattern. Uh, check if one one matches the pattern and it does not. That does not match because the pattern only allows a single number. Add a plus after the zero through nine to allow any strings that start contain one or more numbers and then all right, so plus learning some regular expressions here. It's pretty cool, isn't it? Yeah, so that pattern will match any positive integers. You want to check if the input is not a number, add a uh in front of the comparison with a previous command. All right, so not to number. Um, that doesn't work. Um, yeah, I know. Okay. Oh, right there. I see not uh not where the equal sign is but for the number even there we go. Back in your script below the if input is not a number component and if condition that checks if the input is not a number using the method you just practiced, add the sent a main menu comment in the then area. Yes. All right, so let's copy this part. You can go here I guess the input is not a number, so we can do if paste that and then we can do then and fi and this one one here is going to be the number which is bike ID to rent cosine bike e to rent and then if it is not a number then we have to go back to the main menu, main menu, and we have to add a message and guessing yes. Add the send to main menu comments. Oh, just send me menu comments. I see and then in the next one we're going to send it back to the main menu that does my prediction at least should be good. Cool. If the bike ID to rent variable is not a number add the code. All right, let's copy this and we need to do the main menu again, menu and then paste that there. That is not a valid bike number. Click on the script, go to the rent menu and enter something that isn't a number to make sure it's working. Okay, enter something that's not a number. Uh, let's see here. Rent a bike. Something that's not a number. ASDF and that is not a valid bike number. How may I help? Get cool. All right. Exits and else area for when the input is a number. Yeah, these three single line comments in it. All right, and else area for when the input is a number we can add these comments to it. Let's copy that and else area so right here else what are we going to do? We're gonna do some of these things. Get bike availability if not available and send to main menu. Another send to main menu. Okay. Below the git bike availability comment you just added, create a bike availability availability variable. Okay, set it equal to a query that gets the available column from the bikes table for the input. All right, so we have to add a query and we did that up here somewhere, uh, right here psql, so let's copy this down. Let's put it here and it has to be a query that gets the available column from the bytes table. Cuts gets the available column. All right, it gets the available column from bikes. Um, I'm guessing it's where the right ID to rent is uh there. Yeah, where bike ID equals bike ID to rent like ID to over it. I'm guessing that's what we need. Also make sure to only get the row if it is available. Um, run. Okay, what is going what's wrong? What is wrong? One or at the bike two. Um, it's not I need to Echo this or no. Create a set it equal to query. I could see available column from the bikes table for the input. Also make sure to only get the row if it is available. Select available from bikes where bike ID is that. That looks right to you. Let's see what they say and available equals true. Oh yeah, and available equals true. I see and available and of a l Global equals true. Okay, that should work now. Cool. Right below the variable you just created, use Echo to print it so you can see what it looks like. Use Echo to print this: Echo $like availability ability. There we go. Around the script a few times. Right one. We want negative number four or three and that is available to rent. Code variable will be t or empty. Below the if not available put these send to main menu comment in its statements area. All right, if not available comments that checks if it's empty. All right, so we have to check if it's empty and then if it is sent to them to the main menu and we'd do this with this if statement up here except just changing that's variable. If not available paste. I wonder if I have to tab this over and then inside of here we're going to centimate menu. Okay, I'm going to tab this over as well and then this has to be bike availability instead like availability and build it t. All right, should work right [Music] and if it checks if it's empty bike availability. I don't know. I don't know. Do they want me to get rid of the echo in a statement area? The variable will be t or empty or empty. So if it's empty then send it to main menu buys availability is so hard to spell. Okay, does that work? Okay, I need help here. Yeah, I'm like are you serious right now? So what I have row are you serious? Run to main menu. I guess like available maybe I'll figure it out later. I'm not really sure what's going on, so I'm gonna end up here for now. All right, so I figured out how to get past this one. Uh, the way you do it is you have to type in this command so that you can see the test files. So all these test files uh so yeah, once you run this command this uh free code Camp folder will be open to you and then you can check out all the test files and the test file we're interested in is this 1210 one and this used to be super long and it said like it has to be exactly like this and I just changed it so it could be anything and then I ran it and then it this time. So yeah, it's pretty weird, pretty stupid, but that's what you have to do to uh skip this one and then we can continue and move on. So yeah, just make sure you type uh this in. I should probably get that and get this up here so you know what what the type said: I -e s free that free code Camp true free code Camp false /g project vs code settings. I found it inside of one of the forums. This guy was actually working on a bug in the Mario database, but um then this person replied and said you should type this in and then you can edit the tests. So that's where I found it and it it worked. So let's uh now we can keep going and not have to worry about that. I'm good. So let's continue. All right. In the if condition you just added, send users to the main menu with the message that bike is not available. In the if condition you just added, we need uh send users to the main menu with the message this. All right, so we can copy that and then go main menu and then have this message: that bike's not available. Um, if the input that should work on uh your script should call the main menu function with the correct arguments. Um, it's just not actually gonna work. Seriously? It works. Which one would you like to rent? One true. Okay, one 55 or 66. Netflix not available. How may I help you? Um, that's the input. It's unavailable. Main menu. Yeah. Oh my gosh, are you serious? This isn't working either. What the heck? Okay, I guess I can change this one as well, so it says what 12 20 then or 12 15. In menu that bike is not available. Scratch this and we will cheat into something else if I can actually use a mouse correctly and we'll just change it to anything because let's make a piece of crap. All right, now it should run correctly. Yay. This thing is being so stupid. All right. Remove the line where you print the bike availability variable. You don't need it anymore. Remove the line where you print this. All right, we don't need that anymore. All right, there we go. That one passed on its own. Let's go from the script and go to the rent menu from the script. Okay, it works. In the psql prompt, set all the bikes availability back to true. All right, let's set all these back to True. Update bikes set available to true and I have to connect. Darn it. So quits or actually it did connect. It said attempting reset succeeded already. So now it works. Okay. In your script and else for when a bike is available, add these four comments in the else area. All right, so let's copy that and let's create an else area. Else and paste this and then we just need to comment this out a bunch hashtag and mount it right here. English tag right to your hashtag again. There we go, and I also get rid of that and they might be pretty particular on this and also the correct comments. Run it. I thought this would happen again. Um, shoot. Okay, so this one is what 12 30. Uh, 22 no 12 33. This one like it's not available. Get customers. Yeah, this one. All right, let's just change this to anything again because this thing is being stupid. He reads anything. Go back to here and run it and now it works. You can't stop me. I'm the gingerbread man. As the customer say uh you need to get the customer info and find out if they're an existing customer. Below the get customer info comment, print: what's your phone number with a new line in front of it. A little bit underneath git customer info we need to Echo this with a new line, so we need -e in there, backslash n and paste. There we go. It should work. Sweet. Below the line you just printed, read input into a phone number variable. Since the phone number is unique, you can use it to identify a customer. Create input into a phone number variable below the line you just printed. It's all right. We need a phone number and that should be read or hit read. Yeah, there we go. With the customer's phone number you can get their name. Below where you got get the phone number, create a customer name variable that gets the customer name from the database using the phone number. Okay, so using the phone number we can grab the customer name. Okay, customer plus Domer name equals psql. So we have to do a query here. Copy that paste here and then the query we have to do is Select name from uh could yeah select name from customer or something. What do we have in our customer thing? Select all from customers. We have customer ID phone name. Okay, yeah, so we need to select name from customers Cuts filters where the phone equals dollar sign phone number phone number. That should be fine, right? Our new from the database guidance query here. Yeah, yeah. Phone equals phone number. Oh, it has to be in quotes, single quotes. That's just why there we go. Sweet. All right. If the customer is in the database the phone number use the variable will be set to the name. If not it will be empty and if conditioned below the if customer doesn't exist comment that checks if the variable is empty, place the next two comments in the then area. All right, so we have to do this thing again. If here and put it here placed and then put the next two comments in the this area here, so shift and then we can grab them both at once and we can also shift and tab. There we go. That should work. Please please no. Oh, never mind. Um, this has to be customer name. That's still marker name. Now I don't know if I'm tweaking or they're tweaking. So this is uh that's not very good. Let's see here. That's my name inside of there. Let's just copy this. See if this works. If it doesn't then I'm gonna have to change it again, which is quite a shame actually. It's quite a shame. All right, and hopefully that works. Run. Um, if I run this in here. Yeah. Okay. I thought this would happen and to the rescue we can take a look at that test and change it. It's your phone number. Let's find a phone number. Customer name. Customer doesn't exist. Uh, should
Have the correct if condition added. Yeah, that's this one, isn't it? Yeah. [Music] This is so annoying. Let's change this to anything, and now it should pass. Yippee!
If the customer isn't in the database, you need to get their name so you can add them.
Below the get new customer name comment, print "What's your name" with a new line in front of the message. Print "What's your name". All right.
Below the get new customer name, all right, so we have to echo and then paste `Through -e \n` and then paste that "What's your name". Seriously? [Music] Um, seriously? Shoot. Get new customer name. Why isn't this working? Okay, I guess I have to change this test as well. No problemo. I could do that. Let's grab this anything, go back, and now run it. Sweet!
All right, below the question you just printed, read input into a variable named customer_name. All right, below this read because name, please work. Why aren't you working? Read input and variable. Can I reset? Hopefully, it resets the right spot, and then I can just hopefully it'll work. Like maybe I messed up; I'm not really sure. Greed because Domer name. There we go. Cool.
You have the two pieces of information you need.
Below the insert any customer comments, create an insert_customer_result variable that inserts the customer into the database. All right. Copy. We need to do this, which will insert, and that is going to be an insert statement. PSQL. And do I have to do anything? Yeah, I probably do. Okay, insert into insert into customers customers name and phone number. Right, so I need name and phone, and values are going to be the RSI name or customer_name, nostalmer name, and the dollar sign customer_phone because Domer phone, like so. I think that works. I just need to end this off. Let's try that. Great. Run. I don't know. I don't know. Insert into customers name phone values values. Oh, man. A few of the customers table single quotes around the bar charts. Yeah, customer_name phone number phone number. Let me see. That should be phone number phone number. Yeah, there we go. Run the script and go to the rent menu. Run the scripts, go to the rent menu, and then enter 555 when asked for a phone number. All right, 555-555-5555. And me when it asks for your name, me. All right. Cool. That should have added a new customer to the database.
In the psql prompt, to view all the data in the customers table and select all from customers customers, gonna have to reconnect. There we go, and it is in there. Sweet. View all the data in the rentals table. Select all from rentals. A thing. All right, so you still need to add the rental to the rentals table when the bike is picked out. View all the data in the bikes table. Select all from bikes. That's easy. Cool. And set the available column to false for the bike rented.
Below the end of the if statement that inserts a new customer, add five more comments. Okay. Okay. Let's copy this, and below the end of the if statement that inserts a new customer, below the end of the if statement that inserts a new customer, below this end of the if statement. Okay, so right here, paste, and then we have to get customer ID. Insert bike rental set up like availability false. Get bike info and send to main menu. Control slash. Here we go. Oh, let's go. That actually worked. Thank you. You're getting close to done with the rent functionality.
To add a rental to the database, you need the customer ID. Hello, the git customer ID comment to create a customer ID variable that gets the customer ID using the phone number. Hello. Okay, customer ID comment. Create a customer ID variable that gets the customer ID using the phone number. All right, so paste that. Will get the customer ID using the phone number. I think we can use this here; it's basically going to be the same as this, and the customer ID is going to be here. Customer ID. It should work. Yeah. All right.
Now that you have the bike ID and customer ID, you can add the rental to the database.
Below the insert bike rental comment, create an insert_rental_result variable to add to the rental uh table. All right. Paste that equals and then we have to do an insert, so I'm going to copy this insert statement here if I can. Come on. Copy paste insert into rentals, and the rentals table has what in it? Select all from rentals. Shoot. S um dsql --username=freecodecamp --dbname=links. Here we go. Select all from rentals. Okay, we have a rental ID, customer ID that we need. Rental ID customer ID and then we have the bike ID, date rented, and date returned. Um, bike ID, and the date rented is going to be defaulted to now, and that should be fine. Okay, so the first value is the rental ID, which is going to be the parental ID. Customer ID. Oh, until ID is uh defaulting, so all I need is customer ID and the bike ID. Right? You need bucket. Yeah. Okay, customer ID is going to be this thing dollar sign customer_ID customer_ID, and the rental ID is going to be something up here. Bike availability like ID to rent. Yeah, fake ID to rent should work. Yeah. Let's go. All right. That should add the rental to the database.
The last thing to do is set available to false for the bike.
Below the set bike availability a false comment, create a set_to_false_result variable that does that. Let me grab that. Paste here. I'm going to do an Upstate update statement which should do that, so we're going to do updates update bikes set availability fail of Bill the T equal to false where the bike ID is equal to dollar sign like ID to rent. ID to root. That should work. Great. Come on, baby. Set like availability is false equals. I need equals in here. Ah, darn it. I thought that would work. Create a set_to_false_result variable that does that. Selected false result. Update bytes set availability equal to false where bike ID equals availability. I think that's right. I need to rent. Ah, shoot. Help me. Date bikes set available to false. Okay, I guess I need some spaces around here, and that also has to be lowercase false. Um, I don't know what you are looking for because it seems right to me. Run. Will it run in this? Yes, it does, so I don't really understand why it's not working. [Music] Um, I might have to change this one as well. Iron. That's a shame. That is a shame. Oh, actually, maybe I just need spaces around here. Darn it. Oh, okay. Let me just copy this, see if that works. Let me repeat this here. Paste here. Okay, that did work. I wonder what it changed. I don't even know. Run the script and go to the rent menu. Pick the first bike on the list. Enter 5555. All right. All right. The bike, the first bike on the list. All right, and enter 555-555-5555. Okay. Cool.
In the psql prompt, feel all the day in the rentals table. There should be a new rental. Select all from rentals. There we go. 1 1. The rental was added, and the date rented was filled in automatically. Next, view all the data in the bikes table. Order the results by Becca D. Select all from bikes order by bike ID. The available column was set to false for the bike you rented.
The last thing to do is give a nice message about the rental.
Below the get make info comments, create a like_info variable that gets the size and type in that order of the bike rented. It gets the size and type. All right, sir, and or get like info. All right, let's paste that. It equals one of these little statements here, and it's going to be a select where it equals that by torrents select uh something from bikes and select size and type size type from bikes where biker descent. Should work. Yep.
Below the variable you just created, echo to print it. All right. Echo dollar sign bike_info. Awesome. Unscripted again and go into the rent menu. There should be one less bike displayed. The units and do this one on this bike displayed. I guess so. Yeah, there's now one. Um, pick the next bike on list two uh using 555 again, and then there we go. I picked up this 28-inch mountain bike. Uh, the message you want to print after someone rents a bike would have said, "I have put you down for this". Okay, you need to format that variable for the message. The sub command can be used to replace characters and patterns in text. It looks like this in the terminal. Enter echo to practice. Okay. Go. Create pipe Mountain apostrophe hype said s///= /g. Okay. 28 equals Mountain. That command you use pipe to string 28 Mountain to the sub command where it replaced all the spaces with equals. Enter the same command but replace all the spaces with nothing. With nothing. Okay. The G rejects flag stands for Global. It will replace all instances of the pattern. In this case, it replaced all the spaces. Enter the same command but without the flag. Of the flag. It will just do the first one. Yeah. At that time, only the first instance of the pattern was placed. The first space was removed. Enter the same command but replace the first instance of that with a space. First instance of pipe with a space. So this one, I don't know what you mean. Uh, the previous command was that you want to replace space with that. Enter that. Okay. 28 Mountain said s. I just need to kind of pipe in in here. Okay. Okay. There we go. Into the same command but make the output look like how you want in the message. 28-inch mountain. All right, so we just need to make sure we put this in there. No, no space though. There we go. Back in your script where you echo the bank info, type the output into a set command that replaces that uh with that, so the text will read size type. So that would become 28 Mountain, for instance. All right. Back in your script echo bike_info, and we have to do this thing here, which is uh type it into said of s/ / /g something like that. Yeah. There we go. Cool. Run the script and rent another bike using the customer with phone number 555 again. Make sure the bike info printed what you want it to look like. Let's rent a bike. Which one would you like to rent? Number three. 555-555-5555. And there we go. 29-inch mountain. Cool. Now it is formatted for the message. Take the echo command and report that formats it. Put in a subshell and set the output into a variable named bike_info_formatted. Here's an example. bike_info_formatted equals this format and info here. Let's copy that, and let's just go underneath here. bike_info_formatted. Um, take that echo command and the part that formats it, put it in a subshell, so I just have to throw this in here and here. Paste. Yeah. There we go. Cool. What you put in the subshell will be executed, and the result of it will replace the subshell. In this case, the formatted bike info was printed when you ran the script before, so the bike_info_formatted variable will be set to that.
Below the sentiment menu comment to send users to the main menu with a message that would print, "I have put you down for a 28-inch mountain bike, me" if me rented the 28-inch mountain bike. All right, so let's copy this and send the users back to the main menu. Okay, so send to main menu. Main menu, and then I have to print this. 28-inch mountain bike is this dollar sign bike_info_formatted bike_info_formatted and then me. I think I could leave right for now. If me was rented, it's going to be a some other oh gosh, help me. Oh, customer_name should be customer_name dollar sign customer_name. Now it will work. Come on, maybe. Um, I've put you down for this bike, customer_name. Okay, just echo it. Do they just want me to echo that? Send users to the main menu with a message. Below the send the main menu here. Oh, why isn't this working? bike_info_formatted. Does it run correctly? Yes, it does. I could rent 455-555-5555. If I put you down, how may I help you? I have an extra space in here. That's weird. How does it work on you? I have like two extra spaces. Uh, shoot. That's my name. Foreign. That kind of sucks. How do I get rid of those extra spaces? And I seriously have to do like a sad thing. Um, okay. Let me see if I can just copy this and see if that works. Hopefully it does. Paste. Oh, this formatted bike. Ah, that's what I was forgetting. I see. Okay. Now it'll work. Yeah. Run the script and rent the next bike on the list. Use the customer with 555 as their phone number. When you're done, exit the program. Rent, and then it's back on the list one. I guess it's five now because I did one before. What's your phone number? Five. Okay. Uh, when you're done, exit program three. Cool. There's an extra space around the customer's name. You can use set again to fix that. In the terminal, enter echo me to print me with spaces around it to see if you can find out how. Let go. M e. Me. The printed but you can only assume there's a space at the end. Uh, you can only assume. Yeah. Place the last command in a subshell with quotes around it. Put period right after a subshell and echo the whole thing in terminal. Here's how it looks. Okay. Echo dollar sign. Echo me, and then subshell period. There we go. Now you can be certain there's a space at the end. Within the subshell of the last command, use a pipe and the side command to replace the first space with no space. Here's a set replacement pattern you want. s///. All right. Within the subshell of the last command. Within the subshell, the last command use a pipe and the said command. Use a pipe and the said command to replace the first thing. So s///. There we go. Like so. Here we go. I removed only this first space I found. Change the previous command to replace all instance of a space instead of just the first one. We do that with a g at the end of there. They're replaced all spaces. You only had an extra space at the beginning of the customer name. Add a carrot in front of the space and the replacement pattern of the last command to only replace a space in the beginning of the text. So we just need a carrot in front of the space in the replacement pattern on carrots. Carrots. There we go. The carrot you added means that's the start of the text, so we'll replace the space only if it's at the beginning. Into the last command but add two more spaces at the beginning of the text. Okay. Um, like so. Um, I don't know. Right. So there we go. The carrot space pattern only replaces the first space. Adds star to the end of the matching pattern to place all spaces at the beginning of the text. Okay, so I think I need to start here. It worked. The customer's name only had an extra space at the beginning. I'm sure as to why, but there are maybe others with extra spaces at the end as well. You can match the end of text with dollar sign. Change the matching pattern of the last command so it replaces a single space at the end. All right. With dollar sign like uh like this. Um, I think right. The pattern is I'll still get rid of this first thing. Okay. Add two more spaces to the end of the text in the previous command. Three spaces in total. Um, one two. All right. The only pattern only replaces a single space at the end. Change last command so it places all spaces at the end of the text. Can't we do that with a star dollar sign star? No. Dollar sign plus maybe. Dollar sign plus. Darn it. Help me. Oh signs. Oh, star dollar sign. Okay. Well, can't get them all. Right. Star dollar sign. There we go. Did I replace all the spaces at the end of the text? You can use pipe as an or operator in a matching pattern to place one pattern or another. Use it to change matching pattern so it would place any amount of space at the beginning in any amount of spaces at the end of text. All right, so we need an or, so we need a power with this and an or. That's something like this. Okay. Okay. That worked. Uh, that didn't work actually. It doesn't like that or operator for some reason. Check the manual for the set command to see if you can find anything. And said quit that. Let's go. Somewhere in there is a flag for using extended regular expressions with said that might work. Oh yeah, that's with a like a -e at the end of it. Um, so we can do a set command is in here and -e. I think that works. Right? No. Um, add it to this command. That didn't work. To find out. I thought it was -D, but maybe it comes before -e here. Uh, there we go. Yep. That trimmed all spaces from the front and end of the text. Back in the last message of your script to place the customer name variable in a subshell echo and pipe it into a set command that removes all spaces from the front and back. Use the same method you used in the terminal. Okay. Back in the last message every script place the customer name variable in a subshell. Okay, so the subshell is a quote dollar sign the code dollar sign parenthesis is that parenthesis here with a um quote I guess, and then we can do echo this customer_name and then pipe it into the said command. set of -e s/^* *$//g and hopefully that works. It doesn't seem like it's working because it's not. It does not have the correct formatting, so maybe you have to get rid of these quotes, and maybe it won't work now. No. There's still it's still not correct formatting now. Get rid of the echo. Maybe I'm not really sure. No. Same thing. Um, maybe single quotes around this. Maybe that'll be fine. No. I have no idea, honestly, so I'm just gonna get a hint. I'm gonna go. No, but like how do you change the customer_name variable to this. No quotes around this at all. Okay, so I guess that's fine. Run it now. There's a this thing. It does not like that. Oh, I need a single quote here. Oh. Now. Now it works. Okay. Okay. Run. You echo. All right. I forgot this echo. Echo. There we go. Cool. 73 percent. Run the script and rent another bike with the customer whose phone number is this. When you're done, X Program. All right. Let's find the scripts of the grass one or into bike six. What's your phone number? And there we go. When you're done, exit program three. All right. Run the script again. Rent another byte. Use 000 as the phone numbers, time and test as the name with a new customer. When you're done, X Program. 1700000000. What's your name? Best. All right. When you're done, X Program three. Thank you for stopping in. Run the script again. Rent another bike with the customer you just created. When you're done, X Program. I rent a bike. We want eight. Uh, what's your phone number? 0000000000. There we go. Exit three. And the psql prompt view all the data in your bikes table in order by the bike ID. All right. Select all from bikes order by like ID. I don't really know why we have to order it by bank ID because since they're already ordered there, but I guess we do. There should be two bikes left. Actually, just one because I messed up one time. Uh, next, look at all the data in the customers table. Select all from customers customers. There we go. Come on, baby. We should be all day in the rentals table and select all oh rentals not customers. Select all from rentals. There we go. The rent functionality looks like it all works. Delete the echo return menu line and they return menu functions so you can get started with that. Delete echo return menu in the return menu function. Ctrl+Shift+K. Add three single-line comments in that order. Hopefully this works. It should work. It's just a it's just a function, so it should work. All right, let's shift and then control over slash bend it nice blow the get customer info comment you just added. Print "What's your phone number" with a new line in front. Get customer info. All right, so we have to do echo -e back
To slash n paste that in, so cool. Just below that, use `read` to get input into a phone number variable. Okay, `read phone number`. All right.
Just below that, so the customer ID variable to query that gets comes from customer ID from the database using the phone number they gave you. Uh, haven't we done this before? That's already right here, right? Let's just kind of the same, isn't it? Yeah, it is. If they are in the database, the variables will be their customer ID; if not, it will be empty.
Below the "if not found" and `if` string that checks if it's put some domain menu commented, uh, this is not going to work either during this uh that is annoying actually. It's looking for customer engine. Oh yeah, where's the customizing one? It's all right here, whatever. I'll just grab this "if not found". Uh, no, this is going to be `customer ID`, `customer ID`, and I can get rid of this. Okay, and then throw this `send main menu` up here. Okay, that worked nice. If the customer isn't found, to send them to the main menu with the message "I could not find a record for that phone number". All right, I'll send them back to the main menu if they're not found. All right, so `main menu`, and then paste this in there. Sweet.
I run the script and go to the main menu, uh, and go to the return menu. Enter a phone number that is not in the database when you're done, exit the program. Okay, so enter the return bike menu. What's your phone number that is not in the database? All right, and then enter the program. Okay, and `else` to the `if` condition for the if the phone number is found in the database, place um in the `else` area as single line comments. Okay, let's grab this and put it into an `else` statement here. `else`, `faceless`, and let's make these into comments `/`. There we go.
You'll want to find out what rentals a customer has using their phone number and display them. You will need to join a table. Start by using the `psql` prompt to view all the data in the `bikes` table, uh, to do. Okay, so just go in here and view them: `select all from PS from bikes`.
Next, use `left join` with `bikes` as `left` able to join the `bikes` and `rentals` tables. Use the `using` keyword to join the tables. All right, to `select all from bikes left join on the rentals using the year bike ID`. Cool, let's go. You only need the bikes that are being rented. Use an `inner join` with the same two tables to get only get those. All right, let's do `inner join` here. Awesome. How to join to the previous command that joins the last table so you can get the customer information. Use an `inner join` and `using` keyword again. Okay, so `inner join` here, and we have to `inner join` to the last table, which is the customer information, so `customers customers using the customer ID customer ID`. All right, sweet.
Add two conditions to the last query to narrowed on the results to the bikes that are currently being rented by customer with 555 is their phone number. The second condition should check the `date returned` column. All right. `Square the phone equals 55555-555555 and uh date returned column is not null and date returned is not null or is null something like that`, um, and `date returned not data returned`. Yeah, there we go. 82 percent. Uh, now you have all the rents for one specific customer. Only get the columns you need to display the like information to them. They are the same three columns you use to display the list of available bikes. Only with the columns you need to display the bug information to them, um, okay, I need help. Three columns you want. Oh, I see. Don't press Alt, press Ctrl, and I can go back, do this, and then `select like ID type and size`. There we go, that's awesome. Uh, yeah, one more thing. Order the results of the last query by their `bike ID` column. Okay, that's easy enough. `order by bike ID`. That's the query you will need to use to get the customer to get the bikes of customers renting in the script below the `get customer as a result` comment. Create a `customer rentals` variable that gets the rentals for the customer. Use the phone number variable to get them. Create a `customer rentals` variable under the `get customer rentals` mates and this equals. Use the phone number variable to get them. Use the phone number variable to get them. Oh, I see. Or `phone equals` this. I see. So let's copy this query. Let's copy that. Yeah, let's paste it here. We have to put this `psql` thing around it, so let's do that: `dollar sign dollar sign psql quote`, and then at the end we need a quote and a parenthesis. Yeah, and then the phone number is going to be that `dollar sign phone number`, a little number, and that has to be `dollar sign`, and then that should be good, right? Yeah, I think that's good. Okay, come on, run. Run. You can use the phone number variable to get them. Yeah, `customer rentals phone number`, uh, that should work unless I have a syntax error somewhere, which I guess is possible, but I thought this would work. Our phone number is that, and `day return` isn't isn't all `order by bike ID`. Oh, I don't have that. I see. `order by bike ID`. There we go. That should work. Cool, cool.
All right, below the variable you just created, use `Echo` to print it. Make sure to put double quotes around it. All right, use `Echo` to print this. Let go, uh, `dollar sign customer rentals customer rentals`. Here we go. Run the script and go to the return menu. Menu. Enter 555 for the phone number to see the rentals from me experience. I return a bike 555555. Here they all are. The query is working. If the customer has no rentals, the variable will be empty. Below the "if no rentals" comment and `if` condition that checks if it's empty, put the `send to main menu` comment in the an area again. All right, `if` conditioned to see if it's empty: `if`, and then we have this opening `dash Z dollar sign customer rentals customer rentals`, then, and then `Phi`, and then we can throw this in here like that, I guess. Hey, that works. Sweet. If the customer has no rentals, send them to the main menu with the message "You do not have any bikes rented". Add the code below the next comments. If the customer has no rentals, send them to the main menu with the message this. Yeah, okay, it's easy enough. We can do `your main menu`, and then paste this here. And `else` to the condition for when the customer does have rentals, place four single line comments in it. Uh-huh. Copy that, and `else` to the condition. `else`, place this in here. We have to display the rented bikes, `if not number and send to main menu`, and then make sure these are commented out. Sweet. Below the `display rented bikes` comment, print "Here are your rentals" with a new line in front of it. "Here are your rentals". `display rented bikes`. Let go. `Dash e backslash n`. Paste. There we are. Move the `echo customer rentals` line to below the line you just printed, so we can move this down here to below this. Run the script and go to the return menu. Return. What's your phone number? 555-5555. Okay, cool. Uh, where you print the list of rented bikes, pipe the command into a `while` loop that reads the data. You should read the data into `bike ID bar type bar` and `size` variables. Make it print each rented bike in the same fashion as the list of available bikes. It's a list of available bikes, and that's up here somewhere, so let's bring this down quick. Uh, where is this thing that we did here? Didn't we do, uh, yeah, we did a `while` loop here. Let's copy this `while` loop and let's display the written bikes and `Echo customer rentals customer rentals while`. And then we have to do. Oh, that works. Cool. With any type R. Okay. Run the script and go to the return menu and do the same phone number again to make sure the list is showing up correctly. I think I can get rid of this as well. Um, Ctrl J, bring that up and let's do this again. Enter the same phone number again to make sure it's showing up correctly. Whoops, I did that wrong. Uh, Ctrl C. Bonus. Move on. Whatever.
Below the "ask for bike to return" comments, print "Which one would you like to return" with a new line in front of it. Below the "ask for bike or to return". All right. `Echo Dash e backslash n`. Paste. Below the line, use printed `read` input into a `bike I need to return` variable. Come on. Copy that. `read` input into it, so `read Ace like I need to return`. Below the "if not a number" comments, check if the input for the `bike ID` to return is a number using the same method you did earlier. Place "send to main menu" comment in the `in` statement. All right. Check if the input to is a number using the same method you did earlier. Now we have to go check if it's a number up here somewhere. Where do we do that? Right here. If it's not a valid number, if it's not a number, you have to do this. Paste `like ID to return to return`, and hopefully that works. I don't know how we have to get rid of this, um, earn it. I thought that would work. Then `else`, I have to get rid of `else` why that's in there. No, or what was here before? Nothing. And then I add an `else`, and I see I have to add an `fi` and move that into there. If I maybe get rid of that. Darn. You are new `like ID to return`. Why is this say `rent`? I need that say `return`. There we go. Cool. If they don't input a number, send them to the main menu with "That is not a valid byte number" as a message. Send them back to the main menu. That is not a valid like number. Okay, I need a period. Okay, cool. 91. And `else` for when they do input a number, please check if input is. Oh my gosh, this is so long. And `else`. `else`. Paste. Check if input is rented and send to main menu. Here we go. You need to check if the input is a `bike ID` rented by the customer so you can return it. In the `psql` code prompt, join the `rentals` and `customers` table with an `inner join` using the `using` keyword. Okay, so `psql`. We have to do a `select`, um, all from `rentals`, uh, `interjoin` and `customers using the using the rental ID customer ID customer ID`. There we go. Right? Yeah, there we go. All right. Add three conditions to the previous query. Check the phone, `bike ID`, and `date return` columns to narrow the results to the first bike you entered. We rented with me, right? So check the phone. Okay, so check the phone, check the phone, `bike ID`, and `date returned` to narrow down the results. I thought that would do it. Add three conditions to the previous query. Oh, they want me to add conditions to it. Okay, just give me the answer, please. Give me this, or shoot, I can't. Ah, darn you. And it should be `select all`. Darn you, you tricked me. `select all`. We need `where the phone equals 5555555`. We need `and like ID equals 1 and the date returned is null`. There we go. All right. You only need to know what bike is going to be returned. Narrow the columns from the last query to only get the one column you would need for returning a bike, which would be the `bike ID`. All right, so `like a d`, right? Or what would you need? What column would you need for this? `customer ID`. I don't. `rental ID`. That's why we need `rental ID`. There we go. Back in the script below the "check if input is rented" comment, create a `rental ID` variable that gets a `rental ID` of the bike that was input ID. Uh, check if if input. Okay, here `rental ID rental ID equals` that gets the `rental ID` of the bike that was input. Check if input is rented. We have to do this `psql` prompt thing with this, so let's do that. Actually do the `psql` thing first, um, which is five `dollar sign`. Let's see. `dollar sign psql quotes`, and then paste, and then our phone is the phone and stuff like that. I have to add that, um, I probably do. Yeah. All right, so the phone number is going to be the customer phone `dollar sign customer phone`. I think `customer phone` or `phone number`. `read phone number`. It's gonna be `phone number`, `phone number`, and `bike ID` is going to be the `bike ID dollar sign bike ID`. I don't know actually what is it? `like ID to return` if I get E2 return and the `date return` is null. Okay, that's fine. Sweet. We're moving along fully. If not, if input or into comment and `if` that checks if the `rental ID` variable is empty. If we're going to ID is empty: `if Dash Z rental ID`, then `if I send to main menu`, I'm guessing in that area. Yeah. Run. Why is this not working? `Dollar sign`. There we go. If the input isn't rented by the given customer, send them to the main menu with "You do not have that by granted" as a message. Copy that. Let's do `menu` again and paste that in. And `else` to `if` condition you just added, use `Echo` to print `rental ID found` in it. All right. And `else`. `else`. `Echo code`. Paste this. Let's go. Run the script and go to the return menu. Enter 555 for the rented bikes. You'll put a bike that isn't on the list and then do one that is on the list. All right. Return a bike. Uh, what's your phone number? Enter 555-5555. Which one would you like to return? 54. Uh, how may I help you to return a bike? And then now we want to do number one. All right, cool. Looks like it works. Delete the line where you print the `rental ID`. Leave the line where you pretty lady `parental ID rental ID`. Yeah, right here. Let me know Marco `rental ID`. `Echo` where I print the `rental ID`. This one. I don't know where that is. Where is it? Help me. Move to line where you print the red lady. Oh, delete that one. Okay, delete this one. Cool. Add three single line comments in the `else` area. This stuff inside this here. We need `update date return` and `send to main menu`. Set `bike availability` to true. All sorts of cool stuff. After person picks a bike to return and you know that it's bike they have returned, you need to or like if they have rented, you need to update all the info in the database to return it. Below the `update date return` to comment, create a `return bike result` variable that sets the `date return` to column to now for the bike rented. Use the `rental ID` to figure out which road to update. All right. Copy that's `update date returned`. Paste this `equals psql` thing. Copy. We have to `update` and let's get rid of this quick. `updates our bikes`. We know our `date returned rentals`. `update rentals set date returned equal to now where the bike ID equals dollar sign rental ID` or not `rental ID`, `bike ID to rent to`, uh, return, I mean `like I need to return` something like that. And then yeah, should we go to great? Use the `rental ID` about `rental ID` actually or `rental ID` is that? Okay, great. `rental ID equals dollar sign rental ID`. Let's try that. There we go. That should update the `rentals` table. Lastly, you need to make the bike available again. Below the "set bike available to true" comment, create a `set to True results` variable that makes the bike available again. All right. `equals`. We need to set the bike to available again. `psql update update bikes set available [Music] equal to true uh where the bike ID equals the color sign uh like to return like ID to return` something like that. Yeah, sweet. So close. After that, you're done. Send them to the main menu with "Thank you for returning your bike" as the message. Copy that. Send them to `main menu`. `main menu`. Paste that in. Run the script and return one of the bikes that me has rented out. When you're done, X Program. This might be the last one. I'm not sure. All right. We need to return a bike. 555. We need to return number three. Uh, thank you for returning your bike. How may I help you? When you're done, X Program. Three. Thank you for stopping in. `psql` prompt. View all the data in the `rentals` table. Uh, `select all from rentals`. There we have it. Now the rental has been returned. Feel all the data in the `bikes` table and order by their `bike ID`. `select all from bikes where the bike ID are ordered by`. Whoops. `order by bike ID`. There we go. All right, and the bike is available again. This is the last step and the script once more. Feel free to play around, rent and return some bikes. When you're ready to be done, return all the bikes you rented and exit the program. All right, the moment of truth. What did we build? We can rent a bike and then return a bike, so I'm gonna rent, uh, number three, and I'm going to enter my phone number 000000, and I printed that. Now I'm going to return it to. What's my phone number? 000000. That's the bike I just rented out, which is number three. Which one do I want to return? I want to return number seven maybe. And now I want to return another bike, um, which is going to be number three this time and number seven. It's not showing up anymore, so that's good, and now I can exit, and I returned two bikes and rented one out for a little bit, and sweet. All right, you should run your script by executing it. I did good. Oh my gosh. Fix it accent. Oh, return all the bikes you rented. Oh, shoot. Uh, darn. Returned 0000008. We need to return another one. 555555555. I mean, it's going to take forever to return. What's your phone number? 555555555. Number two. 555555555. Number four. Burn. Number five. We'll return a bike. We need number six now. There we go. Three. Exit. There we go. Tutorial complete. Let's go. So now when I go to freeCodeCamp, this thing will have a nice little checkered next to it. I had to get a little bit hacky there, but I eventually made it through this 210 lesson course, and uh, yeah, not the funnest thing in the world, but we got her done. The next thing we're going to do is the salon appointment scheduler, so stay tuned for that, and I'll see you later. Make sure to give the video a like, subscribe, and leave a comment, and I'll see you next time. Bye.