In: Computer Science
Complete the Monster Attack project by doing the following:
In place of the driver used in homework 2, add a menu with a loop and switch. The user must be able to add attacks, get a report on existing attacks, sve a file, retrieve a file, and clear the list as many times as she wants in any order she wants.
Replace the array of MonsterAttacks with an ArrayList of MonsterAttacks. This will require changes in several methods. You will need to be able to handle any number of MonsterAttacks the user chooses to input.
Add a method to AttackMonitor that saves the list of attacks to a comma separated values file. Iterate through the list, and for each attack, get each field using the getters from MonsterAttack. Write each value to the file, following each one except the last with a comma. Save the date as a single String in the format MM/DD/YYYY. After you have written out all the data for one attack, write out a newline. Add an item to the main menu that calls this method.
Add a method that clears the list of monster attacks, then uses a Scanner to read data from a .csv file, uses it to instantiate MonsterAttack objects, and adds the attacks to the list. This method must be able to read the files you write out in the method described above. You will need to use String's split() method here. Add an item to the main menu that calls this method. Make sure you can input attack data, save to a file, quit the program, start the program again, read your output file, and show the data from the file.
this is in java code
AttackMonster.java
import java.util.Scanner;
import java.util.Date;
public class AttackMonster {
public MonsterAttack[] attacks;
public void reportAttacks() {
this.attacks = new MonsterAttack[5];
int i;
Scanner scan;
for(i=0; i<5; i++) {
scan = new Scanner(System.in);
System.out.print("Enter the name of the monster: ");
String m = scan.nextLine();
System.out.print("Enter location of attack: ");
String l = scan.nextLine();
System.out.print("Enter amount of damage: ");
double dam = scan.nextDouble();
System.out.print("Enter date of attack: ");
int d = scan.nextInt();
System.out.print("Enter month of attack: ");
int mon = scan.nextInt();
System.out.print("Enter year of attack: ");
int y = scan.nextInt();
@SuppressWarnings("deprecation")
Date date = new Date(d, mon, y);
MonsterAttack ma = new MonsterAttack(m,l, dam, date);
attacks[i] = ma;
} //End of for
}
public void showAttacks() {
int i;
for(i=0; i<5; i++) {
System.out.println(this.attacks[i].toString());
}
}
public void calculateDamages() {
double mean=0.0;
int i;
for(i=0; i mean = this.attacks[i].damagesInMillionUSD;
}
mean = mean/this.attacks.length;
System.out.println("Mean damages of attacks in millions USD: " +
mean);
}
public void showMonsters() {
for(MonsterAttack ma: this.attacks) {
System.out.println(ma.getMonsterName());
}
}
public void getEarliestAttack() {
MonsterAttack m = this.attacks[0];
for(MonsterAttack ma: this.attacks) {
if(m.getDate().after(ma.getDate())) {
m = ma;
}
}
System.out.println("Earliest attack: \n" + m.toString());
}
}
MonsterAttack.java
import java.util.Date;
public class MonsterAttack {
String monsterName;
String attackLocation;
double damagesInMillionUSD;
Date date;
public MonsterAttack(String monsterName, String attackLocation,
double damagesInMillionUSD, Date date) {
super();
this.monsterName = monsterName;
this.attackLocation = attackLocation;
this.damagesInMillionUSD = damagesInMillionUSD;
this.date = date;
}
/**
* return the monsterName
*/
public String getMonsterName() {
return monsterName;
}
/**
* param monsterName the monsterName to set
*/
public void setMonsterName(String monsterName) {
this.monsterName = monsterName;
}
/**
* return the attackLocation
*/
public String getAttackLocation() {
return attackLocation;
}
/**
* param attackLocation the attackLocation to set
*/
public void setAttackLocation(String attackLocation) {
this.attackLocation = attackLocation;
}
/**
* return the damagesInMillionUSD
*/
public double getDamagesInMillionUSD() {
return damagesInMillionUSD;
}
/**
* \param damagesInMillionUSD the damagesInMillionUSD to set
*/
public void setDamagesInMillionUSD(double damagesInMillionUSD)
{
this.damagesInMillionUSD = damagesInMillionUSD;
}
/**
* return the date
*/
public Date getDate() {
return date;
}
/**
* param date the date to set
*/
public void setDate(Date date) {
this.date = date;
}
public String toString() {
String str = "Monster name: " + this.monsterName+
" Attack Location: " + this.attackLocation+
" Damage in millions USD : " + this.damagesInMillionUSD+
" Date of attack: " + this.date;
return str;
}
}
MonsterDriver.java
public class MonsterDriver {
public static void main(String[] args) {
AttackMonster am = new
AttackMonster();
am.reportAttacks();
am.showAttacks();
am.showMonsters();
am.calculateDamages();
am.getEarliestAttack();
}
}
This was part 1 to code the first part: Please help me asap
Part A
Create a MonsterAttack class. MonsterAttack has the following private data variables. Think about the correct data type to use for each variable:
monsterName (eg Godzilla)
attackLocation (eg Tokyo)
damagesInMillionUSD (eg 123.45)
date (eg October 27, 1954, OK to use the Java Date class)
Create whatever constructors, getters, and setters you need. Be sure to write a reasonable toString().
Part B
Create an AttackMonitor class. AttackMonitor contains an array of Monster attacks and has methods to generate reports on the attacks. It should include at least these methods:
reportAttacks() creates an array of five attacks, takes user input using a Scanner from System.in, creates the attacks (instances of MonsterAttack), and adds the attacks to the array.
showAttacks() iterates through the array and prints out the result of running the toString() of each attack.
showDamages() calculates and prints the total amount of damages for all attacks and the mean damages
showMonsters() shows the names of all monsters involved in the attacks and the number of attacks for each monster (ie: "Godzilla, 3 attack(s); Bigfoot, 1 attack(s); Yeti: 1 attack(s)). Since we may discover new monsters after compile time, you must find the names using a getter in MonsterAttack. Don't hard- code the monster names.
findEarliestAttack() prints out the to String() of the earliest MonsterAttack in the array.
A menu method that offers the user the opportunity to run any of the methods listed above or to quit. The menu must be contained in a loop with a swtich statement, as shown in several lecture examples.. The user can do any of the above tasks in any order, as many times as she wants to.
Part C
Write a main() method for AttackMonitor. Think about what it needs to do to show that the other classes work correctly. The driver should only call methods from AttackMonitor, not methods from MonsterAttack.
CODE:
MonsterDriver.java
package monsterdriver;
import java.io.File;
import java.util.Scanner;
public class MonsterDriver {
public static void main(String[] args) {
boolean fileread=false;
File file = new File("Attacks.csv");
AttackMoniter am = new AttackMoniter();
boolean loop=true;
while(loop)
{
System.out.println("Enter Choice 1)Report Attacks 2)Show Attacks
3)Show Monsters 4)Calculate Damages 5)Get Earliest Attack 6)Save To
File 7)Read From File 8)Clear List\n");
Scanner in=new Scanner(System.in);
int choice=in.nextInt();
switch(choice)
{
case 1:{
am.reportAttacks();break;
}
case 2:{
am.showAttacks();break;
}
case 3:{
am.showMonsters();break;
}
case 4:{
am.calculateDamages();break;
}
case 5:{
am.getEarliestAttack();break;
}
case 6:{
am.saveToFile(file);break;
}
case 7:{
if(!fileread)
{
am.readFromFile(file);
fileread=true;//To avoid duplicate data
break;
}
}
case 8:{
am.clearList();break;
}
default : loop=false;
}
}
}
}
AttackMoniter.java
/*
* To change this license header, choose License Headers in Project
Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package monsterdriver;
import java.io.File;
import java.io.FileWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Scanner;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class AttackMoniter {
public List<MonsterAttack> attacks =new
ArrayList<MonsterAttack>();
public void reportAttacks() {
Scanner scan;
scan = new Scanner(System.in);
System.out.print("Enter the name of the monster: ");
String m = scan.nextLine();
System.out.print("Enter location of attack: ");
String l = scan.nextLine();
System.out.print("Enter amount of damage: ");
double dam = scan.nextDouble();
System.out.print("Enter date of attack: ");
int d = scan.nextInt();
System.out.print("Enter month of attack: ");
int mon = scan.nextInt();
System.out.print("Enter year of attack: ");
int y = scan.nextInt();
Calendar calendar = Calendar.getInstance();
calendar.set(y,mon-1,d);
Date date =calendar.getTime();
MonsterAttack ma = new MonsterAttack(m,l, dam, date);
attacks.add(ma);
}
public void showAttacks() {
int i;
for(i=0; i<attacks.size(); i++) {
System.out.println(this.attacks.get(i).toString());
}
}
public void calculateDamages() {
double mean=0.0;
int i;
for(i=0; i<this.attacks.size();i++){
mean += this.attacks.get(i).getDamagesInMillionUSD();
}
System.out.println("Total damages of attacks in millions USD: " +
mean);
mean = mean/this.attacks.size();
System.out.println("Mean damages of attacks in millions USD: " +
mean);
}
public void showMonsters() {
Map<String, Integer> map=new HashMap<String,
Integer>();
for(int i=0; i<this.attacks.size();i++){
if(!map.containsKey(attacks.get(i).getMonsterName()))
{
map.put(attacks.get(i).getMonsterName(), 1);
}
else
{
map.put(attacks.get(i).getMonsterName(),
map.get(attacks.get(i).getMonsterName())+1);
}
}
for (String name: map.keySet()){
String key = name;
String value = map.get(name).toString();
System.out.println(key + " " + value+"attack(s)");
}
}
public void getEarliestAttack() {
MonsterAttack m = this.attacks.get(0);
for(MonsterAttack ma: this.attacks) {
if(m.getDate().after(ma.getDate())) {
m = ma;
}
}
System.out.println("Earliest attack: \n" + m.toString());
}
public void saveToFile(File file)
{
try{
FileWriter writer=new FileWriter(file);
String header="MonsterName,Location,DamagePrice,Date\n";
writer.write(header);
for(int i=0;i<attacks.size();i++)
{
writer.append(attacks.get(i).toStringCSV());
}
writer.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
public void readFromFile(File file)
{
try{
Scanner reader = new Scanner(file);
String header=reader.nextLine();
while (reader.hasNextLine()) {
String data = reader.nextLine();
String[] str=data.split(",");
Date date=new SimpleDateFormat("dd/MM/yyyy").parse(str[3]);
MonsterAttack ma = new MonsterAttack(str[0],str[1],
Double.parseDouble(str[2]), date);
if(!attacks.contains(ma))
{
attacks.add(ma);
}
}
reader.close();
}
catch(Exception e)
{
}
}
public void clearList()
{
attacks.clear();
}
}
MonsterAttack.java
/*
* To change this license header, choose License Headers in Project
Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package monsterdriver;
import java.text.SimpleDateFormat;
import java.util.Date;
public class MonsterAttack {
String monsterName;
String attackLocation;
double damagesInMillionUSD;
Date date;
public MonsterAttack(String monsterName, String attackLocation,
double damagesInMillionUSD, Date date) {
super();
this.monsterName = monsterName;
this.attackLocation = attackLocation;
this.damagesInMillionUSD = damagesInMillionUSD;
this.date = date;
}
public String getMonsterName() {
return monsterName;
}
public void setMonsterName(String monsterName) {
this.monsterName = monsterName;
}
public String getAttackLocation() {
return attackLocation;
}
public void setAttackLocation(String attackLocation) {
this.attackLocation = attackLocation;
}
public double getDamagesInMillionUSD() {
return damagesInMillionUSD;
}
public void setDamagesInMillionUSD(double damagesInMillionUSD)
{
this.damagesInMillionUSD = damagesInMillionUSD;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public String toString() {
String str = "Monster name: " + this.monsterName+
" Attack Location: " + this.attackLocation+
" Damage in millions USD : " + this.damagesInMillionUSD+
" Date of attack: " + this.date;
return str;
}
public String toStringCSV() {
SimpleDateFormat formatter = new
SimpleDateFormat("dd/MM/yyyy");
String str = this.monsterName +","+ this.attackLocation+"," +
this.damagesInMillionUSD+","
+formatter.format(this.date)+"\n";
return str;
}
}
OUTPUT:
SECOND RUN