예제 #1
0
파일: Program.cs 프로젝트: Bang0123/SWC2sem
        static void Main(string[] args)
        {
            var hotels = new Hotel[]
            {
                new Hotel() {HotelNo = 1, Name = "The Pope", Address = "Vaticanstreet 1  1111 Bishopcity"},
                new Hotel() {HotelNo = 2, Name = "Lucky Star", Address = "Lucky Road 12  2222 Hometown"},
                new Hotel() {HotelNo = 3, Name = "Discount", Address = "Inexpensive Road 7 3333 Cheaptown"},
                new Hotel() {HotelNo = 4, Name = "deLuxeCapital", Address = "Luxury Road 99  4444 Luxus"},
                new Hotel() {HotelNo = 5, Name = "Discount", Address = "Inexpensive Road 7 3333 Cheaptown"},
                new Hotel() {HotelNo = 6, Name = "Prindsen", Address = "Algade 5, 4000 Roskilde"},
                new Hotel() {HotelNo = 7, Name = "Scandic", Address = "Algade 5, 4000 Roskilde"},
            };

            var rooms = new Room[]
            {
                new Room() {RoomNo = 1, Hotel=hotels[0], Types = 'D', Price = 200},
                new Room() {RoomNo = 2, Hotel=hotels[0], Types = 'D', Price = 200},
                new Room() {RoomNo = 3, Hotel=hotels[0], Types = 'D', Price = 200},
                new Room() {RoomNo = 4, Hotel=hotels[0], Types = 'D', Price = 200},
                new Room() {RoomNo = 11, Hotel=hotels[0], Types = 'S', Price = 150},
                new Room() {RoomNo = 12, Hotel=hotels[0], Types = 'S', Price = 150},
                new Room() {RoomNo = 13, Hotel=hotels[0], Types = 'S', Price = 150},
                new Room() {RoomNo = 21, Hotel=hotels[0], Types = 'F', Price = 220},
                new Room() {RoomNo = 22, Hotel=hotels[0], Types = 'F', Price = 220},
                new Room() {RoomNo = 23, Hotel=hotels[0], Types = 'F', Price = 220},
                new Room() {RoomNo = 1, Hotel=hotels[1], Types = 'D', Price = 230},
                new Room() {RoomNo = 2, Hotel=hotels[1], Types = 'D', Price = 230},
                new Room() {RoomNo = 3, Hotel=hotels[1], Types = 'D', Price = 230},
                new Room() {RoomNo = 4, Hotel=hotels[1], Types = 'D', Price = 230},
                new Room() {RoomNo = 11, Hotel=hotels[1], Types = 'S', Price = 180},
                new Room() {RoomNo = 12, Hotel=hotels[1], Types = 'S', Price = 180},
                new Room() {RoomNo = 21, Hotel=hotels[1], Types = 'F', Price = 300},
                new Room() {RoomNo = 22, Hotel=hotels[1], Types = 'F', Price = 300},
                new Room() {RoomNo = 1, Hotel=hotels[2], Types = 'D', Price = 175},
                new Room() {RoomNo = 2, Hotel=hotels[2], Types = 'D', Price = 180},
                new Room() {RoomNo = 11, Hotel=hotels[2], Types = 'S', Price = 100},
                new Room() {RoomNo = 21, Hotel=hotels[2], Types = 'S', Price = 100},
                new Room() {RoomNo = 31, Hotel=hotels[2], Types = 'F', Price = 200},
                new Room() {RoomNo = 32, Hotel=hotels[2], Types = 'F', Price = 230},
                new Room() {RoomNo = 1, Hotel=hotels[3], Types = 'D', Price = 500},
                new Room() {RoomNo = 2, Hotel=hotels[3], Types = 'D', Price = 550},
                new Room() {RoomNo = 3, Hotel=hotels[3], Types = 'D', Price = 550},
                new Room() {RoomNo = 11, Hotel=hotels[3], Types = 'S', Price = 350},
                new Room() {RoomNo = 12, Hotel=hotels[3], Types = 'S', Price = 360},
                new Room() {RoomNo = 1, Hotel=hotels[4], Types = 'D', Price = 250},
                new Room() {RoomNo = 2, Hotel=hotels[4], Types = 'D', Price = 170},
                new Room() {RoomNo = 11, Hotel=hotels[4], Types = 'S', Price = 150},
                new Room() {RoomNo = 21, Hotel=hotels[4], Types = 'F', Price = 300},
                new Room() {RoomNo = 22, Hotel=hotels[4], Types = 'F', Price = 310},
                new Room() {RoomNo = 23, Hotel=hotels[4], Types = 'F', Price = 320},
                new Room() {RoomNo = 24, Hotel=hotels[4], Types = 'F', Price = 320},
                new Room() {RoomNo = 1, Hotel=hotels[5], Types = 'D', Price = 290},
                new Room() {RoomNo = 11, Hotel=hotels[5], Types = 'S', Price = 185},
                new Room() {RoomNo = 21, Hotel=hotels[5], Types = 'F', Price = 360},
                new Room() {RoomNo = 22, Hotel=hotels[5], Types = 'F', Price = 370},
                new Room() {RoomNo = 23, Hotel=hotels[5], Types = 'F', Price = 380},
                new Room() {RoomNo = 24, Hotel=hotels[5], Types = 'F', Price = 380},
                new Room() {RoomNo = 1, Hotel=hotels[6], Types = 'D', Price = 200},
                new Room() {RoomNo = 2, Hotel=hotels[6], Types = 'D', Price = 200},
                new Room() {RoomNo = 3, Hotel=hotels[6], Types = 'D', Price = 200},
                new Room() {RoomNo = 4, Hotel=hotels[6], Types = 'D', Price = 200},
                new Room() {RoomNo = 11, Hotel=hotels[6], Types = 'S', Price = 150},
                new Room() {RoomNo = 12, Hotel=hotels[6], Types = 'S', Price = 150},
                new Room() {RoomNo = 13, Hotel=hotels[6], Types = 'S', Price = 150},
                new Room() {RoomNo = 14, Hotel=hotels[6], Types = 'S', Price = 150},
                new Room() {RoomNo = 21, Hotel=hotels[6], Types = 'F', Price = 220},
                new Room() {RoomNo = 22, Hotel=hotels[6], Types = 'F', Price = 220},
                new Room() {RoomNo = 23, Hotel=hotels[6], Types = 'F', Price = 220},
                new Room() {RoomNo = 24, Hotel=hotels[6], Types = 'F', Price = 220}
            };

            //Exercise, use LINQ to retrive the following information about Hotels and Rooms:

            // 1) List full details of all Hotels:
            Console.WriteLine("Full List of Hotels: ");
            IEnumerable<Hotel> enumerableHotels = from h in hotels
                                                  select h;
            /** using Linq;                
                var enumHotels = hotels.Select(x => x);
            **/
            foreach (var enumerableHotel in enumerableHotels)
            {
                Console.WriteLine(enumerableHotel);
            }


            // 2) List full details of all hotels in Roskilde:
            Console.WriteLine("\nHotels in Roskilde: ");
            var roskildeHotels = from h in hotels
                                 where h.Address.Contains("Roskilde")
                                 select h;
            /** using Linq;            
            var rosHotelse = hotels
                .Where(x => x.Address.Contains("Roskilde"))
                .Select(x => x);
            **/
            foreach (var roskildeHotel in roskildeHotels)
            {
                Console.WriteLine(roskildeHotel);
            }


            // 3) List the names of all hotels in Roskilde:
            Console.WriteLine("\nPrints the names of each hotel in roskilde: ");
            var roskildeNames = from h in hotels
                                where h.Address.Contains("Roskilde")
                                select h.Name;

            /** using linq;            
            var rosNames = hotels
                .Where(x => x.Address.Contains("Roskilde"))
                .Select(h => h.Name);
            **/
            foreach (var roskildeName in roskildeNames)
            {
                Console.WriteLine(roskildeName);
            }


            // 4) List all double rooms with a price below 400 pr night:
            Console.WriteLine("\nListing all double rooms with a price below 400: ");
            var doubroomsBelow = from r in rooms
                                 where r.Types.Equals('D') && r.Price < 400
                                 select r;
            /** using linq;            
            var doubromsmindre = rooms
                .Where(x => x.Types.Equals('D') && x.Price < 400)
                .Select(x => x);
            **/
            foreach (var room in doubroomsBelow)
            {
                Console.WriteLine(room);
            }


            // 5) List all double or family rooms with a price below 400 pr night in ascending order of price:
            Console.WriteLine("\nListing all double and family rooms and a price below 400 in ascending: ");
            var dAndFRooms = from r in rooms
                             where (r.Types.Equals('F') || r.Types.Equals('D')) && r.Price < 400
                             orderby r.Price
                             select r;
            /** using linq;
            var dnfRooms = rooms
                .Where(x => (x.Types.Equals('F') || x.Types.Equals('D') && x.Price < 400))
                .OrderBy(x => x.Price)
                .Select(x => x);
            **/
            foreach (var dAndFRoom in dAndFRooms)
            {
                Console.WriteLine(dAndFRoom);
            }


            // 6) List all hotels that starts with 'P':
            Console.WriteLine("\nListing all hotels that start with \'P\': ");
            var startsWithP = from h in hotels
                              where h.Name.StartsWith("P")
                              select h;
            /**
            var startsmedP = hotels
                .Where(x => x.Name.StartsWith("P"))
                .Select(x => x);
            **/
            foreach (var hotel in startsWithP)
            {
                Console.WriteLine(hotel);
            }


            // 7) List the number of hotels:
            var numOfHotels = hotels.Count();
            Console.WriteLine("\nAmount of Hotels in Hotels: " + numOfHotels);


            // 8) List the number of hotels in Roskilde:
            var numOfRosHotels = hotels.Count(x => x.Address.Contains("Roskilde"));
            Console.WriteLine("\nAmount of Hotels in Roskilde: " + numOfRosHotels);


            // 9) what is the avarage price of a room:
            var avarageRoomPrice = rooms.Average(x => x.Price);
            Console.WriteLine("\nAverage price of a room: " + (int)avarageRoomPrice);


            //10) what is the avarage price of a room at Hotel Scandic:
            var sandicRooms = from r in rooms
                              join h in hotels
                              on r.Hotel.HotelNo equals h.HotelNo
                              where h.Name == "Scandic"
                              select r.Price;
            /** using linq;
            var scandicrooms = rooms
                .Join(hotels, r => r.Hotel.HotelNo, h => h.HotelNo, (r, h) => new {r, h})
                .Where(x => x.h.Name == "Scandic")
                .Select(x => x.r.Price);
            **/
            Console.WriteLine("\nAverage price of hotel Scandic rooms: " + sandicRooms.Average());


            //11) what is the total reveneue per night from all double rooms:
            var totalRevenueForDs = from r in rooms
                                    where r.Types.Equals('D')
                                    select r;
            /** using linq;
            var totalsumDs = rooms
                .Where(x => x.Types.Equals('D'))
                .Sum(x=>x.Price);
            **/
            Console.WriteLine("\nThe total price for all the double rooms: " + totalRevenueForDs.Sum(x => x.Price));


            //12) List price and type of all rooms at Hotel Prindsen:
            Console.WriteLine("List price and type of all rooms at Hotel Prindsen: ");
            var collectivePrindsen = from r in rooms
                                     join h in hotels
                                         on r.Hotel.HotelNo equals h.HotelNo
                                     where h.Name.Equals("Prindsen")
                                     select new { Price = r.Price, Tp = r.Types };
            /** using linq;
            var colprindsenlinq = rooms
                .Join(hotels, r => r.Hotel.HotelNo, h => h.HotelNo, (r, h) => new {r, h})
                .Where(x => x.h.Name.Equals("Prindsen"))
                .Select(x => new {Price = x.r.Price, Tp = x.r.Types});
            **/
            foreach (var x1 in collectivePrindsen)
            {
                Console.WriteLine($"Type: {x1.Tp}, Price: {x1.Price}.");
            }


            //13) List distinct price and type of all rooms at Hotel Prindsen:
            Console.WriteLine("List distinct price and type of all rooms at Hotel Prindsen: ");
            var collPrindsen = (from r in rooms
                                join h in hotels
                                    on r.Hotel.HotelNo equals h.HotelNo
                                where h.Name.Equals("Prindsen")
                                select new { Price = r.Price, Tp = r.Types }).Distinct();
            // using linq;
            var prindsennrto = rooms
                .Join(hotels, r => r.Hotel.HotelNo, h => h.HotelNo, (r, h) => new {r, h})
                .Where(x => x.h.Name == "Prindsen")
                .Select(x => new {Price = x.r.Price, Tp = x.r.Types})
                .Distinct();
            
            foreach (var x1 in collPrindsen)
            {
                Console.WriteLine($"Type: {x1.Tp}, Price: {x1.Price}.");
            }




            // Holder console vinduet åbent
            Console.ReadKey();
        }