public bool TryAddUser(List <User> users, out User newUser) { PasswordWriter passwordWriter = new PasswordWriter(); newUser = new User(); string usLoginStr, usPsswdStr; Console.WriteLine("New user"); Console.WriteLine("Enter login: "******"Login is already taken!"); return(false); } } if (CheckUsername(usLoginStr)) { Console.WriteLine($"Enter the password (more{MIN_PSSWD_LEN} characters):"); usPsswdStr = passwordWriter.Write(); if (CheckPassword(usPsswdStr)) { newUser.Login = usLoginStr.Trim(); newUser.Password = usPsswdStr; return(true); } else { Console.WriteLine("Password is not long enough!"); } } else { Console.WriteLine("Логин пустой!"); } return(false); }
public void Run() { loginer = new Login(); users = new List <User>(); register = new Register(); passwordWriter = new PasswordWriter(); using (var context = new ShopContext()) { users = context.Users.ToList(); products = context.Products.ToList(); } while (true) { switch (MainMenu()) { case 1: { string loginStr, passwordStr; Console.WriteLine("Enter login: "******"Enter password: "******"Welcome!"); bool flag = true; while (flag) { switch (InnerMenu()) { case 1: { if (products.Count > 0) { for (int i = 0; i < products.Count; i++) { Console.WriteLine($"{i + 1})"); products[i].Print(); } Console.WriteLine("Enter Item Index: "); if (int.TryParse(Console.ReadLine(), out int buyIndex)) { if (buyIndex > 0 && buyIndex <= products.Count) { user.Cart.Products.Add(new Product() { Cost = products[buyIndex - 1].Cost, Name = products[buyIndex - 1].Name }); } } } break; } case 2: { if (user.Cart.Products.Count > 0) { foreach (var product in user.Cart.Products) { product.Print(); } } break; } case 3: { if (user.Cart.Products.Count > 0) { double sum = 0; foreach (var product in user.Cart.Products) { sum += product.Cost; product.Print(); } Console.WriteLine($"To pay: {sum}"); user.Cart.Products.Clear(); } break; } case 4: { flag = false; break; } } } context.SaveChanges(); } } else { Console.WriteLine("Invalid username or password!"); } break; } case 2: { if (register.TryAddUser(users, out User user)) { Console.WriteLine("Registration completed successfully."); user.Cart = new Cart(); users.Add(user); using (var context = new ShopContext()) { context.Users.Add(user); context.SaveChanges(); } } else { Console.WriteLine("Registration is aborted!"); } break; } case 3: { Environment.Exit(0); break; } } } }