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)
![vuong huu phu [T2008A]](https://www.gravatar.com/avatar/307a5cf29780afab49706dc8b15b86c6.jpg?s=80&d=mm&r=g)
vuong huu phu
2021-05-17 10:09:19
using System;
using System.Collections.Generic;
using System.Text;
namespace sanpham2
{
public class Product
{
public String ma;
public String ten;
public int soluong;
public float gia;
public Product() {}
public Product(String ma,String ten,int soluong,float gia) {
this.ma = ma;
this.ten = ten;
this.soluong = soluong;
this.gia = gia;
}
public String Ma{ get; set;}
public String Ten { get; set; }
public String Soluong { get; set; }
public String Gia { get; set; }
public void input() {
Console.WriteLine("Nhap ma: ");
ma = Console.ReadLine();
Console.WriteLine("Nhap ten: ");
ten = Console.ReadLine();
Console.WriteLine("Nhap so luong");
soluong = Int32.Parse(Console.ReadLine());
Console.WriteLine("Nhap gia");
gia = float.Parse(Console.ReadLine());
}
public void display() {
Console.WriteLine(" Ma: " + ma + " Ten: " + ten + " So luong: " + soluong + " gia:" + gia);
}
}
}
using System;
using System.Collections.Generic;
namespace sanpham2
{
class Program
{
static void Main(string[] args)
{
int n;
Console.WriteLine("Nhap so luong san pham: ");
n = Int32.Parse(Console.ReadLine());
List<Product> arrpr = new List<Product>();
for (int i = 0; i < n; i++) {
Product product = new Product();
product.input();
arrpr.Add(product);
}
float max = arrpr[0].gia;
for (int i = 1; i < arrpr.Count; i++) {
if (arrpr[i].gia > max) {
max = arrpr[i].gia;
}
}
for (int i = 0; i< arrpr.Count; i ++ ) {
if (arrpr[i].gia == max) {
arrpr[i].display();
}
}
}
}
}
![Nguyễn Tiến Đạt [T2008A]](https://www.gravatar.com/avatar/b5819cd0adc95c727c7ad0c2bcf6098b.jpg?s=80&d=mm&r=g)
Nguyễn Tiến Đạt
2021-05-17 09:56:29
#Index.cs
using System;
using System.Collections.Generic;
using System.Text;
using ProductHomeWork.thuoctinh;
namespace ProductHomeWork.index
{
class Index
{
static void Main(string[] args)
{
Console.WriteLine("Nhap N phan tu:");
int N = int.Parse(Console.ReadLine());
Product[] productList = new Product[N];
float max = 0;
int k=0;
for(int i = 0; i < N; i++)
{
Console.WriteLine("Nhap san pham thu {0}:", i + 1);
Product product = new Product();
product.input();
productList[i] = product;
if(product.gia1SP > max)
{
max = product.gia1SP;
k = i;
}
}
Console.WriteLine("Test");
for (int i = 0; i < N; i++)
{
Console.WriteLine("San pham thu {0}:", i + 1);
productList[i].display();
}
Console.WriteLine("San pham co gia cao nhat la san pham thu {0}", k + 1);
productList[k].display();
}
}
}
#Program.cs
using System;
namespace ProductHomeWork.thuoctinh
{
class Product
{
private String maHH;
private String tenHH;
private float soLuong;
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("Ma hang hoa: {0}", maHH);
Console.WriteLine("Ten hang hoa: {0}", tenHH);
Console.WriteLine("So luong hang hoa: {0}", soLuong);
Console.WriteLine("Gia 1 san pham: {0}", gia1SP);
}
}
}
![hainguyen [T2008A]](https://www.gravatar.com/avatar/32855ce6db55d60134d830aee06b41e5.jpg?s=80&d=mm&r=g)
hainguyen
2021-05-17 09:43:43
using System;
using System.Collections.Generic;
namespace Lesson02
{
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 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 Show ()
{
Console.WriteLine("maHH = {0}, tenHH = {1}, soLuong = {2}, gia1Sp = {3}", MaHH, TenHH, SoLuong, Gia1Sp);
}
static void Main(string[] args)
{
List<Product> list = new List<Product>();
Console.WriteLine("Nhap N: ");
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 san pham: ");
foreach (var item in list)
{
if (priceMax.Gia1Sp < item.Gia1Sp)
{
priceMax = item;
}
item.Show();
}
Console.WriteLine("San pham co gia cao nhat: ");
priceMax.Show();
}
}
}
![Thành Lâm [T1907A]](https://www.gravatar.com/avatar/fb1b94f4caad069ee6e3f42ea2221b49.jpg?s=80&d=mm&r=g)
Thành Lâm
2020-06-01 08:46:58
using System;
using System.Text;
namespace Quan_ly_san_pham
{
class Program
{
static void Main(string[] args)
{
Console.OutputEncoding = Encoding.UTF8;
Console.WriteLine("Hello World!");
Console.WriteLine("==============Quản lý sản phẩm===================");
sanpham();
}
static void sanpham()
{
Product product = new Product();
product.Fullname = "Nguyễn Thành Lâm";
Console.WriteLine(product.Fullname);
product.input();
product.display();
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace Quan_ly_san_pham
{
class Product
{
public string Fullname { get; set; }
public string MaHH { get; set; }
public string TenHH { get; set; }
private float _soluong;
public float SoLuong {
get
{
return _soluong;
}
set
{
if(value > 0)
{
this._soluong = value;
}
else
{
Console.WriteLine("số lượng sản phẩm phải lớn hơn 0");
}
}
}
private float _gia1sp;
public float Gia1SP {
get
{
return _gia1sp;
}
set
{
if(value > 0)
{
this._gia1sp = value;
}
else
{
Console.WriteLine("giá sản phẩm phải lớn hơn 0");
}
}
}
public void input()
{
Console.WriteLine("Nhập Mã Hàng Hóa: ");
MaHH = Console.ReadLine();
Console.WriteLine("Nhập Tên Hàng Hóa: ");
TenHH = Console.ReadLine();
Console.WriteLine("Nhập số lượng: ");
SoLuong = float.Parse(Console.ReadLine());
Console.WriteLine("Nhập giá bán: ");
Gia1SP = float.Parse(Console.ReadLine());
}
public void display()
{
Console.WriteLine("Mã hàng hóa: {0}\n Tên Hàng Hóa: {1}\n Số Lượng: {2}\n giá bán: {3}", MaHH, TenHH, SoLuong, Gia1SP);
}
}
}
![Phan Bạch Tùng Dương [T1907A]](https://www.gravatar.com/avatar/e74e3ec62fe3a191929e12eecbe01edf.jpg?s=80&d=mm&r=g)
Phan Bạch Tùng Dương
2020-05-29 02:20:16
using System;
using System.Collections.Generic;
namespace Lessson6
{
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 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();
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace Lessson6
{
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());
}
}
}
![NguyenHuuThanh [T1907A]](https://www.gravatar.com/avatar/035e4f4fed661b8e1c3e066e43cd5e41.jpg?s=80&d=mm&r=g)
NguyenHuuThanh
2020-05-25 07:28:15
using System;
using System.Collections.Generic;
using System.Text;
namespace BAITAP.QuanlySanpham
{
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 + tenHH + soLuong + gia1SP);
}
static void main(String[] args)
{
int N;
Console.WriteLine("Nhap vao N :");
N = Int32.Parse(Console.ReadLine());
Product[] productlist = new Product[N];
for (int i = 0; i < N; i++)
{
productlist[i].input();
Console.WriteLine("End");
}
float min = 100;
for (int i = 0; i < N; i++)
{
if (productlist[i].gia1SP < min)
{
min = productlist[i].gia1SP;
}
}
for (int i = 0; i < N; i++)
{
if (min == productlist[i].gia1SP)
{
Console.WriteLine(productlist[i].tenHH);
}
}
}
}
}
![Đường Thanh Bình [T1907A]](https://www.gravatar.com/avatar/c2ef7c316acb82467912bf5677b52a8b.jpg?s=80&d=mm&r=g)
Đường Thanh Bình
2020-05-21 14:00:15
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Text;
namespace Quanlisanpham
{
class product
{
internal int gia1SP;
private string _maHH { set; get; }
private string _tenHH { set; get; }
private float _soLuong { set; get; }
private float _gia1SP { set; get; }
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 hang hoa:");
_maHH = Console.ReadLine();
Console.WriteLine("nhap ten hang hoa:");
_tenHH = Console.ReadLine();
Console.WriteLine("nhap so luong:");
_soLuong = int.Parse(Console.ReadLine());
Console.WriteLine("nhap gia san pham:");
_gia1SP = int.Parse(Console.ReadLine());
}
public void display()
{
Console.WriteLine("product == maHH={0}, tenHH={1}, soLuong={2}, gia1SP={3}", _maHH, _tenHH, _soLuong, _gia1SP);
}
}
}
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
namespace Quanlisanpham
{
class Program
{
static void Main(String[] agrs)
{
List<product> lstpro = new List<product>();
Console.WriteLine("Nhap ma hang hoa (so) :");
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 san pham");
foreach (var item in lstpro)
{
if (priceMax.gia1SP < item.gia1SP)
{
priceMax = item;
}
item.display();
}
Console.WriteLine("San pham co gia cao nhat :");
priceMax.display();
Console.ReadKey();
}
}
}
![lê văn phương [T1907A]](https://www.gravatar.com/avatar/a07ddfb51e1e7189c76b4e42dbdbcddc.jpg?s=80&d=mm&r=g)
lê văn phương
2020-05-21 07:49:42
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Product
{
class product
{
internal int gia1SP;
private string _maHH { set; get; }
private string _tenHH { set; get; }
private float _soLuong { set; get; }
private float _gia1SP { set; get; }
public product()
{
}
public product(string maHH, string tenHH, float soluong, float gia1SP)
{
_maHH = maHH;
_tenHH = tenHH;
_soLuong = soluong;
_gia1SP = gia1SP;
}
public void input()
{
Console.OutputEncoding = Encoding.UTF8;
Console.WriteLine("Nhập mã hàng hóa (số) :");
_maHH = Console.ReadLine();
Console.WriteLine("Nhập tên hàng hóa :");
_tenHH = Console.ReadLine();
Console.WriteLine("Số lượng (số):");
_soLuong = float.Parse(Console.ReadLine());
Console.WriteLine("Giá 1 sản phẩm (số) :");
_gia1SP = float.Parse(Console.ReadLine());
}
public void display()
{
Console.WriteLine("Product >> ma: {0} ,ten: {1}, so luong: {2},gia san pham{3} ", _maHH, _tenHH, _soLuong, _gia1SP);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Product
{
class Program
{
static void Main(string[] args)
{
Console.OutputEncoding = Encoding.UTF8;
List<product> lstpro = new List<product>();
Console.WriteLine("Nhập mã hàng hóa (số) : ");
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 sách sản phẩm :");
foreach (var item in lstpro)
{
if (priceMax.gia1SP < item.gia1SP)
{
priceMax = item;
}
item.display();
}
Console.WriteLine("Sản phẩm có giá cao nhất :");
priceMax.display();
Console.ReadKey();
}
}
}
![Trương Công Vinh [T1907A]](https://www.gravatar.com/avatar/223a7e3a46f4a747f81b921fe023fcc4.jpg?s=80&d=mm&r=g)
Trương Công Vinh
2020-05-20 10:14:03
using System;
using System.Collections.Generic;
using System.Text;
namespace Product
{
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("MaHH : ");
maHH = Console.ReadLine();
Console.WriteLine("TenHH : ");
tenHH = Console.ReadLine();
Console.WriteLine("So luong : ");
soLuong = float.Parse(Console.ReadLine());
Console.WriteLine("Gia 1 SP : ");
gia1SP = float.Parse(Console.ReadLine());
}
public void display()
{
Console.WriteLine(toString());
}
private string toString()
{
return "Product{MaHH : "+ maHH +", tenHH : "+tenHH+", so luong : "+soLuong+", gia 1 SP : "+gia1SP +"}";
}
}
}
using System;
namespace Product
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
Console.WriteLine("nhap so luong ban ghi : ");
int n = int.Parse(Console.ReadLine());
Product[] products = new Product[n];
input(products);
proMaxPrice(products);
}
static void input(Product[] products)
{
for (int i = 0; i < products.Length; i++)
{
Console.WriteLine("Information of Product {0} : ", (i + 1));
products[i] = new Product();
products[i].input();
}
}
static void proMaxPrice(Product[] products)
{
Console.WriteLine("Information of Product is MaxPrice : ");
float maxPrice = products[0].gia1SP;
for (int i = 1; i < products.Length; i++)
{
if (maxPrice < products[i].gia1SP)
{
maxPrice = products[i].gia1SP;
}
}
for (int i = 0; i < products.Length; i++)
{
if (maxPrice == products[i].gia1SP)
{
products[i].display();
}
}
}
}
}
![Lê Minh Bắc [T1907A]](https://www.gravatar.com/avatar/22abcac77d8ca5e01144e240abb48d22.jpg?s=80&d=mm&r=g)
Lê Minh Bắc
2020-05-20 10:13:51
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Lession4
{
class Product
{
private string MaHH { get; set; }
private string TenHH { get; set; }
private float SoLuong { get; set; }
private 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.Write("Nhap maHH: ");
MaHH = Console.ReadLine();
Console.Write("Nhap tenHH: ");
TenHH = Console.ReadLine();
Console.Write("Nhap so luong: ");
SoLuong = float.Parse(Console.ReadLine());
Console.Write("Nhap gia 1 san pham: ");
Gia1SP = float.Parse(Console.ReadLine());
}
public void display()
{
Console.WriteLine("MaHH: {0}\nTenHH: {1}\nSo Luong: {2}\nGia 1 san pham: {3}", MaHH, TenHH, SoLuong, Gia1SP);
}
public static void Main(String[] agrs)
{
Console.Write("Nhap N: ");
int N = Convert.ToInt32(Console.ReadLine());
Product[] pro = new Product[N];
for(int i = 0; i < N; i++)
{
pro[i] = new Product();
pro[i].input();
}
float max = pro[0].Gia1SP;
for (int i = 1; i < N; i++)
{
if(max < pro[i].Gia1SP)
{
max = pro[i].Gia1SP;
}
}
for(int i = 0; i < N; i++)
{
if(max == pro[i].Gia1SP)
{
pro[i].display();
}
}
Console.ReadKey();
}
}
}