Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- namespace App1
- {
- class Program
- {
- static void Main(string[] args)
- {
- Console.WriteLine("Login as: \n\n1) User \n2) Admin \n3) Exit");
- Account user;
- int choice = int.Parse(Console.ReadLine());
- switch (choice)
- {
- case 1:
- {
- user = new User();
- user.LoginAs();
- break;
- }
- case 2:
- {
- user = new Admin();
- user.LoginAs();
- break;
- }
- case 3: break;
- default: break;
- }
- }
- }
- abstract class Account
- {
- public abstract void LoginAs();
- public abstract void ShowAccount();
- public abstract void ChangeAccount();
- public abstract void DeleteAccount();
- protected void Menu()
- {
- Console.WriteLine("1) Show account info \n" +
- "2) Change account password \n3) Delete account \n4) Exit");
- ChoiceItemMenu();
- }
- private void ChoiceItemMenu()
- {
- int choice = int.Parse(Console.ReadLine());
- switch (choice)
- {
- case 1:ShowAccount(); break;
- case 2:ChangeAccount(); break;
- case 3: DeleteAccount(); break;
- case 4: break;
- default: break;
- }
- }
- }
- class User : Account
- {
- public override void LoginAs()
- {
- Menu();
- }
- public override void ShowAccount()
- {
- Console.WriteLine("Normal User - No Password");
- }
- public override void ChangeAccount()
- {
- ForChangeAndDelete();
- }
- public override void DeleteAccount()
- {
- ForChangeAndDelete();
- }
- public void ForChangeAndDelete()
- {
- Console.WriteLine( "This action can't be performed on simple account.");
- }
- }
- class Admin : Account
- {
- public override void LoginAs()
- {
- if (CheckPassword())
- Menu();
- }
- public bool CheckPassword()
- {
- while (true)
- {
- Console.Write("Enter your password please: ");
- string password = Console.ReadLine();
- string basePassword;
- using (StreamReader reader = new StreamReader("d:\\password.txt"))
- {
- basePassword = reader.ReadLine();
- }
- if (password.ToLower() == basePassword)
- return true;
- }
- return false;
- }
- public override void ShowAccount()
- {
- Console.WriteLine("Administrator - High Privilegies - Password Set");
- }
- public override void ChangeAccount()
- {
- if (CheckPassword())
- {
- Console.WriteLine("Enter new password please: ");
- string newPassword = Console.ReadLine();
- using (StreamWriter sWriter = new StreamWriter("D:\\password.txt"))
- {
- sWriter.WriteLine(newPassword);
- }
- }
- Console.WriteLine("New password create");
- }
- public override void DeleteAccount()
- {
- if (CheckPassword())
- {
- FileInfo fileInf = new FileInfo("D:\\password.txt");
- fileInf.Delete();
- }
- Console.WriteLine("Account removed ");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement