Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Challenge1
- {
- class Program
- {
- static Dictionary<string, User> users = new Dictionary<string, User>();
- static void Main(string[] args)
- {
- Loadusers();
- Login();
- }
- static void Login()
- {
- Console.WriteLine("Username:");
- string username = Console.ReadLine();
- Console.WriteLine("Password:");
- string password = Console.ReadLine();
- foreach(KeyValuePair<string, User> u in users)
- {
- if (username == u.Value.Username && password == u.Value.Password)
- {
- Console.WriteLine("Logged in as: " + username);
- Commands();
- }
- }
- Console.WriteLine("Wrong username or password entered.");
- Login();
- }
- static void Loadusers()
- {
- if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + "/users.txt"))
- {
- FileStream file = File.OpenRead(AppDomain.CurrentDomain.BaseDirectory + "/users.txt");
- TextReader reader = new StreamReader(file);
- int length = Int32.Parse(reader.ReadLine());
- users = new Dictionary<string, User>();
- for (int i=0; i<length; i++)
- {
- string line = reader.ReadLine();
- string[] parts = line.Split(';');
- users.Add(parts[0],new User() { Username =parts[0], Password=parts[1], });
- }
- reader.Close();
- } else
- {
- users.Add("admin", new User() { Username = "admin", Password = "123", });
- }
- }
- static void Saveusers()
- {
- if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + "/users.txt"))
- {
- File.Delete(AppDomain.CurrentDomain.BaseDirectory + "/users.txt");
- }
- FileStream file = File.OpenWrite(AppDomain.CurrentDomain.BaseDirectory + "/users.txt");
- TextWriter writer = new StreamWriter(file);
- writer.WriteLine(users.Count());
- foreach (KeyValuePair<string,User> u in users)
- {
- writer.WriteLine(u.Value.Username + ";" + u.Value.Password);
- }
- writer.Flush();
- writer.Close();
- }
- static void Commands()
- {
- string input = Console.ReadLine();
- switch(input)
- {
- default:
- Console.WriteLine("Wrong command!");
- Commands();
- break;
- case "commands":
- Console.WriteLine("adduser | showusers | deleteusers | saveusers | exit |");
- Commands();
- break;
- case "adduser":
- Console.WriteLine("Enter Username:");
- string username = Console.ReadLine();
- Console.WriteLine("Enter password:");
- string password = Console.ReadLine();
- users.Add(username,new User() { Username = username, Password = password, });
- Saveusers();
- Console.WriteLine("User " + username + " has been added!");
- Commands();
- break;
- case "showusers":
- Showusers();
- Commands();
- break;
- case "deleteuser":
- Console.WriteLine("Enter the Username to be deleted");
- Showusers();
- input = Console.ReadLine();
- if (users.ContainsKey(input))
- {
- users.Remove(input);
- Saveusers();
- Console.WriteLine("User with Username: " + input + " has been deleted.");
- } else
- {
- Console.WriteLine("Wrong username entered.");
- }
- Commands();
- break;
- case "exit":
- break;
- }
- }
- static void Showusers()
- {
- foreach (KeyValuePair <string,User> u in users)
- {
- Console.WriteLine("Username: " + u.Value.Username + " Password: " + u.Value.Password);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement