Beispiel #1
0
        static void Main(string[] args)
        {
            var ctx = new BankContext();

            //ctx.Database.Initialize(true);

            //Deposit some money in SavingAccount
            //DepositMoneySaving(ctx);

            //Withdraw some money from SavingAccount
            //WithdrawMoneySaving(ctx);

            //Add interest rate to SavingAccount
            //AddInterest(ctx);


            //Deposit some money in CheckingAccount
            //DepositMoneyChecking(ctx);


            //Withdraw some money from CheckingAccount
            //WithdrawMoneyChecking(ctx);

            //Deduct fee on CheckingAccount
            //DeductFee(ctx);

            //Bank Console
            //BankConsole(ctx);
            ctx.SaveChanges();
        }
Beispiel #2
0
        public static string WithdrawMethod(CashMachine machine, Person person, int money)
        {
            if (person.Purse.Money >= money)
            {
                if (machine.Money >= money)
                {
                    person.Purse.Money -= money;
                    machine.Money      -= money;

                    using (BankContext context = new BankContext())
                    {
                        context.Purses.Find(person.PurseId).Money   -= money;
                        context.CashMachines.Find(machine.Id).Money -= money;

                        History history = new History();
                        history.CashMachine = machine;
                        history.Person      = person;
                        history.Money       = money;
                        history.Time        = DateTime.Now;
                        context.Histories.Add(history);

                        context.SaveChanges();
                    }
                    return("Транзакция успешно завершена");
                }
                else
                {
                    return("К сожалению в банкомате не достаточно средств");
                }
            }
            else
            {
                return("К сожалению у вас не достаточно средств");
            }
        }
Beispiel #3
0
        public static string AddMoneyMethod(CashMachine machine, Person person, int money)
        {
            try
            {
                person.Purse.Money += money;

                using (BankContext context = new BankContext())
                {
                    context.Purses.Find(person.PurseId).Money   += money;
                    context.CashMachines.Find(machine.Id).Money += money;

                    History history = new History();
                    history.CashMachine = machine;
                    history.Person      = person;
                    history.Money       = money;
                    history.Time        = DateTime.Now;
                    context.Histories.Add(history);

                    context.SaveChanges();
                }
                return("Транзакция успешно завершена");
            }
            catch
            {
                return("Во время транзакции возникла ошибка");
            }
        }
        private void SignUpButtonClick(object sender, RoutedEventArgs e)
        {
            List <Person> people;

            using (BankContext context = new BankContext())
            {
                people = context.People.ToList();
            }

            bool isCorrect = true;

            for (int i = 0; i < people.Count; i++)
            {
                if (people[i].Login == loginTextBox.Text)
                {
                    isCorrect = false;
                }
            }
            if (fullNameTextBox.Text == "")
            {
                isCorrect = false;
                if (passwordTextBox.Text == "")
                {
                    isCorrect = false;
                }
            }

            if (isCorrect)
            {
                Person temp = new Person();
                temp.FullName = fullNameTextBox.Text;
                temp.Login    = loginTextBox.Text;
                temp.Password = passwordTextBox.Text;
                Purse purse = new Purse();
                purse.Money = 0;

                List <Purse> purses = new List <Purse>();
                using (BankContext context = new BankContext())
                {
                    context.Purses.Add(purse);
                    context.SaveChanges();
                    purses = context.Purses.ToList();
                }
                temp.Purse   = purses[purses.Count - 1];
                temp.PurseId = temp.Purse.Id;
                using (BankContext context = new BankContext())
                {
                    context.People.Add(temp);
                    context.SaveChanges();
                }

                MessageBox.Show("Регистрация прошла успешно!");
                _window.Content = new SigInPage(_window);
            }
            else
            {
                MessageBox.Show("Не все данные введены верно!");
            }
        }
        public MainWindow()
        {
            InitializeComponent();
            using (BankContext context = new BankContext())
            {
                context.CashMachines.ToList();
                CashMachine cashMachine = new CashMachine();
                cashMachine.Money  = 10000000;
                cashMachine.Addres = "Московская 25";
                context.CashMachines.Add(cashMachine);

                context.SaveChanges();
            }

            this.Content = new SigInPage(this);
        }
