Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ================[Test.cs]======================
- public static void Mar29_ToysForAll()
- {
- Mar29.ToysForAll tfa = new Mar29.ToysForAll();
- Mar29.Branch branch = new Mar29.Branch("New York City");
- Mar29.Car car = new Mar29.Car("China", "Xiao Bei", 100);
- branch.AddCar(car);
- Mar29.Car_Spring carSpring = new Mar29.Car_Spring("China", "Xiao Bei", 200, false);
- branch.AddCar(carSpring);
- Mar29.Car_Electric carElectric = new Mar29.Car_Electric("China", "Xiao Bei", 300, false);
- branch.AddCar(carElectric);
- Mar29.Car_Electric_Remote carElectricRemote = new Mar29.Car_Electric_Remote("China", "Xiao Bei", 400, false, false);
- branch.AddCar(carElectricRemote);
- int[] stock = branch.GetStockQuantity();
- Console.WriteLine($"[{branch.address} Stock]");
- Console.WriteLine($"Total Cars: {stock[0]}");
- Console.WriteLine($"Regular Cars: {stock[1]}");
- Console.WriteLine($"Spring Cars: {stock[2]}");
- Console.WriteLine($"Electric Cars: {stock[3]}");
- Console.WriteLine($"Remote Electric Cars: {stock[4]}");
- tfa.AddBranch(branch);
- Console.WriteLine();
- branch = new Mar29.Branch("San Francisco");
- car = new Mar29.Car("China", "Xiao Bei", 100);
- branch.AddCar(car);
- car = new Mar29.Car("China", "Xiao Bei", 110);
- branch.AddCar(car);
- carSpring = new Mar29.Car_Spring("China", "Xiao Bei", 200, false);
- branch.AddCar(carSpring);
- carSpring = new Mar29.Car_Spring("China", "Xiao Bei", 220, true);
- branch.AddCar(carSpring);
- carElectric = new Mar29.Car_Electric("China", "Xiao Bei", 300, false);
- branch.AddCar(carElectric);
- carElectric = new Mar29.Car_Electric("China", "Xiao Bei", 330, true);
- branch.AddCar(carElectric);
- carElectricRemote = new Mar29.Car_Electric_Remote("China", "Xiao Bei", 400, true, true);
- branch.AddCar(carElectricRemote);
- carElectricRemote = new Mar29.Car_Electric_Remote("China", "Xiao Bei", 440, true, true);
- branch.AddCar(carElectricRemote);
- stock = branch.GetStockQuantity();
- Console.WriteLine($"[{branch.address} Stock]");
- Console.WriteLine($"Total Cars: {stock[0]}");
- Console.WriteLine($"Regular Cars: {stock[1]}");
- Console.WriteLine($"Spring Cars: {stock[2]}");
- Console.WriteLine($"Electric Cars: {stock[3]}");
- Console.WriteLine($"Remote Electric Cars: {stock[4]}");
- tfa.AddBranch(branch);
- Console.WriteLine();
- Console.ForegroundColor = ConsoleColor.Green;
- Console.WriteLine($"Global Price: {tfa.GetGlobalCarsPrice()}");
- }
- // =================[Mar29.cs]======================
- public class Car
- {
- public string manufacturingCountry;
- public string manufacturer;
- public double price;
- public Car(string manufacturingCountry, string manufacturer, double price)
- {
- this.manufacturingCountry = manufacturingCountry;
- this.manufacturer = manufacturer;
- this.price = price;
- }
- }
- public class Car_Spring : Car
- {
- public bool springPull;
- public Car_Spring(string manufacturingCountry, string manufacturer, double price, bool springPull) : base(manufacturingCountry, manufacturer, price)
- {
- this.manufacturingCountry = manufacturingCountry;
- this.manufacturer = manufacturer;
- this.price = price;
- this.springPull = springPull;
- }
- }
- public class Car_Electric : Car
- {
- public bool rechargabaleBatteries;
- public Car_Electric(string manufacturingCountry, string manufacturer, double price, bool rechargabaleBatteries) : base(manufacturingCountry, manufacturer, price)
- {
- this.manufacturingCountry = manufacturingCountry;
- this.manufacturer = manufacturer;
- this.price = price;
- this.rechargabaleBatteries = rechargabaleBatteries;
- }
- }
- public class Car_Electric_Remote : Car_Electric
- {
- public bool wireless;
- public Car_Electric_Remote(string manufacturingCountry, string manufacturer, double price, bool rechargabaleBatteries, bool wireless) : base(manufacturingCountry, manufacturer, price, rechargabaleBatteries)
- {
- this.manufacturingCountry = manufacturingCountry;
- this.manufacturer = manufacturer;
- this.price = price;
- this.rechargabaleBatteries = rechargabaleBatteries;
- this.wireless = wireless;
- }
- }
- public class Branch
- {
- public string address;
- public Node<Car> cars;
- public int totalCars;
- public double totalCarsPrice;
- public Branch(string address)
- {
- this.address = address;
- cars = null;
- totalCars = 0;
- totalCarsPrice = 0;
- }
- public void AddCar(Car car)
- {
- cars = new Node<Car>(car, cars);
- totalCars++;
- totalCarsPrice += car.price;
- }
- public int[] GetStockQuantity()
- {
- // Total, Car_Spring, Car_Electric, Car_Electric_Remote
- int[] stock = new int[] { 0, 0, 0, 0, 0 };
- Node<Car> scan = cars;
- while (scan != null)
- {
- stock[0]++;
- if (scan.GetInfo() is Car &&
- scan.GetInfo() is Car_Spring == false &&
- scan.GetInfo() is Car_Electric == false)
- stock[1]++;
- if (scan.GetInfo() is Car_Spring)
- stock[2]++;
- if (scan.GetInfo() is Car_Electric && scan.GetInfo() is Car_Electric_Remote == false)
- stock[3]++;
- if (scan.GetInfo() is Car_Electric_Remote)
- stock[4]++;
- scan = scan.GetNext();
- }
- return stock;
- }
- public double[] GetStockPrice()
- {
- // Total, Car_Spring, Car_Electric, Car_Electric_Remote
- double[] stock = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0 };
- Node<Car> scan = cars;
- while(scan != null)
- {
- stock[0] += scan.GetInfo().price;
- if (scan.GetInfo() is Car &&
- scan.GetInfo() is Car_Spring == false &&
- scan.GetInfo() is Car_Electric == false)
- stock[1]++;
- if (scan.GetInfo() is Car_Spring)
- stock[2] += scan.GetInfo().price;
- if (scan.GetInfo() is Car_Electric && scan.GetInfo() is Car_Electric_Remote == false)
- stock[3] += scan.GetInfo().price;
- if (scan.GetInfo() is Car_Electric_Remote)
- stock[4] += scan.GetInfo().price;
- scan = scan.GetNext();
- }
- return stock;
- }
- public void SetCarPrice(int carType, double price)
- {
- Node<Car> scan = cars;
- // Nesting While instead of nesting if statement to lower runtime
- if (carType == 0) // All
- {
- while (scan != null)
- {
- scan.GetInfo().price = price;
- scan = scan.GetNext();
- }
- }
- if (carType == 1) // Car_Spring
- {
- while (scan != null)
- {
- if (scan.GetInfo() is Car_Spring)
- scan.GetInfo().price = price;
- scan = scan.GetNext();
- }
- }
- if (carType == 2) // Car_Electric
- {
- while (scan != null)
- {
- // Excluding Car_Electric_Remote cars.
- if (scan.GetInfo() is Car_Electric && scan.GetInfo() is Car_Electric_Remote == false)
- scan.GetInfo().price = price;
- scan = scan.GetNext();
- }
- }
- if (carType == 3) // Car_Electric_Remote
- {
- while (scan != null)
- {
- if (scan.GetInfo() is Car_Electric_Remote)
- scan.GetInfo().price = price;
- scan = scan.GetNext();
- }
- }
- }
- }
- public class ToysForAll
- {
- public Node<Branch> branches;
- public int branchesCount;
- public ToysForAll()
- {
- branches = null;
- branchesCount = 0;
- }
- public void AddBranch(Branch branch)
- {
- branches = new Node<Branch>(branch, branches);
- branchesCount++;
- }
- public double GetGlobalCarsPrice()
- {
- double totalPrice = 0;
- Node<Branch> scan = branches;
- while(scan != null)
- {
- totalPrice += scan.GetInfo().totalCarsPrice;
- scan = scan.GetNext();
- }
- return totalPrice;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement