C# - C Sharp: Bài làm mẫu 1
Giải phóng thời gian, khai phóng năng lực
BÀI LÀM MẪU 1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace examlab56
{
interface IProduct
{
int ID { set; get; }
String Name { set; get; }
String Description { set; get; }
double Price { set; get; }
void ViewInfo();
}
class Product : IProduct
{
public int ID
{
set;
get;
}
public string Name
{
set;
get;
}
public string Description
{
set;
get;
}
public double Price
{
set;
get;
}
public void ViewInfo()
{
Console.WriteLine("ID:" + ID);
Console.WriteLine("Name: " + Name);
Console.WriteLine("Description: " + Description);
Console.WriteLine("Price: " + Price);
Console.WriteLine("");
}
}
class Products
{
public Hashtable ht = new Hashtable();
public int i = 0;
public int[] RateList = new int[100];
public int this[int index]
{
set { RateList[index] = value; }
get { return RateList[index]; }
}
public int GetAvarageRate()
{
int total = 0;
foreach (DictionaryEntry item in this.ht)
{
((Product)item.Value).ViewInfo();
total += this[(int)item.Key];
}
return total / this.ht.Count;
}
}
class SortPrice : IComparer
{
public int Compare(object x, object y)
{
return (int)(((Product)x).Price - ((Product)y).Price);
}
}
class Program
{
static void Main(string[] args)
{
Products s = new Products();
while (true)
{
Console.Clear();
Console.WriteLine("1.Add product");
Console.WriteLine("2.Remove product");
Console.WriteLine("3.Iterate product list");
Console.WriteLine("4.Search product");
Console.WriteLine("5.Sort product list");
Console.WriteLine("6.Exit");
Console.Write("Enter a number (1-6)");
String number;
while (true)
{
try
{
number = Console.ReadLine();
break;
}
catch
{
}
}
switch (number)
{
case "1":
Product p = new Product();
Console.Write("Please input product name: ");
p.Name = Console.ReadLine();
Console.Write("Please input price: ");
while (true)
{
try
{
p.Price = double.Parse(Console.ReadLine().Trim());
if (p.Price < 0 || p.Price > 1000)
{
Console.Write("Input price must be 0 - 1000");
Console.ReadLine();
continue;
}
else
{
break;
}
}
catch
{
continue;
}
}
Console.Write("Please input number of Rate: ");
++s.i;
s[s.i] = int.Parse(Console.ReadLine());
p.ID = s.i;
s.ht.Add(s.i, p);
Console.ReadKey();
break;
case "2":
Console.Write("Please input ID: ");
int j = int.Parse(Console.ReadLine());
foreach (DictionaryEntry item in s.ht)
{
if (j == (int)item.Key)
{
s.ht.Remove(j);
break;
}
}
Console.ReadKey();
break;
case "3":
int total = 0;
foreach (DictionaryEntry item in s.ht)
{
((Product)item.Value).ViewInfo();
total += s[(int)item.Key];
}
Console.Write("Average rate: {0}", s.GetAvarageRate());
Console.ReadKey();
break;
case "4":
double min = 0;
double max = 0;
Console.Write("Min = ");
min = double.Parse(Console.ReadLine());
Console.Write("Max = ");
max = double.Parse(Console.ReadLine());
foreach (DictionaryEntry item in s.ht)
{
if (min <= ((Product)item.Value).Price && max >= ((Product)item.Value).Price)
{
((Product)item.Value).ViewInfo();
}
}
Console.ReadKey();
break;
case "5":
Product[] list = new Product[s.ht.Count];
int k = 0;
foreach (DictionaryEntry item in s.ht)
{
list[k++] = item.Value as Product;
}
Array.Sort(list, new SortPrice());
foreach (Product item in list)
{
item.ViewInfo();
}
Console.ReadKey();
break;
case "6":
Environment.Exit(0);
break;
default:
break;
}
}
}
}
}
Giải phóng thời gian, khai phóng năng lực