summaryrefslogtreecommitdiff
path: root/src/main/java/design/controller/userinput/menus/ManageClubs.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/design/controller/userinput/menus/ManageClubs.java')
-rw-r--r--src/main/java/design/controller/userinput/menus/ManageClubs.java105
1 files changed, 105 insertions, 0 deletions
diff --git a/src/main/java/design/controller/userinput/menus/ManageClubs.java b/src/main/java/design/controller/userinput/menus/ManageClubs.java
new file mode 100644
index 0000000..56d645f
--- /dev/null
+++ b/src/main/java/design/controller/userinput/menus/ManageClubs.java
@@ -0,0 +1,105 @@
+package design.controller.userinput.menus;
+
+import design.controller.userinput.Menu;
+import design.controller.userinput.MenuOption;
+import design.model.Club;
+import design.model.Golfer;
+import design.persistence.PersonalDatabase;
+import design.runtime.Session;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Scanner;
+
+public class ManageClubs extends Menu {
+ Golfer golfer = Session.getCurrentGolfer();
+
+ @Override
+ public String getTitle() {
+ return "manage clubs";
+ }
+
+ @Override
+ public List<MenuOption> getMenuOptions() {
+ List<MenuOption> opts = new java.util.ArrayList<>();
+ opts.add(new MenuOption("return to main menu", () -> new MainMenu().present()));
+
+ opts.add(new MenuOption("list clubs", () -> {
+ for (Club club : golfer.getClubs()) {
+ System.out.printf("- %s\n", club);
+ }
+ this.present();
+ }));
+
+ opts.add(new MenuOption("remove club...", () -> {
+ new Menu() {
+ @Override
+ public String getTitle() {
+ return "remove club";
+ }
+
+ @Override
+ public List<MenuOption> getMenuOptions() {
+ List<MenuOption> list = new ArrayList<>();
+ for (Club c : golfer.getClubs()) {
+ MenuOption menuOption = new MenuOption(c.toString(), () -> {
+ golfer.removeClub(c);
+ this.present();
+ });
+ list.add(menuOption);
+ }
+ return list;
+ }
+ }.present();
+ }));
+
+ opts.add(new MenuOption("add club...", () -> {
+ if (golfer == null) {
+ System.out.println("No user loaded.");
+ new UserSettings().present();
+ return;
+ }
+
+ Scanner sc = new Scanner(System.in);
+
+ System.out.print("Manufacturer: ");
+ String manufacture = sc.nextLine().trim();
+
+ System.out.print("Nickname: ");
+ String nickname = sc.nextLine().trim();
+
+ // Pick type
+ Club.ClubType[] types = Club.ClubType.values();
+ System.out.println("Club type:");
+ for (int i = 0; i < types.length; i++) {
+ System.out.printf("%d: %s%n", i + 1, types[i]);
+ }
+
+ Club.ClubType type = null;
+ while (type == null) {
+ System.out.print("Select (1.." + types.length + "): ");
+ String line = sc.nextLine().trim();
+ int idx = Integer.parseInt(line);
+ if (idx < 1 || idx > types.length) {
+ System.out.println("Out of range. Try again.");
+ continue;
+ }
+ type = types[idx - 1];
+ }
+
+ golfer.addClub(manufacture, nickname, type);
+
+ // Add club to JSON
+ try {
+ PersonalDatabase.INSTANCE.updateGolfer(golfer);
+ System.out.println("Club added and saved.");
+ } catch (IOException e) {
+ throw new RuntimeException("Failed to save club", e);
+ }
+ new UserSettings().present();
+ }));// Pick type
+// Add club to JSON
+ return opts;
+ }
+}