Adding AttackMonitor and Student.java

This commit is contained in:
2024-10-09 12:10:09 -07:00
parent 207ef79678
commit 3c020ad218
35 changed files with 465 additions and 0 deletions

8
.idea/.gitignore generated vendored Normal file
View File

@@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

6
.idea/misc.xml generated Normal file
View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="temurin-17" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

10
.idea/modules.xml generated Normal file
View File

@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/CS2012.iml" filepath="$PROJECT_DIR$/CS2012.iml" />
<module fileurl="file://$PROJECT_DIR$/IO/IO.iml" filepath="$PROJECT_DIR$/IO/IO.iml" />
<module fileurl="file://$PROJECT_DIR$/monsterattack/monsterattack.iml" filepath="$PROJECT_DIR$/monsterattack/monsterattack.iml" />
</modules>
</component>
</project>

6
.idea/vcs.xml generated Normal file
View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>

11
CS2012.iml Normal file
View File

@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

11
IO/IO.iml Normal file
View File

@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

49
IO/src/Main.java Normal file
View File

@@ -0,0 +1,49 @@
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
// creates new array list, with "Student" as the objects in the list
List<Student> students = new ArrayList<>();
// sets the location of the txt file list
String inputFile = "/home/system32/IdeaProjects/CS2012/IO/src/students.txt";
// tries reading the file, splits the file by the comma, and stores it in the "students" list
try (BufferedReader br = new BufferedReader(new FileReader(inputFile))) {
String line;
while ((line = br.readLine()) != null) {
String[] tokens = line.split(",");
String name = tokens[0];
double grade = Double.parseDouble(tokens[1]);
students.add(new Student(name, grade));
}
} catch (IOException e) {
System.out.println("Error reading file:" + e.getMessage());
} // If there is an error in the file, catches the error, displays it, and moves on
System.out.println("students in file:");
for (Student s : students) {
System.out.println(s);
} // prints out students in file
Scanner scanner = new Scanner(System.in);
System.out.println("Enter student name: ");
String name = scanner.nextLine();
System.out.println("Enter grade: ");
double grade = scanner.nextDouble();
students.add(new Student(name, grade));
// Asks for user input to add new student to the list
String outputFile = "/home/system32/IdeaProjects/CS2012/IO/src/updatedStudents.txt";
// sets output location
try (BufferedWriter bw = new BufferedWriter(new FileWriter(outputFile))) {
for (Student s : students) {
bw.write(s.toString());
bw.newLine();
} // Tries to write the Student to the file by converting it into a string via the toString() method
} catch (IOException e) {
System.out.println("Error writing file:" + e.getMessage());
} // Catches error, see above explanation.
System.out.println("Updated Students in file");
// Display Success Message if user succedes.
}
}

27
IO/src/Student.java Normal file
View File

@@ -0,0 +1,27 @@
public class Student {
// Sets 2 private varibles
private String name;
private double grade;
// method of setting given values as varibles
public Student(String name, double grade) {
this.name = name;
this.grade = grade;
}
// setters and getters for each variable
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getGrade() {
return grade;
}
public void setGrade(double grade) {
this.grade = grade;
}
// returns the varibles as strings
public String toString() {
return name + "," + grade;
}
}

5
IO/src/students.txt Normal file
View File

@@ -0,0 +1,5 @@
Al,82.89
Brian,98.93
Carl,96.01
Dennis,75.72
Mike,62.01

View File

@@ -0,0 +1,6 @@
Al,82.89
Brian,98.93
Carl,96.01
Dennis,75.72
Mike,62.01
Bob,100.0

View File

@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

View File

@@ -0,0 +1,104 @@
import java.util.Scanner;
import java.util.Date;
public class AttackMonitor {
private MonsterAttack[] attacks;
private int attackCount;
public AttackMonitor() {
this.attacks =new MonsterAttack[5];
this.attackCount = 0;
}
public void reportAttacks() {
Scanner scanner = new Scanner(System.in);
for (int i = 0; i < 5; i++) {
System.out.println("Enter attack " + (i+1) + ": ");
System.out.println("Enter Monster Name: ");
String monsterName = scanner.nextLine();
System.out.println("Enter Attack Location: ");
String attackLocation = scanner.nextLine();
System.out.println("Enter Damages (Millions): ");
int damagesInMillionsUSD = scanner.nextInt();
scanner.nextLine();
Date date =new Date();
attacks[i] = new MonsterAttack(monsterName, attackLocation, damagesInMillionsUSD, date);
attackCount++;
}
}
public void showAttacks() {
for (int i = 0; i < 5; i++) {
System.out.println("Monster Name: " + attacks[i].toString());
}
}
public void showDamages() {
double totalDamages = 0;
for (int i = 0; i < 5; i++) {
totalDamages += attacks[i].getDamagesInMillionUSD();
}
double meanDamages = totalDamages / attackCount;
System.out.println(meanDamages + " damages in million USD (mean)");
System.out.println(totalDamages + " damages in million USD (total)");
}
public void showMonsters() {
String[] monsterNames = new String[attackCount];
int[] attackCounts = new int[attackCount];
int unique = 0;
for (int i = 0; i < attackCount; i++) {
String monsterName = attacks[i].getMonsterName();
boolean monsterExists = false;
for (int j = 0; j < unique; j++) {
if (monsterNames[j].equals(monsterName)) {
attackCounts[j]++;
break;
}
}
if (!monsterExists) {
monsterNames[unique] = monsterName;
attackCounts[unique] = 1;
unique++;
}
}
for (int i = 0; i < unique; i++) {
System.out.println("Monster Name: " + monsterNames[i] + ", " + attackCounts[i] + " attack(s)");
}
}
public void menu() {
Scanner scanner = new Scanner(System.in);
boolean exit = false;
while (!exit) {
System.out.println("Menu:");
System.out.println("1. Enter Attacks");
System.out.println("2. Show Attacks");
System.out.println("3. Show Damages");
System.out.println("4. Show Monsters");
System.out.println("0. Exit");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();
scanner.nextLine();
switch (choice) {
case 1:
reportAttacks();
break;
case 2:
showAttacks();
break;
case 3:
showDamages();
break;
case 4:
showMonsters();
break;
case 0:
exit = true;
break;
default:
System.out.println("Invalid choice");
break;
}
}
}
}

