コード例 #1
0
        static void Main(string[] args)
        {
            Car               initCar      = Car.Init();
            CarSalesBook      carSalesBook = new CarSalesBook();
            CarDataFileReader cdfr         = new CarDataFileReader();
            var               cars         = cdfr.top3Sales();

            foreach (var car in cars)
            {
                Console.WriteLine(car);
            }
            Console.ReadLine();
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: PiterPSP/TSD
        static void Task2()
        {
            CarSalesBook carSalesBook = new CarSalesBook();

            var cars = carSalesBook.ReadCarsFromFile();

            Console.WriteLine("Which are TOP 3 makes whose sales with regard to the amount of sold cars in 2014?");
            PrintMakes(cars.OrderBy(car => - car.Sales2014).Take(3));

            Console.WriteLine("\nThe sales of which makes increased by at least 50% in 2015 comparing to 2014?");
            PrintMakes(cars.Where(car => car.Sales2015 >= 1.5 * car.Sales2014));

            Console.WriteLine("\nWhich 3 makes opens the second ten of the sales ranking in 2015");
            PrintMakes(cars.OrderBy(car => - car.Sales2015).Skip(10).Take(3));

            Console.WriteLine("\nWhat are the totals of sold cars in 2014 and 2015");
            Console.WriteLine("Total 2014: " + cars.Sum(n => n.Sales2014));
            Console.WriteLine("Total 2015: " + cars.Sum(n => n.Sales2015));
            Console.WriteLine("Total 2014 and 2015: " + (cars.Sum(n => n.Sales2014) + cars.Sum(n => n.Sales2015)));

            Console.WriteLine("\nCreate a list that contains TOP10 and LAST 10 cars with respect to sales in 2015.");
            IEnumerable <Car> result = cars.OrderBy(car => - car.Sales2015).Take(10).Union(
                cars.OrderBy(car => - car.Sales2015).Reverse().Take(10).Reverse());

            PrintMakes(result);

            string path = @"..\..\cars.xml";

            SaveToXML(result, path);
            Console.WriteLine("\nSaved results from previous query to XML.");

            Console.WriteLine("\nLoaded cars from XML:");
            List <Car> loadedCars = ReadFromXML(path);

            PrintMakes(loadedCars);
        }
コード例 #3
0
ファイル: Program.cs プロジェクト: kffl/TSD
        static void Main(string[] args)
        {
            CarSalesBook carSalesBook = new CarSalesBook();

            Func <int, Boolean> isLeapYear = year => (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0));

            Console.WriteLine("2014 is a leap year: {0:B}", isLeapYear(2014));
            Console.WriteLine("2016 is a leap year: {0:B}", isLeapYear(2016));

            var testCollection = new MyCollection <String>();

            Console.WriteLine("Collection is empty: {0:B}", testCollection.isEmpty());
            testCollection.Add("test");
            testCollection.Add("test2");
            testCollection.Add("test3");
            Console.WriteLine("Get 3rd element: {0:S}", testCollection.Get(2));
            Console.WriteLine("Get 3rd element: {0:S}", testCollection.Get(2));
            Console.WriteLine("Collection is empty: {0:B}", testCollection.isEmpty());

            Process[] allProcesses = Process.GetProcesses();
            allProcesses.PrintProcessesInfo();

            Console.ReadLine();
        }
コード例 #4
0
        static void Main(string[] args)
        {
            CarSalesBook carSalesBook = new CarSalesBook();

            carSalesBook._cars = carSalesBook.ReadCarsFromFile();

            /*
             * foreach (var item in carSalesBook._cars)
             * {
             *  Console.WriteLine(item.Make);
             * }
             */

            //Top 3
            var top3 = (from item in carSalesBook._cars
                        orderby item.Sales2014 descending
                        select item).Take(3);

            //least50%
            var least50 = from item in carSalesBook._cars
                          where item.Sales2015 >= item.Sales2014 * 1.5
                          select item;

            //3makesOpens
            var makesOpens = (from item in carSalesBook._cars
                              orderby item.Sales2015 descending
                              select item).Skip(10).Take(3);

            //total
            var total = from item in carSalesBook._cars
                        group item by 1
                        into g
                        select new
            {
                SumTotal = g.Sum(x => x.Sales2014),
                SumDone  = g.Sum(x => x.Sales2015)
            };

            //top10last10
            var tmp = from item in carSalesBook._cars
                      orderby item.Sales2015 descending
                      select item;
            var top10last10 = tmp.Take(10).Concat(tmp.Skip(Math.Max(0, tmp.Count() - 10)));

            //saveToXML
            var filename         = "save.xml";
            var currentDirectory = Directory.GetCurrentDirectory();
            var filepath         = Path.Combine(currentDirectory, filename);

            var xml = new XElement("Cars", carSalesBook._cars.Select(x => new XElement("Car",
                                                                                       new XElement("Make", x.Make),
                                                                                       new XElement("Sales2014", x.Sales2014),
                                                                                       new XElement("Sales2015", x.Sales2015))));

            xml.Save(filepath);

            //loadFromXML
            XElement loadingElement = XElement.Load(filepath);
            var      carsLoad       = from item in loadingElement.Descendants("Car")
                                      select item;

            /*
             * foreach (var row in carsLoad)
             * {
             *  Console.WriteLine(row.Element("Make").Value + ", " + row.Element("Sales2014").Value + ", " + row.Element("Sales2015").Value);
             * }
             */
            /*
             * List<Process> list = new List<Process>();
             * var proc1 = Process.Start("https://www.google.com");
             * list.Add(proc1);
             * Console.WriteLine(list.MemorySum());
             */

            //leap year method
            Func <int, bool> leapYear = x => (x % 4) == 0;

            Console.WriteLine(leapYear(2001));

            /*
             * OwnGenericCollection<String> ogc = new OwnGenericCollection<string>();
             * Console.WriteLine(ogc.IsEmpty());
             * ogc.Add("1");
             * ogc.Add("2");
             * ogc.Add("3");
             * ogc.Add("4");
             * ogc.Add("5");
             * Console.WriteLine(ogc.Get(0));
             * Console.WriteLine(ogc.Get(4));
             * Console.WriteLine(ogc.IsEmpty());
             */
            Console.ReadLine();
        }
コード例 #5
0
        static void Main(string[] args)
        {
            CarSalesBook      carSalesBook  = new CarSalesBook();
            IEnumerable <Car> top3Sales2014 = carSalesBook.Cars.OrderByDescending(c => c.Sales2014).Take(3);

            Console.WriteLine(" ---- Solution for TOP 3 makes whose sales with regard to the amount of sold cars in 2014(2.4) ---- ");
            foreach (Car c in top3Sales2014)
            {
                Console.WriteLine(c.Make);
            }
            Console.WriteLine("----------------------------------------------------------------------------------------------");


            Console.WriteLine(" ---- Solution for the sales of which makes increased by at least 50% in 2015 comparing to 2014(2.5) ---- ");
            IEnumerable <Car> salesOfMakesIncreasedInComparison2015to2014 = carSalesBook.Cars.Where(c => c.Sales2015 >= 1 * c.Sales2014 + 0.5 * c.Sales2014);

            foreach (Car c in salesOfMakesIncreasedInComparison2015to2014)
            {
                Console.WriteLine(c.Make);
            }
            Console.WriteLine("----------------------------------------------------------------------------------------------");

            Console.WriteLine(" ---- Solution for which 3 makes opens the second ten of the sales ranking in 2015(2.6)  ---- ");
            IEnumerable <Car> salesFirst3SecondTOP10 = carSalesBook.Cars.OrderByDescending(c => c.Sales2015).Skip(10).Take(3);

            foreach (Car c in salesFirst3SecondTOP10)
            {
                Console.WriteLine(c.Make);
            }
            Console.WriteLine("----------------------------------------------------------------------------------------------");

            Console.WriteLine(" ---- Solution for what are the totals of sold cars in 2014 and 2015(2.7)  ---- ");
            int soldCarsInTotalIn2014 = carSalesBook.Cars.Sum(c => c.Sales2014);
            int soldCarsInTotalIn2015 = carSalesBook.Cars.Sum(c => c.Sales2015);

            Console.WriteLine("Totals of sold cars in 2014 are {0}.\n Totals of sold cars in 2015 are {1}.",
                              soldCarsInTotalIn2014, soldCarsInTotalIn2015);
            Console.WriteLine("----------------------------------------------------------------------------------------------");

            Console.WriteLine(" ---- Solution for create a list that contains TOP10 and LAST 10 cars with respect to sales in 2015(2.8)  ---- ");
            var query  = carSalesBook.Cars.OrderByDescending(c => c.Sales2015).Take(10);
            var query2 = carSalesBook.Cars.OrderByDescending(c => c.Sales2015).Reverse().Take(10);
            IEnumerable <Car> listTop10Last10In2015 = query.ToList <Car>();

            listTop10Last10In2015 = listTop10Last10In2015.Concat(query2.ToList <Car>());
            foreach (Car c in listTop10Last10In2015)
            {
                Console.WriteLine(c.Make);
            }
            Console.WriteLine("----------------------------------------------------------------------------------------------");

            Console.WriteLine(" ---- Solution for (2.9)  ---- ");
            carSalesBook.WriteToXMLFile();
            Console.WriteLine("The results have been saved to an XML file.");
            Console.WriteLine("----------------------------------------------------------------------------------------------");

            Console.WriteLine(" ---- Solution for (2.10) ---- ");
            carSalesBook.ReadFromXMLFile();
            Console.WriteLine("----------------------------------------------------------------------------------------------");

            Console.ReadLine();
        }
コード例 #6
0
        static void Main(string[] args)
        {
            // Task 1.7
            Car car7 = new Car("Toyota")
            {
                NumberOfSeats = null
            };
            int numOfSeats = car7.NumberOfSeats ??= 0;

            Console.WriteLine("noSeats: " + numOfSeats);
            Console.WriteLine();

            // tests
            CarSalesBook book = new CarSalesBook();

            Console.WriteLine("1st car from hand is: " + book.CarsFromHand[0].Make);
            Console.WriteLine("2nd car from csv  is: " + book.CarsFromCsv[1].Make);

            // Task 2

            // 2.4
            IList <Car> query4 = (
                from car in book.CarsFromCsv
                orderby car.Sales2014 descending
                select car
                )
                                 .Take(5)
                                 .ToList();

            // 2.5
            IList <Car> query5 = (
                from car in book.CarsFromCsv
                where car.Sales2015 >= 1.5 * car.Sales2014
                select car
                )
                                 .ToList();

            // 2.6
            IList <Car> query6 = (
                from car in book.CarsFromCsv
                orderby car.Sales2015 descending
                select car
                )
                                 .Skip(10)
                                 .Take(3)
                                 .ToList();

            // 2.7
            int sum = book.CarsFromCsv.Sum(car => car.Sales2014)
                      + book.CarsFromCsv.Sum(car => car.Sales2015);

            // 2.8
            var tmp = (
                from car in book.CarsFromCsv
                orderby car.Sales2015 descending
                select car
                );

            IList <Car> query8 =
                tmp
                .Take(10)
                .Concat(
                    tmp
                    .Skip(Math.Max(0, tmp.Count() - 10)))
                .ToList();

            //saveToXML
            var currentDirectory = Directory.GetCurrentDirectory();
            var filepath         = Path.Combine(currentDirectory, "saved_cars.xml");

            var xml = new XElement("Cars",
                                   from car in book.CarsFromCsv
                                   select new XElement("Car",
                                                       new XElement("Make", car.Make),
                                                       new XElement("Sales2014", car.Sales2014),
                                                       new XElement("Sales2015", car.Sales2015)
                                                       ));

            xml.Save(filepath);

            //loadFromXML
            XElement    loadXML = XElement.Load(filepath);
            IList <Car> cars    = loadXML
                                  .Descendants("Car")
                                  .Select(car => new Car(car.Element("Make").Value)
            {
                Sales2014 = Convert.ToInt32(car.Element("Sales2014").Value),
                Sales2015 = Convert.ToInt32(car.Element("Sales2015").Value)
            })
                                  .ToList();
        }