+92 332 4229 857 99ProjectIdeas@Gmail.com

ATM (C#.net)


The Automated Teller Machine (ATM)
Record Class contains the name , pincode and balance of one person.
addNewRecord() function adds the new users to arrayList then in file.
loadRecord() function load all the save records from file to arrayList for further usage.
saveRecord() function saving the data in file after users transaction (withdraw or deposit).
enterInATM() function is the main function which let you enter pincode and then further withdraw or deposit the money.

Code

public class Record
    {
        private string name;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        private int pinCode;

        public int PinCode
        {
            get { return pinCode; }
            set { pinCode = value; }
        }

        private double balance;

        public double Balance
        {
            get { return balance; }
            set { balance = value; }
        }

        // Default Constructor
        public Record()
        {
            this.name = String.Empty;
            this.pinCode = 0000;
            this.balance = 0.0;
        }

        // Parameterized Constructor
        public Record(string n, int pCode, double b)
        {
            this.name = n;
            this.pinCode = pCode;
            this.balance = b;
        }

        public void print()
        {
            Console.WriteLine("The Name Is : " + this.name
                            + " , PinCode Is : " + this.pinCode
                            + " , Balance Is : " + this.balance);
        }
    }

ArrayList recordList;
Record record;

// Add a new User Record first to ArrayList(recordList) and then in file
public void addNewRecord()
{
   recordList = new ArrayList();
   record = new Record();

   recordList.Add(new Record("osama", 1122, 30000)); // name , pincode , balance
   recordList.Add(new Record("saad", 2211, 20000));
   recordList.Add(new Record("bilal", 1234, 80000));
   recordList.Add(new Record("maryam", 4321, 10000));

   TextWriter tw = File.CreateText("data.txt");

   for (int i = 0; i < recordList.Count; i++)
   {
        // here getting every record from arrayList and in format
        // (Osama,1234,1999) saving to file.
        record = (Record)recordList[i];
        tw.WriteLine(record.Name + "," + record.PinCode + "," + record.Balance);
        tw.Flush();
   }
  tw.Close();
}
           
// Loads the all reocrds from file and add to ArrayList(recordList)
public void loadRecord()
{
   TextReader tr = File.OpenText("data.txt");
   recordList = new ArrayList();
           
   string name, line;
   int pCode;
   double balance;

   string[] data = null;

   line = tr.ReadLine();

   while (line != null) // reading a file line by line
   {
      data = line.Split(','); // splitting the each record at every ','
      name = data[0]; // after splitting at 1st index is name
      pCode = int.Parse(data[1]); // after splitting at 2nd index is pincode
      balance = double.Parse(data[2]); // after splitting at 3rd index is balance
      Record r = new Record(name, pCode, balance);
      recordList.Add(r); // adding Record object to arrayList
               
     line = tr.ReadLine();
  }
    tr.Close();
}

 // Saving the record to file.
public void saveRecord()
{
   TextWriter tw = File.CreateText("data.txt");
   for (int i = 0; i < recordList.Count; i++)
   {
       record = (Record)recordList[i];
       tw.WriteLine(record.Name + "," + record.PinCode + "," + record.Balance);
       tw.Flush();
   }
}
  
// The main ATM function
public void enterInAtm()
{
   record = new Record();
   loadRecord();
   Console.WriteLine("Enter Your PinCode : ");
   int pinCode = int.Parse(Console.ReadLine()); // Asks user to enter his/her Pincode

   for (int j = 0; j < recordList.Count; j++)
   {
       record = (Record)recordList[j];
        if (record.PinCode == pinCode) // if pincode is correct then
         {
            Console.WriteLine("Welcome , " + record.Name.ToUpper());
            Console.WriteLine("Press 1 to withdraw money");
            Console.WriteLine("Press 2 to deposit money");
            int option = int.Parse(Console.ReadLine()); // Asks user what to do (withraw or deposit)
                  
               switch (option)
               {
                   case 1: // if user input 1 then withdraw
                     Console.WriteLine("Enter The Amount To Withdraw : ");
                     double amount = double.Parse(Console.ReadLine());

                     if (amount > record.Balance)
                        Console.WriteLine("You dont have such amount");
                     else if (amount < 0)
                        Console.WriteLine("Withdraw amount cant be negative");
                     else
                        record.Balance = record.Balance - amount;

                   break;

                   case 2: // if user input 2 then deposit
                     Console.WriteLine("Enter The Amount To Deposit : ");
                     double deposit = double.Parse(Console.ReadLine());

                     if (deposit < 0)
                     Console.WriteLine("Deposit amount cant be negative");
                     else
                     record.Balance = record.Balance + deposit;

                  break;

              }                              
     saveRecord(); // saving the balance after user transactions(withdraw or deposit) back to file.
          }            
       }
    }
 tw.Close();
}

public static void Main(string[] args)
{
   // Front end menu
   Console.WriteLine("----------Menu----------");
   Console.WriteLine("Press 1 to enter in ATM");
   Console.WriteLine("Press 2 to exit");
           
   int input = int.Parse(Console.ReadLine()); // asks user to enter in atm or exit
           
   switch (input)
   {
      case 1:
          enterInAtm();
      break;
               
      case 2:
          Environment.Exit(0);
      break;             
   }
}

The record file is maintained in this format
osama,1122,27879
saad,2211,20000
bilal,1234,158999
maryam,4321,5000

Output of the program is
Press 1 to enter in ATM
Press 2 to exit
1
Enter your Pincode:
2211
Welcome , SAAD
Press 1 to withdraw money
Press 2 to deposit money
1
Enter amount to withdraw
2000

0 comments: