-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserInterface.java
More file actions
127 lines (111 loc) · 5.65 KB
/
UserInterface.java
File metadata and controls
127 lines (111 loc) · 5.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import java.util.ArrayList;
import java.util.Scanner;
public class UserInterface {
// new scanner for this class
private Scanner scanner = new Scanner(System.in);
// new non-static activityManager to handle activity data and operations
private ActivityManager manager;
private ArrayList<Activity> chosenActivities = new ArrayList<>();
// constructor to initialize UserInterface with an existing ActivityManager
public UserInterface(ActivityManager manager) {
this.manager = manager;
}
// method to run the main user interface
public void run() {
System.out.println("Welcome to TrailBlazer!");
while (chosenActivities.size() < 3) {
System.out.println("Available Activities:");
// displays a list of activities available from the manager (expandable - will still print all available)
for (int i = 0; i < manager.getActivities().size(); i++) {
System.out.println((i + 1) + ". " + manager.getActivities().get(i).getName());
}
// prompt user to choose an activity by number
System.out.print("Choose an activity (number): ");
int choice = scanner.nextInt() - 1; // subtract 1 to align with indexes
// validate the choice
if (choice >= manager.getActivities().size() || choice < 0) {
System.out.println("Invalid number. Please try again.");
continue; // prompt user again
}
Activity selectedActivity = manager.getActivities().get(choice);
// check if the activity has already been chosen
if (chosenActivities.contains(selectedActivity)) {
System.out.println("You already chose this activity! Please select another.");
continue; // prompt user again
}
chosenActivities.add(selectedActivity);
// prompt user to enter the month for the activity (1-12)
System.out.print("Enter the month (1-12): ");
int month = scanner.nextInt();
// validate the month
if (month < 1 || month > 12) {
System.out.println("Invalid month. Please try again.");
chosenActivities.remove(selectedActivity); // remove invalid choice
continue; // prompt user again
}
// show location options based on the TYPE of the activity chosen
System.out.println("Choose a location:");
if (selectedActivity instanceof Camping) {
System.out.println("1. South Carlsbad State Beach Campground");
System.out.println("2. Los Padres National Forest");
System.out.println("3. High Desert Hot Springs Hideout");
} else if (selectedActivity instanceof Surfing) {
System.out.println("1. Hermosa Beach");
System.out.println("2. Carmel Beach");
System.out.println("3. Newport Beach");
} else if (selectedActivity instanceof Hiking) {
System.out.println("1. Crystal Cove State Park");
System.out.println("2. Joshua Tree National Park");
System.out.println("3. Topanga State Park");
}
// ask for user's location choice (options are above)
System.out.print("Enter your location choice: ");
int locationChoice = scanner.nextInt();
// find the location string based on the type of activity and user choice
String location = null;
boolean validLocation = true;
switch (locationChoice) {
case 1:
if (selectedActivity instanceof Camping) {
location = "South Carlsbad State Beach Campground";
} else if (selectedActivity instanceof Surfing) {
location = "Hermosa Beach";
} else if (selectedActivity instanceof Hiking) {
location = "Crystal Cove State Park";
}
break;
case 2:
if (selectedActivity instanceof Camping) {
location = "Los Padres National Forest";
} else if (selectedActivity instanceof Surfing) {
location = "Carmel Beach";
} else if (selectedActivity instanceof Hiking) {
location = "Joshua Tree National Park";
}
break;
case 3:
if (selectedActivity instanceof Camping) {
location = "High Desert Hot Springs Hideout";
} else if (selectedActivity instanceof Surfing) {
location = "Newport Beach";
} else if (selectedActivity instanceof Hiking) {
location = "Topanga State Park";
}
break;
default:
System.out.println("Invalid choice. Please try again.");
chosenActivities.remove(selectedActivity); // remove invalid choice
validLocation = false;
break;
}
if (!validLocation) {
continue; // prompt user again
}
// print the selected activity's details
System.out.println(selectedActivity.toString());
// show data specific to the chosen activity, month, and location from the abstract method
selectedActivity.displayActivityData(month, location);
}
System.out.println("Thank you for using the program!");
}
}