Write an application that tracks information about monster attacks. IMPORTANT: D
ID: 3885089 • Letter: W
Question
Write an application that tracks information about monster attacks.
IMPORTANT: DON'T use JOptionPane I/O. Please Use Console I/O (Scanners and System.out) for all input/output in this lab.
For this lab you will create a MonsterAttack class that represents one monster attack, an AttackMonitor class that aggregates data about attacks, and an MonsterAttackDriver class that instantiates an AttackMonitor. One of the key functions of AttackMonitor is to read and write data using a text file, but you will complete this part in a future lab. Note that nearly all the methods in AttackMonitor and MonsterAttack will be instance, not static, methods.
Part I - MonsterAttack class
Each object of this class represents information about a single attack. The data on each attack includes an integer attack id, three ints representing the day of the month, the month, and the year of the attack, the name of the monster involved (assume that each attack involves only one monster), the location of the attack (String), and the name of the person reporting the attack (String).
MonsterAttack needs a constructor that sets all the instance fields listed above using its parameters. It should take the three components of the date as a single String in the format MM/DD/YYYY (eg, 01/31/2010). Use String's split method to split this String on the slashes, parse the resulting Strings to ints, and use them to set the values of the month, day, and year.
Monster attack also needs getters and setters for all the data fields and a toString() that uses the instance data to create and return a String similar to this: "Attack # 125: Godzilla attacked Tokyo on 6/25/2009. Reported by Kyohei Yamane."
Part II - AttackMonitor
Create an AttackMonitor class that keeps track of the MonsterAttack objects. It should contain an array list (not an array) of MonsterAttacks and an instance method called monitor() with a menu that allows the user to choose to quit or to do any of the tasks listed below, which must all be implemented in separate methods. After doing any of these tasks, the program should return to the menu, so that the user can continue working on any series of tasks until he/she chooses to quit. Write instance, not static, methods to do each of the following:
Input information on a MonsterAttack. Get the data from the user, call the MonsterAttack constructor using the input data as arguments, and add the new attack to the list of attacks.
Show the current list of attacks by iterating through the list, calling the toString() of each attack, and outputting the result. Write this method so that the program does not crash if the list is empty.
Choose an attack from the array list and delete it. The easiest way to do this is to call the method that shows the information, then ask the user for the id number of the attack to delete. Use linear search to find the attack with that id and delete it. The id number may not be the same as the index in the list. Make sure the application does not crash if the user enters an id that does not appear in the list.
AttackMonitor must also have a method to create a MonsterAttack using method parameters and add it to the list. In other words, this method will take parameters for the data fields, rather than taking them as user input. You will call this method from the Driver to create test data without needing to type it.
Part III: MonsterAttackDriver
The driver class should instantiate an AttackMonitor and call its monitor() method.
In order to make the application easier to test, the driver should provide hard coded data on three attacks. Use the data to create the three attacks and add them to the list in the monitor. Use a separate method for this and call it from main().
Explanation / Answer
Hi,
Please see below the answer. Please comment for any queries/feedbacks.
Thanks!
MonsterAttack.java
public class MonsterAttack {
private int attackId;
private int day;
private int month;
private int year;
private String monsterName;
private String location;
private String reporterName;
//Constructor
public MonsterAttack(int attackId, String date,
String monsterName, String location, String reporterName) {
super();
this.attackId = attackId;
//Setting the day , month and year values
String [] dateArr = date.split("/");
this.day = Integer.valueOf(dateArr[1]);
this.month = Integer.valueOf(dateArr[0]);
this.year = Integer.valueOf(dateArr[2]);
this.monsterName = monsterName;
this.location = location;
this.reporterName = reporterName;
}
//Getters and Setters
public int getAttackId() {
return attackId;
}
public void setAttackId(int attackId) {
this.attackId = attackId;
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public String getMonsterName() {
return monsterName;
}
public void setMonsterName(String monsterName) {
this.monsterName = monsterName;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public String getReporterName() {
return reporterName;
}
public void setReporterName(String reporterName) {
this.reporterName = reporterName;
}
//ToString
public String toString(){
String retStr = "";
retStr = "Attack # "+this.getAttackId()+" : "+this.getMonsterName()+" attacked "+this.getLocation()
+" on "+this.getMonth()+"/"+this.getDay()+"/"+this.getYear()+"."
+" Reported by "+this.getReporterName();
return retStr;
}
}
AttackMonitor.java
import java.util.ArrayList;
import java.util.Scanner;
public class AttackMonitor {
//array list (not an array) of MonsterAttacks
ArrayList<MonsterAttack> attackList = new ArrayList<MonsterAttack>();
Scanner scan = new Scanner(System.in);
public void monitor() {
Character choice = 'C';
String choiceStr = "";
while('C'==(choice)){
System.out.println("Please enter a choice:");
System.out.println("1. Input data");
System.out.println("2. List attacks");
System.out.println("3. Delete");
choiceStr= scan.nextLine();
choice = choiceStr.charAt(0);
switch(choice){
case '1' : inputData();
break;
case '2' : showList();
break;
case '3' : deleteAttck();
break;
}
System.out.println("Enter C to continue, Q to Quit");
choiceStr = scan.nextLine();
choice = choiceStr.charAt(0);
}
}
//Input information on a MonsterAttack.
//Get the data from the user, call the MonsterAttack constructor using the input data as arguments,
//and add the new attack to the list of attacks.
public void inputData(){
int attackId =0 ;
String monsterName ="";
String date ="";
String location = "";
String reporter ="";
System.out.println("Attack Id: ");
attackId = Integer.valueOf(scan.nextLine());
System.out.println("Monster name: ");
monsterName = scan.nextLine();
System.out.println("Date in DD/MM/YYYY format : ");
date = scan.nextLine();
System.out.println("Location : ");
location = scan.nextLine();
System.out.println("Reporter : ");
reporter = scan.nextLine();
//Creating MonsterAttack object
MonsterAttack attack = new MonsterAttack(attackId, date, monsterName, location, reporter);
//Adding to list
this.getAttackList().add(attack);
System.out.println("Attack added!");
}
//Show the current list of attacks by iterating through the list,
//calling the toString() of each attack, and outputting the result.
//Write this method so that the program does not crash if the list is empty.
public void showList(){
//Checking if list is empty
if(null!=this.getAttackList() && this.getAttackList().size()>0){
for(int i = 0;i<this.getAttackList().size();i++){ //looping through the list
MonsterAttack attack = this.getAttackList().get(i);
System.out.println(attack.toString());
}
}
else{
System.out.println("List is empty ");
}
}
//Choose an attack from the array list and delete it.
//The easiest way to do this is to call the method that shows the information,
//then ask the user for the id number of the attack to delete.
//Use linear search to find the attack with that id and delete it.
//The id number may not be the same as the index in the list.
//The application does not crash if the user enters an id that does not appear in the list.
public void deleteAttck(){
showList();
//ask the user for the id number of the attack to delete
System.out.println("Choose an Id :");
boolean deleted = false;
int idToDelete = Integer.valueOf(scan.nextLine());
for(int i = 0;i<this.getAttackList().size();i++){ //looping through the list
MonsterAttack attack = this.getAttackList().get(i);
if(idToDelete == attack.getAttackId()){
//delete the attck from list
this.getAttackList().remove(i);
System.out.println("Attck # "+idToDelete+" deleted ");
deleted =true;
break;
}
}
if(!deleted){
System.out.println("No Attack found for the Id.");
}
}
// method to create a MonsterAttack using method parameters and add it to the list.
//In other words, this method will take parameters for the data fields, rather than taking them as user input.
public void createAttack(int attackId, String date, String monsterName, String location, String reporter){
//Creating MonsterAttack object
MonsterAttack attack = new MonsterAttack(attackId, date, monsterName, location, reporter);
//Adding to list
this.getAttackList().add(attack);
System.out.println("Attack created and added!");
}
//Getters and setters
public ArrayList<MonsterAttack> getAttackList() {
return attackList;
}
public void setAttackList(ArrayList<MonsterAttack> attackList) {
this.attackList = attackList;
}
}
MonsterAttackDriver.java
import java.util.ArrayList;
public class MonsterAttackDriver {
/**
* @param args
*/
public static void main(String[] args) {
AttackMonitor attackMonitor = new AttackMonitor();
attackMonitor.monitor();
//the driver should provide hard coded data on three attacks.
//Use the data to create the three attacks and add them to the list in the monitor.
attackMonitor.createAttack(8999, "12/30/2010", "DragonR", "Florida", "Wayne Smith");
attackMonitor.createAttack(9677, "09/28/2013", "Drake", "Texas", "Robert Williams");
attackMonitor.createAttack(9899, "07/17/2016", "Godzilla", "New York", "Samson Peter");
}
}
Sample output:
Please enter a choice:
1. Input data
2. List attacks
3. Delete
1
Attack Id:
4565
Monster name:
J1
Date in DD/MM/YYYY format :
09/14/2011
Location :
Illinois
Reporter :
Staley Matt
Attack added!
Enter C to continue, Q to Quit
C
Please enter a choice:
1. Input data
2. List attacks
3. Delete
2
Attack # 4565 : J1 attacked Illinois on 9/14/2011. Reported by Staley Matt
Enter C to continue, Q to Quit
C
Please enter a choice:
1. Input data
2. List attacks
3. Delete
1
Attack Id:
6767
Monster name:
J2
Date in DD/MM/YYYY format :
04/15/2012
Location :
Tampa
Reporter :
Graham Stains
Attack added!
Enter C to continue, Q to Quit
C
Please enter a choice:
1. Input data
2. List attacks
3. Delete
3
Attack # 4565 : J1 attacked Illinois on 9/14/2011. Reported by Staley Matt
Attack # 6767 : J2 attacked Tampa on 4/15/2012. Reported by Graham Stains
Choose an Id :
6767
Attck # 6767 deleted
Enter C to continue, Q to Quit
C
Please enter a choice:
1. Input data
2. List attacks
3. Delete
2
Attack # 4565 : J1 attacked Illinois on 9/14/2011. Reported by Staley Matt
Enter C to continue, Q to Quit
Q
Attack created and added!
Attack created and added!
Attack created and added!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.