So this is part two of generics for Java.
And just before we start getting on to the code I just want to go through a couple of
brief points to the reason we're actually using generics.
So it's commonly understood in programming terms that the earlier a bug is spotted in
the code, the easier and ultimately cheaper it is to fix and that's because
there's less resources and less man power invested to actually fix that error.
So if a bug appears in production code,
production here means the code that has been released to the Google store or
what's commercially available or it's not test code anymore, it has to be fixed.
There's no doubts about it that for most bugs, so some minor bugs that perhaps you
wouldn't need to fix, but ultimately if it's in production mode there must be
fixed and so what is fixing it the code itself must be then fully tested
to insure that the actual fixing made didn't in fact introduce another bug.
And then of course finally the updated code containing the fix or
fixes has to actually be redeployed and uploaded to the Google store or
it what it whichever Android app store you're actually using.
And so any systems, so
that's any other apps that rely on the program, they too have to be tested
to ensure that the fix hasn't broken interoperability between the two systems.
Now if the bug itself is spotted in testing,
the effort is actually reduced slightly, but there's still a requirement for
any tests that previously passed to be performed again.
And of course, if the bug is part of that compile time,
no one else actually notices, except you the programmer.
And the fix has had very low impact on everyone else.
So that's the reason why you want to get this errors fixed early in your code.
You don't want to find all these surprises, bugs, later in the process.
Because particularly with larger applications and when you're involving
other resources, other people, it starts costing a lot of money.
If nothing else in programmers' salaries and time to actually fix those errors.
So basically what we're getting at, what I'm getting at here is that
it follows that anything we can do to pick up these bugs and at compile time,
when we're actually writing our code for
the first time, is well worth the effort to get right.
So in the previous video, I showed you how to generic types can be used and
how they're actually useful to ensure that we don't produce a program that compiles
fine but actually gives us errors at runtime.
Now I use the example there of actually adding a string
to what was an array list that we assumed only contained integers.
So let's actually have a look at taking that to the next step and
that's actually creating our own generic classes.
Now what I'm gonna use for this example is the concept of adding
players to various types of sporting teams.
So to begin we need to actually create some basic classes.
So let's start off creating a basic abstract class,
we're gonna come down here as we've always done, and create the new class.
I'm gonna go new class.
It's gonna be an abstract class called Player.
So I'm gonna make that an abstract class.
Abstract class Player.
And we're going to have a string which will be the player name.
Name.
Gonna add a constructor, which will be name so we can actually create the object
and just a getter as well for the extra player name as well.
A very basic class for our player.
So, what we're going to do now is we're going to create three types, a baseball
player for baseball, a football player, as in Australian rules football and
the soccer player as in soccer which is but in Australia we call football.
That make sense football for UK people.
So that sounds a bit confusing but, that's how we call it over here in Australia.
So I'm gonna quit.
Next classes.
First one is gonna be baseball.
Baseball player I'm gonna call it.
And the baseball player is going to extends from player.
So we need to create a constructor to pass that through so
the actual name is stored cuz that field is actually in the player class.
So there's our baseball player.
Let's create the football Player.
So football player
that's also got an extense from player and
we also need to create the constructor for that so it's happy with that as well.
And lastly let's create the soccer player class.
So soccer player and we need extends
Player, and we also need to create the constructor like so.
So that's our three classes.
There's nothing generic about those at this stage.
But what we're going to do now is create a team class, and
in that team class, add the ability to actually add players.
So alright let's go ahead and do that.
Create new Java class.
I'm going to call it team.
And we're not extending from anything else because this is a basic class.
And we're going to have a name and
this is the name of the team as opposed to the name of the player so private string.
Name, and we're gonna keep this simple.
We're going to just track the number of games played, number won, number tied, and
number lost.
So I'm gonna put int played equal zero, int won equal zero,
int lost equal zero, and int tied equal zero.
Now obviously in a real application,
you'd have to deal with fixtures and those sorts of things.
We're gonna keep it pretty basic cuz there with the basic number of fields.
So we're gonna create an array list that's using generic for our plow objects we're
gonna type private array list and it's going be of type player.
And the player's our abstract class that all the other three classes extend from.
And we're gonna sit call that members and this is member's of the team equals
new array list and I'm going to remove that on a fault.
We're probably getting an error about the level so
I need to make sure I've set the correct level up.
So you may not get that error but if you do, go to project structure and make sure
that the project language level is set to 1.8, so I'm going to click on okay.
And you can see that arrow that disappeared when I did that.
So we need a constructor now.
Constructor is going to just have the name team name passed toward it and saved.
And we also want a getter for the team name as well.
So we'll just use just that one the getter for the name.
Now we're coming to the part where we're actually gonna add the player.
So we're gonna have an option to allow us to do that.
So I'm gonna type public Boolean.
Boolean will be whether we're successful, we successfully added them or not.
AddPlayer, Player.
So Player is the abstract class definition, remember,
that we can pass to this.
And we can put if (members.contains Player,
then we can print message player dot get name,
plus he's already on the team so we're not going to allow duplicate, or duplicates.
And we're going to return false in that case, otherwise we're going to do add it.
So members dot add, player.
And then we're going to put the player name again,
player.getName() + " picked for team".
Plus this.name.
This.name is the actual team name, and
we're going to return true in that case, cuz we've successfully added it.
You can see that's now a method, add Player.
We're accepting the parameter of player, which is actually going to enable us with
one call, to actually pass a baseball player, football player, or soccer player.
And we're able to do that because we're using an abstract class and
obviously we're using a generics here for the array list definition of type player.
By continuing on we need also need a getter and
the getter's gonna return the number of players on the team.
So I'm gonna put public int num players and
that's just gonna return the size of the array of the array list.
So this.members.
Member.size.
Like so.
Okay, so
next we're going to actually create a method that actually gives us a result.
The how, what actually happened with the team, whether we won or lost.
So I'm gonna put public, void, match result.
A past team opponent.
Int our score int their score.
Instead of actually having to pass whether we won or
lost we get the method within this team class to actually determine that.
So we can put if our score is greater than their
score Won plus plus cuz we obviously won the game.
Else if our score is equal to their score.
Tied plus plus.
Else we obviously lost, cuz that's the only other potential option.
Then we increase the number of games played.
And then interestingly enough we put,
if opponent is not equal to null, opponent dot
match result, null their score ourScore.
So we're doing it this way now so we can actually also update the opponent's score.
And when we call, basically call this opponent match result again,
we're passing null this time.
So if this doesn't get executed again.
So this way we're actually saving our results for our team but
we're also saving the results in the opponent's team as well.
Saving the results in their sort of object a separate object.
So that's the match results and finally we want a ranking.
So we just come up with some sort of ranking system that's pretty simple.
So let's do ranking, public ranking, return.
Let's just do our ranking view, number of games won, modified by two, plus tied.
Now that's obviously just one way of doing it, there's thousands of different ways of
rankings, and you could actually implement this dependent on the actual class itself.
We're just keeping it simple for now.
So now that we've actually got the base code in place,
we can actually start creating various types of teams, and adding players.
So let's go to the main method and start that.
So I can start by putting FootballPlayer joe equals new FootballPlayer Joe.
And say a baseball player Pat equals new.
Baseball player Pat and soccer player
Beckham equals new soccer player Beckham.
Even though I think he's retired.
Okay and team that's try to having a team.
Adelaide Crows.
The team I follow.
It was new team Adelaide Crows.
And we can put Adelaide Crows.add Player Joe.
Adelaide Crows.add player Pat.
And Adelaide Crows.add player Beckham.
Now why Beckham would be playing for I for what I don't know.
But anyway that's an aside.
And if you put that out now just to make sure that the basic thing's working
Adelaide Crows dot numPlayers.
We should get the right result if we do that.
So I'm actually gonna do is I'm gonna stop the video here.
In the next video, we'll actually tackle this problem.
And this is the problem of actually making sure that a particular team will
only accept a player of the right type.
So in this case, if we actually specified team, then we only allow the correct
people who are part of that team, in other words a football player in
this scenario for the antelope grows to be accepted as part of that team.
So we'll do that in the next video.