Advertisement
Guest User

Challenge

a guest
Jul 30th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.41 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7.  
  8. namespace Challenge1
  9. {
  10.     class Program
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             Console.ForegroundColor = ConsoleColor.Green;
  15.             Console.WriteLine("Welcome! Please Login");
  16.             Login();
  17.             Console.ReadKey();
  18.         }
  19.         static void Login()
  20.         {
  21.             Console.WriteLine("Username:");
  22.             string username = Console.ReadLine();
  23.             Console.WriteLine("Password:");
  24.             string password = Console.ReadLine();
  25.             if (username == "admin" && password == "123")
  26.             {
  27.                 Console.WriteLine("Welcome back! " + username);
  28.                 Console.WriteLine("Check the available list of commands by typing 'commands'");
  29.                 string input = Console.ReadLine();
  30.                 switch (input)
  31.                 {
  32.                     default:
  33.                         Console.WriteLine("Oops! You entered a wrong command! Use 'commands' for help");
  34.                         input = Console.ReadLine();
  35.                         break;
  36.                     case "commands":
  37.                         Console.WriteLine("Available commands: adduser | addfile | showusers | showfiles | saveusers | savefiles | loadusers | loadfiles | exit");
  38.                         input = Console.ReadLine();
  39.                         break;
  40.                     case "adduser":
  41.                         Addusers();
  42.                         break;
  43.                     case "addfile":
  44.                         Addfiles();
  45.                         break;
  46.                     case "showusers":
  47.                         Showusers();
  48.                         break;
  49.                     case "showfiles":
  50.                         Showfiles();
  51.                         break;                    
  52.                     case "deleteusers":
  53.                         Deleteusers();
  54.                         break;
  55.                     case "deletefiles":
  56.                         //code
  57.                         break;                    
  58.                     case "exit":
  59.                         break;
  60.                 }
  61.             } else
  62.             {
  63.                 Console.WriteLine("You have entered a wrong Username or Password! Please try again");
  64.                 Login();
  65.             }
  66.  
  67.         }
  68.         static void Addusers()
  69.         {
  70.             Console.WriteLine("New Username:");
  71.             string username = Console.ReadLine();
  72.             Console.WriteLine("New Password for user " + username);
  73.             string password = Console.ReadLine();
  74.             string userdata = username + ";" + password;
  75.             FileStream userfile = File.OpenWrite(AppDomain.CurrentDomain.BaseDirectory + "/users.txt");
  76.             TextWriter writer = new StreamWriter(userfile);
  77.             writer.WriteLine(userdata);
  78.             writer.Flush();
  79.             writer.Close();
  80.             Console.WriteLine("User with Username: " + username + " and Password: " + password + " has been added Successfully!");
  81.         }
  82.         static void Addfiles()
  83.         {
  84.             Console.WriteLine("New FIle Name:");
  85.             string filename = Console.ReadLine();
  86.             Console.WriteLine("Contents of the new file named: " + filename);
  87.             string contents = Console.ReadLine();
  88.             string userdata = filename + ";" + contents;
  89.             FileStream userfile = File.OpenWrite(AppDomain.CurrentDomain.BaseDirectory + "/files.txt");
  90.             TextWriter writer = new StreamWriter(userfile);
  91.             writer.WriteLine(userdata);
  92.             writer.Flush();
  93.             writer.Close();
  94.             Console.WriteLine("File with Name: " + filename + " and Contents: " + contents + " has been added Successfully!");
  95.         }
  96.         static void Showusers() // Don't know how to make it loop not just read 1st line
  97.         {            
  98.             Console.WriteLine("Opening users.txt Please wait...");
  99.             Console.WriteLine("Users in Database:");
  100.             FileStream userfile = File.OpenRead(AppDomain.CurrentDomain.BaseDirectory + "/users.txt");
  101.             TextReader reader = new StreamReader(userfile);
  102.             string userdata = reader.ReadLine();
  103.             reader.Close();
  104.             Console.WriteLine(userdata);
  105.         }
  106.         static void Showfiles() // Don't know how to make it loop not just read 1st line
  107.         {
  108.             Console.WriteLine("Opening files.txt Please wait...");
  109.             Console.WriteLine("Files in Database:");
  110.             FileStream userfile = File.OpenRead(AppDomain.CurrentDomain.BaseDirectory + "/files.txt");
  111.             TextReader reader = new StreamReader(userfile);
  112.             string userdata = reader.ReadLine();
  113.             reader.Close();
  114.             Console.WriteLine(userdata);
  115.         }
  116.         // Not working bellow!
  117.         static void Deleteusers()
  118.         {
  119.             Console.WriteLine("The following users are present in the file: ");
  120.             Showusers();
  121.             Console.WriteLine("Enter the Username to be deleted:");
  122.             string username = Console.ReadLine();
  123.             FileStream userfile = File.OpenWrite(AppDomain.CurrentDomain.BaseDirectory + "/users.txt");
  124.             TextWriter writer = new StreamWriter(userfile);
  125.             writer.Close();
  126.             // Missing code //
  127.         }
  128.     }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement