cs-3443: finish up lab2 with uml

This commit is contained in:
Price Hiller 2024-06-24 22:37:50 -05:00
parent aca6abf2ef
commit 958aeef38f
Signed by: Price
GPG Key ID: C3FADDE7A8534BEB
9 changed files with 411 additions and 65 deletions

View File

@ -0,0 +1,67 @@
@startuml
skinparam classAttributeIconSize 0
class 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)
}
skinparam classAttributeIconSize 0
class 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)
}
skinparam classAttributeIconSize 0
class 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)
}
skinparam classAttributeIconSize 0
class Lab2 {
+ _ main(args: String[]): void
}
Fleet o-- Starship
Starship o-- CrewMember
Lab2 ..|> Fleet
@enduml

File diff suppressed because one or more lines are too long

After

Width:  |  Height:  |  Size: 14 KiB

View File

@ -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.

View File

@ -0,0 +1,66 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>zfp106 UML Diagram for Lab2</title>
</head>
<body>
<h1 style="color: red; background: yellow">
Made with <u>infinite, undying, <i>hatred</i></u> in PlantUML (Lab2,
zfp106)
</h1>
<p>
Some notes on the decisions made here.
<table border=1>
<tr>
<th>Question</th>
<th>Answer</th>
</tr>
<tr>
<td>Why is <code>Starship</code> associated to <code>CrewMember</code> via
aggregation?</td>
<td>Because a <code>Starship</code> has 0 or more crew members and crew
members aren't dependent on a <code>Starship</code> to exist. It is possible for a
crew member to not be assigned to <i>any</i>
<code>Starship</code>.
</td>
</tr>
<tr>
<td>
Why is <code>Fleet</code> associated to <code>Starship</code> via aggregation?
</td>
<td>
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 <i>n</i> number of ships in the first
place anyhow.
</td>
</tr>
<tr>
<td>Why does <code>Lab2</code> with its <code>main</code> method have a dependency
relationship with <code>Fleet</code>?</td>
<td>Because a near identical example of this relationship was in the slides. <q>We will
primarily use it for classes referenced in <strong><code>main</code></strong>.</q></td>
</tr>
<tr>
<td>Why don't you show associations/other connections to Java standard library classes like
<code>ArrayList</code>?
</td>
<td>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 <code>Object</code> class.</td>
</tr>
</table>
</p>
<p><q>Your answers are wrong and you should feel bad.</q> Them's fighting words, and I have a
whole hill here to defend and die upon, sword gleaming in the midnight sun.</p>
<p><u>The actual UML diagram below:<u></p>
<img src="./Layout.svg" alt="Uml Diagram" />
</body>
</html>

View File

@ -1,10 +1,10 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.zfp106.lab1</groupId>
<artifactId>lab1</artifactId>
<groupId>org.zfp106.lab2</groupId>
<artifactId>lab2</artifactId>
<packaging>jar</packaging>
<version>0.1</version>
<name>lab1</name>
<name>lab2</name>
<url>http://maven.apache.org</url>
<dependencyManagement>
<dependencies>
@ -37,7 +37,7 @@
<configuration>
<archive>
<manifest>
<mainClass>com.zfp106.lab1.Lab1</mainClass>
<mainClass>com.zfp106.lab2.lab2</mainClass>
</manifest>
</archive>
</configuration>

View File

@ -1,5 +1,12 @@
package com.zfp106.lab2;
/**
* A crew member that can be assigned to a starship
*
* <p>A crew member has a name, a position, a rank, a species, and an assignment.
*
* <p>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;
}

View File

@ -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
*
* <p>Each fleet has the following: - A name - Starships said fleet contains
* <p>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<Starship>());
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public ArrayList<Starship> getStarships() {
return starships;
}
public void setStarships(ArrayList<Starship> 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<Starship> 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<Starship> 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<String> starshipInfo = new ArrayList<String>();
try (Stream<String> 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--;
}

View File

@ -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
*
* <p>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<CrewMember> 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<CrewMember>());
}
/**
* 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.
*
* <p>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<CrewMember> 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<CrewMember> crew) {
this.crew = crew;
this.crew = new ArrayList<CrewMember>();
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<String> starshipInfo = new ArrayList<String>();
try (Stream<String> 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;
}
}