Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

so I have this code... and when I am in the signin method it takes me to the user class and login method... what I

so I have this code... and when I am in the signin method it takes me to the user class and login method...

what I want to happen is that when they go to the signin method and if the user does not exist that it take them back to the app class and the main menu...

right now the user class and login method just continues to loop if the user does not exist... how do I fix this???

here is my code:

using System; using ADF_2023_GartinMamie; namespace ADF_2023_GartinMamie { public class App { //fields private bool _loggedIn; //properties public static User CurrentUser { get; set; } public App() { //initialize and display the menu Menu.Init("Main Menu", "Create User", "Login", "About", "Exit" ); Menu.Display(); UI.Separator(); //call the selection method Selection(); } private void Selection() { if (_loggedIn) { int selection = Validation.RangeValidation(" Please select a Menu Option: ", 0,4); { switch (selection) { case 1: About(); break; case 2: CurrentUser.UserProfile(); Continue(); break; case 3: Logout(); break; case 4: Exit(); break; default: Console.WriteLine("Invalid selection."); Continue(); break; } } } else { int selection = Validation.RangeValidation(" Please select a Menu Option: ",0,4); { switch (selection) { case 1: User.CreateNewUser(); Continue(); break; case 2: SignIn(); break; case 3: About(); break; case 4: Exit(); break; default: Console.WriteLine("Invalid selection."); Continue(); break; } } } } private void SignIn() { Console.Clear(); UI.Header("Login"); CurrentUser = User.Login(); _loggedIn = true; Continue(); } private void Logout() { Console.Clear(); UI.Loading("Logging Out"); _loggedIn = false; Menu.Init("Main Menu", "Create User", "Login", "About", "Exit"); Continue(); } private void About() { Console.Clear(); UI.Header("About"); Console.WriteLine("This is the about section!"); Continue(); } private void Exit() { Console.Clear(); UI.Loading("Exiting"); } private void Continue() { UI.Separator(); Console.WriteLine(" Press any key to continue..."); Console.ReadKey(); Console.Clear(); Menu.Display(); UI.Separator(); Selection(); } } }

public static User Login() { User user = null; while (user == null) { Console.Clear(); //ask user to login with username and password //use header UI.Header("please login"); string username = Validation.StringValidation("[Username]:"); string password = Validation.StringValidation("[Password]:"); //create steamreader using (StreamReader sr = new StreamReader(_dataPath)) { //consume first two lines sr.ReadLine(); sr.ReadLine(); //read all lines from the file string[] lines = File.ReadAllLines(_dataPath); //flow thru each line and check for matching username and password foreach (string line in lines) { string[] userInfo = line.Split(','); if (userInfo[0] == username && userInfo[1] == password) { //create new user object and return it user = new User(userInfo[0], Int32.Parse(userInfo[2]), userInfo[3], userInfo[4]); Menu.Init($"Welcome {username}!", "About", "User Profile", "Logout", "Exit"); } } if (user == null) { UI.Error("Login incorrect, please try again!"); Console.WriteLine("Press any key to continue..."); Console.ReadKey(); } } } return user; } // method to display user information public void UserProfile() { Console.Clear(); UI.Header("UserProfile"); Console.WriteLine($"Username: {Username}"); Console.WriteLine($"Age: {_age}"); Console.WriteLine($"City: {_city}"); Console.WriteLine($"State: {_state}"); } // method to register a new user public static void CreateNewUser() { Console.Clear(); UI.Header("Create New User"); string username = Validation.StringValidation("[Username]: "); string password = Validation.StringValidation("[Password]: "); int age = Validation.IntergerValidation("[Age]: "); string city = Validation.StringValidation("[City]:"); string state = Validation.StringValidation("[State]:"); // create new User object User newUser = new User(username, age, city, state); //save user to users.txt using (StreamWriter stream = File.AppendText(_dataPath)) { stream.WriteLine($"{username},{password}, {age},{city},{state}"); } } } }

using static System.Runtime.InteropServices.JavaScript.JSType; namespace ADF_2023_GartinMamie { public class Menu { //fields private static string _title; private static List _menuItems; //constructor public static void Init(params string[] menuData) { _title = menuData[0]; _menuItems = menuData.ToList(); } //display public static void Display() { UI.Header(_title); for (int i = 1; i<_menuItems.Count; i ++) { Console.ForegroundColor = UI.Colors["textAlt"]; Console.Write(" ["); Console.ForegroundColor = UI.Colors["text"]; Console.Write($"{i+0}"); Console.ForegroundColor = UI.Colors["textAlt"]; Console.Write("]:"); Console.ForegroundColor = UI.Colors["text"]; Console.Write($"{_menuItems[i]}"); } Console.WriteLine(); } } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Mobile Communications

Authors: Jochen Schiller

2nd edition

978-0321123817, 321123816, 978-8131724262

More Books

Students also viewed these Programming questions