コード例 #1
0
 /// <summary>
 /// Saves the customer to the database
 /// </summary>
 /// <param name="customer">The customer with changes to be saved</param>
 public void saveCustomer(lib.Customer customer)
 {
     try
     {
         using var context = new StoreDBContext(_options);
         var dataCustomer = context.Customers.Where(c => c.CustomerId == customer.CustomerId).FirstOrDefault();
         dataCustomer.Balance = customer.Balance;
         context.Update(dataCustomer);
         context.SaveChanges();
     }
     catch (Exception)
     {
         Console.WriteLine("Error saving customer's.");
     }
 }
コード例 #2
0
 public void createCustomer(lib.Customer customer)
 {
     if (customer.Balance < 0)
     {
         throw new ArgumentException("Customer balance cannot be less than zero.");
     }
     if (!customer.FirstName.All(char.IsLetter))
     {
         throw new ArgumentException("Customer name must only be a alphabet letter");
     }
     if (!customer.LastName.All(char.IsLetter))
     {
         throw new ArgumentException("Customer name must only be a alphabet letter");
     }
     _dataRepository.addCustomer(customer);
 }
コード例 #3
0
 /// <summary>
 /// Adds a new customer to the database
 /// </summary>
 /// <param name="customer">The newly created customer</param>
 public void addCustomer(lib.Customer customer)
 {
     try
     {
         var dataCustomer = new DataAccess.Customer();
         dataCustomer.Balance   = customer.Balance;
         dataCustomer.FirstName = customer.FirstName;
         dataCustomer.LastName  = customer.LastName;
         _context.Add(dataCustomer);
         _context.SaveChanges();
     }
     catch (Exception)
     {
         Console.WriteLine("Error saving customer.");
     }
 }
コード例 #4
0
        public void handleCustomer(bool isForeground)
        {
            bool handled = false;

            while (!handled)
            {
                try
                {
                    _outputter.printString("\n1 - Enter New Customer\n2 - Enter Current Customer\n3 - View All Customers\n4 - View Customer Orders" + (isForeground ? "\n5 - Back\n" : "\n"));
                    var input = _inputter.getNumber();
                    if (input == 1)
                    {
                        _outputter.printString("What is the customer's first name?");
                        var firstNameInput = _inputter.getString();
                        _outputter.printString("What is the customer's last name?");
                        var lastNameInput = _inputter.getString();
                        _outputter.printString("What is the customer's balance?");
                        var balanceInput = _inputter.getDouble();

                        _customer = new lib.Customer(_customers.Max((customer) => customer.CustomerId) + 1, firstNameInput, lastNameInput, balanceInput);
                        _storeRespository.addCustomer(_customer);
                        handled = true;
                    }
                    else if (input == 2)
                    {
                        _outputter.printString("What is the customer's id?");
                        var idInput = _inputter.getNumber();

                        _customer = _customers.Find((customer) => customer.CustomerId == idInput);
                        if (_customer != null)
                        {
                            handled = true;
                        }
                        else
                        {
                            _outputter.printString("Customer Id not found.");
                        }
                    }
                    else if (input == 3)
                    {
                        _outputter.printAllCustomers(_customers);
                    }
                    else if (input == 4)
                    {
                        if (_customer == null)
                        {
                            _outputter.printString("No Customer Selected.");
                            continue;
                        }
                        _outputter.printOrders(_storeRespository.getCustomerOrders(_customer.CustomerId));
                    }
                    else if (input == 5)
                    {
                        if (isForeground)
                        {
                            handled = true;
                        }
                    }
                    else
                    {
                        throw new InvalidOperationException("Invalid user input.");
                    }
                }
                catch (Exception)
                {
                    _outputter.printString("Invalid Input.");
                    continue;
                }
            }
        }