Exemple #1
0
        internal bool AddNewPersonAccount(PersonAccount personAccount)
        {
            string sqlQuery = @"INSERT INTO PersonAccounts ([AccountNumber], [CustomerId]) 
                              VALUES 
                              (@AccountNumber, @CustomerId)";

            try
            {
                using (SqlConnection connection = new SqlConnection(App.ConnectionString))
                    using (SqlCommand command = new SqlCommand(sqlQuery, connection))
                    {
                        connection.Open();
                        command.Parameters.Add(new SqlParameter("AccountNumber", personAccount.AccountNumber));
                        command.Parameters.Add(new SqlParameter("CustomerId", personAccount.CustmerId));

                        command.ExecuteNonQuery();
                        return(true);
                    }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.ReadLine();
                throw new Exception(e.Message);
            }
        }
Exemple #2
0
        private void AddAccountToCustomer(Customer customer, Account account, AccountType accountType, int index)
        {
            if (customer == null)
            {
                Console.Write("Mata in ID:et på kunden du vill använda: ");
                if (int.TryParse(GreenInput(), out int result1) == true)
                {
                    customer = _dataAccess.GetCustomerById(result1);
                    if (customer == null)
                    {
                        WriteLine("Invalid input! ", ConsoleColor.Red);
                        AddAccountToCustomer(customer, new Account(), new AccountType(), 0);
                    }
                }
                Console.Clear();
            }

            WriteLine("Kund:\n", ConsoleColor.Yellow);
            ListCustomers(new List <Customer> {
                customer
            }, false);
            WriteLine("\nKonto:\n", ConsoleColor.Yellow);
            WriteLine($"Kontotyp: {accountType.AccountTypeName}", ConsoleColor.White);
            WriteLine($"Saldo:    {account.Balance}", ConsoleColor.White);
            WriteLine($"Ränta:    {accountType.Interest}", ConsoleColor.White);
            Console.WriteLine();
            //Kontotyp, ränta
            if (index == 0)
            {
                WriteLine($"Mata in ID:et på kontotypen det nya konto bör vara: \n", ConsoleColor.Yellow);
                ListAccountTypes(_dataAccess.GetAllAccountTypes(), false);
                string input = GreenInput();
                if (int.TryParse(input, out int accType) == true && _dataAccess.GetAccountTypeByAccountTypeId(accType) != null)
                {
                    accountType.AccountTypeName = _dataAccess.GetAccountTypeByAccountTypeId(accType).AccountTypeName;
                    accountType.Interest        = _dataAccess.GetAccountTypeByAccountTypeId(accType).Interest;
                    account.AccountTypeId       = _dataAccess.GetAccountTypeByAccountTypeId(accType).Id;
                    index++;
                    Console.Clear();
                    AddAccountToCustomer(customer, account, accountType, index);
                }
                else
                {
                    Console.Clear();
                    WriteLine("Invalid output", ConsoleColor.Red);
                    AddAccountToCustomer(customer, account, accountType, 0);
                }
            }
            //saldo
            if (index == 1)
            {
                Write($"Mata in saldo i det nya kontot: ", ConsoleColor.Yellow, 0);
                string inputBalance = GreenInput().Replace(",", ".");
                if (decimal.TryParse(inputBalance, out decimal balance) == true)
                {
                    account.Balance = balance;
                    index++;
                    Console.Clear();
                    AddAccountToCustomer(customer, account, accountType, index);
                }
                else
                {
                    Console.Clear();
                    WriteLine("Invalid output", ConsoleColor.Red);
                    AddAccountToCustomer(customer, account, accountType, 0);
                }
            }

            Console.WriteLine("Mata in 'save' för att spara kontot: ");
            Console.WriteLine("Mata in 'restart' för att ångra och börja om: ");
            Console.WriteLine("Mata in RETUR för att gå tillbaka utan att spara: ");

            string input2 = GreenInput();

            if (input2.ToUpper() == "SAVE")
            {
                PersonAccount personAccount = new PersonAccount();
                _dataAccess.AddNewAccount(account);

                personAccount.CustmerId     = customer.Id;
                personAccount.AccountNumber = _dataAccess.GetLatestAccountId();

                _dataAccess.AddNewPersonAccount(personAccount);
            }
            if (input2.ToUpper() == "RESTART")
            {
                AddAccountToCustomer(customer, account, accountType, 0);
            }

            Run();
        }