0% found this document useful (0 votes)
58 views5 pages

Week02 - Lab - Pet: Using Using Using Using Using

The document contains code for a C# program that defines classes to represent pets and medals. For pets, it creates a list of sample pet objects, sets some of their properties like owner, and outputs information about the pets. For medals, it similarly defines a Medal class, creates a list of medal objects, and outputs the medals in various formats like by year, color, or those setting records. It also saves the medal objects to a text file.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views5 pages

Week02 - Lab - Pet: Using Using Using Using Using

The document contains code for a C# program that defines classes to represent pets and medals. For pets, it creates a list of sample pet objects, sets some of their properties like owner, and outputs information about the pets. For medals, it similarly defines a Medal class, creates a list of medal objects, and outputs the medals in various formats like by year, color, or those setting records. It also saves the medal objects to a text file.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Week02_Lab_Pet

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
/*Name: Mel Vincent Anonuevo
*SIN: 301167069
*Date: Sept 21, 2021
*/
namespace Week02_Lab_Pet
{
class Program
{
static void Main(string[] args)
{

List<Pets> pets = new List<Pets>


{
new Pets("Cobby", 1, "A cute puppy"),
new Pets("Miguel", 10, "Sweet dog"),
new Pets("Maria", 13, "A very gentle dog"),
new Pets("Pablo", 1, "Big Appetite"),

};

pets[0].SetOwner("Mel");
pets[1].SetOwner("Trisha");
pets[2].SetOwner("Trisha");

pets[0].Train();
pets[2].Train();

Console.WriteLine("All pets:");
foreach (var Pets in pets)
{
Console.WriteLine($" - {Pets}");

}
Console.WriteLine();

Console.Write("Please enter the name of the owner to see which pets they own:
");
string owner = Console.ReadLine();
Console.WriteLine($"These pets are owned by {owner}");
foreach (Pets pet in pets)
{
if (pet.Owner == owner)
Console.WriteLine($" - {pet}");
}

}
}

}
class Pets
{
public string Name { get; }
public string Owner { get; private set; }
public int Age { get; }
public string Description { get; }
public bool IsHouseTrained { get; private set; }

public Pets(string name, int age, string description)


{
Name = name;
Age = age;
Description = description;
Owner = "no one";
IsHouseTrained = false;

public void SetOwner(string owner)


{

Owner = owner;
}

public void Train()


{
IsHouseTrained = true;
}

public override string ToString()


{
string trained = IsHouseTrained ? "Trained" : "Untrained";
return $"{Name}({Age}), {trained}, owned by {Owner}: {Description}";

}
}
Week02_Lab_Medal

using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
/*Name: Mel Vincent Anonuevo
*SIN: 301167069
*Date: Sept 19, 2021
*/
namespace Week02_Lab_Medal
{
class Program
{
static void Main(string[] args)
{

//create a medal object


Medal m1 = new Medal("Horace Gwynne", "Boxing", MedalColor.Gold, 2012, true);
//print the object
Console.WriteLine(m1);
//print only the name of the medal holder
Console.WriteLine(m1.Name);

//create another object


Medal m2 = new Medal("Michael Phelps", "Swimming", MedalColor.Gold, 2012,
false);
//print the updated m2
Console.WriteLine(m2);

//create a list to store the medal objects


List<Medal> medals = new List<Medal>() { m1, m2 };

medals.Add(new Medal("Ryan Cochrane", "Swimming", MedalColor.Silver, 2012,


false));
medals.Add(new Medal("Adam van Koeverden", "Canoeing", MedalColor.Silver,
2012, false));
medals.Add(new Medal("Rosie MacLennan", "Gymnastics", MedalColor.Gold, 2012,
false));
medals.Add(new Medal("Christine Girard", "Weightlifting", MedalColor.Bronze,
2012, false));
medals.Add(new Medal("Charles Hamelin", "Short Track", MedalColor.Gold, 2014,
true));
medals.Add(new Medal("Alexandre Bilodeau", "Freestyle skiing",
MedalColor.Gold, 2012, true));
medals.Add(new Medal("Jennifer Jones", "Curling", MedalColor.Gold, 2014,
false));
medals.Add(new Medal("Charle Cournoyer", "Short Track", MedalColor.Bronze,
2014, false));
medals.Add(new Medal("Mark McMorris", "Snowboarding", MedalColor.Bronze,
2014, false));
medals.Add(new Medal("Sidney Crosby ", "Ice Hockey", MedalColor.Gold, 2014,
false));
medals.Add(new Medal("Brad Jacobs", "Curling", MedalColor.Gold, 2014,
false));
medals.Add(new Medal("Ryan Fry", "Curling", MedalColor.Gold, 2014, false));
medals.Add(new Medal("Antoine Valois-Fortier", "Judo", MedalColor.Bronze,
2012, false));
medals.Add(new Medal("Brent Hayden", "Swimming", MedalColor.Bronze, 2012,
false));

//prints a numbered list of 16 medals.


Console.WriteLine("\n\nAll 16 medals");
for (int i = 0; i < medals.Count; i++)
{
Console.WriteLine($"{i + 1}. {medals[i]}");
}

//prints a numbered list of 16 names (ONLY)


Console.WriteLine("\n\nAll 16 names");
for (int i = 0; i < medals.Count; i++)
{
Console.WriteLine($"{i + 1}. {medals[i].Name}");
}

//prints a numbered list of 9 gold medals


Console.WriteLine("\n\nAll 9 gold medals");
int indx = 0;
foreach (var Medal in medals)
{
if (Medal.Color == MedalColor.Gold)
Console.WriteLine($"{++indx}. {Medal}");

}
/*foreach (var Medal in medals)
{
Console.WriteLine($"{Medal.Name}({MedalColor.Gold})");
}*/

//prints a numbered list of 9 medals in 2012


Console.WriteLine("\n\nAll 9 medals in 2012");
indx = 0;
foreach (var Medal in medals)
{
if (Medal.Year == 2012)
Console.WriteLine($"{++indx}. {Medal}");
}

//prints a numbered list of 4 gold medals in 2012


Console.WriteLine("\n\nAll 4 gold medals in 2012");
indx = 0;
foreach (var Medal in medals)
{
if (Medal.Year == 2012 && Medal.Color == MedalColor.Gold)
Console.WriteLine($"{++indx}. {Medal}");
}

//prints a numbered list of 3 world record medals


Console.WriteLine("\n\nAll 3 records");
indx = 0;
foreach (var Medal in medals)
{
if (Medal.IsRecord)
Console.WriteLine($"{++indx}. {Medal}");
}

//saving all the medal to file Medals.txt


Console.WriteLine("\n\nSaving to file");
TextWriter writer = new StreamWriter("./Medals.txt");
foreach (var Medal in medals)
{
writer.WriteLine(Medal);

}
writer.Close();

enum MedalColor {Bronze, Silver, Gold}


class Medal
{
public string Name { get; }
public string TheEvent { get; }
public MedalColor Color { get; }
public int Year { get; }
public bool IsRecord { get; }

public Medal(string name, string theEvent, MedalColor color, int year, bool
isRecord)
{
Name = name;
TheEvent = theEvent;
Color = color;
Year = year;
IsRecord = isRecord;
}

public override string ToString()


{
string M = "";
if (IsRecord)
{
M = ("(R)");
}
return $"{Year} - {TheEvent}{M} - {Name}({Color})";
}
}
}

You might also like