Skip to content

Instantly share code, notes, and snippets.

@nelsonlaquet
Created April 27, 2012 02:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nelsonlaquet/2505138 to your computer and use it in GitHub Desktop.
Save nelsonlaquet/2505138 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
interface IItem
{
string Name { get; }
}
interface IMovable
{
void Move();
}
interface IBuyable
{
decimal Price { get; }
void Buy();
}
class Car : IItem, IMovable, IBuyable
{
public decimal Price { get; private set; }
public string Name { get; private set; }
public Car(string name, decimal price)
{
Price = price;
Name = name;
}
public void Buy()
{
Console.WriteLine("You bought a car for {0}",
Price);
}
public void Move()
{
Console.WriteLine("VROOOOOOOM");
}
}
class Chair : IItem, IMovable
{
public string Name { get; private set; }
public Chair(string name)
{
Name = name;
}
public void Move()
{
Console.WriteLine("Moving the {0} chair!", Name);
}
}
class Program
{
static void Main(string[] args)
{
var items = new List<IItem>();
items.Add(new Car("Stuff", 32.45m));
items.Add(new Chair("Awesome"));
items.Add(new Car("And", 23.4526m));
items.Add(new Car("Things", 32.432423m));
items.Add(new Chair("Comfy"));
while (true)
{
var chosenItem = ChooseItem(items);
var movable = chosenItem as IMovable;
var buyable = chosenItem as IBuyable;
Console.Write("What do you want to do: ");
var input = Console.ReadLine();
if (movable != null && input == "move")
{
movable.Move();
}
else if (buyable != null && input == "buy")
{
buyable.Buy();
}
else
{
Console.WriteLine("Invalid choice!");
}
}
}
static IItem ChooseItem(List<IItem> items)
{
while (true)
{
Console.WriteLine("------------------------");
var index = 1;
foreach (var item in items)
{
Console.Write("[{0}] - {1} ", index, item.Name);
var buyable = item as IBuyable;
if (buyable != null)
{
Console.Write("- costs {0}", buyable.Price);
}
var movable = item as IMovable;
if (movable != null)
{
Console.Write("- can move");
}
Console.WriteLine();
index++;
}
Console.Write("Choose item: ");
var itemIndex = int.Parse(Console.ReadLine());
if (itemIndex <= 0 || itemIndex > items.Count)
{
Console.WriteLine("You are dumb");
continue;
}
return items[itemIndex - 1];
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment