Пример #1
0
        public static void Add()
        {
            //  Add customer / car to table
            using (var ctx = new CustomerCarContext())
            {
                //  Accept first name as input
                Console.WriteLine("Enter Customer's First Name:");
                string firstName = Console.ReadLine();
                Console.WriteLine();

                //  Accept last name as input
                Console.WriteLine("Enter Customer's Last Name:");
                string lastName = Console.ReadLine();
                Console.WriteLine();

                //  Accept phone number as input, parse as string
                long phone = PhoneMethod.PhoneAdd();

                //  Grab current DateTime
                DateTime dateAdded = DateTime.Now;

                var customer = new Customer(firstName, lastName, phone, dateAdded);

                //  Accept year of vehicle from input
                Console.WriteLine("Enter Year of Customer's Vehicle:");
                int year = int.Parse(Console.ReadLine());
                Console.WriteLine();

                //  Accept make from input
                Console.WriteLine("Enter Make of Customer's Vehicle:");
                string make = Console.ReadLine();
                Console.WriteLine();

                //  Accept model from input
                Console.WriteLine("Enter Model of Customer's Vehicle:");
                string model = Console.ReadLine();
                Console.WriteLine();

                //  Accept colour from input
                Console.WriteLine("Enter Colour of Customer's Vehicle:");
                string color = Console.ReadLine();
                Console.WriteLine();

                var car = new Car(year, make, model, color, customer.Id);

                //  Add car to customer object
                customer.Car = car;

                //  Add to table
                ctx.Customers.Add(customer);
                ctx.Cars.Add(car);
                ctx.SaveChanges();
            }
        }
Пример #2
0
        public static void View()
        {
            //  View contents of entire table
            using (var ctx = new CustomerCarContext())
            {
                //Display all customers in ctx
                //  Join Customers and Cars at Id and OwnerId
                //  respectively
                var query = from x in ctx.Customers
                            join y in ctx.Cars
                            on x.Id equals y.OwnerId
                            orderby x.LastName

                            //  Place custom/newely joined table in query
                            select new
                {
                    x.FirstName,
                    x.LastName,
                    x.Phone,
                    y.Year,
                    y.Make,
                    y.Model,
                    y.Color
                };

                Console.WriteLine("All customers added:");
                Console.WriteLine();

                //  Iterate through table
                foreach (var item in query)
                {
                    string resultString = string.Format("{0} {1} - {2} : {3} {4} {5} ({6})",
                                                        item.FirstName, item.LastName, item.Phone,
                                                        item.Year, item.Make, item.Model, item.Color);

                    Console.WriteLine(resultString);
                    Console.WriteLine();
                }
            }
        }
Пример #3
0
        public static void Delete()
        {
            //  Delete customer and their car from database
            using (var ctx = new CustomerCarContext())
            {
                //  Use customer last name from input
                Console.WriteLine("Enter Customer Last Name You Wish To Delete");
                string deleteCustomer = Console.ReadLine();
                Console.WriteLine();

                //  Query customers for matches
                var queryDelete = from x in ctx.Customers
                                  where x.LastName == deleteCustomer
                                  select x;

                foreach (var item in queryDelete)
                {
                    string queryResult = string.Format(
                        "{0} {1} - {2}", item.FirstName, item.LastName,
                        item.Phone);

                    Console.WriteLine(queryResult);
                    Console.WriteLine();
                }

                if (queryDelete.Count() > 1)
                {
                    Console.WriteLine(string.Format("There are multiple {0}'s in the system." +
                                                    " Please enter the phone number of whome you wish to delete.", deleteCustomer));

                    long deleteCustomerPhone = long.Parse(Console.ReadLine());
                    Console.WriteLine();

                    var delete = (from x in ctx.Customers
                                  where x.Phone == deleteCustomerPhone
                                  select x).Single();

                    var carDelete = (from x in ctx.Cars
                                     where x.OwnerId == delete.Id
                                     select x).Single();

                    ctx.Customers.Remove(delete);
                    ctx.SaveChanges();

                    ctx.Cars.Remove(carDelete);
                    ctx.SaveChanges();
                }
                else if (queryDelete.Count() == 1)
                {
                    //  Use customer last name to locate customer
                    var delete = (from x in ctx.Customers
                                  where x.LastName == deleteCustomer
                                  select x).Single();

                    //  Use Id from customer to locate their vehicle
                    var carDelete = (from x in ctx.Cars
                                     where x.OwnerId == delete.Id
                                     select x).Single();

                    //  Delete customer from table
                    ctx.Customers.Remove(delete);
                    ctx.SaveChanges();

                    //  Delete car from table
                    ctx.Cars.Remove(carDelete);
                    ctx.SaveChanges();
                }
            }
        }
Пример #4
0
        public static void Delete()
        {
            //  Delete customer and their car from database
            using (var ctx = new CustomerCarContext())
            {
                //  Use customer last name from input
                Console.WriteLine("Enter Customer Last Name You Wish To Delete");
                string deleteCustomer = Console.ReadLine();
                Console.WriteLine();

                var queryDelete = ctx.Customers.Where(x => x.LastName == deleteCustomer);

                if (queryDelete.Count() > 1)
                {
                    Console.WriteLine(string.Format("There are multiple {0}'s in the system." +
                                                    " Please enter the number left of the customer you wish to delete.",
                                                    deleteCustomer));
                    Console.WriteLine();

                    string deleteResult = "";

                    int i = 0;
                    Dictionary <int, string> deleteDictionary = new Dictionary <int, string>();
                    var query = from x in ctx.Customers.Where(x => x.LastName == deleteCustomer)
                                join y in ctx.Cars
                                on x.Id equals y.OwnerId
                                orderby x.LastName

                                //  Place custom/newely joined table in query
                                select new
                    {
                        x.FirstName,
                        x.LastName,
                        x.Phone,
                        y.Year,
                        y.Make,
                        y.Model,
                        y.Color,
                        x.Id
                    };

                    foreach (var item in query)
                    {
                        i++;
                        deleteResult = string.Format("{0}: {1} {2} - {3} : {4} {5} {6} ({7})",
                                                     i, item.FirstName, item.LastName, item.Phone,
                                                     item.Year, item.Make, item.Model, item.Color);
                        deleteDictionary.Add(i, item.Id.ToString());

                        Console.WriteLine(deleteResult);
                    }

                    Console.WriteLine();
                    int deleteCustomerInt = int.Parse(Console.ReadLine());
                    Console.WriteLine();

                    string deleteDictionaryValue = (from x in deleteDictionary
                                                    where x.Key == deleteCustomerInt
                                                    select x.Value).Single();

                    var delete = (from x in ctx.Customers
                                  where x.Id.ToString() == deleteDictionaryValue
                                  select x).Single();

                    var carDelete = (from x in ctx.Cars
                                     where x.OwnerId == delete.Id
                                     select x).Single();

                    ctx.Customers.Remove(delete);
                    ctx.SaveChanges();

                    ctx.Cars.Remove(carDelete);
                    ctx.SaveChanges();
                }
                else if (queryDelete.Count() == 1)
                {
                    foreach (var item in queryDelete)
                    {
                        string queryResult = string.Format(
                            "{0} {1} - {2}", item.FirstName, item.LastName,
                            item.Phone);

                        Console.WriteLine(queryResult);
                        Console.WriteLine("DELETED");
                        Console.WriteLine();
                    }

                    //  Use customer last name to locate customer
                    var delete = (from x in ctx.Customers
                                  where x.LastName == deleteCustomer
                                  select x).Single();

                    //  Use Id from customer to locate their vehicle
                    var carDelete = (from x in ctx.Cars
                                     where x.OwnerId == delete.Id
                                     select x).Single();

                    //  Delete customer from table
                    ctx.Customers.Remove(delete);
                    ctx.SaveChanges();

                    //  Delete car from table
                    ctx.Cars.Remove(carDelete);
                    ctx.SaveChanges();
                }
            }
        }