public EnemyShip makeEnemyShip(String newShipType) { EnemyShip newShip = null; if (newShipType.Equals("U")) { return(new UFOEnemyShip()); } else { if (newShipType.Equals("R")) { return(new UFOEnemyShip()); } else { if (newShipType.Equals("B")) { return(new BigUFOEnemyShip()); } else { return(null); } } } }
static void Main(string[] args) { EnemyShipFactory shipFactory = new EnemyShipFactory(); EnemyShip theEnemy = null; string readLine; readLine = Console.ReadLine(); //Scanner userInput = new Scanner(System.in); Console.WriteLine("What type of ship? (U / R / B)"); String typeOfShip = readLine; theEnemy = shipFactory.makeEnemyShip(typeOfShip); if (theEnemy != null) { DoStuffEnemy(theEnemy); } else { Console.WriteLine("Enter U / R / B"); } }
/// <summary> /// Makes a new instance of a subclass of enemyship /// </summary> /// <param name="shipType">some input to evaluate at runtime</param> /// <returns>an new instance based on runtime input</returns> public EnemyShip MakeEnemyShip(string shipType) { EnemyShip newShip = null; if (shipType == "rocket") { newShip = new RocketEnemyShip(); } else if (shipType == "big") { newShip = new BigUFOEnemyShip(); } else { newShip = new UFOEnemyShip(); } return newShip }
private static void DoStuff(EnemyShip ufo) { ufo.DisplayEnemyShip(); ufo.FollowHeroShip(); ufo.EnemyShipShoots(); }
public static void DoStuffEnemy(EnemyShip anEnemyShip) { anEnemyShip.DisplayEnenmyShip(); anEnemyShip.FollowHeroShip(); anEnemyShip.EnemyShipShoots(); }
// Executes methods of the super class public static void doStuffEnemy(EnemyShip anEnemyShip) { anEnemyShip.displayEnemyShip(); anEnemyShip.followHeroShip(); anEnemyShip.enemyShipShoots(); }
static void Main(string[] args) { // Create the factory object EnemyShipFactory shipFactory = new EnemyShipFactory(); // Enemy ship object EnemyShip theEnemy = null; Console.WriteLine("What type of ship? (U / R / B)"); var txt = Console.ReadLine(); if (!string.IsNullOrEmpty(txt)) { String typeOfShip = txt; theEnemy = shipFactory.makeEnemyShip(typeOfShip); if (theEnemy != null) { doStuffEnemy(theEnemy); } else { Console.WriteLine("Please enter U, R, or B next time"); } } /* * EnemyShip theEnemy = null; * * // Old way of creating objects * // When we use new we are not being dynamic * * EnemyShip ufoShip = new UFOEnemyShip(); * * doStuffEnemy(ufoShip); * * System.out.print("\n"); * * // ----------------------------------------- * * // This allows me to make the program more dynamic * // It doesn't close the code from being modified * // and that is bad! * * // Defines an input stream to watch: keyboard * Scanner userInput = new Scanner(System.in); * * String enemyShipOption = ""; * * System.out.print("What type of ship? (U or R)"); * * if (userInput.hasNextLine()){ * * enemyShipOption = userInput.nextLine(); * * } * * if (enemyShipOption == "U"){ * * theEnemy = new UFOEnemyShip(); * * * } else * * if (enemyShipOption == "R"){ * * theEnemy = new RocketEnemyShip(); * * } else { * * theEnemy = new BigUFOEnemyShip(); * * } * * doStuffEnemy(theEnemy); * * // -------------------------------------------- */ }