5 Replies - 761 Views - Last Post: 16 July 2012 - 09:21 PM Rate Topic: -----

#1 Niceman99  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 07-July 12

login from text file username-password

Posted 16 July 2012 - 07:49 PM

i have done lot of reading
and this is way harder than expected
im trying to load the program from a set of predefined usernames and passwords
and it just wont work.
here is my login form

<<<<<------->>>>>>>>
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ATM2LIBRARY;
using System.IO;
using System.Collections;


namespace atm
{
    public partial class Login : Form
    {
      public ATMManager Sim = new ATMManager();
     
         int login_unsuccessful = 3;


        public Login()
        {
            InitializeComponent();
        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }


        private void button2_Click(object sender, EventArgs e)
        {
            for (int x = 0; x < allcustomers.count; x++)
            {
                if (allcustomers[x].name == customername)
                {




                    if (textBox1.Text == "" && textBox2.Text == "")

                    //if (textBox1.Text == "CGS" && textBox2.Text == "admin")
                    {
                        MainAtm main = new MainAtm();
                        main.Visible = true;
                        main.Activate();
                        textBox1.Clear();
                        textBox2.Clear();
                        this.Hide();




                    }

                    else
                    {
                        if (textBox1.Text == "Korben Dallas" && textBox2.Text == "D001")
                        {
                            Supervisor supervisorwindow = new Supervisor();
                            supervisorwindow.Visible = true;
                            supervisorwindow.Activate();
                            textBox1.Clear();
                            textBox2.Clear();
                            this.Hide();
                        }

                        else
                        {
                            login_unsuccessful--;
                            if (login_unsuccessful <= 0)
                            {
                                MessageBox.Show("0 times left! App will exit,Please try again later!");
                                Application.Exit();
                            }
                            else
                            {

                                MessageBox.Show(login_unsuccessful.ToString() + " Times Left ");
                                textBox1.Clear();
                                textBox2.Clear();
                            }


                        }
                    }

                }
            }
        }
        private void btnLogin_Click(object sender, EventArgs e)
        {

        }

            private void textBox1_TextChanged(object sender, EventArgs e)
            {

            }

            private void textBox2_TextChanged(object sender, EventArgs e)
            {

            }

            private void Login_Load(object sender, EventArgs e)
            {
                string strLine;
                string[] strArray;
                char[] charArray = new char[] { ',' };





                try
                {
                    FileStream aFile = new FileStream("customers.txt", FileMode.Open);
                    StreamReader sr = new StreamReader(aFile);

                    strLine = sr.ReadLine();
                    while (strLine != null)
                    {

                        strArray = strLine.Split(charArray);
                        Sim.addcustomer(strArray[1], strArray[2]);

                        strLine = sr.ReadLine();
                    }
                    sr.Close();
                    // return ;

                }
                catch (IOException ex)
                {

                    Console.WriteLine(ex.ToString());
                    Console.ReadLine();



                    return;

                }
            }

            public object customername { get; set; }
    }

        }
    


