Open the terminal in folder ex15-17, which contains folder party:
mcs -recurse:party/*.cs -main:party.Party -out:Party.exe
// -recurse:party/*.cs search recursively in folder party for source files
// main: class containing the Main() method
// -out:Party.exe output in the current folder
mono Party.exe
House on 228 Maple Rd. holds robot Robbie, 2 hosts: John, Patricia,
and 3 rooms: Kitchen, Dining room, Bedroom,
Robbie makes the bed
John and Patricia decide to have a party!
Robbie invited: Martin, Linda,
Start the party!
Dining table is luxurious; foods: Fruit cake, drinks: Coca Cola,
Coca Cola is refreshing
Fruit cake is delicious
Match is practical
Lighting a match
Candle is romantic
Lighting a candle
Robbie cleans the kitchen
Robbie cleans the diner
Robbie says Good bye to all the guests
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
using System.Collections.Generic; // for List
using party.house; // for House
using party.house.rooms; // for Room, Kitchen, Diner, BedRoom
using party.house.rooms.furniture; // for Bed, BedType, Table,
// DiningTable, KitchenTable, Chair, ArmChair, DiningChair, KitchenChair
using party.house.occupants; // for Robot, Host, Guest
using party.foods; // for Food, Drink, Cake, Soda
namespace party
{
public class Party
{
static void StartParty()
{
System.Console.WriteLine("Start the party!");
}
public static void Main()
{
Robot r = new Robot("Robbie");
House house = new House("228 Maple Rd.", r);
Table kt = new KitchenTable("Kitchen table");
Kitchen kitchen = new Kitchen("Kitchen", kt);
Chair kc1 = new KitchenChair("kc1");
Chair kc2 = new DiningChair("kc2");
kitchen.AddChair(kc1);
kitchen.AddChair(kc2);
// System.Console.WriteLine(kitchen); // kitchen.ToString()
house.AddRoom(kitchen);
Diner diner = new Diner("Dining room");
Chair ac = new ArmChair("armchair");
Chair dc1 = new DiningChair("dc1");
Chair dc2 = new DiningChair("dc2");
diner.AddChair(ac);
diner.AddChair(dc1);
diner.AddChair(dc2);
// System.Console.WriteLine(diner); // diner.ToString()
house.AddRoom(diner);
Bed bed = new Bed(BedType.Double);
BedRoom bedroom = new BedRoom("Bedroom", bed);
bedroom.SetChair(new ArmChair("armchair"));
// System.Console.WriteLine(bedroom); // bedroom.ToString()
house.AddRoom(bedroom);
Host h1 = new Host("John");
Host h2 = new Host("Patricia");
house.AddHost(h1);
house.AddHost(h2);
Guest g1 = new Guest("Martin");
Guest g2 = new Guest("Linda");
r.AddGuest(g1);
r.AddGuest(g2);
System.Console.WriteLine(house.ToString());
bed.MakeBed(r);
System.Console.WriteLine(h1 + " and " + h2 + " decide to have a party!");
System.Console.WriteLine(r.Invitations());
DiningTable dt = (DiningTable)(diner.GetTable());
Drink coke = new Soda("Coca Cola");
Food cake = new Cake("Fruit cake");
dt.AddFood(cake);
dt.AddDrink(coke);
StartParty();
System.Console.WriteLine(dt);
System.Console.WriteLine(coke);
System.Console.WriteLine(cake);
((Cake)cake).Celebrate(); // Food cake downcast to Cake
kitchen.CleanKitchen(r);
diner.CleanDiner(r);
r.ClearGuests();
}
}
}
/*
// Open terminal in the folder containing folder party:
mcs -recurse:party/*.cs -main:party.Party -out:Party.exe
// -recurse:party/*.cs search recursively in folder party for source files
// main: class containing the Main() method
// -out:Party.exe output in the current folder
mono Party.exe
House on 228 Maple Rd. holds robot Robbie, 2 hosts: John, Patricia,
and 3 rooms: Kitchen, Dining room, Bedroom,
Robbie makes the bed
John and Patricia decide to have a party!
Robbie invited: Martin, Linda,
Start the party!
Dining table is luxurious; foods: Fruit cake, drinks: Coca Cola,
Coca Cola is refreshing
Fruit cake is delicious
Match is practical
Lighting a match
Candle is romantic
Lighting a candle
Robbie cleans the kitchen
Robbie cleans the diner
Robbie says Good bye to all the guests
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
namespace party.foods
{
abstract class Food
{
string name;
public string Id
{
get
{
return name;
}
set
{
name = value;
}
}
public Food(string s)
{
Id = s; // calling the set accessor of the Id property
}
override public string ToString()
{
return $"{Id}";
}
}
}
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
using party.house.rooms.ornaments; // for Candle
using party.house.rooms.tools; // for Match
namespace party.foods
{
class Cake : Food // Cake is a type of Food
{
public Cake(string name) : base(name) // call base constructor
{}
public void Celebrate()
{
Match m = new Match("Match");
System.Console.WriteLine(m); // m.ToString()
m.Light();
Candle c = new Candle("Candle");
System.Console.WriteLine(c); // c.ToString()
c.Light();
}
override public string ToString()
{
string message = base.ToString(); // name
message += " is delicious";
return message;
}
}
}
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
namespace party.foods
{
abstract class Drink
{
string name;
public string Id
{
get
{
return name;
}
set
{
name = value;
}
}
public Drink(string s)
{
Id = s; // calling the set accessor of the Id property
}
override public string ToString()
{
return $"{Id}";
}
}
}
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
namespace party.foods
{
class Soda : Drink // Soda is a type of Drink
{
public Soda(string name) : base(name) // call base constructor
{}
override public string ToString()
{
string message = base.ToString(); // name
message += " is refreshing";
return message;
}
}
}
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
using System.Collections.Generic; // for List
using party.house.rooms; // for Room
using party.house.occupants; // for Robot, Host
namespace party.house
{
class House
{
string address;
Robot robot;
List<Room> rooms;
List<Host> hosts;
public House(string address, Robot robot)
{
this.address = address;
this.robot = robot;
rooms = new List<Room>();
hosts = new List<Host>();
}
public List<Room> getRooms()
{
return rooms;
}
public void setRooms(List<Room> rooms)
{
this.rooms = rooms;
}
public void AddRoom (Room r)
{
rooms.Add(r);
}
public void RemoveRoom (Room r)
{
rooms.Remove(r);
}
public List<Host> getHosts()
{
return hosts;
}
public void setHosts(List<Host> hosts)
{
this.hosts = hosts;
}
public void AddHost (Host h)
{
hosts.Add(h);
}
public void RemoveHost (Host h)
{
hosts.Remove(h);
}
override public string ToString()
{
string house = "House on " + address;
house += " holds robot ";
house += robot;
house += ", ";
house += hosts.Count;
if (hosts.Count == 1)
{house += " host: ";}
else{house += " hosts: ";}
foreach (Host h in hosts)
{
house += h.ToString();
house += ", ";
}
house += "\nand ";
house += rooms.Count;
if (rooms.Count == 1)
{house += " room: ";}
else{house += " rooms: ";}
foreach (Room r in rooms)
{
house += r.Id;
house += ", ";
}
return house;
}
}
}
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
namespace party.house.occupants
{
class Guest
{
string name;
public Guest(string name)
{
this.name = name;
}
override public string ToString()
{
return name;
}
}
}
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
namespace party.house.occupants
{
class Host
{
string name;
public Host(string name)
{
this.name = name;
}
override public string ToString()
{
return name;
}
}
}
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
using System.Collections.Generic; // for List
namespace party.house.occupants
{
class Robot
{
string name;
List<Guest> guests;
public Robot(string name)
{
this.name = name;
guests = new List<Guest>();
}
public List<Guest> getGuests()
{
return guests;
}
public void setGuests(List<Guest> guests)
{
this.guests = guests;
}
public void AddGuest (Guest g)
{
guests.Add(g);
}
public void RemoveGuest (Guest g)
{
guests.Remove(g);
}
public string Invitations()
{
string invitations = name + " invited: ";
foreach (Guest g in guests)
{
invitations += g.ToString();
invitations += ", ";
}
return invitations;
}
public void ClearGuests()
{
System.Console.WriteLine(name + " says Good bye to all the guests");
guests.Clear();
}
override public string ToString()
{
return name;
}
}
}
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
namespace party.house.rooms
{
abstract class Room
{
string name;
public string Id
{
get
{
return name;
}
set
{
name = value;
}
}
public Room(string s)
{
Id = s; // calling the set accessor of the Id property
}
override public string ToString()
{
return $"{Id}";
}
}
}
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
using party.house.rooms.furniture; // for Bed, BedType, Chair
namespace party.house.rooms
{
class BedRoom : Room // BedRoom is a type of Room
{
Bed bed;
Chair chair;
public BedRoom(string name) : base(name) // call base constructor
{bed = new Bed();}
public BedRoom(string name, Bed bed) : base(name) // call base constructor
{
this.bed = bed;
}
public BedRoom(string name, Bed bed, Chair chair) : base(name)
{
this.bed = bed;
this.chair = chair;
}
public Chair GetChair()
{
return chair;
}
public void SetChair(Chair chair)
{
this.chair = chair;
}
public void RemoveChair()
{
chair = null;
}
override public string ToString()
{
string message = base.ToString(); // name
message += " is silent; ";
message += bed.ToString();
if (chair != null)
{
message += ", chair: ";
message += chair.Id;
}
return message;
}
}
}
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
using System.Collections.Generic; // for List
using party.house.rooms.furniture; // for Chair, Table, DiningTable
using party.house.occupants; // for Robot
namespace party.house.rooms
{
class Diner : Room // Diner is a type of Room
{
List<Chair> chairs;
Table table;
public Diner(string name) : base(name) // call base constructor
{
chairs = new List<Chair>();
table = new DiningTable("Dining table");
}
public Diner(string name, Table table) : base(name) // call base constructor
{
chairs = new List<Chair>();
this.table = table;
}
public Table GetTable()
{
return table;
}
public void SetTable(Table table)
{
this.table = table;
}
public void AddChair(Chair chair)
{
chairs.Add(chair);
}
public void RemoveChair(Chair chair)
{
chairs.Remove(chair);
}
public void CleanDiner(Robot r)
{
System.Console.WriteLine(r.ToString() + " cleans the diner");
table.ClearTable();
}
override public string ToString()
{
string message = base.ToString(); // name
message += " is spacious; it has a ";
message += table.Id;
message += " and ";
message += chairs.Count;
if (chairs.Count == 1)
{message += " chair: ";}
else {message += " chairs: ";}
foreach (Chair c in chairs)
{
message += c.Id;
message += ", ";
}
return message;
}
}
}
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
using System.Collections.Generic; // for List
using party.house.rooms.furniture; // for Chair, Table, KitchenTable
using party.house.occupants; // for Robot
namespace party.house.rooms
{
class Kitchen : Room // Kitchen is a type of Room
{
List<Chair> chairs;
Table table;
public Kitchen(string name) : base(name) // call base constructor
{
chairs = new List<Chair>();
table = new KitchenTable("Kitchen table");
}
public Kitchen(string name, Table table) : base(name) // call base constructor
{
chairs = new List<Chair>();
this.table = table;
}
public Table GetTable()
{
return table;
}
public void SetTable(Table table)
{
this.table = table;
}
public void AddChair(Chair chair)
{
chairs.Add(chair);
}
public void RemoveChair(Chair chair)
{
chairs.Remove(chair);
}
public void CleanKitchen(Robot r)
{
System.Console.WriteLine(r.ToString() + " cleans the kitchen");
table.ClearTable();
}
override public string ToString()
{
string message = base.ToString(); // name
message += " is practical; it has a ";
message += table.Id;
message += " and ";
message += chairs.Count;
if (chairs.Count == 1)
{message += " chair: ";}
else {message += " chairs: ";}
foreach (Chair c in chairs)
{
message += c.Id;
message += ", ";
}
return message;
}
}
}
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
using party.house.occupants; // for Robot
namespace party.house.rooms.furniture
{
enum BedType {Single, Double}
class Bed
{
BedType type;
public Bed()
{
type = BedType.Single;
}
public Bed(BedType bedtype)
{
type = bedtype;
}
public BedType GetBedType()
{
return type;
}
public void SetBedType(BedType type)
{
this.type = type;
}
public void MakeBed(Robot r)
{
System.Console.WriteLine(r + " makes the bed");
}
override public string ToString()
{
string message = "Bed type: " + type;
return message;
}
}
}
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
namespace party.house.rooms.furniture
{
abstract class Chair
{
string name;
public string Id
{
get
{
return name;
}
set
{
name = value;
}
}
public Chair(string s)
{
Id = s; // calling the set accessor of the Id property
}
override public string ToString()
{
return $"{Id}";
}
}
}
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
namespace party.house.rooms.furniture
{
class ArmChair : Chair // ArmChair is a type of Chair
{
public ArmChair(string name) : base(name) // call base constructor
{}
override public string ToString()
{
string message = base.ToString(); // name
message += " is cozy";
return message;
}
}
}
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
namespace party.house.rooms.furniture
{
class DiningChair : Chair // DiningChair is a type of Chair
{
public DiningChair(string name) : base(name) // call base constructor
{}
override public string ToString()
{
string message = base.ToString(); // name
message += " is comfortable";
return message;
}
}
}
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
namespace party.house.rooms.furniture
{
class KitchenChair : Chair // KitchenChair is a type of Chair
{
public KitchenChair(string name) : base(name) // call base constructor
{}
override public string ToString()
{
string message = base.ToString(); // name
message += " is useful";
return message;
}
}
}
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
namespace party.house.rooms.furniture
{
abstract class Table
{
string name;
public string Id
{
get
{
return name;
}
set
{
name = value;
}
}
public Table(string s)
{
Id = s; // calling the set accessor of the Id property
}
abstract public void ClearTable();
override public string ToString()
{
return $"{Id}";
}
}
}
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
using System.Collections.Generic; // for List
using party.foods; // for Food, Drink
namespace party.house.rooms.furniture
{
class DiningTable : Table // DiningTable is a type of Table
{
List<Food> foods;
List<Drink> drinks;
public DiningTable(string name) : base(name) // call base constructor
{
foods = new List<Food>();
drinks = new List<Drink>();
}
public void AddFood(Food f)
{
foods.Add(f);
}
public void RemoveFood(Food f)
{
foods.Remove(f);
}
public void AddDrink(Drink d)
{
drinks.Add(d);
}
public void RemoveDrink(Drink d)
{
drinks.Remove(d);
}
override public void ClearTable()
{
foods.Clear();
drinks.Clear();
}
override public string ToString()
{
string message = base.ToString(); // name
message += " is luxurious; foods: ";
foreach (Food f in foods)
{
message += f.Id;
message += ", ";
}
message += " drinks: ";
foreach (Drink d in drinks)
{
message += d.Id;
message += ", ";
}
return message;
}
}
}
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
using System.Collections.Generic; // for List
using party.house.rooms.tools; // for Tool
namespace party.house.rooms.furniture
{
class KitchenTable : Table // KitchenTable is a type of Table
{
List<Tool> tools; // tools for cooking
public KitchenTable(string name) : base(name) // call base constructor
{
tools = new List<Tool>();
}
public void AddTool(Tool t)
{
tools.Add(t);
}
public void RemoveTool(Tool t)
{
tools.Remove(t);
}
override public void ClearTable()
{
tools.Clear();
}
override public string ToString()
{
string message = base.ToString(); // name
message += " is useful";
foreach (Tool t in tools)
{
message += t.Id;
message += ", ";
}
return message;
}
}
}
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
namespace party.house.rooms.ornaments
{
abstract class Ornament
{
string name;
public string Id
{
get
{
return name;
}
set
{
name = value;
}
}
public Ornament(string s)
{
Id = s; // calling the set accessor of the Id property
}
override public string ToString()
{
return $"{Id}";
}
}
}
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
namespace party.house.rooms.ornaments
{
class Candle : Ornament // Candle is a type of Ornament
{
public Candle(string name) : base(name) // call base constructor
{}
public void Light()
{
System.Console.WriteLine("Lighting a candle");
}
override public string ToString()
{
string message = base.ToString(); // name
message += " is romantic";
return message;
}
}
}
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
namespace party.house.rooms.tools
{
abstract class Tool
{
string name;
public string Id
{
get
{
return name;
}
set
{
name = value;
}
}
public Tool(string s)
{
Id = s; // calling the set accessor of the Id property
}
override public string ToString()
{
return $"{Id}";
}
}
}
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
namespace party.house.rooms.tools
{
class Match : Tool // Match is a type of Tool
{
public Match(string name) : base(name) // call base constructor
{}
public void Light()
{
System.Console.WriteLine("Lighting a match");
}
override public string ToString()
{
string message = base.ToString(); // name
message += " is practical";
return message;
}
}
}