Advertisement
Guest User

Untitled

a guest
Apr 18th, 2015
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.82 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3.  
  4. namespace App1
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             Console.WriteLine("Login as: \n\n1) User \n2) Admin \n3) Exit");
  11.             Account user;
  12.             int choice = int.Parse(Console.ReadLine());
  13.             switch (choice)
  14.             {
  15.                 case 1:
  16.                     {
  17.                         user = new User();
  18.                         user.LoginAs();
  19.                         break;
  20.                     }
  21.                 case 2:
  22.                     {
  23.                         user = new Admin();
  24.                         user.LoginAs();
  25.                         break;
  26.                     }
  27.                 case 3: break;
  28.                 default: break;
  29.             }
  30.  
  31.         }
  32.     }
  33.     abstract class Account
  34.     {
  35.         public abstract void LoginAs();
  36.         public abstract void ShowAccount();
  37.         public abstract void ChangeAccount();
  38.         public abstract void DeleteAccount();
  39.         protected void Menu()
  40.         {
  41.             Console.WriteLine("1) Show account info \n" +
  42.             "2) Change account password \n3) Delete account \n4) Exit");
  43.             ChoiceItemMenu();
  44.         }
  45.         private void ChoiceItemMenu()
  46.         {
  47.             int choice = int.Parse(Console.ReadLine());
  48.             switch (choice)
  49.             {
  50.                 case 1:ShowAccount(); break;
  51.                 case 2:ChangeAccount(); break;
  52.                 case 3: DeleteAccount(); break;
  53.                 case 4: break;
  54.                 default: break;
  55.             }
  56.         }
  57.     }
  58.     class User : Account
  59.     {
  60.         public override void LoginAs()
  61.         {
  62.             Menu();
  63.         }
  64.         public override void ShowAccount()
  65.         {
  66.             Console.WriteLine("Normal User - No Password");
  67.         }
  68.         public override void ChangeAccount()
  69.         {
  70.             ForChangeAndDelete();
  71.         }
  72.         public override void DeleteAccount()
  73.         {
  74.             ForChangeAndDelete();
  75.         }
  76.         public void ForChangeAndDelete()
  77.         {
  78.             Console.WriteLine( "This action can't be performed on simple account.");
  79.         }
  80.     }
  81.     class Admin : Account
  82.     {
  83.         public override void LoginAs()
  84.         {
  85.             if (CheckPassword())
  86.                 Menu();
  87.         }
  88.         public bool CheckPassword()
  89.         {
  90.             while (true)
  91.             {
  92.                 Console.Write("Enter your password please: ");
  93.                 string password = Console.ReadLine();
  94.  
  95.                 string basePassword;
  96.                 using (StreamReader reader = new StreamReader("d:\\password.txt"))
  97.                 {
  98.                     basePassword = reader.ReadLine();
  99.                 }
  100.                 if (password.ToLower() == basePassword)
  101.                     return true;
  102.             }
  103.             return false;
  104.         }
  105.         public override void ShowAccount()
  106.         {
  107.             Console.WriteLine("Administrator - High Privilegies - Password Set");
  108.         }
  109.         public override void ChangeAccount()
  110.         {
  111.             if (CheckPassword())
  112.             {
  113.                 Console.WriteLine("Enter new password please: ");
  114.                 string newPassword = Console.ReadLine();
  115.                 using (StreamWriter sWriter = new StreamWriter("D:\\password.txt"))
  116.                 {
  117.                     sWriter.WriteLine(newPassword);
  118.                 }
  119.             }
  120.             Console.WriteLine("New password create");
  121.         }
  122.         public override void DeleteAccount()
  123.         {
  124.             if (CheckPassword())
  125.             {
  126.                 FileInfo fileInf = new FileInfo("D:\\password.txt");
  127.                 fileInf.Delete();
  128.             }
  129.             Console.WriteLine("Account removed ");
  130.         }
  131.     }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement