diff --git a/Summer-2024/CS-3443/Labs/Lab2/Layout.puml b/Summer-2024/CS-3443/Labs/Lab2/Layout.puml new file mode 100644 index 0000000..31ae33e --- /dev/null +++ b/Summer-2024/CS-3443/Labs/Lab2/Layout.puml @@ -0,0 +1,67 @@ +@startuml +skinparam classAttributeIconSize 0 +class CrewMember { + - name: String + - position: String + - rank: String + - species: String + - assignment: String + + <> CrewMember(name: String, position: String, rank: String, species: String, assignment: String) + + <> CrewMember(name: String, position: String, rank: String, species: String) + + toString(): String + + getName(): String + + setName(name: String) + + getPosition(): String + + setPosition(position: String) + + getRank(): String + + setRank(rank: String) + + getSpecies(): String + + setSpecies(species: String) + + getAssignment(): String + + setAssignment(assignment: String) +} + +skinparam classAttributeIconSize 0 +class Starship { + - name: String + - registry: String + - starshipClass: String + - crew: ArrayList + + <> Starship(name: String, registry: String, starshipClass: String) + + toString(): String + + addCrewMember(crewMember: CrewMember) + + getNumberOfPersonnel(): Integer + + getName(): String + + setName(name: String) + + getRegistry(): String + + setRegistry(registry: String) + + getCrew(): ArrayList + + setCrew(crew: ArrayList) + + getStarshipClass(): String + + setStarshipClass(starshipClass: String) +} + +skinparam classAttributeIconSize 0 +class Fleet { + - name: String + - starships: ArrayList + + <> Fleet(name: String) + + toString(): String + + getSizeOfFleet(): Integer + + addStarship(starship: Starship) + + getName(): String + + setName(name: String) + + getStarships(): ArrayList + + setStarships(starships: ArrayList) + + loadStarships(starshipsDirectory: String) +} + +skinparam classAttributeIconSize 0 +class Lab2 { + + _ main(args: String[]): void +} + +Fleet o-- Starship +Starship o-- CrewMember +Lab2 ..|> Fleet +@enduml diff --git a/Summer-2024/CS-3443/Labs/Lab2/Layout.svg b/Summer-2024/CS-3443/Labs/Lab2/Layout.svg new file mode 100644 index 0000000..322742b --- /dev/null +++ b/Summer-2024/CS-3443/Labs/Lab2/Layout.svg @@ -0,0 +1 @@ +CrewMember-name: String-position: String-rank: String-species: String-assignment: String+«constructor» CrewMember(name: String, position: String, rank: String, species: String, assignment: String)+«constructor» CrewMember(name: String, position: String, rank: String, species: String)+toString(): String+getName(): String+setName(name: String)+getPosition(): String+setPosition(position: String)+getRank(): String+setRank(rank: String)+getSpecies(): String+setSpecies(species: String)+getAssignment(): String+setAssignment(assignment: String)Starship-name: String-registry: String-starshipClass: String-crew: ArrayList<CrewMember>+«constructor» Starship(name: String, registry: String, starshipClass: String)+toString(): String+addCrewMember(crewMember: CrewMember)+getNumberOfPersonnel(): Integer+getName(): String+setName(name: String)+getRegistry(): String+setRegistry(registry: String)+getCrew(): ArrayList<CrewMember>+setCrew(crew: ArrayList<CrewMember>)+getStarshipClass(): String+setStarshipClass(starshipClass: String)Fleet-name: String-starships: ArrayList<Starship>+«constructor» Fleet(name: String)+toString(): String+getSizeOfFleet(): Integer+addStarship(starship: Starship)+getName(): String+setName(name: String)+getStarships(): ArrayList<Starship>+setStarships(starships: ArrayList<Starship>)+loadStarships(starshipsDirectory: String)Lab2+_ main(args: String[]): void \ No newline at end of file diff --git a/Summer-2024/CS-3443/Labs/Lab2/README.org b/Summer-2024/CS-3443/Labs/Lab2/README.org index 5f0690a..2404f1e 100644 --- a/Summer-2024/CS-3443/Labs/Lab2/README.org +++ b/Summer-2024/CS-3443/Labs/Lab2/README.org @@ -4,21 +4,31 @@ ABC123: =zfp106= Date: =2024-06-22= URL: [[https://git.orion-technologies.io/Price/college/src/branch/Development/Summer-2024/CS-3443/Labs/Lab2]] +** UML Diagram + +#+ATTR_HTML: :alt :align left :class img +[[./Layout.svg][UML Diagram of the program]] + +You can see the HTML rendering at [[./UML.html]] + ** Running the program Recording of the program running: [[file:./assets/run-recording.webm]] +To operate correctly the program must be ran from the top level directory! + *** via Eclipse - Open Eclipse - Select File - Select Import - In the dialogue that appears, expand =General= -- Select =Archive File= +- Select =Existing Projects into Workspace= - Click =Next= -- In the top right of the new menu, select =Browse...= +- Select =Select archive file:= +- To the right of that dialogue click =Browse...= - Find the archive on your file system and select it -- Select a valid path for =Into folder:= +- Click =Add project to working sets= - Click =Finish= - Select the project on the left bar - On the top bar, expand the =Run= category @@ -26,3 +36,5 @@ Recording of the program running: [[file:./assets/run-recording.webm]] *** 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.lab2.Lab2"~. + +Tests can be run via ~mvn test~ from the top level of the project as well. diff --git a/Summer-2024/CS-3443/Labs/Lab2/UML.html b/Summer-2024/CS-3443/Labs/Lab2/UML.html new file mode 100644 index 0000000..26c67f3 --- /dev/null +++ b/Summer-2024/CS-3443/Labs/Lab2/UML.html @@ -0,0 +1,66 @@ + + + + + + + zfp106 UML Diagram for Lab2 + + + +

+ Made with infinite, undying, hatred in PlantUML (Lab2, + zfp106) +

+

+ Some notes on the decisions made here. + + + + + + + + + + + + + + + + + + + + + + +
QuestionAnswer
Why is Starship associated to CrewMember via + aggregation?Because a Starship has 0 or more crew members and crew + members aren't dependent on a Starship to exist. It is possible for a + crew member to not be assigned to any + Starship. +
+ Why is Fleet associated to Starship via aggregation? + + Same reason as the previous. It is possible for a fleet to exist in name + only without any Starships contained within at all. A fleet is quite + literally an aggregation of any n number of ships in the first + place anyhow. +
Why does Lab2 with its main method have a dependency + relationship with Fleet?Because a near identical example of this relationship was in the slides. We will + primarily use it for classes referenced in main.
Why don't you show associations/other connections to Java standard library classes like + ArrayList? + Because we can keep going down that rabbit hole until infinity (or by my count in my IDE ~5 - 6 levels of + indirection from any given Java standard lib class to the bottom of the chain). I'm not interested in showing + the full diagram all the way down until we hit the base Object class.
+

+ +

Your answers are wrong and you should feel bad. Them's fighting words, and I have a + whole hill here to defend and die upon, sword gleaming in the midnight sun.

+

The actual UML diagram below:

+ Uml Diagram + + + diff --git a/Summer-2024/CS-3443/Labs/Lab2/assets/run-recording.webm b/Summer-2024/CS-3443/Labs/Lab2/assets/run-recording.webm index 2fcd6d9..4bf3ab2 100644 Binary files a/Summer-2024/CS-3443/Labs/Lab2/assets/run-recording.webm and b/Summer-2024/CS-3443/Labs/Lab2/assets/run-recording.webm differ diff --git a/Summer-2024/CS-3443/Labs/Lab2/pom.xml b/Summer-2024/CS-3443/Labs/Lab2/pom.xml index 641738a..03d49dc 100644 --- a/Summer-2024/CS-3443/Labs/Lab2/pom.xml +++ b/Summer-2024/CS-3443/Labs/Lab2/pom.xml @@ -1,10 +1,10 @@ 4.0.0 - org.zfp106.lab1 - lab1 + org.zfp106.lab2 + lab2 jar 0.1 - lab1 + lab2 http://maven.apache.org @@ -37,7 +37,7 @@ - com.zfp106.lab1.Lab1 + 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 index 5252c7e..716217a 100644 --- 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 @@ -1,5 +1,12 @@ package com.zfp106.lab2; +/** + * A crew member that can be assigned to a starship + * + *

A crew member has a name, a position, a rank, a species, and an assignment. + * + *

A crew member may not have an active assignment, meaning they're not assigned to a ship + */ public class CrewMember { private String name; private String position; @@ -7,6 +14,15 @@ public class CrewMember { private String species; private String assignment; + /** + * Create a new crew member with an active assignment to a starship + * + * @param name The name of the crew member + * @param position The position of the crew member + * @param rank The rank of the crew member + * @param species The species of the crew member + * @param assignment The ship registry the crew member is assigned to + */ public CrewMember(String name, String position, String rank, String species, String assignment) { this.setName(name); this.setPosition(position); @@ -15,6 +31,14 @@ public class CrewMember { this.setAssignment(assignment); } + /** + * Create a new crew member with an active assignment to a starship + * + * @param name The name of the crew member + * @param position The position of the crew member + * @param rank The rank of the crew member + * @param species The species of the crew member + */ public CrewMember(String name, String position, String rank, String species) { this.setName(name); this.setPosition(position); @@ -22,47 +46,102 @@ public class CrewMember { this.setSpecies(species); } + /** + * Get the string representation for the crew member + * + * @return String representation of the crew member + */ public String toString() { return String.format( "%s (%s) - %s [%s]", this.getName(), this.getRank(), this.getPosition(), this.getSpecies()); } + /** + * Get the name of the crew member + * + * @return The name of the crew member + */ public String getName() { - return name; + return this.name; } + /** + * Set the name of the crew member + * + * @param name The new name for the crew member + */ public void setName(String name) { this.name = name; } + /** + * Get the position of the crew member + * + * @return The position of the crew member + */ public String getPosition() { - return position; + return this.position; } + /** + * Set the position of the crew member + * + * @param position The new position for the crew member + */ public void setPosition(String position) { this.position = position; } + /** + * Get the rank of the crew member + * + * @return The rank of the crew member + */ public String getRank() { - return rank; + return this.rank; } + /** + * Set the rank of the crew member + * + * @param rank The new rank for the crew member + */ public void setRank(String rank) { this.rank = rank; } + /** + * Get the species of the crew member + * + * @return The species of the crew member + */ public String getSpecies() { - return species; + return this.species; } + /** + * Set the species of the crew member + * + * @param species The new species for the crew member + */ public void setSpecies(String species) { this.species = species; } + /** + * Get the starship registry that the crew member is assigned to + * + * @return The starship registry that the crew member is assigned to + */ public String getAssignment() { - return assignment; + return this.assignment; } + /** + * Set the starship registry that the crew member is assigned to + * + * @param assignment The new starship registry to assign the crew member to + */ 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 index 0b24c78..e38be61 100644 --- 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 @@ -2,13 +2,17 @@ package com.zfp106.lab2; import java.io.File; import java.io.IOException; +import java.nio.file.Files; import java.nio.file.Paths; import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; /** * Represents a fleet of starships * - *

Each fleet has the following: - A name - Starships said fleet contains + *

Each fleet has a name and 0 or more starships */ public class Fleet { private String name; @@ -19,22 +23,11 @@ public class Fleet { 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; - } - + /** + * Get the string representation of a fleet + * + * @return string representation of the fleet + */ public String toString() { String out = String.format( @@ -47,8 +40,70 @@ public class Fleet { return out; } - public void loadStarships(String starshipsDirectoryStr) throws IOException { - File[] starshipFiles = Paths.get(starshipsDirectoryStr).toFile().listFiles(); + /** + * Get the number of ships in the fleet + * + * @return Number of ships in the fleet + */ + public Integer getSizeOfFleet() { + return this.getStarships().size(); + } + + /** + * Add a starship to the fleet + * + * @param starship A starship to add to the fleet + */ + public void addStarship(Starship starship) { + if (!this.getStarships().contains(starship)) { + this.starships.add(starship); + } + } + + /** + * Get the name of the fleet + * + * @return name of the fleet + */ + public String getName() { + return this.name; + } + + /** + * Set the name of the fleet + * + * @param name New name for the fleet + */ + public void setName(String name) { + this.name = name; + } + + /** + * Get all the starships in the fleet + * + * @return starships in the fleet + */ + public ArrayList getStarships() { + return this.starships; + } + + /** + * Set the fleet's starship's to a new list of ships + * + * @param starships New starship's to set for the fleet + */ + public void setStarships(ArrayList starships) { + this.starships = starships; + } + + /** + * Load many starships from a given directory + * + * @param starshipsDirectory A directory containing one or more csv files with starship data + * @throws IOException When a valid csv file cannot be read + */ + public void loadStarships(String starshipsDirectory) throws IOException { + File[] starshipFiles = Paths.get(starshipsDirectory).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 @@ -68,7 +123,24 @@ public class Fleet { } // 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())); + List starshipInfo = new ArrayList(); + try (Stream lines = Files.lines(starshipFiles[curFile].toPath())) { + 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(","); + String registry = header[1]; + Starship starship = new Starship(header[0], registry, 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])); + } + this.addStarship(starship); starshipFiles[curFile] = null; remainingFiles--; } 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 index c487d4b..5beb6c8 100644 --- 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 @@ -1,26 +1,38 @@ 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; +/** + * A starship + * + *

Each starship has a name, a registry code, a class, and 0 or more crew members + */ public class Starship { private String name; private String registry; private String starshipClass; private ArrayList crew; - public Starship(String name, String registry, String starship_class) { + /** + * Instantiates a starship with the given name, registry, the class of starship, and an empty list + * of crew members. + * + * @param name The name of the starship + * @param registry The registry code of the starship + * @param starshipClass The class of the starship + */ + public Starship(String name, String registry, String starshipClass) { this.setName(name); this.setRegistry(registry); - this.setStarshipClass(starship_class); + this.setStarshipClass(starshipClass); this.setCrew(new ArrayList()); } + /** + * Returns the string representation of a Starship + * + * @return string representation of a Starship + */ public String toString() { String out = String.format( @@ -37,64 +49,101 @@ public class Starship { return out; } + /** + * Adds the given crew member to the starship and sets their assignment to the starship. + * + *

Note that this will reassign the crew member to the starship + * + * @param crewMember The crew member to assign to the starship + */ public void addCrewMember(CrewMember crewMember) { if (!this.crew.contains(crewMember)) { + crewMember.setAssignment(this.getRegistry()); this.crew.add(crewMember); } } + /** + * Get the number of crew assigned to the starship + * + * @return The number of crew assigned to the starship + */ public Integer getNumberOfPersonnel() { - return this.crew.size(); + return this.getCrew().size(); } + /** + * Get the name of the starship + * + * @return The name of the starship + */ public String getName() { - return name; + return this.name; } + /** + * Set the name of the starship + * + * @param name New name for the starship + */ public void setName(String name) { this.name = name; } + /** + * Get the registry code of the starship + * + * @return The registry code of the starship + */ public String getRegistry() { - return registry; + return this.registry; } + /** + * Set the registry code of the starship + * + * @param registry The new registry code for the starship + */ public void setRegistry(String registry) { this.registry = registry; } + /** + * Get all of the crew assigned to the starship + * + * @return The crew assigned to the starship + */ public ArrayList getCrew() { - return crew; + return this.crew; } + /** + * Set all the crew members of the ship. This will replace the existing crew. + * + * @param crew The new crew members to assign to the ship + */ public void setCrew(ArrayList crew) { - this.crew = crew; + this.crew = new ArrayList(); + for (CrewMember crewMember : crew) { + this.addCrewMember(crewMember); + } } + /** + * Get the class of the starship + * + * @return The class of the starship + */ public String getStarshipClass() { - return starshipClass; + return this.starshipClass; } + /** + * Set the starship's class + * + * @param starshipClass The new class for the starship + */ 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; - } }