Codes for Car Inventory Control Application (ITERATOR Pattern)
Total Page:16
File Type:pdf, Size:1020Kb
// Codes for Car Inventory Control Application (ITERATOR Pattern)
//PRORAM.CS using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Abner._658.IteratorPattern { class Program { static void Main(string[] args) { Cars car; NissanDealershipInventory NissanCars = new NissanDealershipInventory(); ToyotaDealershipInventory ToyotaCars = new ToyotaDealershipInventory(); BmwDealershipInventory BmwCars = new BmwDealershipInventory();
car = new Cars("Toyota","Corolla"," New red corolla with sports package ", 19999); ToyotaCars.addItems(car); car = new Cars("Toyota", "Camry", " New silver camry with the basic package", 25999); ToyotaCars.addItems(car); car = new Cars("Toyota", "Avalon", " Used black Avalon with leather seats ", 15999); ToyotaCars.addItems(car); car = new Cars("Nissan", "Sentra", " Used black Sentra with basic package", 10999); NissanCars.addItems(car); car = new Cars("Nissan", "Maxima", " New gray Maxima with all the upgrades", 31999); NissanCars.addItems(car); car = new Cars("Nissan", "GT-R", " New blue GT-R with a v8 turbo engine", 35999); NissanCars.addItems(car); car = new Cars("Nissan", "Murano", " New silver Murano great family car", 31999); NissanCars.addItems(car); car = new Cars("BMW", "325I", "New charcoal 2010 bmw325i 5speed manual ", 38999); BmwCars.addItems(car); car = new Cars("BMW", "745I", "New metallic blue 2010 bmw745i luxury pckg ", 80999); BmwCars.addItems(car); car = new Cars("BMW", "X6", "New black 2010 bmwX6 premium package ", 60999); BmwCars.addItems(car); car = new Cars("BMW", "M5", "New Red 2010 bmw M5 6speed manual ", 58999); BmwCars.addItems(car);
Iterator NissanIterator = NissanCars.CreateIterator(); Iterator ToyotaIterator = ToyotaCars.CreateIterator(); Iterator BmwIterator = BmwCars.CreateIterator(); Console.WriteLine("Nissan Inventory"); Console.WriteLine("Name Model Description Price"); Cars Ncar = NissanIterator.First();
while (Ncar != null) { Console.WriteLine("{0} {1} {2} {3}", Ncar.getBrand,Ncar.getModel,Ncar.getDescription,Ncar.getPrice); Ncar = NissanIterator.Next(); } Cars Tcar = ToyotaIterator.First(); Console.WriteLine("\nToyota Inventory"); Console.WriteLine("Name Model Description Price");
while (Tcar != null) { Console.WriteLine("{0} {1} {2} {3}", Tcar.getBrand, Tcar.getModel, Tcar.getDescription, Tcar.getPrice); Tcar = ToyotaIterator.Next(); } Cars Bcar = BmwIterator.First(); Console.WriteLine("\nBMW Inventory"); Console.WriteLine("Name Model Description Price"); while (Bcar != null) { Console.WriteLine("{0} {1} {2} {3}", Bcar.getBrand, Bcar.getModel, Bcar.getDescription, Bcar.getPrice); Bcar = BmwIterator.Next(); }
Console.ReadKey(); } } } // ITERATOR.CS using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Abner._658.IteratorPattern { abstract class Iterator { public abstract Cars First(); public abstract Cars Next(); public abstract bool IsDone(); public abstract Cars CurrentItem();
} }
// NISSANDEALERSHIPITERATOR.CS using System; using System.Collections.Generic; using System.Collections; using System.Linq; using System.Text; namespace Abner._658.IteratorPattern { class NissanDealershipIterator : Iterator { private ArrayList NissanCarsArray = new ArrayList(); private int position = 0; public NissanDealershipIterator(ArrayList NissanCarsArray) { this.NissanCarsArray = NissanCarsArray; } public override Cars First() { return (Cars)NissanCarsArray[0]; }
public override Cars Next() { Cars ret = null;
if (position < NissanCarsArray.Count - 1) { ret = (Cars)NissanCarsArray[++position]; }
return ret; }
public override bool IsDone() { return (position >= NissanCarsArray.Count); }
public override Cars CurrentItem() { return (Cars)NissanCarsArray[position]; } } }
//TOYOTACARDEALERSHIPITERATOR.CS using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Abner._658.IteratorPattern { class ToyotaDealershipIterator : Iterator { private List
public override Cars First() { return (Cars)ToyotaCarsList[0]; }
public override Cars Next() { Cars ret = null; if (position < ToyotaCarsList.Count - 1) { ret = (Cars)ToyotaCarsList[++position]; }
return ret; }
public override bool IsDone() { return position >= ToyotaCarsList.Count; }
public override Cars CurrentItem() { return (Cars)ToyotaCarsList[position]; } } }
//BMWCARDEALERSHIPITERATOR using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Abner._658.IteratorPattern { class BmwDealershipIterator : Iterator { private List
public override Cars First() { return (Cars)BmwCarsList[0]; }
public override Cars Next() { Cars ret = null; if (position < BmwCarsList.Count - 1) { ret = (Cars)BmwCarsList[++position]; }
return ret; }
public override bool IsDone() { return position >= BmwCarsList.Count; }
public override Cars CurrentItem() { return (Cars)BmwCarsList[position]; } } }
// CARABSTRACTINVENTORY.CS using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Abner._658.IteratorPattern { abstract class CarAbstractInventory { public abstract Iterator CreateIterator(); } }
//NISSANDEALERSINVENTORY.CS using System; using System.Collections.Generic; using System.Collections; using System.Linq; using System.Text; namespace Abner._658.IteratorPattern { class NissanDealershipInventory : CarAbstractInventory { ArrayList NissanCarsArray = new ArrayList();
public override Iterator CreateIterator() { return new NissanDealershipIterator(NissanCarsArray); }
public int Count { get { return NissanCarsArray.Count; } }
public void addItems(Cars car) { NissanCarsArray.Add(car); }
} }
//TOYOTADEALERSHIPINVENTORY.CS using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Abner._658.IteratorPattern { class ToyotaDealershipInventory : CarAbstractInventory {
private List
public override Iterator CreateIterator() { return new ToyotaDealershipIterator(ToyotaCarsList); }
public int Count { get { return ToyotaCarsList.Count; } }
public void addItems(Cars car) { ToyotaCarsList.Add(car); } } } //BMWDEALERSHIPINVENTORY.CS using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Abner._658.IteratorPattern { class BmwDealershipInventory : CarAbstractInventory { private List
public override Iterator CreateIterator() { return new ToyotaDealershipIterator(BmwCarsList); }
public int Count { get { return BmwCarsList.Count; } }
public void addItems(Cars car) { BmwCarsList.Add(car); } } }
// CARS.CS using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Abner._658.IteratorPattern { class Cars { string Brand; string Model; string Description; double price; public Cars(string brand, string model, string description, double price) { this.Brand = brand; this.Model = model; this.Description = description; this.price = price; }
public string getBrand { get{return Brand;} } public string getModel { get { return Model; } } public string getDescription { get { return Description; } } public double getPrice { get { return price; } } } }
// Codes for Car Inventory Control Application (Composite Pattern)
//PRORAM.CS using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Abner._658.CompositePattern { class Program { static void Main(string[] args) { CarsComponent NissanCars = new Inventory("Nissan Car Inventory "); CarsComponent ToyotaCars = new Inventory("Toyota Car Inventory "); CarsComponent BMWCars = new Inventory(" BMW Car Inventory "); ToyotaCars.addItems(new Cars("Toyota","Corolla"," New red corolla with sports package ", 19999)); ToyotaCars.addItems(new Cars("Toyota", "Camry", " New silver camry with the basic package", 25999)); ToyotaCars.addItems(new Cars("Toyota", "Avalon", " Used black Avalon with leather seats ", 15999)); NissanCars.addItems(new Cars("Nissan", "Sentra", " Used black Sentra with basic package", 10999)); NissanCars.addItems(new Cars("Nissan", "Maxima", " New gray Maxima with all the upgrades", 31999)); NissanCars.addItems(new Cars("Nissan", "GT-R", " New blue GT-R with a v8 turbo engine", 35999)); BMWCars.addItems(new Cars("BMW", "325I", "New charcoal 2010 bmw325i 5speed manual ", 38999)); BMWCars.addItems(new Cars("BMW", "745I", "New metallic blue 2010 bmw745i luxury pckg ", 80999)); BMWCars.addItems(new Cars("BMW", "X6", "New black 2010 bmwX6 premium package ", 60999)); BMWCars.addItems(new Cars("BMW", "M5", "New Red 2010 bmw M5 6speed manual ", 58999)); CarsComponent AllInventory = new Inventory(" All THE CARS ON THE LOT !!!! "); AllInventory.addItems(ToyotaCars); AllInventory.addItems(NissanCars); AllInventory.addItems(BMWCars); AllInventory.PrintInventory(); //Dealer CarSalesMan = new Dealer(AllInventory); //CarSalesMan.print();
Console.ReadKey();
} } }
//CARSCOMPONENT.CS using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Abner._658.CompositePattern {
abstract class CarsComponent { public abstract int Count(); public abstract void addItems(CarsComponent CarsComponent); public abstract string getBrand(); public abstract string getModel(); public abstract string getDescription(); public abstract double getPrice(); public abstract void PrintInventory(); public abstract string InventoryName(); } }
//DEALER.CS using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Abner._658.CompositePattern { class Dealer { CarsComponent allCarInventory; public Dealer(CarsComponent allCarInventory) { this.allCarInventory = allCarInventory; } public void print() { allCarInventory.PrintInventory(); } } }
//INVENTORY.CS using System; using System.Collections.Generic; using System.Collections; using System.Linq; using System.Text; namespace Abner._658.CompositePattern { class Inventory : CarsComponent { ArrayList InventoryList = new ArrayList(); string name; //string description; public Inventory(string name) { this.name = name; }
public override string InventoryName() { return name; } public override string getModel() { throw new NotImplementedException(); } public override string getBrand() { throw new NotImplementedException(); } public override double getPrice() { throw new NotImplementedException(); } public override string getDescription() { throw new NotImplementedException(); } public override void addItems(CarsComponent CarsComponent) { InventoryList.Add(CarsComponent); } public override int Count() { return InventoryList.Count; }
public override void PrintInventory() { Console.WriteLine("\n" + InventoryName()); Console.WriteLine(); // don't forget to mention the IEnumerator in c# IEnumerator iterator = InventoryList.GetEnumerator();
while (iterator.MoveNext()) { CarsComponent carComponents = (CarsComponent)iterator.Current; carComponents.PrintInventory(); }
}
} } //CARS.CS using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Abner._658.CompositePattern { class Cars : CarsComponent { string Brand; string Model; string Description; double price; public Cars(string brand, string model, string description, double price) { this.Brand = brand; this.Model = model; this.Description = description; this.price = price; } //public override Iterator CreateIterator() //{ // throw new NotImplementedException(); //} public override string InventoryName() { throw new NotImplementedException(); }
public override string getModel() { return Model; } public override string getBrand() { return Brand; } public override double getPrice() { return price; } public override string getDescription() { return Description; } public override void addItems(CarsComponent CarsComponent) { throw new NotImplementedException(); } public override int Count() { throw new NotImplementedException(); }
public override void PrintInventory() { Console.WriteLine("{0} {1} {2} {3}", getBrand(), getModel(), getDescription(), getPrice());
Console.WriteLine("------"); } } }
// Codes for Vehicle Test (Template Method Pattern) using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Vehicle { class Program { static void Main() { VehicleDesign teamA = new Car(); teamA.Tasks();
Console.WriteLine("\n"); VehicleDesign teamB = new Truck(); teamB.Tasks();
Console.ReadKey(); }
// VehicleDesign.cs //superclass public abstract class VehicleDesign { //Template public void Tasks() { AssignedToCarDesign(); AssignedToEngine(); AssignedToProgram(); //condition if (VehicleFeaturesTested()) { Repair(); Test(); }
} //abstract for sub to use their own methods public abstract void Repair(); public abstract void Test();
public void AssignedToCarDesign() { Console.WriteLine("Team A is responsible for vehicle Design"); }
public void AssignedToEngine() { Console.WriteLine("Team B are assigned to build vehicle Engine"); }
public void AssignedToProgram() { Console.WriteLine("Joe and Tim are assigned to build software"); }
//Using Hook method To conform vehicle tests bool VehicleFeaturesTested() { return true; }
}
// Car.cs // ConcreteClassA public class Car : VehicleDesign { public override void Repair()
{ Console.WriteLine("Car components functioning well (y/n)?"); string answer = getUserInput();
if (answer.ToLower().StartsWith("y")) {
Console.WriteLine("All components of a car are functioning well"); } else { Console.WriteLine("Patrick repair the brakes"); Console.WriteLine("Brakes need to be fixed"); } }
public override void Test() { Console.WriteLine("\nCar features working well (y/n)?"); string answer = getUserInput(); if (answer.ToLower().StartsWith("y")) { Console.WriteLine("All features of a car are functioning well");
} else { Console.WriteLine("Paul test the audio system"); Console.WriteLine("Audio system is not functioning well"); }
} private string getUserInput() { string answer = null;
try { answer = Console.ReadLine(); }
catch { Console.Error.WriteLine("IO error trying to read your answer"); }
if (answer == "n") { return "no"; } else if (answer == "y") { return "y"; } else { return "no"; } }
}
// Truck.cs //ConcreteClassB public class Truck : VehicleDesign {
public override void Repair() { Console.WriteLine("Truck components functioning well (y/n)?"); string answer = getUserInput();
if (answer.ToLower().StartsWith("y")) {
Console.WriteLine("All components of a truck are functioning well"); } else { Console.WriteLine("Tom repair air conditioner"); Console.WriteLine("Air conditioner is not functioning well"); } }
public override void Test() { Console.WriteLine("\nTruck features working well (y/n)?"); string answer = getUserInput();
if (answer.ToLower().StartsWith("y")) { Console.WriteLine("All features of truck are functioning well"); } else { Console.WriteLine("Robert test the cruise control"); Console.WriteLine("Cruise control is not functioning well"); } }
private string getUserInput() { string answer = null;
try { answer = Console.ReadLine(); } catch { Console.Error.WriteLine("IO error trying to read your answer"); }
if (answer == "n") { return "no"; } else if (answer == "y") { return "y"; } else { return "no"; } } }
} }
// Code for Vehicle Gears application (State Pattern)
// Code in Program.cs class Program { static void Main(string[] args) {
AutomaticGear gear = new AutomaticGear(1);
gear.Info(); gear.parkingGear(); gear.driveGear(); gear.neutralGear(); gear.reverseGear(); gear.changeGear(); Console.WriteLine("\n");
gear.Info(); gear.reverseGear(); gear.parkingGear(); gear.driveGear(); gear.neutralGear(); gear.changeGear(); Console.WriteLine("\n");
gear.Info(); gear.neutralGear(); gear.parkingGear(); gear.driveGear(); gear.changeGear(); Console.WriteLine("\n");
gear.Info(); gear.driveGear(); gear.neutralGear(); gear.reverseGear(); gear.parkingGear(); gear.changeGear(); Console.WriteLine("\n");
gear.Info();
gear.driveGear(); gear.neutralGear(); gear.reverseGear(); gear.parkingGear();
Console.ReadKey(); } } }
// State.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace VehicleGears { public interface State { void parkingGear(); void reverseGear(); void changeGear(); void neutralGear(); void driveGear();
} }
// AutomaticGear.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace VehicleGears { public class AutomaticGear {
State parkingState; State driveState; State neutralState; State reverseState; State _state;
int count = 1; // constructor for state instances public AutomaticGear(int testNumber) { parkingState = new ParkingState(this); reverseState = new ReverseState(this); neutralState = new NeutralState(this); driveState = new DriveState(this); _state = new SetState(this); this.count = testNumber;
if (testNumber > 0) { _state = parkingState;
} }
public void parkingGear() {
_state.parkingGear();
}
public void reverseGear() {
_state.reverseGear(); }
public void changeGear() {
_state.changeGear();
}
public void neutralGear() {
_state.neutralGear(); }
public void driveGear() {
_state.driveGear(); } public void setState(State state) { this._state = state;
}
public void NumberOfTests() { if (count >= 1) { count += 1; } } // get States public State getParkingState() { return parkingState; } public State getNeutralState() { return neutralState; } public State getReverseState() { return reverseState; } public State getDriveState() { return driveState; }
public int getCount() { return count; }
public void Info() { if (count == 1) { Console.WriteLine("Test #1: Shift vehicle gear from parking to reverse\n"); } else if (count == 2) { Console.WriteLine("Test #2: Shift vehicle gear from reverse to neutral\n"); } else if (count == 3) { Console.WriteLine("Test #3: Shift vehicle gear from neutral to drive\n"); } else if (count == 4) { Console.WriteLine("Test #4: Shift vehicle gear from drive to parking\n"); } else { Console.WriteLine("Test #5: Check whether gear shifted to parking"); } }
}
}
// ParkingState.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace VehicleGears { public class ParkingState : State { AutomaticGear automaticGear;
public ParkingState(AutomaticGear automaticGear) { this.automaticGear = automaticGear;
} public void parkingGear() {
Console.WriteLine("Vehicle is currently in Parking gear"); }
public void reverseGear() { Console.WriteLine("Vehicle cannot switch to reverse gear until brake is applied "); }
public void changeGear() { Console.WriteLine("Brakes applied .... "); Console.WriteLine("Gear can shift to reverse gear now"); automaticGear.NumberOfTests(); if (automaticGear.getCount() == 2) { automaticGear.setState(automaticGear.getReverseState()); }
}
public void neutralGear() { Console.WriteLine("Gear is not in neutral"); // automaticGear.setState(automaticGear.getNeutralState()); }
public void driveGear() { Console.WriteLine("Gear is not in drive"); } } } // ReverseState.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace VehicleGears { public class ReverseState : State { AutomaticGear automaticGear;
public ReverseState(AutomaticGear automaticGear) { this.automaticGear = automaticGear; }
public void parkingGear() { Console.WriteLine("Gear is not in Parking"); }
public void reverseGear() { Console.WriteLine("Gear is in reverse "); }
public void changeGear() { Console.WriteLine("Brakes applied .... "); Console.WriteLine("Gear can shift to neutral gear now"); automaticGear.NumberOfTests(); if (automaticGear.getCount() == 3) { automaticGear.setState(automaticGear.getNeutralState());
} } public void neutralGear() { Console.WriteLine("Brake needs to be applied first before shifting to neutral"); }
public void driveGear() { Console.WriteLine("Gear is not in drive"); }
} }
// NeutralState.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace VehicleGears { public class NeutralState : State { AutomaticGear automaticGear;
public NeutralState(AutomaticGear automaticGear) { this.automaticGear = automaticGear; }
public void parkingGear() { Console.WriteLine("Gear is not in parking"); }
public void reverseGear() { Console.WriteLine("Gear is not in reverse "); }
public void changeGear() {
Console.WriteLine("Brakes applied .... "); Console.WriteLine("Gear can shift to drive gear now"); automaticGear.NumberOfTests(); if (automaticGear.getCount() == 4) { automaticGear.setState(automaticGear.getDriveState()); //change this to driveState();
} } public void neutralGear() { Console.WriteLine("Gear is currently in neutral");
}
public void driveGear() { Console.WriteLine("Vehicle cannot switch to drive gear until brake is applied"); }
} }
// DriveState.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace VehicleGears { public class DriveState : State { AutomaticGear automaticGear;
public DriveState(AutomaticGear automaticGear) { this.automaticGear = automaticGear; } public void parkingGear() { Console.WriteLine("Brakes need to be applied before shifting gear to parking"); }
public void reverseGear() { Console.WriteLine("Gear is not in reverse"); }
public void changeGear() { Console.WriteLine("Brakes applied .... "); Console.WriteLine("Gear can shift to parking gear now"); automaticGear.NumberOfTests(); if (automaticGear.getCount() == 5) { automaticGear.setState(automaticGear.getParkingState()); } }
public void neutralGear() { Console.WriteLine("Gear is not in neutral"); }
public void driveGear() { Console.WriteLine("Vehicle is currently in drive gear"); } }
} //SetState.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace VehicleGears { public class SetState : State { AutomaticGear automaticGear;
public SetState(AutomaticGear automaticGear) { this.automaticGear = automaticGear; } public void parkingGear() { } public void reverseGear() { } public void neutralGear() { } public void changeGear() { } public void driveGear() { } } }