Details | Last modification | View Log | SVN | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 915 | migan | 1 | import items.*; |
| 2 | |||
| 3 | import java.io.BufferedReader; |
||
| 4 | import java.io.InputStreamReader; |
||
| 5 | import java.util.Iterator; |
||
| 6 | |||
| 7 | |||
| 8 | public class Input |
||
| 9 | { |
||
| 10 | public static void addClass(Database db) throws Exception |
||
| 11 | { |
||
| 12 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
||
| 13 | |||
| 14 | System.out.println("You want to create a new class, please answer the questions."); |
||
| 15 | |||
| 16 | String nameString = ""; |
||
| 17 | String descriptionString = ""; |
||
| 18 | |||
| 19 | while (true) |
||
| 20 | { |
||
| 21 | System.out.print("Name: "); |
||
| 22 | |||
| 23 | nameString = br.readLine().trim(); |
||
| 24 | |||
| 25 | if (nameString.length() == 0) |
||
| 26 | { |
||
| 27 | System.out.println("Name can not be empty"); |
||
| 28 | } |
||
| 29 | else |
||
| 30 | { |
||
| 31 | if (db.getClassByName(nameString) != null) |
||
| 32 | { |
||
| 33 | System.out.println("A class by that name already exists, enter another name"); |
||
| 34 | nameString = ""; |
||
| 35 | } |
||
| 36 | else |
||
| 37 | { |
||
| 38 | break; |
||
| 39 | } |
||
| 40 | } |
||
| 41 | } |
||
| 42 | |||
| 43 | System.out.print("Description: "); |
||
| 44 | descriptionString = br.readLine().trim(); |
||
| 45 | |||
| 46 | CanClass canClass = new CanClass(); |
||
| 47 | canClass.setName(nameString); |
||
| 48 | canClass.setDescription(descriptionString); |
||
| 49 | canClass.setId(db.getNewClassId(canClass)); |
||
| 50 | |||
| 51 | System.out.println("Adding new class"); |
||
| 52 | db.addClass(canClass); |
||
| 53 | System.out.println("Successfully added class:"); |
||
| 54 | System.out.println(canClass.toString()); |
||
| 55 | } |
||
| 56 | |||
| 57 | public static void addCommand(Database db) throws Exception |
||
| 58 | { |
||
| 59 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
||
| 60 | |||
| 61 | System.out.println("You want to create a new command, please answer the questions."); |
||
| 62 | |||
| 63 | String nameString = ""; |
||
| 64 | CommandType commandType; |
||
| 65 | String descriptionString = ""; |
||
| 66 | String moduleString = ""; |
||
| 67 | |||
| 68 | System.out.println("Avaiable command types:"); |
||
| 69 | |||
| 70 | Iterator<CommandType> iterator = db.getCommandTypes().iterator(); |
||
| 71 | |||
| 72 | while (iterator.hasNext()) |
||
| 73 | { |
||
| 74 | System.out.println("* " + iterator.next().getName()); |
||
| 75 | } |
||
| 76 | |||
| 77 | while (true) |
||
| 78 | { |
||
| 79 | System.out.print("Type: "); |
||
| 80 | |||
| 81 | String typeString = br.readLine().trim(); |
||
| 82 | |||
| 83 | if (typeString.length() == 0) |
||
| 84 | { |
||
| 85 | System.out.println("Type can not be empty"); |
||
| 86 | } |
||
| 87 | else |
||
| 88 | { |
||
| 89 | commandType = db.getCommandType(typeString); |
||
| 90 | if (commandType == null) |
||
| 91 | { |
||
| 92 | System.out.println("That type does not exist, choose one from the list above"); |
||
| 93 | } |
||
| 94 | else |
||
| 95 | { |
||
| 96 | break; |
||
| 97 | } |
||
| 98 | } |
||
| 99 | } |
||
| 100 | |||
| 101 | while (true) |
||
| 102 | { |
||
| 103 | System.out.print("Name: "); |
||
| 104 | |||
| 105 | nameString = br.readLine().trim(); |
||
| 106 | |||
| 107 | if (nameString.length() == 0) |
||
| 108 | { |
||
| 109 | System.out.println("Name can not be empty"); |
||
| 110 | } |
||
| 111 | else |
||
| 112 | { |
||
| 113 | Command command = db.getCommandByName(nameString); |
||
| 114 | if (command != null) |
||
| 115 | { |
||
| 116 | if (command.getType().getName().compareTo(commandType.getName()) == 0) |
||
| 117 | { |
||
| 118 | System.out.println("A command by that name already exists, enter another name"); |
||
| 119 | nameString = ""; |
||
| 120 | } |
||
| 121 | else |
||
| 122 | { |
||
| 123 | break; |
||
| 124 | } |
||
| 125 | } |
||
| 126 | else |
||
| 127 | { |
||
| 128 | break; |
||
| 129 | } |
||
| 130 | } |
||
| 131 | } |
||
| 132 | |||
| 133 | |||
| 134 | |||
| 135 | if (commandType.getName().compareTo("module") == 0) |
||
| 136 | { |
||
| 137 | while (true) |
||
| 138 | { |
||
| 139 | System.out.print("Module name: "); |
||
| 140 | |||
| 141 | moduleString = br.readLine().trim(); |
||
| 142 | |||
| 143 | if (moduleString.length() == 0) |
||
| 144 | { |
||
| 145 | System.out.println("Module name can not be empty"); |
||
| 146 | } |
||
| 147 | else |
||
| 148 | { |
||
| 149 | ///FIXME: Maybe check against existing modules? |
||
| 150 | break; |
||
| 151 | } |
||
| 152 | } |
||
| 153 | } |
||
| 154 | |||
| 155 | System.out.print("Description: "); |
||
| 156 | descriptionString = br.readLine().trim(); |
||
| 157 | |||
| 158 | Command command = new Command(); |
||
| 159 | command.setName(nameString); |
||
| 160 | command.setModule(moduleString); |
||
| 161 | command.setDescription(descriptionString); |
||
| 162 | command.setType(commandType); |
||
| 163 | command.setId(db.getNewCommandId(command)); |
||
| 164 | |||
| 165 | System.out.println("Adding new command"); |
||
| 166 | db.addCommand(command); |
||
| 167 | System.out.println("Successfully added command:"); |
||
| 168 | System.out.println(command.toString()); |
||
| 169 | } |
||
| 170 | |||
| 171 | public static void addModule(Database db) throws Exception |
||
| 172 | { |
||
| 173 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
||
| 174 | |||
| 175 | System.out.println("You want to create a new module type, please answer the questions."); |
||
| 176 | |||
| 177 | String classString = ""; |
||
| 178 | String nameString = ""; |
||
| 179 | String descriptionString = ""; |
||
| 180 | |||
| 181 | while (true) |
||
| 182 | { |
||
| 183 | System.out.print("Name: "); |
||
| 184 | |||
| 185 | nameString = br.readLine().trim(); |
||
| 186 | |||
| 187 | if (nameString.length() == 0) |
||
| 188 | { |
||
| 189 | System.out.println("Name can not be empty"); |
||
| 190 | } |
||
| 191 | else |
||
| 192 | { |
||
| 193 | if (db.getModuleByName(nameString) != null) |
||
| 194 | { |
||
| 195 | System.out.println("A module type by that name already exists, enter another name"); |
||
| 196 | nameString = ""; |
||
| 197 | } |
||
| 198 | else |
||
| 199 | { |
||
| 200 | break; |
||
| 201 | } |
||
| 202 | } |
||
| 203 | } |
||
| 204 | |||
| 205 | System.out.println("Avaiable classes:"); |
||
| 206 | |||
| 207 | Iterator<CanClass> iterator = db.getClasses().iterator(); |
||
| 208 | |||
| 209 | while (iterator.hasNext()) |
||
| 210 | { |
||
| 211 | System.out.println("* " + iterator.next().getName()); |
||
| 212 | } |
||
| 213 | |||
| 214 | while (true) |
||
| 215 | { |
||
| 216 | System.out.print("Class: "); |
||
| 217 | |||
| 218 | classString = br.readLine().trim(); |
||
| 219 | |||
| 220 | if (classString.length() == 0) |
||
| 221 | { |
||
| 222 | System.out.println("Class can not be empty"); |
||
| 223 | } |
||
| 224 | else |
||
| 225 | { |
||
| 226 | if (db.getClassByName(classString) == null) |
||
| 227 | { |
||
| 228 | System.out.println("That class does not exist, choose one from the list above"); |
||
| 229 | } |
||
| 230 | else |
||
| 231 | { |
||
| 232 | break; |
||
| 233 | } |
||
| 234 | } |
||
| 235 | } |
||
| 236 | |||
| 237 | System.out.print("Description: "); |
||
| 238 | descriptionString = br.readLine().trim(); |
||
| 239 | |||
| 240 | Module module = new Module(); |
||
| 241 | module.setName(nameString); |
||
| 242 | module.setDescription(descriptionString); |
||
| 243 | module.setId(db.getNewModuleId(module)); |
||
| 244 | module.setCanClass(db.getClassByName(classString)); |
||
| 245 | |||
| 246 | System.out.println("Adding new module type"); |
||
| 247 | db.addModule(module); |
||
| 248 | System.out.println("Successfully added module type:"); |
||
| 249 | System.out.println(module.toString()); |
||
| 250 | } |
||
| 251 | } |