Skip to content

Instantly share code, notes, and snippets.

@opdo
Last active September 17, 2019 17:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save opdo/ac50c83486b4b7a61c426d486b79e497 to your computer and use it in GitHub Desktop.
Save opdo/ac50c83486b4b7a61c426d486b79e497 to your computer and use it in GitHub Desktop.
Demo lưu danh sách sản phẩm
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
// Nhớ vào Nuget package console cài cái này
// install-package Newtonsoft.Json
namespace DemoJson
{
// xây dựng 1 lớp sản phẩm
class SanPham
{
public string TenSP { get; set; }
public int GiaSP { get; set; }
// tạo phương thức nhập info sản phẩm
public void NhapThongTin()
{
Console.Clear();
Console.Write("+ Nhap ten san pham: ");
TenSP = Console.ReadLine();
while (true)
{
Console.Write("+ Nhap gia san pham: ");
string sgia = Console.ReadLine();
int gia = 0;
if (!int.TryParse(sgia, out gia))
{
Console.WriteLine("Nhap gia that bai, vui long nhap lai");
}
else
{
GiaSP = gia;
break;
}
}
}
// tạo phương thức chỉnh giá
public void ChinhSuaGia()
{
Console.Clear();
while (true)
{
Console.Write("Nhap gia moi cua san pham " + TenSP + ": ");
string sgia = Console.ReadLine();
int gia = 0;
if (!int.TryParse(sgia, out gia))
{
Console.WriteLine("Nhap gia that bai, vui long nhap lai");
}
else
{
GiaSP = gia;
break;
}
}
}
}
class Program
{
// đường dẫn file text
private static string PATH = "D:\\data.txt";
// list sản phẩm
private static List<SanPham> list = new List<SanPham>();
static void Main(string[] args)
{
// xóa sạch màn hình
Console.Clear();
// kiểm tra và đọc json list từ file
readData();
// ghi ra màn hình
print();
// chờ lệnh
Console.WriteLine("\n+ 1: Them san pham");
Console.WriteLine("+ 2: Sua san pham");
var input = Console.ReadLine();
switch (input)
{
case "1":
// thêm sản phẩm
SanPham sp = new SanPham();
sp.NhapThongTin();
list.Add(sp);
// lưu lại list json
writeToFile();
break;
case "2":
// chỉnh giá sản phẩm
editPrice();
break;
default:
return;
}
// gọi lại hàm main
Main(args);
}
private static void editPrice()
{
Console.Clear();
Console.WriteLine("Vui long nhap ten SP can chinh: ");
string tenSP = Console.ReadLine();
tenSP = tenSP.ToLower();
// tim sản phẩm
var sp = list.Where(x => (x.TenSP ?? "").ToLower().Equals(tenSP)).FirstOrDefault();
if (sp == null)
{
Console.WriteLine("Khong tim thay ten SP nay");
Console.ReadLine();
return;
}
// chinh gia sản phẩm
sp.ChinhSuaGia();
// lưu lại list json
writeToFile();
}
private static void writeToFile()
{
// biến list thành chuỗi json, sau đó lưu vào file
string dataText = Newtonsoft.Json.JsonConvert.SerializeObject(list);
System.IO.File.WriteAllText(PATH, dataText);
}
private static void print()
{
Console.WriteLine("Danh sach san pham");
foreach (var sanPham in list)
{
Console.WriteLine(sanPham.TenSP +"\t\t" + String.Format("{0:N0}", sanPham.GiaSP));
}
}
private static void readData()
{
if (System.IO.File.Exists(PATH))
{
string dataText = System.IO.File.ReadAllText(PATH);
// parse từ json về list sản phẩm, có try catch để đề phòng lỗi
try
{
list = Newtonsoft.Json.JsonConvert.DeserializeObject<List<SanPham>>(dataText);
}
catch
{
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment