Commit e659c017 authored by TTK18SP Mustakangas Joni Johannes's avatar TTK18SP Mustakangas Joni Johannes 🤴🏻
Browse files

Week4 inheritanse

parent 631862c2

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.30523.141
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Olioita", "Olioita\Olioita.csproj", "{E5BF442C-6C3C-4A26-850A-FE1BE72AF771}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{E5BF442C-6C3C-4A26-850A-FE1BE72AF771}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E5BF442C-6C3C-4A26-850A-FE1BE72AF771}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E5BF442C-6C3C-4A26-850A-FE1BE72AF771}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E5BF442C-6C3C-4A26-850A-FE1BE72AF771}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {A78879E5-6B2E-4271-A3A4-0C1D049C2FA1}
EndGlobalSection
EndGlobal
using System;
using System.Collections.Generic;
using System.Text;
namespace Olioita
{
class Animal
{
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
public string GetName() { return name; }
public void SetName(string input) { name = input; }
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace Olioita
{
class Bird : Animal
{
public string race { get; set; }
public string GetRace()
{ return race; }
public Bird(string birdRace)
{
race = birdRace;
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace Olioita
{
class Dog : Animal
{
public string race { get; set; }
public string GetRace()
{ return race; }
public Dog(string dogRace)
{
race = dogRace;
}
}
}
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
</Project>
using System;
using System.Collections.Generic;
namespace Olioita
{
class Program
{
static bool letsLoop = true;
static List<Animal> animals = new List<Animal>();
static void Main(string[] args)
{
while (letsLoop)
{
AskUserIfWeConinueLoop();
Console.WriteLine("Create dog or bird. Write bird or dog");
if (Console.ReadLine().ToLower().Contains("Bird"))
CreateBird();
else if (Console.ReadLine().ToLower().Contains("Dog"))
CreateDog();
else
Console.WriteLine("Input not walid");
ListAllAnimals();
}
Console.WriteLine("Luuppi loppu");
}
static void AskUserIfWeConinueLoop()
{
Console.WriteLine("Do you wanna still loop Y or N");
if (Console.ReadLine().ToUpper().Contains("N"))
{
letsLoop = false;
}
}
static void CreateDog()
{
if (Console.ReadLine().ToUpper().Contains("Y"))
{
Console.WriteLine("Give bird a name");
string name = Console.ReadLine();
Console.WriteLine("Give bird a race");
string race = Console.ReadLine();
Dog tempDogName = new Dog(race);
tempDogName.Name = name;
animals.Add(tempDogName);
}
}
static void CreateBird()
{
if (Console.ReadLine().ToUpper().Contains("Y"))
{
Console.WriteLine("Give bird a name");
string name = Console.ReadLine(); // Otetaan talteen nimi
Console.WriteLine("Give bird a race");
string race = Console.ReadLine(); // Otetaan talteen rotu
Bird tempBird = new Bird(race); // Luodaan valiaikainen muuttuja linnulle ja luodessa asetetaan rotu
tempBird.Name = name; //asetetaan tempbird nimi, nimi sijaitsee animal perittavassa animal luokassa
animals.Add(tempBird); // Lopuksi lisataan tempBird variable listaan
}
}
static void ListAllAnimals()
{
Console.WriteLine("Tell me all the animals in the zoo Y or N");
if (Console.ReadLine().ToUpper().Contains("Y"))
{
Console.WriteLine("============================");
int i = 0;
foreach (Animal animal in animals) // ota animals listalta yksitellen jokainen animal, kunnes listalla ei ole elaimia
{
Console.WriteLine("Animal id " + i + " Animal name " + animal.Name);
if (animal.GetType().ToString() == "Olioita.Bird") //Jos animal on tyyppia linty
{
Bird x = (Bird)animal; //Luodaan lintu muutuja ja forward cast animal to bird
Console.WriteLine("Bird Race " + x.GetRace());
}
i++;
}
}
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment