cs-3443: finish lab2 functionality
TODO: add docs
This commit is contained in:
parent
9b9795753b
commit
06bea0b965
@ -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;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,69 @@
|
|||||||
|
package com.zfp106.lab2;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.file.DirectoryStream;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.nio.file.Paths;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.stream.StreamSupport;
|
||||||
|
|
||||||
|
public class Fleet {
|
||||||
|
private String name;
|
||||||
|
private ArrayList<Starship> starships;
|
||||||
|
|
||||||
|
public Fleet(String name) {
|
||||||
|
this.setName(name);
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
while (remainingFiles > 0) {
|
||||||
|
curFile = Math.floorMod(curFile - 2, starshipFiles.length);
|
||||||
|
while (starshipFiles[curFile] == null) {
|
||||||
|
curFile--;
|
||||||
|
if (curFile < 0) {
|
||||||
|
curFile = Math.floorMod(curFile, starshipFiles.length);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (starshipFiles[curFile] != null) {
|
||||||
|
this.starships.add(Starship.loadStarshipFromPath(starshipFiles[curFile].toPath()));
|
||||||
|
starshipFiles[curFile] = null;
|
||||||
|
remainingFiles--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,27 +1,32 @@
|
|||||||
|
package com.zfp106.lab2;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Lab2 is a Java class containing a main method to run your program when completed.
|
* Lab2 is a Java class containing a main method to run your program when
|
||||||
* This class will not compile until you have completed the requirements outlined in
|
* completed.
|
||||||
|
* This class will not compile until you have completed the requirements
|
||||||
|
* outlined in
|
||||||
* the lab description.
|
* the lab description.
|
||||||
*
|
*
|
||||||
* @author Amanda Fernandez (abc123)
|
* @author Price Hiller (zfp106)
|
||||||
* UTSA CS 3443 - Lab 2
|
* UTSA CS 3443 - Lab 2
|
||||||
* Fall 2022
|
* Fall 2024
|
||||||
*/
|
*/
|
||||||
public class Lab2 {
|
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 {
|
try {
|
||||||
unitedFederation.loadStarships( "data" );
|
unitedFederation.loadStarships("data");
|
||||||
|
|
||||||
}catch( Exception e ) {
|
} catch (Exception e) {
|
||||||
// this is a try/catch which will show any errors to the console (more on these in coming weeks!)
|
// this is a try/catch which will show any errors to the console (more on these
|
||||||
|
// in coming weeks!)
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
System.out.println( unitedFederation );
|
System.out.println(unitedFederation);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -0,0 +1,96 @@
|
|||||||
|
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<CrewMember> crew;
|
||||||
|
|
||||||
|
public Starship(String name, String registry, String starship_class) {
|
||||||
|
this.setName(name);
|
||||||
|
this.setRegistry(registry);
|
||||||
|
this.setStarshipClass(starship_class);
|
||||||
|
this.setCrew(new ArrayList<CrewMember>());
|
||||||
|
}
|
||||||
|
|
||||||
|
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<CrewMember> getCrew() {
|
||||||
|
return crew;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCrew(ArrayList<CrewMember> 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<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;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,81 @@
|
|||||||
|
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());
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user