By GokiSoft.com|
13:56 28/10/2021|
C Sharp
Bài 1: Chương trình quản lý sản phẩm - Lập Trình C# - Lập Trình C Sharp - Làm quen OOP
Cài đặt lớp Product gồm các thuộc tính (phải khai báo là private)
- String maHH;
- String tenHH;
- float soLuong;
- float gia1SP;
Cài đặt 2 construcors, các hàm get/set.
Cài đặt hàm input(), display().
Khai báo hàm main và thực hiện như sau:
- Khai báo mảng có n phần tử kiểu Product.
- Gọi nhập thông tin cho các phần tử của mảng.
- Tìm ra sản phẩm nào có giá bán cao nhất.
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)
![Hieu Ngo [community,C2009G]](https://www.gravatar.com/avatar/cee2973101b9cf9580ef561ff3eeecf0.jpg?s=80&d=mm&r=g)
Hieu Ngo
2021-10-12 04:15:20
#Product.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace QuanLySanPham
{
class Product
{
private string maHH;
public string Mahh
{
get
{
return maHH;
}
set
{
this.maHH = value;
}
}
private string tenHH;
public string TenHH
{
get
{
return tenHH;
}
set
{
this.tenHH = value;
}
}
private float soLuong;
public float SoLuong
{
get
{
return soLuong;
}
set
{
this.soLuong = value;
}
}
private float gia;
public Product()
{
}
public float Gia
{
get
{
return gia;
}
set
{
this.gia = value;
}
}
public Product(string mahh, string tenHH, float soLuong, float gia)
{
Mahh = mahh;
TenHH = tenHH;
SoLuong = soLuong;
Gia = gia;
}
public void Input()
{
Console.WriteLine("Nhap ma hang hoa:");
Mahh = Console.ReadLine();
Console.WriteLine("Nhap ten hang hoa:");
TenHH = Console.ReadLine();
Console.WriteLine("Nhap so luong");
SoLuong = Utility.ReadFloat();
Console.WriteLine("Nhap gia ban:");
Gia = Utility.ReadFloat();
}
public void Display()
{
Console.WriteLine("Ma hang hoa: {0}, Ten hang hoa: {1}, So luong: {2}, Gia ban: {3}", Mahh, TenHH, SoLuong, Gia);
}
}
}
#Program.cs
using System;
using System.Collections.Generic;
namespace QuanLySanPham
{
class Program
{
static void Main(string[] args)
{
List<Product> productList = new List<Product>();
Console.WriteLine("Number of Product:");
int n = int.Parse(Console.ReadLine());
for(int i =0; i< n; i++)
{
Product product = new Product();
product.Input();
productList.Add(product);
}
getMAx(productList);
}
static void getMAx(List<Product> products)
{
float max = products[0].Gia;
int count = 0;
for(int i = 0; i < products.Count; i++)
{
if(max < products[i].Gia)
{
max = products[i].Gia;
count++;
}
}
if(count >=0)
{
for(int i = 0; i < products.Count; i++)
{
if(max == products[i].Gia)
{
products[i].Display();
}
}
} else
{
products[0].Display();
}
}
}
}
#Utility.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace QuanLySanPham
{
public class Utility
{
public static float ReadFloat()
{
while(true)
{
try
{
float value = float.Parse(Console.ReadLine());
return value;
} catch (Exception ex)
{
Console.WriteLine("Nhap sai!!");
}
}
}
}
}
![Hoàng Thiện Thanh [community,AAHN-C2009G]](https://www.gravatar.com/avatar/58e377dde293f6da38c0b5168578557a.jpg?s=80&d=mm&r=g)
Hoàng Thiện Thanh
2021-10-01 10:27:27
#Product.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace ProductManagerRev1
{
class Product
{
private string maHH;
private string tenHH;
private float soLuong;
public float SoLuong { get { return soLuong; } set { this.soLuong = value; } }
private float gia1SP;
public float Gia1SP { get { return gia1SP; } set { this.gia1SP = value; } }
public Product()
{
}
public Product(string maHH, string tenHH, float SoLuong, float Gia1SP)
{
this.maHH = maHH;
this.tenHH = tenHH;
this.SoLuong = SoLuong;
this.Gia1SP = Gia1SP;
}
public void input()
{
Console.WriteLine("Nhap ma hang:");
maHH = Console.ReadLine();
Console.WriteLine("Nhap ten hang:");
tenHH = Console.ReadLine();
Console.WriteLine("Nhap so luong:");
SoLuong = validFloat(Console.ReadLine());
Console.WriteLine("Nhap gia 1 SP:");
Gia1SP = validFloat(Console.ReadLine());
}
public void display()
{
Console.WriteLine("Ma hang: {0}\nTen hang: {1}\nSo luong:{2}\nGia 1 san pham:{3}\n", maHH, tenHH, soLuong, Gia1SP);
}
public float validFloat(string str)
{
bool check()
{
try
{
float.Parse(str);
}
catch (Exception) { return false; }
return true;
}
while (!check())
{
Console.WriteLine("Hay nhap gia tri theo so thuc (hay so nguyen).");
str = Console.ReadLine();
}
return float.Parse(str);
}
}
}
#Program.cs
using System;
using System.Collections.Generic;
namespace ProductManagerRev1
{
class Program
{
static void Main(string[] args)
{
List<Product> Products = new List<Product>();
Console.WriteLine("Nhap so luong cac loai hang hoa muon them:");
int N = validInt(Console.ReadLine());
int i = 0;
while(i < N)
{
Product pr = new Product();
pr.input();
Products.Add(pr);
i++;
}
Console.WriteLine("\nCac loai hang: ");
foreach (Product pr in Products)
{
pr.display();
}
Products.Sort((x, y) => y.Gia1SP.CompareTo(x.Gia1SP));
Console.WriteLine("\nSan pham co gia cao nhat: ");
Products[0].display();
}
static int validInt(string str)
{
bool check()
{
try
{
int.Parse(str);
}
catch (Exception) { return false; }
return true;
}
while (!check())
{
Console.WriteLine("Hay nhap gia tri theo so nguyen.");
str = Console.ReadLine();
}
return int.Parse(str);
}
}
}
![Nguyễn Việt Hoàng [community,AAHN-C2009G]](https://www.gravatar.com/avatar/bdbde8074d82148dcda6927ccb016466.jpg?s=80&d=mm&r=g)
Nguyễn Việt Hoàng
2021-10-01 10:25:42
#Mains.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Ss5.B2
{
class Mains
{
static void Main(string[] args)
{
List<Product> p = new List<Product>();
int i = 0;
Console.WriteLine("Enter sum of product :");
int sum = int.Parse(Console.ReadLine());
do
{
Console.WriteLine("Product[" + (i + 1) + "]");
Product pr = new Product();
pr.input();
p.Add(pr);
i++;
} while (i < sum);
Console.WriteLine("Product with highest :");
Product max = new Product();
foreach (Product x in p)
{
if(x.Gia1SP > max.Gia1SP)
{
max = x;
}
}
max.display();
}
}
}
#Product.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Ss5.B2
{
class Product
{
private string maHH;
public string MaHH
{
get
{
return this.maHH;
}
set
{
this.maHH = value;
}
}
private string tenHH;
public string TenHH
{
get
{
return this.tenHH;
}
set
{
this.tenHH = value;
}
}
private float soLuong;
public float SoLuong
{
get
{
return this.soLuong;
}
set
{
this.soLuong = value;
}
}
private float gia1SP;
public float Gia1SP
{
get
{
return this.gia1SP;
}
set
{
this.gia1SP = value;
}
}
public Product()
{
}
public Product(string maHH, string tenHH, float soLuong, float gia1SP)
{
this.MaHH = maHH;
this.TenHH = tenHH;
this.SoLuong = soLuong;
this.Gia1SP = gia1SP;
}
public void input()
{
Console.WriteLine("Enter maHH :");
MaHH = Console.ReadLine();
Console.WriteLine("Enter tenHH :");
TenHH = Console.ReadLine();
Console.WriteLine("Enter soLuong :");
SoLuong = float.Parse(Console.ReadLine());
Console.WriteLine("Enter gia1SP :");
Gia1SP = float.Parse(Console.ReadLine());
}
public void display()
{
Console.WriteLine("MaHH :{0}, TenHH :{1}, SoLuong :{2}, Gia1Sp :{3}",MaHH,TenHH,SoLuong,Gia1SP);
}
}
}
![Hoàng Thiện Thanh [community,AAHN-C2009G]](https://www.gravatar.com/avatar/58e377dde293f6da38c0b5168578557a.jpg?s=80&d=mm&r=g)
Hoàng Thiện Thanh
2021-10-01 03:49:41
#Product.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace ProductManagerRev1
{
class Product
{
private string maHH;
private string tenHH;
private float soLuong;
public float SoLuong { get { return soLuong; } set { this.soLuong = value; } }
private float gia1SP;
public Product()
{
}
public Product(string maHH, string tenHH, float SoLuong, float gia1SP)
{
this.maHH = maHH;
this.tenHH = tenHH;
this.SoLuong = SoLuong;
this.gia1SP = gia1SP;
}
public void input()
{
Console.WriteLine("Nhap ma hang:");
maHH = Console.ReadLine();
Console.WriteLine("Nhap ten hang:");
tenHH = Console.ReadLine();
Console.WriteLine("Nhap so luong:");
SoLuong = validFloat(Console.ReadLine());
Console.WriteLine("Nhap gia 1 SP:");
gia1SP = validFloat(Console.ReadLine());
}
public void display()
{
Console.WriteLine("Ma hang: {0}\nTen hang: {1}\nSo luong:{2}\nGia 1 san pham:{3}\n", maHH, tenHH, soLuong, gia1SP);
}
public float validFloat(string str)
{
bool check()
{
try
{
float.Parse(str);
}
catch (Exception) { return false; }
return true;
}
while (!check())
{
Console.WriteLine("Hay nhap gia tri theo so thuc (hay so nguyen).");
str = Console.ReadLine();
}
return float.Parse(str);
}
}
}
#Program.cs
using System;
using System.Collections.Generic;
namespace ProductManagerRev1
{
class Program
{
static void Main(string[] args)
{
List<Product> Products = new List<Product>();
Console.WriteLine("Nhap so luong cac loai hang hoa muon them:");
int N = validInt(Console.ReadLine());
int i = 0;
while(i < N)
{
Product pr = new Product();
pr.input();
Products.Add(pr);
i++;
}
Console.WriteLine("\nCac loai hang: ");
foreach (Product pr in Products)
{
pr.display();
}
Products.Sort((x, y) => y.SoLuong.CompareTo(x.SoLuong));
Console.WriteLine("\nSap xep tung loai hang theo so luong: ");
foreach (Product pr in Products)
{
pr.display();
}
}
static int validInt(string str)
{
bool check()
{
try
{
int.Parse(str);
}
catch (Exception) { return false; }
return true;
}
while (!check())
{
Console.WriteLine("Hay nhap gia tri theo so nguyen.");
str = Console.ReadLine();
}
return int.Parse(str);
}
}
}
![Nguyễn Anh Vũ [T2008A]](https://www.gravatar.com/avatar/8863d24ed74b396082becbc4db8331fd.jpg?s=80&d=mm&r=g)
Nguyễn Anh Vũ
2021-05-22 14:35:11
using System;
using QuanLySanPham1434;
using System.Collections.Generic;
namespace QuanLySanPham1434
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
List<Product> arr = new List<Product>();
Console.WriteLine("Nhap so luong can them : ");
int n = Convert.ToInt32(Console.ReadLine());
for (int i = 0; i < n; i++) ;
{
Product pr = new Product();
pr.Input();
arr.Add(pr);
}
foreach (Product pr in arr)
{
pr.Display();
}
float max = arr[0].gia1sp;
foreach (var sp in arr)
{
if (arr[0].gia1sp > max) ;
}
max = arr[0].gia1sp;
{
Console.WriteLine("san pahm co gia cao nhat:" + max);
}
}
}
}
#Productusing System;
using System.Collections.Generic;
using QuanLySanPham1434;
namespace QuanLySanPham1434
{
class Product
{
public String maHH { get; set; }
public String tenHH { get; set; }
public float soluongHH { get; set; }
public float gia1sp { get; set; }
public Product()
{
}
public Product(String maHH, String tenHH, float soluongHH, float gia1SP)
{
this.maHH = maHH;
this.tenHH = tenHH;
this.soluongHH = soluongHH;
this.gia1sp = gia1SP;
}
public void Input()
{
Console.WriteLine("nhap ma hang hoa: ");
maHH = Console.ReadLine();
Console.WriteLine("nhap ten hang hoa: ");
tenHH = Console.ReadLine();
Console.WriteLine("nhap so luong: ");
soluongHH = float.Parse(Console.ReadLine());
Console.WriteLine("nhap gia 1 san pham: ");
gia1sp = float.Parse(Console.ReadLine());
}
public void Display()
{
Console.WriteLine("{0},{1},{2},{3}", maHH, tenHH, soluongHH, gia1sp);
}
}
}
![Triệu Văn Lăng [T2008A]](https://www.gravatar.com/avatar/1348e3562c6492c26f796cb1f45982a1.jpg?s=80&d=mm&r=g)
Triệu Văn Lăng
2021-05-19 08:01:52
using System;
using System.Collections.Generic;
using System.Text;
namespace bai1434
{
class product
{
public string maHH { get; set; }
public string tenHH { get; set; }
public float soLuong { get; set; }
public float gia1SP { get; set; }
public product()
{
}
public product(string maHH, string tenHH, float soLuong, float gia1SP)
{
this.maHH = maHH;
this.tenHH = tenHH;
this.soLuong = soLuong;
this.gia1SP = gia1SP;
}
public void input()
{
Console.WriteLine("Nhap maHH: ");
maHH = Console.ReadLine();
Console.WriteLine("Nhap tenHH: ");
tenHH = Console.ReadLine();
Console.WriteLine("Nhap soLuong: ");
soLuong = float.Parse(Console.ReadLine());
Console.WriteLine("Nhap gia1SP: ");
gia1SP = float.Parse(Console.ReadLine());
}
public void display()
{
Console.WriteLine("maHH: " + maHH);
Console.WriteLine("tenHH: " + tenHH);
Console.WriteLine("soLuong: " + soLuong);
Console.WriteLine("gia1SP: " + gia1SP);
}
}
}
![Do Trung Duc [T2008A]](https://www.gravatar.com/avatar/2973ac07124f066b4605c535e8d39a99.jpg?s=80&d=mm&r=g)
Do Trung Duc
2021-05-18 04:47:28
using System;
using System.Collections.Generic;
using System.Text;
namespace Lesson2_ProductManager
{
class Product
{
public string MaHH { get; set; }
public string TenHH { get; set; }
public float SoLuong { get; set; }
public float GiaSanPham { get; set; }
public Product() { }
public Product(string MaHH, string TenHH, float SoLuong, float GiaSanPham)
{
this.MaHH = MaHH;
this.TenHH = TenHH;
this.SoLuong = SoLuong;
this.GiaSanPham = GiaSanPham;
}
public void Input()
{
Console.WriteLine("Nhap ma hang hoa");
MaHH = Console.ReadLine();
Console.WriteLine("Nhap ten hang hoa");
TenHH = Console.ReadLine();
Console.WriteLine("Nhap so luong hang hoa");
SoLuong = float.Parse(Console.ReadLine());
Console.WriteLine("Nhap gia san pham");
GiaSanPham = float.Parse(Console.ReadLine());
}
public void Display()
{
Console.WriteLine("MaHH: {0}, tenHH: {1}, SoLuong: {2}, Gia: {3}",MaHH, TenHH, SoLuong,GiaSanPham);
}
}
}
![Do Trung Duc [T2008A]](https://www.gravatar.com/avatar/2973ac07124f066b4605c535e8d39a99.jpg?s=80&d=mm&r=g)
Do Trung Duc
2021-05-18 04:47:09
using System;
using System.Collections.Generic;
using Lesson2_ProductManager;
namespace Lesson2_ProductManager
{
class Program
{
static void Main(string[] args)
{
List<Product> ProductList = new List<Product>();
Console.WriteLine("Nhap so luong hang hoa muon them: ");
int N = Int32.Parse(Console.ReadLine());
int MaxPrice = 0;
int IndexMax=0;
for ( int i=0; i < N; i++)
{
Product product = new Product();
product.Input();
ProductList.Add(product);
if (ProductList[i].GiaSanPham >= MaxPrice)
{
IndexMax = i;
}
}
Console.WriteLine("San pham co gia cao nhat la:");
ProductList[IndexMax].Display();
}
}
}
![Triệu Văn Lăng [T2008A]](https://www.gravatar.com/avatar/1348e3562c6492c26f796cb1f45982a1.jpg?s=80&d=mm&r=g)
Triệu Văn Lăng
2021-05-18 01:59:12
using System;
using bai1434;
using System.Collections.Generic;
namespace bai1434
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
List<product> prList = new List<product>();
Console.WriteLine("Nhap so luong san pham can them: ");
int n = Convert.ToInt32(Console.ReadLine());
for(int i = 0; i < n; i++)
{
product prd = new product();
Console.WriteLine("Nhap thong tin san pham thu {0}", i + 1);
prd.input();
prList.Add(prd);
}
Console.WriteLine("Thong tin san pham co gia cao nhat: ");
float max = prList[0].gia1SP;
for (int i = 0; i < prList.Count; i++)
{
if (prList[i].gia1SP > max)
{
max = prList[i].gia1SP;
}
}
for (int i = 0; i < prList.Count; i++)
{
if (prList[i].gia1SP == max)
{
prList[i].display();
}
}
}
}
}
![Trần Văn Lâm [T2008A]](https://www.gravatar.com/avatar/cfc15c8cb7781ad669b013e01f9f1a6b.jpg?s=80&d=mm&r=g)
Trần Văn Lâm
2021-05-17 15:54:18
#Product.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace les2bt3
{
class Product
{
public String maHH { get; set; }
public String tenHH { get; set; }
public float soLuong { get; set; }
public float gia1SP { get; set; }
public Product()
{
}
public Product(String maHH,String tenHH,float soLuong,float gia1SP)
{
this.maHH = maHH;
this.tenHH = tenHH;
this.soLuong = soLuong;
this.gia1SP = gia1SP;
}
public void Input()
{
Console.WriteLine("nhap ma hang hoa:");
maHH = Console.ReadLine();
Console.WriteLine("nhap ten hang hoa:");
tenHH = Console.ReadLine();
Console.WriteLine("nhap so luong:");
soLuong = float.Parse(Console.ReadLine());
Console.WriteLine("nhap gia 1 san pham:");
gia1SP = float.Parse(Console.ReadLine());
}
public void Display()
{
Console.WriteLine("{0},{1},{2},{3}", maHH, tenHH, soLuong, gia1SP);
}
}
}
#Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace les2bt3
{
class Program
{
static void Main(string[] args)
{
List<Product> arr = new List<Product>();
Console.WriteLine("Nhap so luong san pham:");
int n = Convert.ToInt32(Console.ReadLine());
for(int i = 0; i < n; i++)
{
Product pr = new Product();
pr.Input();
arr.Add(pr);
}
foreach(Product pr in arr)
{
pr.Display();
}
float max = arr[0].gia1SP;
foreach (var sp in arr)
{
if (arr[0].gia1SP> max)
{
max = arr[0].gia1SP;
}
}
Console.WriteLine("san pahm co gia cao nhat:" + max);
}
}
}