Example #1
0
        public void execute()
        {
            //委托起始的使用方法:
            //定义方法,定义同方法签名的委托,初始化委托实例,使用委托
            BuyBook buybook = new BuyBook(Book);

            buybook("委托初始使用方法");

            //使用Action内置委托
            Action <string> BookAction = new Action <string>(Book);

            BookAction("使用Action内置委托");
            //使用Func内置委托,含返回值
            Func <string, string> RetBook = new Func <string, string>(FuncBook);

            Console.WriteLine(RetBook("使用Func内置委托,含返回值"));

            //内置委托和匿名委托,同action
            Func <string, string> funcAnnoyDelegate = delegate(string str)
            {
                return("匿名委托定义的方法" + str);
            };

            Console.WriteLine(funcAnnoyDelegate("funcValue"));

            //内置委托和lamda表达式
            Func <string, string> funcLamda = (str) =>
            {
                return("匿名委托定义的方法" + str);
            };

            Console.WriteLine(funcLamda("funcLamda"));
        }
Example #2
0
        public int Buy(BuyBook buybook)
        {
            var bookToBuy = context.Books.FirstOrDefault(ww => ww.Title == buybook.BookName);

            if (buybook.UserId != null)
            {
                if (bookToBuy.NumberOfCopys > 0)
                {
                    Purchase purchase = new Purchase();
                    purchase.BookID       = bookToBuy.BookID;
                    purchase.UserID       = buybook.UserId;
                    purchase.PurchaseData = DateTime.Now;
                    context.Purchases.Add(purchase);
                    bookToBuy.NumberOfCopys--;
                    context.SaveChanges();
                    return(1);
                }
                else
                {
                    return(2);
                }
            }
            else
            {
                return(3);
            }
        }
Example #3
0
        static void test9()
        {
            //建立联系
            BuyBook buybook = new BuyBook(Book);

            //触发
            isdelegate = true;
            buybook();
        }
Example #4
0
        private void button1_Click(object sender, EventArgs e)
        {
            BuyBook buybook = new BuyBook(Book);

            buybook();
            BuyBook1 buybook1 = new BuyBook1(Book);

            buybook1("teasd");
        }
Example #5
0
        public void GetHashCodeTest()
        {
            ConstantDataFiller filler     = new ConstantDataFiller();
            DataRepository     repository = new DataRepository(filler.Fill(new DataContext()));

            BuyBook buyBookA = new BuyBook(repository.GetPublisher(0), repository.GetBookDetails(0), new DateTime(), 12);
            BuyBook buyBookB = new BuyBook(repository.GetPublisher(0), repository.GetBookDetails(0), new DateTime(), 12);

            Assert.AreEqual(buyBookA.GetHashCode(), buyBookB.GetHashCode());
        }
        public async Task BuyBookTest()
        {
            var book = new BuyBook
            {
                Username = "******",
                BookId   = "1"
            };

            var result = await _controller.BuyBook(book);

            Assert.IsTrue(result.Result);
        }
        /// <summary>
        /// Buy Book implementation
        /// </summary>
        /// <param name="data">Model BuyBok that is receiving</param>
        /// <returns></returns>
        internal async Task <ResultsResponse> BuyBookAction(BuyBook data)
        {
            return(await Task.Run(() =>
            {
                try
                {
                    using (var db = new StoreContext())
                    {
                        var book = db.Books.FirstOrDefault(m => m.BookId == Int32.Parse(data.BookId));
                        var user = db.Users.FirstOrDefault(m => m.Name == data.Username);
                        if (book.Price <= user.Amount)
                        {
                            var purchase = new Purchases
                            {
                                UserId = user.UserId,
                                BookId = book.BookId,
                                Price = book.Price
                            };

                            db.Purchases.Add(purchase);

                            var account = db.Users.Where(U => U.Name == data.Username).FirstOrDefault();
                            account.Amount = account.Amount - book.Price;
                            db.SaveChanges();

                            return new ResultsResponse
                            {
                                Result = true,
                                Message = "The book was bought successfully."
                            };
                        }
                        else
                        {
                            return new ResultsResponse
                            {
                                Result = false,
                                Message = "There is not enough money in your account. Please refill your account."
                            };
                        }
                    }
                }
                catch (Exception e)
                {
                    //Logging Implementation
                    return new ResultsResponse
                    {
                        Result = false,
                        Message = e.Message
                    };
                }
            }));
        }
 public BookViewModel(MainViewModel mainViewModel)
 {
     Books       = new ObservableCollection <BookEntity>();
     filials     = new List <FilialEntity>();
     BuyBookCMD  = new BuyBook(mainViewModel, this);
     RentCMD     = new Rent(mainViewModel, this);
     addBook     = new AddBook(this);
     DeleteBook  = new DeleteBook(this);
     UpdateBook  = new UpdateBook(this);
     currentbook = new BookEntity();
     selectbook  = new BookEntity();
     //if (File.Exists("Filials.json"))
     //{
     //    string jsonFilial = File.ReadAllText("Filials.json");
     //   this.filials = JsonConvert.DeserializeObject<List<FilialEntity>>(jsonFilial);
     //}
     //if (File.Exists("Books.json"))
     //{
     //    string jsonBook = File.ReadAllText("Books.json");
     //    this.Books = JsonConvert.DeserializeObject<ObservableCollection<BookEntity>>(jsonBook);
     //}
 }
Example #9
0
        public ActionResult BuyBook(int id)
        {
            BuyBook data = new BuyBook {
                login  = false,
                Money  = false,
                succes = false
            };
            UserView      user = new UserView();
            BookViewModel book = new BookViewModel();

            data.login = true;
            user       = UIHelper.GetUser(User.Identity.GetUserId());
            book       = UIHelper.Get1Book(id);
            double moneyUser = user.CIF;
            double price     = book.Price;

            if (moneyUser < price)
            {
                data.Money = true;
                return(Json(new { OK = false, Message = "Tài khoản không đủ xin vui lòng nạp thêm" }, JsonRequestBehavior.AllowGet));
            }
            else
            {
                data.succes = true;
                moneyUser  -= price;
                DBHelper.UpdateMoneyUser(moneyUser, User.Identity.GetUserId());
                BookOrderView a = new BookOrderView {
                    BookId            = id,
                    ApplicationUserId = User.Identity.GetUserId(),
                    BuyDate           = DateTime.Now
                };
                DBHelper.AddBookOrder(a);
            }

            return(Json(new { OK = true, Message = "Mua thành công" }, JsonRequestBehavior.AllowGet));
            //return
        }
Example #10
0
        public static void TestAllTwo()
        {
            BuyBook buyBook = new BuyBook(BookShop);

            buyBook();

            Action <string> bookActions = new Action <string>(BookShop);

            bookActions("程序");

            Action <string, string> bookActionsTwo = new Action <string, string>(BookShop);

            bookActionsTwo("程序", "长春");

            Func <string, string> func = new Func <string, string>(FuncBook);

            Console.WriteLine(func("程序"));


            // string FuncValue(string s) => $"{s} got";
            Func <string, string> funcValue = s => $"{s} got";

            Console.WriteLine(funcValue("程序"));
        }
Example #11
0
 public async Task <ResultsResponse> BuyBook(BuyBook data)
 {
     return(await user.BuyBook(data));
 }
Example #12
0
 public async Task <ResultsResponse> BuyBook(BuyBook data)
 {
     return(await BuyBookAction(data));
 }