Advertisement
Guest User

Untitled

a guest
Jan 25th, 2016
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.55 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace BankÖvning
  8. {
  9.     class Bank
  10.     {
  11.         private List<Person> personList;
  12.         private Person loggedInUser;
  13.         private void Welcome()
  14.         {
  15.             Console.WriteLine("Welcome to the worlds best bank!\nTo logg in press [A]\nTo create new account press[B]");
  16.         }
  17.  
  18.         private bool LoggedIn()
  19.         {
  20.             return (loggedInUser != null);// om loggedInUser inte är null är man inloggad
  21.         }
  22.  
  23.         private void Login(String uName, String pass)
  24.         {
  25.             Person p = personList.Find(x => x.GetName() == uName);
  26.             if( p != null)
  27.             {
  28.                 if(p.Checkpass(pass))
  29.                 {
  30.                     loggedInUser = p;
  31.                 }
  32.                 else
  33.                 {
  34.                     Console.WriteLine("Felaktigt lösenord");
  35.                 }
  36.             }
  37.             else
  38.             {
  39.                 Console.WriteLine("Ogiltigt användarnamn");
  40.             }
  41.  
  42.         }
  43.  
  44.         private bool CreateUser()
  45.         {
  46.             Console.WriteLine("Enter username of your choice");
  47.             string uName = Console.ReadLine();
  48.             Console.WriteLine("Enter password of your choice");
  49.             string pass = Console.ReadLine();
  50.             if (null != personList.Find(x => x.GetName() == uName))
  51.             {
  52.                 Console.WriteLine("Username taken");
  53.                 return false;
  54.             }
  55.  
  56.             if(pass.Length < 3)
  57.             {
  58.                 Console.WriteLine("För kort lösenord");
  59.                 return false;
  60.             }
  61.             personList.Add(new Person(uName, pass));
  62.             Console.WriteLine("Användare skapad");
  63.             return true;
  64.         }
  65.  
  66.         private void Choice()
  67.         {
  68.             char uChoice = char.Parse(Console.ReadLine());
  69.             switch (uChoice)
  70.             {
  71.                 case 'A':
  72.                     Console.Clear();
  73.                     Console.WriteLine("Enter your username");
  74.                     string uName = Console.ReadLine();
  75.                     Console.WriteLine("Enter your password");
  76.                     string uPass = Console.ReadLine();
  77.                     Login(uName, uPass);
  78.                     break;
  79.                 case 'B':
  80.                     Console.Clear();
  81.                     while (!CreateUser()) ;
  82.                     break;
  83.             }
  84.         }
  85.         private void BankChoice()
  86.         {
  87.             // User meny för det olika valen
  88.             Console.WriteLine("To check your balance press [B]\n");
  89.             Console.WriteLine("To Deposit money press [D]\n");
  90.             Console.WriteLine("To Withdraw money press [W]\n");
  91.             Console.WriteLine("To quit the application press [Q]\n");
  92.             char bankChoise = char.Parse(Console.ReadLine());
  93.  
  94.             switch (bankChoise) // Kollar kontots summa +- deposits och withdraws.
  95.             {
  96.                 case 'B':
  97.                     Console.WriteLine("Your account balance is " + loggedInUser.GetBalance());
  98.                     break;
  99.                 case 'D':
  100.                     Console.WriteLine("How much money do you want to deposit?");
  101.                     int deposit = int.Parse(Console.ReadLine());
  102.                     loggedInUser.Deposit(deposit);
  103.                     Console.WriteLine("Your new balance is" + loggedInUser.GetBalance());
  104.                     break;
  105.                 case 'W':
  106.                     Console.WriteLine("How much do you want to withdraw?");
  107.                     int withdraw = int.Parse(Console.ReadLine());
  108.                     loggedInUser.Withdraw(withdraw);
  109.                     Console.WriteLine("Your new balance is" + loggedInUser.GetBalance());
  110.                     break;
  111.             }
  112.         }
  113.  
  114.         public Bank()
  115.         {
  116.             personList = new List<Person>();
  117.             loggedInUser = null;
  118.             personList.Add(new Person("sture", "1234")); // Lägger till en användare som redan ska finnas i banksystemet!
  119.             for (int i = 0; i < 100; i++) // lägger till 100 personer i listan för att testköra att listan fungerar.
  120.             {
  121.                 personList.Add(new Person("Person" + i, "" + i));
  122.             }
  123.         }
  124.  
  125.         public void Start()
  126.         {
  127.             while (!LoggedIn())
  128.             {
  129.                 Welcome();
  130.                 Choice();
  131.             }
  132.             while(true)  BankChoice();
  133.         }
  134.  
  135.     }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement