diff --git a/Summer-2024/CS-3443/Labs/Lab2/README.org b/Summer-2024/CS-3443/Labs/Lab2/README.org index 3095285..5f0690a 100644 --- a/Summer-2024/CS-3443/Labs/Lab2/README.org +++ b/Summer-2024/CS-3443/Labs/Lab2/README.org @@ -1,8 +1,8 @@ -* Lab 1 Submission +* Lab 2 Submission Name: =Price Hiller= ABC123: =zfp106= -Date: =2024-06-12= -URL: [[https://git.orion-technologies.io/Price/college/src/branch/Development/Summer-2024/CS-3443/Labs/Lab1]] +Date: =2024-06-22= +URL: [[https://git.orion-technologies.io/Price/college/src/branch/Development/Summer-2024/CS-3443/Labs/Lab2]] ** Running the program @@ -25,4 +25,4 @@ Recording of the program running: [[file:./assets/run-recording.webm]] - Click =Run= *** via Maven -If you have Maven installed, you can easily run this program when at the top level of the project via ~mvn exec:java -Dexec.mainClass="com.zfp106.lab1.Lab1"~. +If you have Maven installed, you can easily run this program when at the top level of the project via ~mvn exec:java -Dexec.mainClass="com.zfp106.lab2.Lab2"~. diff --git a/Summer-2024/CS-3443/Labs/Lab2/src/main/java/com/zfp106/lab2/CrewMember.java b/Summer-2024/CS-3443/Labs/Lab2/src/main/java/com/zfp106/lab2/CrewMember.java new file mode 100644 index 0000000..5252c7e --- /dev/null +++ b/Summer-2024/CS-3443/Labs/Lab2/src/main/java/com/zfp106/lab2/CrewMember.java @@ -0,0 +1,69 @@ +package com.zfp106.lab2; + +public class CrewMember { + private String name; + private String position; + private String rank; + private String species; + private String assignment; + + public CrewMember(String name, String position, String rank, String species, String assignment) { + this.setName(name); + this.setPosition(position); + this.setRank(rank); + this.setSpecies(species); + this.setAssignment(assignment); + } + + public CrewMember(String name, String position, String rank, String species) { + this.setName(name); + this.setPosition(position); + this.setRank(rank); + this.setSpecies(species); + } + + public String toString() { + return String.format( + "%s (%s) - %s [%s]", this.getName(), this.getRank(), this.getPosition(), this.getSpecies()); + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getPosition() { + return position; + } + + public void setPosition(String position) { + this.position = position; + } + + public String getRank() { + return rank; + } + + public void setRank(String rank) { + this.rank = rank; + } + + public String getSpecies() { + return species; + } + + public void setSpecies(String species) { + this.species = species; + } + + public String getAssignment() { + return assignment; + } + + public void setAssignment(String assignment) { + this.assignment = assignment; + } +} diff --git a/Summer-2024/CS-3443/Labs/Lab2/src/main/java/com/zfp106/lab2/Fleet.java b/Summer-2024/CS-3443/Labs/Lab2/src/main/java/com/zfp106/lab2/Fleet.java new file mode 100644 index 0000000..0b24c78 --- /dev/null +++ b/Summer-2024/CS-3443/Labs/Lab2/src/main/java/com/zfp106/lab2/Fleet.java @@ -0,0 +1,77 @@ +package com.zfp106.lab2; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Paths; +import java.util.ArrayList; + +/** + * Represents a fleet of starships + * + *

Each fleet has the following: - A name - Starships said fleet contains + */ +public class Fleet { + private String name; + private ArrayList starships; + + public Fleet(String name) { + this.setName(name); + this.setStarships(new ArrayList()); + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public ArrayList getStarships() { + return starships; + } + + public void setStarships(ArrayList starships) { + this.starships = starships; + } + + public String toString() { + String out = + String.format( + "----------------------------\n\n%s\n\n----------------------------\n", this.getName()); + StringBuilder starships = new StringBuilder(); + for (Starship starship : this.getStarships()) { + starships.append("\n" + starship.toString() + "\n"); + } + out = out + starships.toString(); + return out; + } + + public void loadStarships(String starshipsDirectoryStr) throws IOException { + File[] starshipFiles = Paths.get(starshipsDirectoryStr).toFile().listFiles(); + Integer curFile = 0; + Integer remainingFiles = starshipFiles.length; + // This is, imo, not *great* code, but it gets the job done. Based on the assignment + // requirements, the files are loaded in a weird order. To support said order, iterate backwards + // using modulo arithmetic to wrap values to their positive counterparts. If we encounter an + // already read file, we keep reading backwards by one until we hit an unread file. + while (remainingFiles > 0) { + // Read two values backwards and wrap positively + curFile = Math.floorMod(curFile - 2, starshipFiles.length); + // If the current "found" file has already been read, starting going one + // back over an over until we find an unread file + while (starshipFiles[curFile] == null) { + curFile--; + if (curFile < 0) { + curFile = Math.floorMod(curFile, starshipFiles.length); + } + } + // Once we locate a file, load it and then mark the file as read by setting it to null + if (starshipFiles[curFile] != null) { + this.starships.add(Starship.loadStarshipFromPath(starshipFiles[curFile].toPath())); + starshipFiles[curFile] = null; + remainingFiles--; + } + } + } +} diff --git a/Summer-2024/CS-3443/Labs/Lab2/src/main/java/com/zfp106/lab2/Lab2.java b/Summer-2024/CS-3443/Labs/Lab2/src/main/java/com/zfp106/lab2/Lab2.java index 53c140a..40b486b 100644 --- a/Summer-2024/CS-3443/Labs/Lab2/src/main/java/com/zfp106/lab2/Lab2.java +++ b/Summer-2024/CS-3443/Labs/Lab2/src/main/java/com/zfp106/lab2/Lab2.java @@ -1,28 +1,26 @@ +package com.zfp106.lab2; + /** - * Lab2 is a Java class containing a main method to run your program when completed. - * This class will not compile until you have completed the requirements outlined in - * the lab description. + * Lab2 is a Java class containing a main method to run your program when completed. This class will + * not compile until you have completed the requirements outlined in the lab description. * - * @author Amanda Fernandez (abc123) - * UTSA CS 3443 - Lab 2 - * Fall 2022 + * @author Price Hiller (zfp106) UTSA CS 3443 - Lab 2 Fall 2024 */ public class Lab2 { - public static void main( String[] args ) { + public static void main(String[] args) { - Fleet unitedFederation = new Fleet( "United Federation of Planets" ); + Fleet unitedFederation = new Fleet("United Federation of Planets"); - try { - unitedFederation.loadStarships( "data" ); + try { + unitedFederation.loadStarships("data"); - }catch( Exception e ) { - // this is a try/catch which will show any errors to the console (more on these in coming weeks!) - e.printStackTrace(); - } - - System.out.println( unitedFederation ); - - } + } catch (Exception e) { + // this is a try/catch which will show any errors to the console (more on these in coming + // weeks!) + e.printStackTrace(); + } + System.out.println(unitedFederation); + } } diff --git a/Summer-2024/CS-3443/Labs/Lab2/src/main/java/com/zfp106/lab2/Starship.java b/Summer-2024/CS-3443/Labs/Lab2/src/main/java/com/zfp106/lab2/Starship.java new file mode 100644 index 0000000..c487d4b --- /dev/null +++ b/Summer-2024/CS-3443/Labs/Lab2/src/main/java/com/zfp106/lab2/Starship.java @@ -0,0 +1,100 @@ +package com.zfp106.lab2; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +public class Starship { + private String name; + private String registry; + private String starshipClass; + private ArrayList crew; + + public Starship(String name, String registry, String starship_class) { + this.setName(name); + this.setRegistry(registry); + this.setStarshipClass(starship_class); + this.setCrew(new ArrayList()); + } + + public String toString() { + String out = + String.format( + "%s, %s. Registry: %s\n\n%d crew members assigned.", + this.getName(), + this.getStarshipClass(), + this.getRegistry(), + this.getNumberOfPersonnel()); + StringBuilder crew = new StringBuilder(); + for (CrewMember crewMember : this.getCrew()) { + crew.append("\n\n - " + crewMember.toString()); + } + out = out + crew.toString(); + return out; + } + + public void addCrewMember(CrewMember crewMember) { + if (!this.crew.contains(crewMember)) { + this.crew.add(crewMember); + } + } + + public Integer getNumberOfPersonnel() { + return this.crew.size(); + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getRegistry() { + return registry; + } + + public void setRegistry(String registry) { + this.registry = registry; + } + + public ArrayList getCrew() { + return crew; + } + + public void setCrew(ArrayList crew) { + this.crew = crew; + } + + public String getStarshipClass() { + return starshipClass; + } + + public void setStarshipClass(String starshipClass) { + this.starshipClass = starshipClass; + } + + public static Starship loadStarshipFromPath(Path filePath) throws IOException { + List starshipInfo = new ArrayList(); + try (Stream lines = Files.lines(filePath)) { + starshipInfo = lines.collect(Collectors.toList()); + } + // First element of the starship file is the the starship name, registry, and class + String[] header = starshipInfo.remove(0).split(","); + Starship starship = new Starship(header[0], header[1], header[2]); + // Then we go through and grab all the crew members assigned to the starship and add 'em to the + // starship + for (String crewMemberLine : starshipInfo) { + String[] crewMemberInfo = crewMemberLine.split(","); + starship.addCrewMember( + new CrewMember( + crewMemberInfo[0], crewMemberInfo[1], crewMemberInfo[2], crewMemberInfo[3])); + } + return starship; + } +} diff --git a/Summer-2024/CS-3443/Labs/Lab2/src/test/java/com/zfp106/lab2/Lab2Test.java b/Summer-2024/CS-3443/Labs/Lab2/src/test/java/com/zfp106/lab2/Lab2Test.java index e69de29..4f79411 100644 --- a/Summer-2024/CS-3443/Labs/Lab2/src/test/java/com/zfp106/lab2/Lab2Test.java +++ b/Summer-2024/CS-3443/Labs/Lab2/src/test/java/com/zfp106/lab2/Lab2Test.java @@ -0,0 +1,82 @@ +package com.zfp106.lab2; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.api.Test; + +class FleetTests { + @Test + void fleetStringReprIsCorrect() { + Fleet fleet = new Fleet("United Federation of Planets"); + try { + fleet.loadStarships("data"); + } catch (Exception e) { + e.printStackTrace(); + } + + String expected = + "----------------------------\n" + + "\n" + + "United Federation of Planets\n" + + "\n" + + "----------------------------\n" + + "\n" + + "USS Endeavour, Nebula. Registry: NCC-71805\n" + + "\n" + + "0 crew members assigned.\n" + + "\n" + + "USS Bozeman, Sovereign. Registry: NCC-1941-A\n" + + "\n" + + "0 crew members assigned.\n" + + "\n" + + "USS Enterprise, Constitution. Registry: NCC-1701-A\n" + + "\n" + + "8 crew members assigned.\n" + + "\n" + + " - James T. Kirk (Captain) - Commanding Officer [Human]\n" + + "\n" + + " - Spock (Commander) - First Officer [Vulcan/Human]\n" + + "\n" + + " - Leonard McCoy (Lieutenant Commander) - Chief Medical Officer [Human]\n" + + "\n" + + " - Montgomery Scott (Lieutenant Commander) - Chief Engineering Officer [Human]\n" + + "\n" + + " - Christine Chapel (Crewman) - Nurse [Human]\n" + + "\n" + + " - Nyota Uhura (Lieutenant) - Communications Officer [Human]\n" + + "\n" + + " - Hikaru Sulu (Lieutenant) - Helmsman [Human]\n" + + "\n" + + " - Pavel Chekov (Ensign) - Navigator [Human]\n" + + "\n" + + "USS Bozeman, Soyuz. Registry: NCC-1941\n" + + "\n" + + "0 crew members assigned.\n" + + "\n" + + "USS Enterprise, Galaxy. Registry: NCC-1701-D\n" + + "\n" + + "8 crew members assigned.\n" + + "\n" + + " - Jean-Luc Picard (Captain) - Commanding Officer [Human]\n" + + "\n" + + " - William T. Riker (Commander) - First Officer [Human]\n" + + "\n" + + " - Beverly Crusher (Lieutenant Commander) - Chief Medical Officer [Human]\n" + + "\n" + + " - Geordi La Forge (Lieutenant) - Chief Engineering Officer [Human]\n" + + "\n" + + " - Deanna Troi (Lieutenant Commander) - Counselor [Betazoid]\n" + + "\n" + + " - Worf (Lieutenant) - Helmsman [Klingon]\n" + + "\n" + + " - Data (Lieutenant Commander) - Chief Operations Officer [Android]\n" + + "\n" + + " - Tasha Yar (Lieutenant) - Chief Security Officer [Human]\n" + + "\n" + + "USS Gibraltar, Sovereign. Registry: NCC-75689\n" + + "\n" + + "0 crew members assigned.\n"; + + assertEquals(expected, fleet.toString()); + } +}