View File

@@ -0,0 +1,6 @@
public class Main {
public static void main(String[] args) {
AttackMonitor monitor = new AttackMonitor();
monitor.menu();
}
}

View File

@@ -0,0 +1,54 @@
import java.util.Date;
public class MonsterAttack {
private String monsterName;
private String attackLocation;
private double damagesInMillionUSD;
private Date date;
public MonsterAttack(String monsterName, String attackLocation, double damagesInMillionUSD, Date date) {
this.monsterName = monsterName;
this.attackLocation = attackLocation;
this.damagesInMillionUSD = damagesInMillionUSD;
this.date = date;
}
public String getMonsterName() {
return monsterName;
}
public String getAttackLocation() {
return attackLocation;
}
public double getDamagesInMillionUSD() {
return damagesInMillionUSD;
}
public Date getDate() {
return date;
}
public void setMonsterName(String monsterName) {
this.monsterName = monsterName;
}
public void setAttackLocation(String attackLocation) {
this.attackLocation = attackLocation;
}
public void setDamagesInMillionUSD(double damagesInMillionUSD) {
this.damagesInMillionUSD = damagesInMillionUSD;
}
public void setDate(Date date) {
this.date = date;
}
public String toString() {
return "Monster Attack Details: \n" +
"Monster Name: " + monsterName + "\n" +
"Location: " + attackLocation + "\n" +
"Damages: $" + damagesInMillionUSD + " million\n" +
"Date: " + date;
}
}

View File

@@ -0,0 +1,106 @@
/home/system32/.jdks/temurin-17.0.12/bin/java -javaagent:/opt/intellij-idea-ultimate/lib/idea_rt.jar=44999:/opt/intellij-idea-ultimate/bin -Dfile.encoding=UTF-8 -classpath /home/system32/IdeaProjects/CS2012/out/production/monsterattack Main
Menu:
1. Enter Attacks
2. Show Attacks
3. Show Damages
4. Show Monsters
0. Exit
Enter your choice: 1
Enter attack 1:
Enter Monster Name:
Fubuzilla
Enter Attack Location:
tokyo
Enter Damages (Millions):
44
Enter attack 2:
Enter Monster Name:
Godzilla
Enter Attack Location:
new york
Enter Damages (Millions):
9
Enter attack 3:
Enter Monster Name:
king kong
Enter Attack Location:
new york
Enter Damages (Millions):
93
Enter attack 4:
Enter Monster Name:
Fubuzilla
Enter Attack Location:
osaka
Enter Damages (Millions):
33
Enter attack 5:
Enter Monster Name:
mothman
Enter Attack Location:
Enter Damages (Millions):
64
Menu:
1. Enter Attacks
2. Show Attacks
3. Show Damages
4. Show Monsters
0. Exit
Enter your choice: 2
Monster Name: Monster Attack Details:
Monster Name: Fubuzilla
Location: tokyo
Damages: $44.0 million
Date: Tue Oct 08 22:41:46 PDT 2024
Monster Name: Monster Attack Details:
Monster Name: Godzilla
Location: new york
Damages: $9.0 million
Date: Tue Oct 08 22:42:01 PDT 2024
Monster Name: Monster Attack Details:
Monster Name: king kong
Location: new york
Damages: $93.0 million
Date: Tue Oct 08 22:42:14 PDT 2024
Monster Name: Monster Attack Details:
Monster Name: Fubuzilla
Location: osaka
Damages: $33.0 million
Date: Tue Oct 08 22:42:31 PDT 2024
Monster Name: Monster Attack Details:
Monster Name: mothman
Location:
Damages: $64.0 million
Date: Tue Oct 08 22:43:13 PDT 2024
Menu:
1. Enter Attacks
2. Show Attacks
3. Show Damages
4. Show Monsters
0. Exit
Enter your choice: 3
48.6 damages in million USD (mean)
243.0 damages in million USD (total)
Menu:
1. Enter Attacks
2. Show Attacks
3. Show Damages
4. Show Monsters
0. Exit
Enter your choice: 4
Monster Name: Fubuzilla, 2 attack(s)
Monster Name: Godzilla, 1 attack(s)
Monster Name: king kong, 1 attack(s)
Monster Name: Fubuzilla, 1 attack(s)
Monster Name: mothman, 1 attack(s)
Menu:
1. Enter Attacks
2. Show Attacks
3. Show Damages
4. Show Monsters
0. Exit
Enter your choice: 0
Process finished with exit code 0

8
out/production/CS2012/.idea/.gitignore generated vendored Normal file
View File

@@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

6
out/production/CS2012/.idea/misc.xml generated Normal file
View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="temurin-17" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

8
out/production/CS2012/.idea/modules.xml generated Normal file
View File

@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/CS2012.iml" filepath="$PROJECT_DIR$/CS2012.iml" />
</modules>
</component>
</project>

6
out/production/CS2012/.idea/vcs.xml generated Normal file
View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>

Binary file not shown.

View File

@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -0,0 +1 @@

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -0,0 +1,5 @@
Al,82.89
Brian,98.93
Carl,96.01
Dennis,75.72
Mike,62.01

View File

Binary file not shown.

Binary file not shown.

Binary file not shown.