// fill the database with fake data for last month, using a test clock
        public static void FillLastMonthData(ParkingDatabase database, IClock testClock)
        {
            try
            {
                // fill with database with fake data
                for (int i = 0; i < 80; i++)
                {
                    CustomerAccount testAccount = new CustomerAccount(RandomString(), RandomString(), "555-555-5555",
                        RandomString() + "@tester.com", "password");
                    Billing.CreditCard.CreditCardType randomCardType = (Billing.CreditCard.CreditCardType)random.Next(3);
                    Billing.CreditCard testCard = new Billing.CreditCard(1234123412349876, Billing.CreditCard.CreditCardType.MASTERCARD);
                    Accounts.Vehicle testVehicle = new Accounts.Vehicle(RandomString() + " " + RandomString(),
                        RandomString());
                    DateTime startDate = new DateTime(2012, 11, 25, 10, 0, 0); // 11-25-12 @ 10a
                    DateTime testResDate = RandomTime(startDate);
                    database.AddAccount(testAccount);
                    database.AddCreditCard(testCard, testAccount.CustomerID);
                    database.AddVehicle(testVehicle, testAccount.CustomerID);
                    ParkingReservation testReservation = new ParkingReservation(testAccount, testVehicle,
                        testResDate, 120);

                    // generate a random transaction date in the past
                    int hours = random.Next(1, 500);
                    TimeSpan timeSpan = new TimeSpan(hours, 0, 0);
                    DateTime transactionDate = testResDate.Subtract(timeSpan);

                    // add reservation (and transaction) to database
                    database.AddReservation(testReservation, testCard.CardID, transactionDate);
                }
            }
            catch (Exception)
            { } // confict when adding reservation (ignore)
        }
 public CustomerForm(CustomerAccount cust, ParkingDatabase db, IClock clock, IMailer mailer)
 {
     CustomerClock = clock;
     Mailer = mailer;
     InitializeComponent();
     Customer = cust;
     Database = db;
 }
 public ParkingReservation(CustomerAccount cust, Accounts.Vehicle veh, DateTime date,
     int durationMinutes)
 {
     Customer = cust;
     ReservationVehicle = veh;
     Date = date;
     DurationMinutes = durationMinutes;
     IsCheckedIn = false;
 }
 public ParkingReservation(int resID, CustomerAccount cust, Accounts.Vehicle veh, DateTime date,
     int durationMinutes, int spotID, int ticketKey, bool isCheckedIn)
 {
     ReservationID = resID;
     Customer = cust;
     ReservationVehicle = veh;
     Date = date;
     DurationMinutes = durationMinutes;
     ParkingSpotID = spotID;
     TicketKey = ticketKey;
     IsCheckedIn = isCheckedIn;
 }
 // Constuctor for initalizing this form
 // source: http://stackoverflow.com/questions/600869/how-to-bind-a-list-to-a-combobox-winforms
 public CreateReservationForm(ParkingDatabase db, CustomerAccount customer)
 {
     Database = db;
     Customer = customer;
     VehicleList = Database.GetVehicles(Customer.CustomerID);
     CardList = Database.GetCreditCards(Customer.CustomerID);
     InitializeComponent();
     vehicleComboBox.DataSource = VehicleList;
     vehicleComboBox.DisplayMember = "AsString";
     paymentMethodComboBox.DataSource = CardList;
     paymentMethodComboBox.DisplayMember = "AsString";
     desiredMonthCalendar.MaxSelectionCount = 1;
 }
        // create a customer account in the database
        public void CreateAccount(CustomerAccount accountToCreate)
        {
            long cardNumber;

            // validate input
            if (accountToCreate.FirstName.Equals(""))
            {
                throw new CreateAccountException("First name cannot be left empty.");
            }
            if (accountToCreate.LastName.Equals(""))
            {
                throw new CreateAccountException("Last name cannot be left empty.");
            }
            if (accountToCreate.EmailAddress.Equals(""))
            {
                throw new CreateAccountException("Email cannot be left empty.");
            }
            if (accountToCreate.Password.Equals(""))
            {
                throw new CreateAccountException("Password field cannot be left empty.");
            }
            if (cardNumberTextBox.Text.Length != 16 || !long.TryParse(cardNumberTextBox.Text, out cardNumber))
            {
                throw new CreateAccountException("Card number format is incorrect.");
            }
            if (cardTypeComboBox.SelectedIndex == -1)
            {
                throw new CreateAccountException("You must select a credit card type.");
            }
            if (!EmailIsValid(accountToCreate.EmailAddress))
            {
                throw new CreateAccountException("Email is not valid.");
            }
            if (!PhoneNumberIsValid(accountToCreate.PhoneNumber))
            {
                throw new CreateAccountException("Phone number is not valid.");
            }

            // add account to database
            try
            {
                Database.AddAccount(accountToCreate);
            }
            catch (CreateAccountException)
            {
                throw new CreateAccountException("Could not create account in database.");
            }
        }
        // add an customer account to the database
        public void AddAccount(CustomerAccount account)
        {
            try
            {
                // insert customer into table
                int custID = Convert.ToInt32(customerTableAdapater.InsertAndGetID(account.LastName, account.FirstName, account.PhoneNumber,
                     account.EmailAddress, account.Password));

                // set customer ID that was retrieved from the database
                account.CustomerID = custID;
            }
            catch (Exception)
            {
                throw new CreateAccountException("Could not add customer data to database.");
            }
        }
        // constructor
        public SelectReservationToModifyForm(ParkingDatabase db, IClock custClock, IMailer mailer, CustomerAccount cust)
        {
            Mailer = mailer;
            Database = db;
            this.CustomerClock = custClock;
            Customer = cust;

            // create a binding list from the database list
            Reservations = new BindingList<ParkingReservation>(Database.GetCurrentAndFutureReservations(Customer.CustomerID));

            InitializeComponent();

            // bind the bindinglist to the combobox datasource
            reservationComboBox.DataSource = Reservations;
            reservationComboBox.DisplayMember = "ReservationID";
        }
        // create a customer account
        private void createButton_Click(object sender, EventArgs e)
        {
            bool createAccountSuccess = false;
            bool createCardSuccess = false;
            CustomerAccount accountToCreate = null;

            try
            {
                // attempt to create account
                accountToCreate = new CustomerAccount(firstNameTextBox.Text, lastNameTextBox.Text, phoneNumberTextBox.Text,
                   emailTextBox.Text, passwordTextBox.Text);

                // create customer account and pass to method
                CreateAccount(accountToCreate);
                createAccountSuccess = true;
            }
            catch (CreateAccountException ex)
            {
                // if error, show the error and quit
                MessageBox.Show(ex.Message);
                return;
            }

            // automatically login user with new account
            Customer cust = Database.LoginCustomer(accountToCreate);
            accountToCreate = new CustomerAccount(cust.FirstName, cust.LastName, cust.PhoneNumber, cust.Email, cust.Password);
            accountToCreate.CustomerID = cust.CustomerID;
            accountToCreate.CreditCards = new List<Billing.CreditCard>();
            accountToCreate.RegisteredVehicles = new List<Accounts.Vehicle>();

            // create credit card
            try
            {
                CreateCreditCard(accountToCreate);
                createCardSuccess = true;
            }
            catch (CreditCardException ex)
            {
                // if error creating the customer credit card, show the error message
                MessageBox.Show(ex.Message);
            }

            // if successful, show the customer form
            if (createAccountSuccess && createCardSuccess)
            {
                CustomerForm custForm = new CustomerForm(accountToCreate, Database, FormClock, Mailer);
                this.Visible = false;
                custForm.ShowDialog();
            }

            // afterwards, close this form
            this.Close();
        }
 // create a customer credit card in the database
 public void CreateCreditCard(CustomerAccount cust)
 {
     // validate card number and create in database
     Billing.CreditCard.CreditCardType cardType = (Billing.CreditCard.CreditCardType)cardTypeComboBox.SelectedIndex;
     long cardNumber;
     if ( !long.TryParse(cardNumberTextBox.Text, out cardNumber))
     {
         throw new CreditCardException("Card number format is incorrect.");
     }
     else
     {
         // add credit card to database tables
         Billing.CreditCard creditCard = new Billing.CreditCard(cardNumber, (Billing.CreditCard.CreditCardType)cardType);
         Database.AddCreditCard(creditCard, cust.CustomerID);
         cust.CreditCards.Add(creditCard);
     }
 }
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            // set up the print, mailer, and barrier external components to be used (stubs for now)
            ITerminalPrinter printer = new TerminalPrinterStub();
            IMailer mailer = new MailerStub();
            IBarrier barrier = new BarrierStub();

            // set up test environment
            // source: http://stackoverflow.com/questions/43711/whats-a-good-way-to-overwrite-datetime-now-during-testing
            DateTime testTime = new DateTime(2012, 12, 30, 13, 1, 0);
            IClock testClock = new TestClock(testTime); // a static clock for testing

            ParkingDatabase database = new ParkingDatabase(testClock, mailer);

            // fill with database with fake data
            for (int i = 0; i < 80; i++)
            {
                // create a random customer (including vehicle and billing info) and reservation
                CustomerAccount testAccount = new CustomerAccount(RandomString(), RandomString(), "555-555-5555",
                    RandomString() + "@tester.com", "password");
                Billing.CreditCard.CreditCardType randomCardType = (Billing.CreditCard.CreditCardType)random.Next(3);
                Billing.CreditCard testCard = new Billing.CreditCard(1234123412349876, Billing.CreditCard.CreditCardType.MASTERCARD);
                Accounts.Vehicle testVehicle = new Accounts.Vehicle(RandomString() + " " + RandomString(),
                    RandomString());
                DateTime startDate = new DateTime(2012, 12, 30, 10, 0, 0); // date to start generating from
                DateTime testResDate = RandomTime(startDate);

                // add fake customer data to database
                database.AddAccount(testAccount);
                database.AddCreditCard(testCard, testAccount.CustomerID);
                database.AddVehicle(testVehicle, testAccount.CustomerID);
                ParkingReservation testReservation = new ParkingReservation(testAccount, testVehicle,
                    testResDate, 120);

                // generate a random transaction date in the past
                int hours = random.Next(1, 500);
                TimeSpan timeSpan = new TimeSpan(hours, 0, 0);
                DateTime transactionDate = testResDate.Subtract(timeSpan);

                try
                {
                    // add fake reservation data to the database
                    database.AddReservation(testReservation, testCard.CardID, transactionDate);

                    // check in with 50% chance
                    if (random.Next(2) == 0)
                    {
                        // change status of reservation, if reservation is not in the future
                        if (testReservation.Date < testClock.Now)
                        {
                            database.ModifyReservation(testReservation, testReservation.ParkingSpotID,
                                testReservation.Date, testReservation.DurationMinutes, true,
                                testReservation.ReservationVehicle.CarID);

                            // change status of parking garage spot
                            database.SetParkingSpotStatus(testReservation.ParkingSpotID, 1);
                        }
                    }
                }
                catch (Exception)
                { } // conflict when adding reservation (ignore)
            }

            // fill database with fake data for last month
            FillLastMonthData(database, testClock);

            // reminder of admin credentials, for testing
            System.Windows.Forms.MessageBox.Show(
                string.Format("You can login as admin with\nemail: {0}\npassword: {1}",
                "*****@*****.**", "password"));
            Application.Run(new UserForm(testClock, mailer, database, true));
        }
        // get a customer account from a customer ID
        public CustomerAccount GetCustomerFromID(int custID)
        {
            CustomerAccount custToGet = null;
            try
            {
                // get customer table
                ParkingManagementDataContext db = new ParkingManagementDataContext();
                Table<Customer> Customers = db.GetTable<Customer>();

                // Query for customers with given ID
                var query =
                    from cust in Customers
                    where cust.CustomerID == custID
                    select cust;

                // check if any results were found
                if (query.Any())
                {
                    // if so, create a new customer object from the LINQ-to-SQL class object
                    Customer dbCust = query.First();
                    custToGet = new CustomerAccount(dbCust.CustomerID, dbCust.FirstName, dbCust.LastName, dbCust.Password,
                        dbCust.Email, dbCust.Password);
                }
                else
                {
                    throw new AccountException("Customer ID could not be found.");
                }

            }
            catch (Exception)
            {
                throw new AccountException("Attempt to retrieve customer using database failed.");
            }

            return custToGet;
        }