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)
![Luong Dinh Dai [T1907A]](https://www.gravatar.com/avatar/ca08fa4090e1038e541197564747f93c.jpg?s=80&d=mm&r=g)
Luong Dinh Dai
2020-05-20 09:22:47
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Text;
namespace Lession4
{
class Product
{
private string maHH;
private string tenHH;
private float soLuong;
private float gia1SP;
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)
{
MaHH = maHH;
TenHH = tenHH;
SoLuong = soLuong;
Gia1SP = gia1SP;
}
public void input()
{
Console.WriteLine("Nhap ma: ");
MaHH = Console.ReadLine();
Console.WriteLine("Nhap ten: ");
TenHH = Console.ReadLine();
Console.WriteLine("Nhap so luong: ");
SoLuong = float.Parse(Console.ReadLine());
Console.WriteLine("Nhap gia: ");
Gia1SP = float.Parse(Console.ReadLine());
}
public void output()
{
Console.WriteLine("MaHH: "+MaHH+"\nTenHH: "+TenHH+"\nSoLuong: "+SoLuong+"\nGia1SP: "+Gia1SP);
}
}
}
using System;
using System.Collections.Generic;
namespace Lession4
{
class Program
{
static void Main(string[] args)
{
List<Product> lstpro = new List<Product>();
Console.WriteLine("Nhap so product: ");
int n = int.Parse(Console.ReadLine());
for (int i = 0; i < n; i++)
{
Product pro = new Product();
pro.input();
lstpro.Add(pro);
}
Product priceMax = lstpro[0];
Console.WriteLine("Danh sach cac san pham");
foreach (var item in lstpro)
{
if(priceMax.Gia1SP< item.Gia1SP)
{
priceMax = item;
}
item.output();
}
Console.WriteLine("san pham co gia cao nhat");
priceMax.output();
}
}
}
![Trần Mạnh Dũng [T1907A]](https://www.gravatar.com/avatar/ee4661d1f9133c241d6c8287c7ea8ceb.jpg?s=80&d=mm&r=g)
Trần Mạnh Dũng
2020-05-20 09:20:55
using System;
using System.Collections.Generic;
using System.Text;
namespace QuanliSP
{
class Program
{
static void Main(string[] args)
{
Console.OutputEncoding = Encoding.UTF8;
List<Product> listProduct = new List<Product>();
Console.Write("Nhập số sản phẩm: ");
int n = int.Parse(Console.ReadLine());
for (int i = 1; i <= n; i++)
{
Product pro = new Product();
Console.WriteLine("===============");
Console.WriteLine("Nhập sản phẩm thứ {0}:", i);
pro.input();
listProduct.Add(pro);
}
Product product = listProduct[0];
Console.WriteLine("====================");
Console.WriteLine("Sản phẩm có giá bán cao nhất: ");
foreach (var item in listProduct)
{
if(product.Gia1SP < item.Gia1SP)
{
product = item;
}
}
product.output();
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace QuanliSP
{
class Product
{
private String maHH;
private String tenHH;
private float soLuong;
private float gia1SP;
public Product()
{
}
public Product(String maHH, String tenHH, float soLuong, float gia1SP)
{
MaHH = maHH;
TenHH = tenHH;
SoLuong = soLuong;
Gia1SP = gia1SP;
}
public string MaHH { get; set; }
public string TenHH { get; set; }
public float SoLuong { get; set; }
public float Gia1SP { get; set; }
public void input()
{
Console.Write("Nhập maHH: ");
maHH = Console.ReadLine();
Console.Write("Nhập tenHH: ");
tenHH = Console.ReadLine();
Console.Write("Nhập soLuong: ");
soLuong = float.Parse(Console.ReadLine());
Console.Write("Nhập gia1SP: ");
gia1SP = float.Parse(Console.ReadLine());
}
public void output()
{
Console.WriteLine("MaHH: {0}, TenHH: {1}, Soluong: {2}, Gia1SP: {3}", maHH, tenHH, soLuong, gia1SP);
}
}
}
![thienphu [T1907A]](https://www.gravatar.com/avatar/c4573ea65e411176c1852fd8584f1ab1.jpg?s=80&d=mm&r=g)
thienphu
2020-05-20 08:59:29
using System;
using System.Collections.Generic;
using System.Text;
namespace Lessson4
{
class Product
{
private string maHH;
private string tenHH;
private float soLuong;
private float gia1Sp;
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)
{
MaHH = maHH;
TenHH = tenHH;
SoLuong = soLuong;
Gia1Sp = gia1Sp;
}
public void display()
{
Console.WriteLine("maHH = {0} || tenHH = {1} || soluong = {2} || gia1Sp = {3}",
maHH, TenHH, SoLuong, 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());
}
}
}
using System;
using System.Collections.Generic;
namespace Lessson4
{
class Program
{
static void Main(string[] args)
{
List<Product> list = new List<Product>();
Console.WriteLine("Nhap N product");
int n = Convert.ToInt32(Console.ReadLine());
for (int i = 0; i < n; i++)
{
Product pro = new Product();
pro.input();
list.Add(pro);
}
Product priceMax = list[0];
Console.WriteLine("Danh sach cac san pham");
foreach (var item in list)
{
if(priceMax.Gia1Sp < item.Gia1Sp)
{
priceMax = item;
}
item.display();
}
Console.WriteLine("San pham co gia cao nhat la");
priceMax.display();
}
}
}