C# - C Sharp: Products Management System
Giải phóng thời gian, khai phóng năng lực
Tập tin Product.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Solution_Ex1_Prac2
{
class Product
{
public String Name;
public String Description;
public double Price;
public int[] Rate;
public void viewInfo()
{
Console.WriteLine("Name: "+Name);
Console.WriteLine("Price: "+Price);
Console.WriteLine("Description: "+Description);
}
}
}
Tập tin Shop.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace Solution_Ex1_Prac2
{
class Shop
{
ArrayList ProductList = new ArrayList();
public void addProduct(Product p)
{
Console.Write("Name: ");
p.Name = Console.ReadLine();
do{
Console.Write("Price: ");
p.Price = double.Parse(Console.ReadLine());
}while(!(p.Price>0 && p.Price<=100));
Console.Write("Description: ");
p.Description = Console.ReadLine();
Console.Write("Number of rates: ");
int N = int.Parse(Console.ReadLine());
p.Rate = new int[N];
for (int i = 0; i < N; i++)
{
do{
Console.Write("Rate "+(i+1)+": ");
p.Rate[i] = int.Parse(Console.ReadLine());
}while(!(p.Rate[i]>=1 && p.Rate[i]<=5));
}
ProductList.Add(p);
}
public void removeProduct()
{
bool search = false;
Console.Write("Enter the name of the product you want to delete: ");
String name = Console.ReadLine();
foreach (Product p in ProductList)
{
if (name.Equals(p.Name))
{
search = true;
ProductList.Remove(p);
break;
}
}
if (search == true)
Console.WriteLine("Remove Sucessful!");
else
Console.WriteLine("Not Found!");
}
public void iterateProductList()
{
foreach (Product p in ProductList)
{
p.viewInfo();
float averateRate = 0;
foreach (int rate in p.Rate)
{
averateRate += rate;
}
Console.WriteLine("Averate Rate: "+averateRate/p.Rate.Length);
}
}
public void searchProduct()
{
double minPrice;
double maxPrice;
bool search = false;
do{
Console.Write("Please enter Min price: ");
minPrice = double.Parse(Console.ReadLine());
}while(!(minPrice>0 && minPrice<=100));
do
{
Console.Write("Please enter Max price: ");
maxPrice = double.Parse(Console.ReadLine());
} while (!(minPrice <= 100 && maxPrice>minPrice));
foreach (Product p in ProductList)
{
if (p.Price >= minPrice && p.Price <= maxPrice)
{
search = true;
p.viewInfo();
}
}
if (search == false)
{
Console.WriteLine("Not Found!");
}
}
}
}
Tập tin điều khiển Program.cs (chứa hàm Main()):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Solution_Ex1_Prac2
{
class Program
{
static void Menu()
{
Console.WriteLine("1. Add new Product");
Console.WriteLine("2. Remove Product");
Console.WriteLine("3. Iterate product list");
Console.WriteLine("4. Search product");
Console.WriteLine("5. Exit");
}
static void Main(string[] args)
{
int choice;
Shop shop = new Shop();
Menu();
while (true)
{
Console.Write("Please choose an item: ");
choice = int.Parse(Console.ReadLine());
switch (choice)
{
case 1:
Product p = new Product();
shop.addProduct(p);
break;
case 2:
shop.removeProduct();
break;
case 3:
shop.iterateProductList();
break;
case 4:
shop.searchProduct();
break;
case 5: return;
}
}
}
}
}
Giải phóng thời gian, khai phóng năng lực