From e76b96dab4f6ce20088b4b16a0e71724f1a3221e Mon Sep 17 00:00:00 2001 From: Sayfullah Eid Date: Mon, 6 Dec 2021 16:17:27 -0500 Subject: [PATCH] Fixed bug with `Graph.addAttack()` Fixed bug where new `Attack` objects created in outbreak were not parsing due to string error --- src/Attack.java | 9 +++--- src/Graph.java | 78 +++++++++++++++++++++---------------------------- src/Main.java | 11 ------- structure.puml | 2 +- 4 files changed, 38 insertions(+), 62 deletions(-) diff --git a/src/Attack.java b/src/Attack.java index 3328ce6..ddddce3 100644 --- a/src/Attack.java +++ b/src/Attack.java @@ -18,17 +18,16 @@ public class Attack { private String city; /** - * Basic constructor of Attack object. The{@code dateTime String} will be parsed - * into a {@code Date} object. + * Basic constructor of Attack object. Mainly used for outbreaks * * @param type Type of attack - * @param dateTime String of date and time of attack (yyyy-MM-dd HH:mm:ss) + * @param dateTime {@code Date} object of date and time of attack * @param city String name of the target city * @throws ParseException Throws exception if parsing dateTime String fails */ - public Attack(Type type, String dateTime, String city) throws ParseException { + public Attack(Type type, Date dateTime, String city) { this.type = type; - this.dateTime = toDate(dateTime); + this.dateTime = dateTime; this.city = city; } diff --git a/src/Graph.java b/src/Graph.java index 9ebbdab..b582a53 100644 --- a/src/Graph.java +++ b/src/Graph.java @@ -112,52 +112,41 @@ private City[] findCities(String c1, String c2) { * * @param a Attack object to be parsed */ - public void attack(Attack a) { - // TODO: #2 Implement logic for adding attacks to graphs + public void attack(Attack a) { /* - * Pseudo Code: - * Parse the attack object "a" --> in Attack - * Get the city - * add attack to the city - * if city has routes - * add attack to the routes as well - * redo the process for the routes as well - * recursive method?? - * */ + * Pseudo Code: + * Parse the attack object "a" --> in Attack + * Get the city + * add attack to the city + * if city has routes + * add attack to the routes as well + * redo the process for the routes as well + * recursive method?? + */ // Get the city String targetCity = a.getCity(); // If the targetCity has routes - //if() + // if() // Add the attack to the city? // if the gotten city is equal to its keyset, set that city as target for (City city : this.citiesMap.keySet()) { if (city.getName().compareToIgnoreCase(targetCity) == 0) { // found city representing attack source - if (city.addAttack(a)){ - City [] cities = this.citiesMap.get(city); - for(City connectedCity : cities){ - try{ - Attack connectedAttack = new Attack(a.getType(), a.getDateTime().toString(), connectedCity.getName()); - this.attack(connectedAttack); - } - catch (ParseException e) { - System.out.println("Parse Error"); - System.exit(1); - } - } - } + if (city.addAttack(a)) { + City[] cities = this.citiesMap.get(city); + for (City connectedCity : cities) { + Attack connectedAttack = new Attack(a.getType(), a.getDateTime(), + connectedCity.getName()); + this.attack(connectedAttack); + } + } } } - - - - - } /** @@ -213,7 +202,6 @@ public String findPath(String c1, String c2) { } } - return "No path available between '" + c1 + "' and '" + c2 + "'"; } @@ -226,10 +214,10 @@ public String findPath(String c1, String c2) { public String isInfected() { String str = ""; int count = 0; - for (City city : this.citiesMap.keySet()){ - if (city.getCurrStatus() == City.Status.UNSAFE || city.getCurrStatus() == City.Status.OUTBREAK){ + for (City city : this.citiesMap.keySet()) { + if (city.getCurrStatus() == City.Status.UNSAFE || city.getCurrStatus() == City.Status.OUTBREAK) { count++; - str += String.valueOf(count)+ ' '+ city.getName()+'\n'; + str += String.valueOf(count) + ' ' + city.getName() + '\n'; } } return str; @@ -244,10 +232,10 @@ public String isInfected() { public String hasFirewall() { String str = ""; int count = 0; - for (City city : this.citiesMap.keySet()){ - if (city.getFirewall() == true){ + for (City city : this.citiesMap.keySet()) { + if (city.getFirewall() == true) { count++; - str += String.valueOf(count) + ' ' + city.getName() +'\n'; + str += String.valueOf(count) + ' ' + city.getName() + '\n'; } } return str; @@ -262,10 +250,10 @@ public String hasFirewall() { public String hasAttackedFirewall() { String str = ""; int count = 0; - for (City city : this.citiesMap.keySet()){ - if (city.getFirewall() == true && city.getAttacks().isEmpty() == false){ + for (City city : this.citiesMap.keySet()) { + if (city.getFirewall() == true && city.getAttacks().isEmpty() == false) { count++; - str += String.valueOf(count) + ' ' +city.getName() + '\n'; + str += String.valueOf(count) + ' ' + city.getName() + '\n'; } } return str; @@ -279,10 +267,10 @@ public String hasAttackedFirewall() { public String outbreaks() { String str = ""; int count = 0; - for (City city : this.citiesMap.keySet()){ - if (city.getCurrStatus() == City.Status.OUTBREAK){ + for (City city : this.citiesMap.keySet()) { + if (city.getCurrStatus() == City.Status.OUTBREAK) { count++; - str += String.valueOf(count) + ' ' + city.getName() +'\n'; + str += String.valueOf(count) + ' ' + city.getName() + '\n'; } } return str; @@ -296,8 +284,8 @@ public String outbreaks() { public String inactive() { String str = ""; int count = 0; - for (City city : this.citiesMap.keySet()){ - if (city.getCurrStatus() == City.Status.OFFLINE){ + for (City city : this.citiesMap.keySet()) { + if (city.getCurrStatus() == City.Status.OFFLINE) { count++; str += String.valueOf(count) + ' ' + city.getName() + '\n'; } diff --git a/src/Main.java b/src/Main.java index c81c753..66b0b8c 100644 --- a/src/Main.java +++ b/src/Main.java @@ -139,7 +139,6 @@ private static void aMenu() { input = in.next(); switch (input) { -<<<<<<< HEAD case "q" -> aRun = false; case "Q" -> aRun = false; default -> { @@ -149,16 +148,6 @@ private static void aMenu() { for (Attack attack : attacksA) { mainframe.attack(attack); } -======= - case "q" -> aRun = false; - case "Q" -> aRun = false; - default -> { - parseAttacks(input); - out.println("Read in " + attacksA.length + " attacks."); - //sortAttacks(attacksA, 0, attacksA.length - 1); - for (Attack attack : attacksA) { - mainframe.attack(attack); ->>>>>>> 579261d06aac0500fda6a845e0a8b490f6844334 } } } diff --git a/structure.puml b/structure.puml index b9af18f..1276619 100644 --- a/structure.puml +++ b/structure.puml @@ -30,7 +30,7 @@ class Attack { -city String --[Constructors]-- +Attack(String, String, String, String) - +Attack(Type, String, String, String) + +Attack(Type, Date, String, String) --[Methods]-- -toDate(String) Date +toEpoch() long