예제 #1
0
        public void ShowCart()
        {
            while (true)
            {
                if (File.Exists("shoppingcart" + cusAll.UserName + ".dat"))
                {
                    List <Items> itemsa = null;
                    Orders       or     = new Orders();
                    decimal      amount = 0;
                    try
                    {
                        FileStream   fs = new FileStream("shoppingcart" + cusAll.UserName + ".dat", FileMode.OpenOrCreate, FileAccess.ReadWrite);
                        BinaryReader br = new BinaryReader(fs);
                        string       a  = br.ReadString();

                        itemsa = JsonConvert.DeserializeObject <List <Items> >(a);

                        br.Close();
                        fs.Close();
                    }
                    catch (System.Exception)
                    {
                        throw;
                    }
                    var table = new ConsoleTable("ID", "ITEM NAME");
                    foreach (var itema in itemsa)
                    {
                        table.AddRow(itema.ItemID, itema.ItemName);
                        amount += itema.ItemPrice;
                    }
                    table.Write();
                    string orde;
                    Console.Write("Do you want create order ? (Y/N):");
                    orde = checkYN();
                    if (orde == "Y")
                    {
                        Console.Write("Enter your note: ");
                        string   note = Console.ReadLine();
                        DateTime date = DateTime.Now;
                        or.OrderDate  = date;
                        or.Status     = "Not yet";
                        or.Amount     = amount;
                        or.CustomerID = cusAll;
                        foreach (var item in itemsa)
                        {
                            or.Items = new List <Items>();
                            or.Items.Add(itBl.GetItemByItemID(or.ItemID));
                            // ordersbl.CreateOrder(or);
                        }
                        bool a = true;
                        try
                        {
                            a = ordersbl.CreateOrder(or);
                        }
                        catch (System.Exception)
                        {
                            a = false;
                            Console.WriteLine("\n ☹  Create order faild , press anykey to continue !\n");
                            Console.ReadKey();
                            break;
                        }
                        if (a == true)
                        {
                            Console.WriteLine("Create order success ! ");
                            try
                            {
                                // Check if file exists with its full path
                                if (File.Exists(Path.Combine("shoppingcart" + cusAll.UserName + ".dat")))
                                {
                                    // If file found, delete it
                                    File.Delete(Path.Combine("shoppingcart" + cusAll.UserName + ".dat"));
                                }
                                else
                                {
                                    Console.WriteLine("Cart not found");
                                }
                            }
                            catch (IOException ioExp)
                            {
                                Console.WriteLine(ioExp.Message);
                            }
                        }
                        else
                        {
                            Console.WriteLine("\n ☹  Create order faild , press anykey to continue !\n");
                            Console.ReadKey();
                            break;
                        }
                    }
                    else
                    {
                        break;
                    }
                }
                else
                {
                    Console.WriteLine("\nNo shopping cart yet , press anykey to continue !\n");
                    Console.ReadKey();
                    break;
                }
            }
        }
예제 #2
0
        public void showItemDetail()
        {
            while (true)
            {
                string row = ("==================================================================");


                ItemsBL      itBL    = new ItemsBL();
                Items        it      = new Items();
                List <Items> li      = new List <Items>();
                Customers    custom  = new Customers();
                CustomersBL  cutomBL = new CustomersBL();

                string choice;
                int    itID;
                try
                {
                    li = itBL.getItemsByItemID(1);
                    Console.Write("Enter item id: ");
                    itID = int.Parse(Console.ReadLine());
                }
                catch (System.Exception)
                {
                    Console.WriteLine("Item id must be integer and in the options !");
                    continue;
                }

                if (itID > li.Count || validateChoice(itID.ToString()) == false)
                {
                    Console.Write("You are only entered in the number of existing ids !");

                    while (true)
                    {
                        Console.Write("Do  you want re-enter ? (Y/N): ");
                        choice = Console.ReadLine().ToUpper();
                        if (choice != "Y" && choice != "N")
                        {
                            Console.Write("You can only enter  (Y/N): ");
                            choice = Console.ReadLine().ToUpper();
                            continue;
                        }
                        break;
                    }

                    switch (choice)
                    {
                    case "Y":
                        continue;

                    case "N":
                        showItems();
                        break;

                    default:
                        continue;
                    }
                }


                try
                {
                    it = itBL.GetItemByItemID(itID);
                }
                catch (System.Exception)
                {
                    Console.WriteLine("Disconnect from database !");
                }
                if (it == null)
                {
                    Console.WriteLine("The item does not exist !");
                }
                else
                {
                    Console.WriteLine(row);
                    Console.WriteLine("DETAIL OF ITEM");
                    Console.WriteLine(row);
                    var table = new ConsoleTable("ID", "ITEM NAME", "ITEM PRICE", "SIZE");

                    table.AddRow(it.ItemID, it.ItemName, it.ItemPrice, it.Size);

                    table.Write();
                    Console.WriteLine("DESCRIPTION : ");
                    Console.WriteLine(it.ItemDescription);
                }
                string select;
                Console.WriteLine("\n" + row + "\n");
                Console.WriteLine("1. Add to cart");
                Console.WriteLine("2. Back to Menu");
                Console.Write("Enter your selection: ");
                select = Console.ReadLine();

                switch (select)
                {
                case "1":
                    AddToCart(it);
                    break;

                case "2":
                    showItems();
                    break;

                default:
                    Console.WriteLine("You are only entered in the number existing !");
                    continue;
                }
                string conti;
                Console.Write("Do you continue ? (Y/N): ");
                conti = checkYN();
                if (conti == "Y")
                {
                    showItems();
                }
                else
                {
                    break;
                }
            }
        }