Welcome back.
In this video, I want to start building our first new little application that will be a weather station
simulator.
Okay.
So let's go ahead and create a console app.
And this time let me call this one Weather Station Simulator.
So I've prepared the code on the left hand side.
And I think in your case in the right hand side.
So whenever I'm looking at this it's really just to make the whole watching experience for you better.
I'm not going to build the entire application, um, like ad hoc, so to speak.
I did prepare it, and this is just for your better learning experience, because I see sometimes people
saying, oh yeah, you're just copying code or whatever, or you're just writing code.
That's like looking code up or whatever.
Yeah, that's the whole point.
The idea is that you don't look at someone, uh, struggling to build an app from scratch and always
having the right structure and everything, uh, when teaching, because teaching is a very different
beast, right?
As to just building an app.
So, uh, we are going to check this box.
Do not use top level statements.
And let's go ahead and create this project.
So you see we have the namespace weather station simulator.
That's the app name.
And we have our internal class program as well as our static void main method.
So this will be a project where we're going to use a bunch of different things.
So we're going to use arrays quite a bit the random generator for loops.
Then we're going to use methods.
We're going to also do something really cool where we're going to check for the highest value inside
of an array, which is really useful.
This is a skill that you need to have.
And then we're even going to compare strings to each other.
So that's pretty good.
So this will be a bigger project, but let's just start building it.
So what do we need or what's the general idea of the app.
The idea is that we have a bunch of temperatures for a predefined amount of days.
So we enter the amount of days where we want to simulate the weather.
So in our case we're not going to download the weather.
That's something we can do a lot later in the course.
But so we're just going to create random weather as well as random weather conditions for a set amount
of days that we define.
So inside of our main method, let's let the user know that he's supposed to enter a number, enter
the number of days to simulate okay.
And then we enter it.
So we create a variable that will get the amount of days using int pass Console.readline.
Now based on that we're going to get the temperature.
So temperature which will be new int days.
So however many days we entered that's the amount of items that we want to have inside of our temperature
array.
So at this point we don't know how many it is.
It depends on what we enter here.
And then I want to have weather conditions as well.
So string I'm going to call this one conditions.
And here I'm going to define the type of conditions that we can have.
So it can be sunny.
It can be rainy.
It can be cloudy and it can be let's say snowy.
These are going to be the different weather conditions.
And I'm going to have another variable which will be the weather conditions which will be the actual
conditions.
Okay.
So here I'm using the new keyword string.
And here I'm using the amount of days as well.
So what's going on here.
Well the thing is we have this string that holds the different conditions that we can have.
And then we have a string that that holds the actual conditions for the different days.
Let's say on Monday it's sunny.
On Tuesday it's rainy, on Wednesday it's cloudy.
And on Thursday it's snowy.
Okay.
So all of these details will be stored inside of this string here where we just have the condition for
a given day.
So we're creating a variable that will hold as many days as we enter as a user.
Okay.
Now at that point we will need to create a random generator.
So I'm going to use the random keyword here random class which I'm going to create a variable of called
random.
And here I'm creating a new instance of that random class.
So that just means that this random object will now be available to me so that I can use its methods.
This allows me to use the methods of random.
All right.
And then I'm going to generate weather data.
So for int I equals zero I less than days I plus plus.
So for however.
Many days we entered.
I now want to go ahead and create random temperatures and random weather conditions.
So how do we do that?
So we can just say temperature at position I is going to be random next.
So let's say here from minus ten to let's say 40 degrees.
That's the temperature that we can have.
And then we're going to get the weather conditions which will be conditions and then inside of brackets.
So that's the thing.
We are now assigning a random value out of the four that we have here.
So we so we can use something between 0 and 3.
So what we can do here instead is we can use random dot next and then in brackets conditions dot length.
So what's going on here.
There's a lot going on in this line.
This is something where we're combining so many different concepts that we have learned so far all in
one.
So first of all we have a for loop okay.
And in this for loop we are iterating through however many days we entered let's say we entered five
days.
So for those five days what we do is for the first day we're storing at the temperature array.
At index zero we're storing a random value between 10 and 39 degrees okay.
And then for weather conditions at that same position.
So at position zero.
At index zero we are taking one of the conditions that we have here.
So this string here has those four conditions that we're storing.
And we're getting a random condition out of those.
So we could have the situation where our app is going to say it's 39 degrees but it's snowy okay.
So our app is very random.
But that's what it is right?
So then what we're saying is get me a random value out of however many conditions you have by doing
it this way.
By not saying something like 0 to 4, for example, we could have done it like this zero comma four.
This would also have values between zero, one, two and three.
One of those four values, it would give us a random one out of those.
But the problem by doing so is that what if we decide that we want to add more weather conditions later
on?
Or what if we decide we want to get rid of the snowy weather condition, then our program would crash
if we don't adjust it here.
And the thing is, we wouldn't see that we need to adjust it because yeah, the compiler wouldn't notice.
So instead what we do is we just use the length of our conditions array.
So however many items are inside of that array, that's the amount of random values that we can take
from.
So we can take four random values because the length of conditions is four because it has four values.
It has sunny, rainy, cloudy and snowy.
And now we're just taking one of the random ones here.
Okay.
So now let's just run this once just to see the values that we're going to get.
So I'm going to run this.
Let's say we enter five days that we want to simulate.
So then we get into this for loop here.
All right.
And we can see temperature at position I zero is going to be a random value.
And then we create this string here.
So at this point it's all zero.
Let me look at this variable here.
Go over temperature.
Click on this one.
So you see for the first iteration we got the random temperature of 31.
Now if we run this multiple times now.
So first of all we now also have a weather condition.
So the first weather condition is going to be cloudy.
So now I run it a few times until I have gotten the values.
And you see I got cloudy twice and I got snowy.
And then I got rainy.
And now let me go once again because I said I would have five weather conditions.
So I'm going to go through the process once again.
And there we are.
We see we have cloudy, cloudy, snowy, rainy snowy.
So these are the random values that we now have gotten.
So the random conditions so to speak.
And the temperature that we got is minus seven, 34, minus three, 13 and 14.
Now you could obviously say if the temperature is below zero then make the weather snowy or something
like that.
And if it's not below zero, don't allow that to happen.
But that is something that I'd be happy for you to try out yourself.
You would need to add an if statement in here and check whatever the random value actually was.
So you can either create a separate variable for that and store it in there and use that for checking.
Or you can check the array value at that.
Position, so either way is fine.
So that is the starting point of our application.
Now what I want to do next is I want to display the weather data, which means I want to display the
average temperature, the highest temperature, lowest temperature, and the most condition or most
common condition.
So that's what we're going to set up in the next video.
So see you there.