Example #1
0
        // Loads the User Data from a data file
        public void LoadUserList()
        {
            if (!File.Exists(_userDataFile))
            {
                // throw "file does not exist error"
                return;
            }

            StreamReader fin = new StreamReader(_userDataFile);

            while (!fin.EndOfStream)
            {
                string tempBuffer = fin.ReadLine();

                if (tempBuffer.Equals("$START USERLIST$"))
                {
                    tempBuffer = fin.ReadLine();

                    while (!tempBuffer.Equals("$END USERLIST$"))
                    {
                        // Read data
                        tempBuffer = fin.ReadLine();

                        // check if end of user list
                        if (tempBuffer.Equals("$END USERLIST$"))
                        {
                            break;
                        }

                        if (tempBuffer.Equals("$$START USER$$"))
                        {
                            tempBuffer = fin.ReadLine();
                            while (!tempBuffer.Equals("$$END USER$$"))
                            {
                                // Read user data
                                string[] userToken = tempBuffer.Split('%');

                                string tempuserName = userToken[0];
                                string tempMobileNum = userToken[1];
                                int tempAccount = System.Convert.ToInt32(userToken[2]);
                                string tempCreditCardNum = userToken[3];
                                PAYMENT_MODE tempPaymentMode1 = (PAYMENT_MODE)Enum.Parse(typeof(PAYMENT_MODE), userToken[4]);
                                PAYMENT_MODE tempPaymentMode2 = (PAYMENT_MODE)Enum.Parse(typeof(PAYMENT_MODE), userToken[5]);
                                int tempTicketExpiry = System.Convert.ToInt32(userToken[6]);
                                USER_STATUS tempUserStatus = (USER_STATUS)Enum.Parse(typeof(USER_STATUS), userToken[7]);
                                string tempLocation = userToken[8];
                                string tempPassword = userToken[9];

                                // create user
                                User tempUser = new User(tempuserName, tempMobileNum, tempAccount, tempCreditCardNum, tempPaymentMode1, tempPaymentMode2, tempPassword);
                                tempUser.UpdateTicketExpiry(tempTicketExpiry);
                                tempUser.UpdateUserStatus(tempUserStatus);
                                tempUser.UpdateUserLocation(tempLocation);

                                // Read sms log
                                tempBuffer = fin.ReadLine();
                                if (tempBuffer.Equals("$$$START SMSLOG$$$"))
                                {
                                    tempBuffer = fin.ReadLine();

                                    while (!tempBuffer.Equals("$$$END SMSLOG$$$"))
                                    {
                                        // add sms
                                        tempUser.AddSMSWhenLoadData(tempBuffer);
                                        tempBuffer = fin.ReadLine();
                                    }
                                }
                                else
                                {
                                    // throw "wrong format" exception
                                }

                                // Read Journey Log
                                tempBuffer = fin.ReadLine();
                                if (tempBuffer.Equals("$$$START JOURNEYLOG$$$"))
                                {
                                    tempBuffer = fin.ReadLine();

                                    while (!tempBuffer.Equals("$$$END JOURNEYLOG$$$"))
                                    {
                                        string[] token = tempBuffer.Split('%');

                                        string tempFrom = token[0];
                                        string tempTo = token[1];
                                        string tempBusName = token[2];
                                        int tempDepartTime = System.Convert.ToInt32(token[3]);
                                        int tempArriveTime = System.Convert.ToInt32(token[4]);

                                        // add journey log to user
                                        Journey tempJourney = new Journey(tempFrom, tempTo, tempBusName, tempDepartTime, tempArriveTime);
                                        tempUser.AddJourney(tempJourney);

                                        tempBuffer = fin.ReadLine();
                                    }
                                }
                                else
                                {
                                    // throw "wrong format" exception
                                }

                                // Read subscribe list
                                tempBuffer = fin.ReadLine();
                                if (tempBuffer.Equals("$$$START JOURNEYPLAN$$$"))
                                {
                                    tempBuffer = fin.ReadLine();
                                    while (!tempBuffer.Equals("$$$END JOURNEYPLAN$$$"))
                                    {
                                        string[] token = tempBuffer.Split('%');

                                        string tempFrom = token[0];
                                        string tempTo = token[1];
                                        string tempBusName = token[2];
                                        int tempDepartTime = System.Convert.ToInt32(token[3]);
                                        int tempArriveTime = System.Convert.ToInt32(token[4]);

                                        // add journey log to user
                                        // Journey tempJourneyPlan = new Journey(tempFrom, tempTo, tempBusName, tempDepartTime, tempArriveTime);
                                        tempUser.UpdateJourneyPlan(tempFrom, tempBusName, tempDepartTime);

                                        tempBuffer = fin.ReadLine();
                                    }
                                }
                                else
                                {
                                    // throw "wrong format" exception
                                }

                                // add current user to the list
                                _userList.Add(tempUser);

                                // read next user in the list
                                tempBuffer = fin.ReadLine();
                            }
                        }
                        else
                        {
                            // throw "format error" exception
                        }

                        // read empty line
                        tempBuffer = fin.ReadLine();
                    }
                    // end of file
                }
                else
                {
                    // throw "file format is wrong" error
                }
            }
            fin.Close();
        }
Example #2
0
        /****************************************************************************************************/
        // METHODS - MODIFIERS
        /****************************************************************************************************/

        // add a user to the list
        public void Add(User newUser)
        {
            _userList.Add(newUser);
        }
Example #3
0
 // creates a user object and adds it to the database
 public void CreateUser(string name, string mobileNum, float account, string creditCardNum, PAYMENT_MODE paymentMode1, PAYMENT_MODE paymentMode2, string password)
 {
     User newUser = new User(name, mobileNum, account, creditCardNum, paymentMode1, paymentMode2, password);
     _userList.Add(newUser);
 }