Skip to content

Instantly share code, notes, and snippets.

@Xainey
Last active March 29, 2016 15:05
Show Gist options
  • Save Xainey/7f0f6cea197cf94c3a28 to your computer and use it in GitHub Desktop.
Save Xainey/7f0f6cea197cf94c3a28 to your computer and use it in GitHub Desktop.
/*
Create an application named MarketDemo that creates instance of class Stock.
This class can contain collection of objects of a class Fruit or Vegetable, or Beverage (use generics).
Class Stock must contain a method of displaying goods and a method of sorting by price.
Each class must contain
* constructors (with parameters and without parameters),
* properties (title, price, weight)
* overridden method ToString.
*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace Activity_25
{
class Program
{
public static void Main(string[] args)
{
/*
var fruitList = new Stock<Fruit>(
new Fruit("Apple", 0.99, 2),
new Fruit("Banana", 1.00, 3),
new Fruit("Orange", 0.73, 4)
);
Array.Sort(fruitList.Data, new Stock<IStockable>.SortByPrice());
fruitList.DisplayGoods();
var vegetableList = new Stock<Vegetable>(
new Vegetable("Lettuce", 5.00, 7),
new Vegetable("Carrot", 3.00, 4),
new Vegetable("Raddish", 3.33, 1)
);
Array.Sort(vegetableList.Data, new Stock<IStockable>.SortByPrice());
Console.WriteLine();
vegetableList.DisplayGoods();
var beverageList = new Stock<Beverage>(
new Beverage("Cola", 3.00, 4),
new Beverage("Wine", 9.33, 3),
new Beverage("Beer", 5.00, 23)
);
Array.Sort(beverageList.Data, new Stock<IStockable>.SortByPrice());
Console.WriteLine();
beverageList.DisplayGoods();
*/
var stockList = new Stock<IStockable>(
new Fruit("Apple", 0.99, 2),
new Fruit("Banana", 1.00, 3),
new Fruit("Orange", 0.73, 4),
new Vegetable("Lettuce", 5.00, 7),
new Vegetable("Carrot", 3.00, 4),
new Vegetable("Raddish", 3.33, 1),
new Beverage("Cola", 3.00, 4),
new Beverage("Wine", 9.33, 3),
new Beverage("Beer", 5.00, 23)
);
Array.Sort(stockList.Data, new Stock<IStockable>.SortByPrice());
stockList.DisplayGoods();
Console.ReadKey();
}
}
public interface IStockable
{
string Title { get; set; }
double Price { get; set; }
double Weight { get; set; }
string ToString();
}
public class Stock<T> where T : IStockable
{
//collection
public T[] Data { get; set; }
public Stock(params T[] data)
{
Data = data;
}
public void DisplayGoods()
{
foreach (var item in Data)
Console.WriteLine(item);
}
// Didnt need implentation here since main class implemented
// public class SortByPrice : IComparer<IStockable> {
// public int Compare(IStockable x, IStockable y) {
public class SortByPrice : Comparer<T>
{
public override int Compare(T x, T y)
{
if (x == null || y == null)
throw new ArgumentException("One of the arguments is null");
if (x.Price > y.Price)
return 1;
if (x.Price < y.Price)
return -1;
return 0;
}
}
}
public class Fruit : IStockable
{
public string Title { get; set; }
public double Price { get; set; }
public double Weight { get; set; }
public Fruit(string title, double price, double weight)
{
Title = title;
Price = price;
Weight = weight;
}
public Fruit()
: this("Unnamed", 0, 0)
{
// throw new Exception("Must be initialized");
}
public override string ToString()
{
return string.Format("Title: {0}\tPrice: {1:c}\tWeight: {2}", Title, Price, Weight);
}
}
public class Vegetable : IStockable
{
public string Title { get; set; }
public double Price { get; set; }
public double Weight { get; set; }
public Vegetable(string title, double price, double weight)
{
Title = title;
Price = price;
Weight = weight;
}
public Vegetable()
: this("Unnamed", 0, 0)
{
// throw new Exception("Must be initialized");
}
public override string ToString()
{
return string.Format("Title: {0}\tPrice: {1:c}\tWeight: {2}", Title, Price, Weight);
}
}
public class Beverage : IStockable
{
public string Title { get; set; }
public double Price { get; set; }
public double Weight { get; set; }
public Beverage(string title, double price, double weight)
{
Title = title;
Price = price;
Weight = weight;
}
public Beverage()
: this("Unnamed", 0, 0)
{
// throw new Exception("Must be initialized");
}
public override string ToString()
{
return string.Format("Title: {0}\tPrice: {1:c}\tWeight: {2}", Title, Price, Weight);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment