Advertisement
Lirbo

Mar29 OOP

Mar 28th, 2023
680
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.11 KB | None | 0 0
  1. // ================[Test.cs]======================
  2. public static void Mar29_ToysForAll()
  3.         {
  4.             Mar29.ToysForAll tfa = new Mar29.ToysForAll();
  5.             Mar29.Branch branch = new Mar29.Branch("New York City");
  6.             Mar29.Car car = new Mar29.Car("China", "Xiao Bei", 100);
  7.             branch.AddCar(car);
  8.             Mar29.Car_Spring carSpring = new Mar29.Car_Spring("China", "Xiao Bei", 200, false);
  9.             branch.AddCar(carSpring);
  10.             Mar29.Car_Electric carElectric = new Mar29.Car_Electric("China", "Xiao Bei", 300, false);
  11.             branch.AddCar(carElectric);
  12.             Mar29.Car_Electric_Remote carElectricRemote = new Mar29.Car_Electric_Remote("China", "Xiao Bei", 400, false, false);
  13.             branch.AddCar(carElectricRemote);
  14.             int[] stock = branch.GetStockQuantity();
  15.             Console.WriteLine($"[{branch.address} Stock]");
  16.             Console.WriteLine($"Total Cars: {stock[0]}");
  17.             Console.WriteLine($"Regular Cars: {stock[1]}");
  18.             Console.WriteLine($"Spring Cars: {stock[2]}");
  19.             Console.WriteLine($"Electric Cars: {stock[3]}");
  20.             Console.WriteLine($"Remote Electric Cars: {stock[4]}");
  21.             tfa.AddBranch(branch);
  22.  
  23.             Console.WriteLine();
  24.  
  25.             branch = new Mar29.Branch("San Francisco");
  26.             car = new Mar29.Car("China", "Xiao Bei", 100);
  27.             branch.AddCar(car);
  28.             car = new Mar29.Car("China", "Xiao Bei", 110);
  29.             branch.AddCar(car);
  30.             carSpring = new Mar29.Car_Spring("China", "Xiao Bei", 200, false);
  31.             branch.AddCar(carSpring);
  32.             carSpring = new Mar29.Car_Spring("China", "Xiao Bei", 220, true);
  33.             branch.AddCar(carSpring);
  34.             carElectric = new Mar29.Car_Electric("China", "Xiao Bei", 300, false);
  35.             branch.AddCar(carElectric);
  36.             carElectric = new Mar29.Car_Electric("China", "Xiao Bei", 330, true);
  37.             branch.AddCar(carElectric);
  38.             carElectricRemote = new Mar29.Car_Electric_Remote("China", "Xiao Bei", 400, true, true);
  39.             branch.AddCar(carElectricRemote);
  40.             carElectricRemote = new Mar29.Car_Electric_Remote("China", "Xiao Bei", 440, true, true);
  41.             branch.AddCar(carElectricRemote);
  42.             stock = branch.GetStockQuantity();
  43.             Console.WriteLine($"[{branch.address} Stock]");
  44.             Console.WriteLine($"Total Cars: {stock[0]}");
  45.             Console.WriteLine($"Regular Cars: {stock[1]}");
  46.             Console.WriteLine($"Spring Cars: {stock[2]}");
  47.             Console.WriteLine($"Electric Cars: {stock[3]}");
  48.             Console.WriteLine($"Remote Electric Cars: {stock[4]}");
  49.             tfa.AddBranch(branch);
  50.  
  51.             Console.WriteLine();
  52.  
  53.             Console.ForegroundColor = ConsoleColor.Green;
  54.             Console.WriteLine($"Global Price: {tfa.GetGlobalCarsPrice()}");
  55.         }
  56.  
  57.  
  58.  
  59.  
  60. // =================[Mar29.cs]======================
  61.         public class Car
  62.         {
  63.             public string manufacturingCountry;
  64.             public string manufacturer;
  65.             public double price;
  66.             public Car(string manufacturingCountry, string manufacturer, double price)
  67.             {
  68.                 this.manufacturingCountry = manufacturingCountry;
  69.                 this.manufacturer = manufacturer;
  70.                 this.price = price;
  71.             }
  72.         }
  73.  
  74.         public class Car_Spring : Car
  75.         {
  76.             public bool springPull;
  77.             public Car_Spring(string manufacturingCountry, string manufacturer, double price, bool springPull) : base(manufacturingCountry, manufacturer, price)
  78.             {
  79.                 this.manufacturingCountry = manufacturingCountry;
  80.                 this.manufacturer = manufacturer;
  81.                 this.price = price;
  82.                 this.springPull = springPull;
  83.             }
  84.         }
  85.  
  86.         public class Car_Electric : Car
  87.         {
  88.             public bool rechargabaleBatteries;
  89.             public Car_Electric(string manufacturingCountry, string manufacturer, double price, bool rechargabaleBatteries) : base(manufacturingCountry, manufacturer, price)
  90.             {
  91.                 this.manufacturingCountry = manufacturingCountry;
  92.                 this.manufacturer = manufacturer;
  93.                 this.price = price;
  94.                 this.rechargabaleBatteries = rechargabaleBatteries;
  95.             }
  96.         }
  97.  
  98.         public class Car_Electric_Remote : Car_Electric
  99.         {
  100.             public bool wireless;
  101.             public Car_Electric_Remote(string manufacturingCountry, string manufacturer, double price, bool rechargabaleBatteries, bool wireless) : base(manufacturingCountry, manufacturer, price, rechargabaleBatteries)
  102.             {
  103.                 this.manufacturingCountry = manufacturingCountry;
  104.                 this.manufacturer = manufacturer;
  105.                 this.price = price;
  106.                 this.rechargabaleBatteries = rechargabaleBatteries;
  107.                 this.wireless = wireless;
  108.             }
  109.         }
  110.  
  111.         public class Branch
  112.         {
  113.             public string address;
  114.             public Node<Car> cars;
  115.             public int totalCars;
  116.             public double totalCarsPrice;
  117.  
  118.             public Branch(string address)
  119.             {
  120.                 this.address = address;
  121.                 cars = null;
  122.                 totalCars = 0;
  123.                 totalCarsPrice = 0;
  124.             }
  125.  
  126.             public void AddCar(Car car)
  127.             {
  128.                 cars = new Node<Car>(car, cars);
  129.                 totalCars++;
  130.                 totalCarsPrice += car.price;
  131.             }
  132.  
  133.             public int[] GetStockQuantity()
  134.             {
  135.                 // Total, Car_Spring, Car_Electric, Car_Electric_Remote
  136.                 int[] stock = new int[] { 0, 0, 0, 0, 0 };
  137.                 Node<Car> scan = cars;
  138.                 while (scan != null)
  139.                 {
  140.                     stock[0]++;
  141.                     if (scan.GetInfo() is Car &&
  142.                         scan.GetInfo() is Car_Spring == false &&
  143.                         scan.GetInfo() is Car_Electric == false)
  144.                         stock[1]++;
  145.                     if (scan.GetInfo() is Car_Spring)
  146.                         stock[2]++;
  147.                     if (scan.GetInfo() is Car_Electric && scan.GetInfo() is Car_Electric_Remote == false)
  148.                         stock[3]++;
  149.                     if (scan.GetInfo() is Car_Electric_Remote)
  150.                         stock[4]++;
  151.                     scan = scan.GetNext();
  152.                 }
  153.                 return stock;
  154.             }
  155.  
  156.             public double[] GetStockPrice()
  157.             {
  158.                                         // Total, Car_Spring, Car_Electric, Car_Electric_Remote
  159.                 double[] stock = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0 };
  160.                 Node<Car> scan = cars;
  161.                 while(scan != null)
  162.                 {
  163.                     stock[0] += scan.GetInfo().price;
  164.                     if (scan.GetInfo() is Car &&
  165.                         scan.GetInfo() is Car_Spring == false &&
  166.                         scan.GetInfo() is Car_Electric == false)
  167.                         stock[1]++;
  168.                     if (scan.GetInfo() is Car_Spring)
  169.                         stock[2] += scan.GetInfo().price;
  170.                     if (scan.GetInfo() is Car_Electric && scan.GetInfo() is Car_Electric_Remote == false)
  171.                         stock[3] += scan.GetInfo().price;
  172.                     if (scan.GetInfo() is Car_Electric_Remote)
  173.                         stock[4] += scan.GetInfo().price;
  174.                     scan = scan.GetNext();
  175.                 }
  176.                 return stock;
  177.             }
  178.  
  179.             public void SetCarPrice(int carType, double price)
  180.             {
  181.                 Node<Car> scan = cars;
  182.  
  183.                 // Nesting While instead of nesting if statement to lower runtime
  184.                 if (carType == 0) // All
  185.                 {
  186.                     while (scan != null)
  187.                     {
  188.                         scan.GetInfo().price = price;
  189.                         scan = scan.GetNext();
  190.                     }
  191.                 }
  192.                 if (carType == 1) // Car_Spring
  193.                 {
  194.                     while (scan != null)
  195.                     {
  196.                         if (scan.GetInfo() is Car_Spring)
  197.                             scan.GetInfo().price = price;
  198.                         scan = scan.GetNext();
  199.                     }
  200.                 }
  201.                 if (carType == 2) // Car_Electric
  202.                 {
  203.                     while (scan != null)
  204.                     {
  205.                         // Excluding Car_Electric_Remote cars.
  206.                         if (scan.GetInfo() is Car_Electric && scan.GetInfo() is Car_Electric_Remote == false)
  207.                             scan.GetInfo().price = price;
  208.                         scan = scan.GetNext();
  209.                     }
  210.                 }
  211.                 if (carType == 3) // Car_Electric_Remote
  212.                 {
  213.                     while (scan != null)
  214.                     {
  215.                         if (scan.GetInfo() is Car_Electric_Remote)
  216.                             scan.GetInfo().price = price;
  217.                         scan = scan.GetNext();
  218.                     }
  219.                 }
  220.  
  221.  
  222.             }
  223.         }
  224.  
  225.         public class ToysForAll
  226.         {
  227.             public Node<Branch> branches;
  228.             public int branchesCount;
  229.             public ToysForAll()
  230.             {
  231.                 branches = null;
  232.                 branchesCount = 0;
  233.             }
  234.  
  235.             public void AddBranch(Branch branch)
  236.             {
  237.                 branches = new Node<Branch>(branch, branches);
  238.                 branchesCount++;
  239.             }
  240.  
  241.             public double GetGlobalCarsPrice()
  242.             {
  243.                 double totalPrice = 0;
  244.                 Node<Branch> scan = branches;
  245.                 while(scan != null)
  246.                 {
  247.                     totalPrice += scan.GetInfo().totalCarsPrice;
  248.                     scan = scan.GetNext();
  249.                 }
  250.                 return totalPrice;
  251.             }
  252.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement