C# - C Sharp: Bài làm mẫu 2

Các khóa học qua video:
Python SQL Server PHP C# Lập trình C Java HTML5-CSS3-JavaScript
Học trên YouTube <76K/tháng. Đăng ký Hội viên
Viết nhanh hơn - Học tốt hơn
Giải phóng thời gian, khai phóng năng lực
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections;
 
namespace ProductManagement
{
  class Product : IProduct
  {
    public static int autoID = 0;
    private int id;
    private String name;
    private String description;
    private double price;
    int[] RateList;
    public int this[int index]
    {
      set
      {
        RateList[index] = value;
      }
      get
      {
        return RateList[index];
      }
 
    }
 
    static void Main(string[] args)
    {
      Hashtable list = new Hashtable();
 
      while (true)
      {
        Console.WriteLine("PRODUCT MANAGEMENT SYSTEM");
        Console.WriteLine("");
        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.WriteLine("");
        Console.Write("Enter your choice: ");
        String line = Console.ReadLine().Trim();
        int choice = 0;
 
        if (line.Length <= 0 || line == null)
        {
          Console.WriteLine("Not allowed null!!!");
        }
        else
        {
          if (!int.TryParse(line, out choice))
          {
            Console.WriteLine("Pls enter a number");
          }
          else
          {
            if (choice < 1 || choice > 6)
            {
              Console.WriteLine("Enter a number 1 - 6");
            }
            else
            {
              switch (choice)
              {
                case 1:
                  Product p = new Product();
                  p.Insert();
                  list.Add(p.ID, p);
                  break;
                case 2:
                  new Product().removeProduct(list);
                  break;
                case 3:
                  foreach (Product pro in list.Values)
                  {
                    pro.ViewInfo();
                    pro.GetAvarageRate();
                  }
                  break;
                case 4:
                  new Product().Search(list);
                  break;
                case 5:
                  new Product().SortProducts(list);
                  break;
                case 6:
                  Environment.Exit(0);
                  break;
              }
            }
          }
        }
      }
    }
 
    #region IProduct Members
 
    public int ID
    {
      get
      {
        return id;
      }
      set
      {
        id = value;
      }
    }
 
    public string Name
    {
      get
      {
        return name;
      }
      set
      {
        name = value;
      }
    }
 
    public string Description
    {
      get
      {
        return description;
      }
      set
      {
        description = value;
      }
    }
 
    public double Price
    {
      get
      {
        return price;
      }
 
    }
 
    public void ViewInfo()
    {
      Console.WriteLine("Product Information");
      Console.WriteLine("");
      Console.WriteLine("ID: " + ID);
      Console.WriteLine("Name: " + Name);
      Console.WriteLine("Price: " + Price);
      Console.WriteLine("Description: " + Description);
    }
 
    public bool checkline(String line)
    {
      if (line != null && line.Length > 0)
      {
        return true;
      }
      Console.WriteLine("This field is not allowed null");
      return false;
    }
 
    public int CompareTo(object obj)
    {
      Product pro = obj as Product;
      if (this.Price > pro.Price)
      {
        return -1;
      }
      return 1;
    }
 
    public void Insert()
    {
      autoID++;
 
      ID = autoID;
      while (true)
      {
        Console.Write("Enter product name: ");
        String line = Console.ReadLine().Trim();
 
        if (checkline(line))
        {
          Name = line;
          break;
        }
      }
      while (true)
      {
        Console.Write("Enter product price($): ");
        String line = Console.ReadLine().Trim();
 
        if (checkline(line))
        {
          if (!double.TryParse(line, out price))
          {
            Console.WriteLine("Pls enter a number");
          }
          else
          {
            if (price <= 0 || price > 1000)
            {
              Console.WriteLine("Pls enter a number 1-999");
            }
            else
            {
              break;
            }
          }
        }
      }
      while (true)
      {
        Console.Write("Pls input description: ");
        String line = Console.ReadLine().Trim();
 
        if (checkline(line))
        {
          Description = line;
          break;
        }
      }
      while (true)
      {
        Console.Write("Pls input number of rates: ");
        String line = Console.ReadLine().Trim();
        int rates = 0;
 
        if (checkline(line))
        {
          if (!int.TryParse(line, out rates) || rates <= 0)
          {
            Console.WriteLine("Pls enter a number > 0");
          }
          else
          {
            RateList = new int[rates];
            break;
          }
        }
      }
      for (int i = 0; i < RateList.Length; i++)
      {
        while (true)
        {
          Console.Write("Pls input rate {0}: ", i + 1);
          String line = Console.ReadLine().Trim();
 
          if (checkline(line))
          {
            if (int.TryParse(line, out RateList[i]))
            {
              if (RateList[i] < 0 || RateList[i] > 10)
              {
                Console.WriteLine("Number must be 1-9");
              }
              else
              {
                break;
              }
            }
            else
            {
              Console.WriteLine("Pls enter a number!!!");
            }
          }
        }
      }
    }
 
    public void SortProducts(Hashtable list)
    {
      ArrayList arr = new ArrayList(list.Values);
      arr.Sort();
      foreach (Product p in arr)
      {
        p.ViewInfo();
      }
    }
    public void Search(Hashtable list)
    {
      int[] nums = new int[2];
      for (int i = 0; i < 2; i++)
      {
        while (true)
        {
          Console.WriteLine("Enter number {0}: ", i + 1);
          String line = Console.ReadLine().Trim();
 
          if (checkline(line))
          {
            if (int.TryParse(line, out nums[i]))
              if (nums[i] < 0 || nums[i] > 1000)
              {
                Console.WriteLine("Number must be 1 - 999");
              }
              else
              {
                break;
              }
            else
            {
              Console.WriteLine("Pls enter a number");
            }
          }
        }
      }
    }
 
    public void removeProduct(Hashtable list)
    {
      while (true)
      {
        Console.Write("Enter product ID: ");
        String line = Console.ReadLine().Trim();
        int rm = -1;
        if (checkline(line))
        {
          if (int.TryParse(line, out rm))
          {
            if (rm <= 0)
            {
              Console.WriteLine("ID must be > 0");
            }
            else
            {
              if (list.ContainsKey(rm))
              {
                list.Remove(rm);
                Console.WriteLine("You have deleted successfully product ID: " + rm);
                break;
              }
              else
              {
                Console.WriteLine("ID " + rm + "is not exist");
                break;
              }
            }
          }
        }
        else
        {
          Console.WriteLine("Pls enter a number!!!");
        }
      }
    }
 
    public void GetAvarageRate()
    {
      if (RateList != null)
      {
        int tong = 0;
        for (int i = 0; i < RateList.Length; i++)
        {
          tong += RateList[i];
        }
        double avarageRate = tong / (double)RateList.Length;
        Console.WriteLine("Avarage rate: " + avarageRate.ToString("0.0"));
      }
    }
 
    #endregion
  }
 
  interface IProduct
  {
    int ID { get; set; }
    String Name { get; set; }
    String Description { get; set; }
    double Price { get; }
 
    void ViewInfo();
  }
}

 

» Tiếp: Products Management System
« Trước: Bài làm mẫu 1
Các khóa học qua video:
Python SQL Server PHP C# Lập trình C Java HTML5-CSS3-JavaScript
Học trên YouTube <76K/tháng. Đăng ký Hội viên
Viết nhanh hơn - Học tốt hơn
Giải phóng thời gian, khai phóng năng lực
Copied !!!