Beispiel #6
0
        private static void BankConsole(BankContext ctx)
        {
            Console.WriteLine("Hello, summoner! Welcome to Soft Uni Bank (SUB)");
            Console.WriteLine("-----------------------------------------------");
            Console.WriteLine("Type 'commands' to see the available commands!");
            Console.WriteLine("Type 'Quit' to quit the application!");
            Console.WriteLine("-----------------------------------------------");
            string loggedUsername     = "";
            string loggedUserPassword = "";
            var    command            = Console.ReadLine();

            while (command != "Quit")
            {
                var array = command.Split(new char[] { ' ', ',' }, StringSplitOptions.RemoveEmptyEntries).ToArray();
                if (command == "commands")
                {
                    Console.WriteLine("1. Register <username> <password> <email> - That command add new user to the database in case username, password and email are valid. Otherwise print appropriate message informing why the user cannot be registered");
                    Console.WriteLine("2. Login <username> <password> - - That command set the current logged in user if exists. Otherwise print appropriate message.");
                    Console.WriteLine("3. Logout - log out the user from the system. If there is no logged in user print appropriate message.");
                    Console.WriteLine("4. Add SavingAccount <initial balance> <interest rate> - add saving account to the currently logged in user. ");
                    Console.WriteLine("5. Add CheckingAccount <initial balance> <fee> - add checking account to the currently logged in user. ");
                    Console.WriteLine("6. ListAccounts – prints a list of overall information for all accounts of currently logged in user.");
                    Console.WriteLine("7. Deposit <Account number> <money> - adds money to the account with given number");
                    Console.WriteLine("8. Withdraw <Account number> <money> - subtracts money from the account with given number");
                    Console.WriteLine("9. DeductFee <Account number> - deduct the fee from the balance of the account with given number");
                    Console.WriteLine("10. AddInterest <Account number> - add interest to the balance of the account with given number");
                    Console.WriteLine("---------------------------------------------------------------------------------------------------------------------");
                }
                if (array[0] == "Register")
                {
                    var user = new User();
                    user.Username = array[1];
                    user.Password = array[2];
                    user.Email    = array[3];
                    ctx.Users.Add(user);
                    ctx.SaveChanges();
                    Console.WriteLine($"{user.Username} was registered.");
                }
                if (array[0] == "Login")
                {
                    var username    = array[1];
                    var password    = array[2];
                    var user        = ctx.Users.Where(u => u.Username == username).FirstOrDefault();
                    var listOfUsers = ctx.Users.ToList();
                    if (listOfUsers.Contains(user))
                    {
                        loggedUsername     = user.Username;
                        loggedUserPassword = user.Password;
                        Console.WriteLine("Succesfully logged in!");
                    }
                    else
                    {
                        Console.WriteLine("There is no such user!");
                    }
                }
                if (array[0] == "Logout")
                {
                    if (loggedUsername == string.Empty)
                    {
                        Console.WriteLine("Cannot log out. No user was logged in.");
                    }
                    else
                    {
                        Console.WriteLine($"{loggedUsername} succesfully logged out.");
                        loggedUsername     = "";
                        loggedUserPassword = "";
                    }
                }
                if (array[0] == "Add")
                {
                    if (loggedUsername == "")
                    {
                        Console.WriteLine("Please login first!");
                    }
                    else
                    {
                        if (array[1] == "SavingAccount")
                        {
                            var savingAccount = new SavingAccount();
                            savingAccount.Balance       = decimal.Parse(array[2]);
                            savingAccount.InterestRate  = float.Parse(array[3]);
                            savingAccount.AccountNumber = RandomString();
                            ctx.SavingAccounts.Add(savingAccount);
                            ctx.SaveChanges();
                            Console.WriteLine($"Succesfully added saving account with number: {savingAccount.AccountNumber}");
                        }
                        if (array[1] == "CheckingAccount")
                        {
                            var checkingAccount = new CheckingAccount();
                            checkingAccount.Balance       = decimal.Parse(array[2]);
                            checkingAccount.Fee           = decimal.Parse(array[3]);
                            checkingAccount.AccountNumber = RandomString();
                            ctx.CheckingAccounts.Add(checkingAccount);
                            ctx.SaveChanges();
                            Console.WriteLine($"Succesfully added checking account with number: {checkingAccount.AccountNumber}");
                        }
                    }
                }
                if (array[0] == "ListAccounts")
                {
                    var list1 = ctx.SavingAccounts.OrderBy(c => c.AccountNumber).ToList();
                    var list2 = ctx.CheckingAccounts.OrderBy(c => c.AccountNumber).ToList();
                    Console.WriteLine("Saving accounts:");
                    foreach (var sa in list1)
                    {
                        Console.WriteLine($"-- {sa.AccountNumber} {sa.Balance} ");
                    }
                    Console.WriteLine("Checking accounts:");
                    foreach (var ca in list2)
                    {
                        Console.WriteLine($"-- {ca.AccountNumber} {ca.Balance}");
                    }
                }
                if (array[0] == "Deposit" && loggedUsername != "")
                {
                    var accountNumber = array[1];
                    var sum           = decimal.Parse(array[2]);
                    var list1         = ctx.SavingAccounts.ToList();
                    var list2         = ctx.CheckingAccounts.ToList();
                    foreach (var item in list1)
                    {
                        if (item.AccountNumber == accountNumber && item.User.Username == loggedUsername)
                        {
                            item.DepositMoney(sum);
                            ctx.SaveChanges();
                            Console.WriteLine($"Succesfully added money to saving account: {item.AccountNumber}");
                            Console.WriteLine($"Balance: {item.Balance}$");
                        }
                    }
                    foreach (var item in list2)
                    {
                        if (item.AccountNumber == accountNumber && item.User.Username == loggedUsername)
                        {
                            item.DepositMoney(sum);
                            ctx.SaveChanges();
                            Console.WriteLine($"Succesfully added money to checking account: {item.AccountNumber}");
                            Console.WriteLine($"Balance: {item.Balance}$");
                        }
                    }
                }
                if (array[0] == "Withdraw" && loggedUsername != "")
                {
                    var accountNumber = array[1];
                    var sum           = decimal.Parse(array[2]);
                    var list1         = ctx.SavingAccounts.ToList();
                    var list2         = ctx.CheckingAccounts.ToList();
                    foreach (var item in list1)
                    {
                        if (item.AccountNumber == accountNumber && item.User.Username == loggedUsername)
                        {
                            item.WithdrawMoney(sum);
                            ctx.SaveChanges();
                            if (item.Balance > sum)
                            {
                                Console.WriteLine($"Succesfully withdrawed money from saving account: {item.AccountNumber}");
                                Console.WriteLine($"Balance: {item.Balance}$");
                            }
                        }
                    }
                    foreach (var item in list2)
                    {
                        if (item.AccountNumber == accountNumber && item.User.Username == loggedUsername)
                        {
                            item.WithdrawMoney(sum);
                            ctx.SaveChanges();
                            if (item.Balance > sum)
                            {
                                Console.WriteLine($"Succesfully withdrawed money from saving account: {item.AccountNumber}");
                                Console.WriteLine($"Balance: {item.Balance}$");
                            }
                        }
                    }
                }

                if (array[0] == "DeductFee" && loggedUsername != "")
                {
                    var accountNumber = array[1];
                    var list          = ctx.CheckingAccounts.ToList();
                    foreach (var item in list)
                    {
                        if (item.AccountNumber == accountNumber && item.User.Username == loggedUsername)
                        {
                            var fee = decimal.Parse(array[2]);
                            item.DeductFee(fee);
                            ctx.SaveChanges();
                            Console.WriteLine("Succesfully added new fee!");
                        }
                    }
                }
                if (array[0] == "AddInterest" && loggedUsername != "")
                {
                    var accountNumber = array[1];
                    var list          = ctx.SavingAccounts.ToList();
                    foreach (var item in list)
                    {
                        if (item.AccountNumber == accountNumber && item.User.Username == loggedUsername)
                        {
                            var interest = float.Parse(array[2]);
                            item.AddInterest(interest);
                            ctx.SaveChanges();
                            Console.WriteLine("Succesfully added new interest!");
                            Console.WriteLine($"Balance: {item.Balance}$");
                        }
                    }
                }
                command = Console.ReadLine();
            }
            Console.WriteLine("GG WP!");
        }