From c55a79964cd8681edbd197d04aa619620f74a7da Mon Sep 17 00:00:00 2001 From: 101zh <67253838+101zh@users.noreply.github.com> Date: Fri, 16 Feb 2024 21:48:50 +0000 Subject: [PATCH] feat(9): finish Assignment9: Ultimate Frisbee --- src/main/java/Unit9/Captain.java | 32 ++++++++++ src/main/java/Unit9/Coach.java | 17 ++++++ src/main/java/Unit9/Person.java | 28 +++++++++ src/main/java/Unit9/UltimatePlayer.java | 37 ++++++++++++ src/main/java/Unit9/UltimateTeam.java | 52 ++++++++++++++++ src/test/java/Unit9/runner_Ultimate.java | 75 ++++++++++++++++++++++++ 6 files changed, 241 insertions(+) create mode 100644 src/main/java/Unit9/Captain.java create mode 100644 src/main/java/Unit9/Coach.java create mode 100644 src/main/java/Unit9/Person.java create mode 100644 src/main/java/Unit9/UltimatePlayer.java create mode 100644 src/main/java/Unit9/UltimateTeam.java create mode 100644 src/test/java/Unit9/runner_Ultimate.java diff --git a/src/main/java/Unit9/Captain.java b/src/main/java/Unit9/Captain.java new file mode 100644 index 0000000..9fcabb5 --- /dev/null +++ b/src/main/java/Unit9/Captain.java @@ -0,0 +1,32 @@ +package Unit9; + +// Have to get rid of package statement + +public class Captain extends UltimatePlayer { + private boolean type; + + public Captain(String firstName, String lastName, String position, boolean type) { + super(firstName, lastName, position); + this.type = type; + } + + @Override + public int throwDisc(int pow) { + return (int) (super.throwDisc(pow) * 1.25); + } + + @Override + public String toString() { + String typeName; + + if (type) { + typeName = "offense"; + } else { + typeName = "defense"; + } + + return super.toString() + "\n" + + " Captain: " + typeName; + } + +} diff --git a/src/main/java/Unit9/Coach.java b/src/main/java/Unit9/Coach.java new file mode 100644 index 0000000..aa7574a --- /dev/null +++ b/src/main/java/Unit9/Coach.java @@ -0,0 +1,17 @@ +package Unit9; + +// Have to get rid of package statement + +public class Coach extends Person { + String role; + + public Coach(String firstName, String lastName, String role) { + super(firstName, lastName); + this.role = role; + } + + public String toString() { + return super.toString() + "\n" + + " Role: " + role; + } +} diff --git a/src/main/java/Unit9/Person.java b/src/main/java/Unit9/Person.java new file mode 100644 index 0000000..572fe2f --- /dev/null +++ b/src/main/java/Unit9/Person.java @@ -0,0 +1,28 @@ +package Unit9; + +// Have to get rid of package statement + +public class Person { + private String firstName; + private String lastName; + + public Person(String firstName, String lastName) { + this.firstName = firstName; + this.lastName = lastName; + } + + public int throwDisc(int pow) { + if (pow > 10) { + pow = 10; + } else if (pow < 1) { + pow = 1; + } + + return pow * 2; + } + + @Override + public String toString() { + return lastName + ", " + firstName; + } +} diff --git a/src/main/java/Unit9/UltimatePlayer.java b/src/main/java/Unit9/UltimatePlayer.java new file mode 100644 index 0000000..0f45357 --- /dev/null +++ b/src/main/java/Unit9/UltimatePlayer.java @@ -0,0 +1,37 @@ +package Unit9; + +// Have to get rid of package statement + +public class UltimatePlayer extends Person { + private static int players = 1; + private int jerseyNumber; + private String position; + + public UltimatePlayer(String firstName, String lastName, String position) { + super(firstName, lastName); + if (!(position.equals("handler") || position.equals("cutter"))) { + position = "handler"; + } + + this.position = position; + this.jerseyNumber = players; + players++; + } + + public String getPosition() { + return position; + } + + @Override + public int throwDisc(int pow) { + return super.throwDisc(pow) * 2; + } + + @Override + public String toString() { + return super.toString() + "\n" + + " Jersey #: " + jerseyNumber + "\n" + + " Position: " + getPosition(); + } + +} diff --git a/src/main/java/Unit9/UltimateTeam.java b/src/main/java/Unit9/UltimateTeam.java new file mode 100644 index 0000000..f6e5a61 --- /dev/null +++ b/src/main/java/Unit9/UltimateTeam.java @@ -0,0 +1,52 @@ +package Unit9; + +// Have to get rid of package statement + +import java.util.ArrayList; + +public class UltimateTeam { + ArrayList players; + ArrayList coaches; + + UltimateTeam(ArrayList players, ArrayList coaches) { + this.players = players; + this.coaches = coaches; + } + + public String getCutters() { + String cutters = ""; + for (UltimatePlayer player : players) { + if (player.getPosition().equals("cutter")) { + cutters += player.toString() + "\n"; + } + } + + return cutters; + } + + public String getHandlers() { + String handlers = ""; + for (UltimatePlayer player : players) { + if (player.getPosition().equals("handler")) { + handlers += player.toString() + "\n"; + } + } + + return handlers; + } + + @Override + public String toString() { + String team = "COACHES\n"; + + for (Coach coach : coaches) { + team += coach.toString() + "\n"; + } + team += "\nPLAYERS\n"; + for (UltimatePlayer player : players) { + team += player.toString() + "\n"; + } + + return team; + } +} diff --git a/src/test/java/Unit9/runner_Ultimate.java b/src/test/java/Unit9/runner_Ultimate.java new file mode 100644 index 0000000..3b4e66d --- /dev/null +++ b/src/test/java/Unit9/runner_Ultimate.java @@ -0,0 +1,75 @@ +package Unit9; + +// Have to get rid of package statement + +import java.util.ArrayList; +import java.util.Scanner; + +public class runner_Ultimate { + + public static void main(String[] args) { + ArrayList players = new ArrayList(); + ArrayList coaches = new ArrayList(); + Scanner scan = new Scanner(System.in); + String ins = ""; + while (!ins.equals("q")) { + System.out.println( + "\nWhat do you want to do?\np - make a person\nt - make a team from the current player/coach lists\nq - quit"); + ins = scan.nextLine().toLowerCase(); + if (ins.equals("p")) { + Person p; + System.out.println( + "\nWhich class do you want to use?\np - Person\nu - UltimatePlayer\nc - Captain\no - Coach"); + String cls = scan.nextLine().toLowerCase(); + System.out.println("First name?"); + String fn = scan.nextLine(); + System.out.println("Last name?"); + String ln = scan.nextLine(); + if (cls.equals("u") || cls.equals("c")) { + System.out.println("Position?"); + String ps = scan.nextLine(); + if (cls.equals("c")) { + System.out.println("Offensive coach? (t/f)"); + boolean tp = scan.nextLine().toLowerCase().equals("t"); + p = new Captain(fn, ln, ps, tp); + } else + p = new UltimatePlayer(fn, ln, ps); + players.add((UltimatePlayer) p); + System.out.println("\n" + fn + " " + ln + " was added to the players list."); + } else if (cls.equals("o")) { + System.out.println("Role?"); + String rl = scan.nextLine(); + p = new Coach(fn, ln, rl); + coaches.add((Coach) p); + System.out.println("\n" + fn + " " + ln + " was added to the coaches list."); + } else { + p = new Person(fn, ln); + System.out.println("\nSorry, only UltimatePlayers, Captains and Coaches can be added to the team."); + } + System.out.println("\n" + p); + System.out.println("\nType \"t\" for " + fn + " to throw a disc."); + if (scan.nextLine().toLowerCase().equals("t")) { + System.out.println("Enter power level between 1 and 10."); + System.out.println(fn + " threw the disc " + p.throwDisc(scan.nextInt()) + " yards."); + scan.nextLine(); + } + } else if (ins.equals("t")) { + UltimateTeam t = new UltimateTeam(players, coaches); + System.out.println("\nYour team is ready!\n"); + while (!ins.equals("q")) { + System.out.println( + "\nWhat do you want to do?\nc - see the cutters\nh - see handlers\nt = see the whole team\nq - quit"); + ins = scan.nextLine().toLowerCase(); + if (ins.equals("h")) + System.out.println("\n" + t.getHandlers()); + else if (ins.equals("c")) + System.out.println("\n" + t.getCutters()); + else if (ins.equals("t")) + System.out.println("\n" + t + "\n"); + } + } + } + + scan.close(); + } +}