this gives me error at ------private void button2_Click(object sender, EventArgs e)
{
for (int x = 0; x < allcustomers.count; x++)
{
if (allcustomers[x].name == customername)
{

even though is created just fine at ATMMANAGER
any input is much apreciated

here its ATMMANAGER
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ATM2LIBRARY
{
    public class ATMManager
    {
         private   string bank;
         private   string customers;
         private   string chequingaccounts;
         private   string savingaccounts;
         private   double currentaccountbalance;
         private   double dailybalance;
         private   double AccountBalance;
         private   int   amount;
         private   string pin;
    

           // Bank banc = new Bank();

           Customers allcustomers = new Customers();

          ChequinAccounts chequingacc = new ChequinAccounts();

          SavingAccounts savingacc = new SavingAccounts();



          Account allaccounts = new Account();
        
          DailyBalance dailyBalance = new DailyBalance();

          DailyBalances dailybalances = new DailyBalances();
          

    
        //CheckDailyBalances checkdailybalances = new CheckDailyBalances


       
        private bool ValidateUser()
 {
                string strLine;
            string[] strArray;
            char[] charArray = new char[] { ',' };

            
        
            try
            {
                FileStream aFile = new FileStream("Customers.txt", FileMode.Open);
                StreamReader sr = new StreamReader(aFile);

                strLine = sr.ReadLine();
                while (strLine != null)
                {
                    strArray = strLine.Split(charArray);
                    addcustomer(strArray[0], strArray[1]);

                    strLine = sr.ReadLine();
                }
                sr.Close();
                return true;

            }
            catch (IOException ex)
            {

                Console.WriteLine(ex.ToString());
                Console.ReadLine();



                return false;
            }

        }

       

            // new line


            //**************************************************************************************************************
            public bool writecustomers()
            {
                //NEW
                bool write = false;
            
                FileStream Writecustomers = new FileStream("Customers.txt", FileMode.Create);
                StreamWriter sw = new StreamWriter(Writecustomers);

            

                for (int i = 0; i < allcustomers.Count; i++)
                {
                    sw.WriteLine(allcustomers[i].Name  + allcustomers[i].PinNumber);
                    write = true;
                }
                sw.Close();
                return write;
            }

         public void addcustomer ( string N,string P)
        {
            Customer customer = new Customer(N, P);
            allcustomers.Add(customer);
        }
 
            //***********************************************************************************************************************************
          public bool readcustomers()
            {

                //NEW
                string strLine;
                string[] strArray;
                char[] charArray = new char[] { ',' };



                try
                {
                    FileStream aFile = new FileStream("customers.txt", FileMode.Open);
                    StreamReader sr = new StreamReader(aFile);

                    strLine = sr.ReadLine();
                    while (strLine != null)
                    {
                        strArray = strLine.Split(charArray);
                        addcustomer(strArray[1], strArray[2]);

                        strLine = sr.ReadLine();
                    }
                    sr.Close();
                    return true;
                }
                catch (IOException ex)
                {

                    Console.WriteLine(ex.ToString());
                    Console.ReadLine();



                    return false;
                }
          }

        //  private void icustomers(string p, string p_2, string p_3)
        //  {
        //      throw new NotImplementedException();
         // }
    
      //  public bool WithdrawChequing(string pin,int amount)
        //{

      //  }


     //   public bool WithdrawSavings()
      //  {
       // }



          public bool DepositChequing(string pin,int amount)
          {
              //if (pin = "12345")
              if (amount <= 0)
                  return false;
          
                  AccountBalance += amount;
                  return true;

                  
                      bool write = false;

                      FileStream DepositChequing = new FileStream("Accounts.txt", FileMode.Open);
                      StreamWriter sw = new StreamWriter(DepositChequing);



                      for (int i = 0; i < Convert.ToDouble(allaccounts.ToString()); i++)
                      {
                          sw.WriteLine(chequingacc[i].AccBal);
                          write = true;
                      }
                      sw.Close();

                      return write;


                  }

              
          
//new
                public bool DepositSavings()
                {
                    if (amount <= 0)
                    return false;
                AccountBalance += amount;
                return true;
            

        //NEW
            bool write = false;

            FileStream DepositSavings = new FileStream("Accounts.txt", FileMode.Create);
            StreamWriter sw = new StreamWriter(DepositSavings);

            
            
            for (int i = 0; i < Convert.ToDouble(allaccounts.ToString());i++)
            {
                sw.WriteLine(savingacc[i].AccountBalance);
                write = true;
            }
            sw.Close(); 
            return write;
}

            
                public bool GetBalance()
                {
                    
                    return Convert.ToBoolean(AccountBalance);
                }
                public void SetBalance(double aBalance)
                {
                    AccountBalance = aBalance;
                }
               // public bool account(string iname,string ipin)
              //  {
              //  }


      //  public bool PayBillPayment()
       // {
      //  }

      //  public bool TransferFunds()
       // {
       // }



        //******************************************************************************************************************
      //  public void cuentas(int pinnumb, int D, double E, double F,double G )
      //  {
      //    Account allaccounts = new Account();
      //    allaccounts.add(allaccount);
      //    }

        // ******************************************************************************************************

        //    public bool writeAccounts()
        // {
        //     //NEW
        //     bool write = false;

        //     FileStream writeAccounts = new FileStream("Accounts.txt", FileMode.Create);
        //      StreamWriter sw = new StreamWriter(writeAccounts);



        //   for (int i = 0; i < ChequingAccounts.Count; i++)
        //   {
        //       sw.WriteLine([i].+ "," + chequingaccounts[i].FirstName + "," + allaccounts[i].LastName + "," + allaccounts[i].Commission);
        //       write = true;
        //   }
        //   sw.Close();
        //   return write;
        //  }


        // ***********************************************************************************************************************************8

       /* public bool ReadDailyBalances()

         {
                //NEW
                bool write = false;

                FileStream Writecustomers = new FileStream("DailyBalances.txt", FileMode.Create);
                StreamWriter sw = new StreamWriter(Writecustomers);



                for (int i = 0; i < allcustomers.Count; i++)
                {
                    sw.WriteLine(allcustomers[i].Name  + allcustomers[i].PinNumber);
                    write = true;
                }
                sw.Close();
              //  return write;
          //  }*/
  
  //  public struct dailybalance (string x,string y)
//{
    //DailyBalances ibank = new DailyBalances(string x,string y);
   // dailybalances.

//}

      /* public bool CheckDailyBalances()
      

               {
           string strLine;
            string[] strArray;
            char[] charArray = new char[] { ',' };

            

            try
            {
                FileStream aFile = new FileStream("DailyBalances.txt", FileMode.Open);
                StreamReader sr = new StreamReader(aFile);

                strLine = sr.ReadLine();
                while (strLine != null)
                {
                    strArray = strLine.Split(charArray);
                    addcurator(strArray[0], strArray[1], strArray[2], Convert.ToDouble(strArray[3]));

                    strLine = sr.ReadLine();
                }
                sr.Close();
                return true;

            }
            catch (IOException ex)
            {

                Console.WriteLine(ex.ToString());
                Console.ReadLine();



                return false;
            }

        }

       

        }
        */
       // public bool AddDailyBalance()
        //{
//}


        /*  public bool writeDailyBalances()
           {
             //NEW
             bool write = false;

             FileStream writeDailyBalance = new FileStream("DailyBalances.txt", FileMode.Create);
             StreamWriter sw = new StreamWriter(writeDailyBalance);



             for (int i = 0; i < allacounts.Count; i++)
             {
                 sw.WriteLine(allaccounts[i].CuratorID + "," + allaccounts[i].FirstName + "," + allaccounts[i].LastName + "," + allaccounts[i].Commission);
                 write = true;
             }
             sw.Close();
             return write;
             }
             */

}
}



Is This A Good Question/Topic? 0
  • +

Replies To: login from text file username-password

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3450
  • View blog
  • Posts: 10,649
  • Joined: 05-May 12

Re: login from text file username-password

Posted 16 July 2012 - 08:10 PM

allcustomers is a private member variable of ATMManager. Of course, you don't have access to it.

It's like expecting the Car to have access to the CD in the CDPlayer:
class CDPlayer
{
    CompactDisc CD = new CompactDisc("Can't Touch This");

    void Play()
    {
        foreach(var track in CD.Tracks)
           track.Play();
    }
}

class Car
{
    CDPlayer player = new CDPlayer();

    void PlayTunes()
    {
        player.Play();
    }

    void PeekAtCD()
    {
        // This won't compile.
        CompactDisc currentCD = player.CD;
    }
}


This post has been edited by Skydiver: 16 July 2012 - 08:16 PM

Was This Post Helpful? 0
  • +
  • -

#3 Niceman99  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 07-July 12

Re: login from text file username-password

Posted 16 July 2012 - 08:17 PM

View PostSkydiver, on 16 July 2012 - 08:10 PM, said:

allcustomers is a private member variable of ATMManager. Of course, you don't have access to it.

i dont see where its private
i modify public VALIDATE USER and still getting Error "The name 'allcustomers' does not exist in the current context"
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3450
  • View blog
  • Posts: 10,649
  • Joined: 05-May 12

Re: login from text file username-password

Posted 16 July 2012 - 08:43 PM

If you don't declare a member variable or method as private it defaults to private.

Let's try a different approach... allcustomers is a member variable inside your ATMManager class. You have code in your Login class' click handler. How do you expect the code in the handler to have access to the allcustomers member variable when it is in a completely different class.

class Foo
{
    private int zebra;
}

class Bar
{
    Foo foo;

    void CanNotGetToZebra()
    {
        // This will not compile
        zebra = 21;
    }
}


This post has been edited by Skydiver: 16 July 2012 - 08:45 PM

Was This Post Helpful? 0
  • +
  • -

#5 Niceman99  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 07-July 12

Re: login from text file username-password

Posted 16 July 2012 - 08:59 PM

i understand the classes being differents
but i still dont see it
i have modified all variables from private to public
and nothing..
have changed alot and still nothing
i took it as exemple from another project wich compiles just fine
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3450
  • View blog
  • Posts: 10,649
  • Joined: 05-May 12

Re: login from text file username-password

Posted 16 July 2012 - 09:21 PM

Following with the previous example, here's how to fix the problem:
class Foo
{
    public int zebra;
}

class Bar
{
    Foo foo = new Foo();

    void TouchFoosZebra()
    {
        // This will compile, but the jury is still out on
        // whether encapsulation rules have been broken or not.
        foo.zebra = 21;
    }
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1