Skip to content

Commit

Permalink
Fixed bug with Graph.addAttack()
Browse files Browse the repository at this point in the history
Fixed bug where new `Attack` objects created in outbreak were not parsing due to string error
  • Loading branch information
FusionStreak committed Dec 6, 2021
1 parent ba17cce commit e76b96d
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 62 deletions.
9 changes: 4 additions & 5 deletions src/Attack.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
78 changes: 33 additions & 45 deletions src/Graph.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

}

}






}

/**
Expand Down Expand Up @@ -213,7 +202,6 @@ public String findPath(String c1, String c2) {
}
}


return "No path available between '" + c1 + "' and '" + c2 + "'";
}

Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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';
}
Expand Down
11 changes: 0 additions & 11 deletions src/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,6 @@ private static void aMenu() {

input = in.next();
switch (input) {
<<<<<<< HEAD
case "q" -> aRun = false;
case "Q" -> aRun = false;
default -> {
Expand All @@ -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
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion structure.puml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit e76b96d

Please sign in to comment.