Skip to content
This repository was archived by the owner on Feb 12, 2023. It is now read-only.

Commit b824769

Browse files
committed
Implement the App Store Menu
1 parent 39bac58 commit b824769

File tree

1 file changed

+94
-9
lines changed

1 file changed

+94
-9
lines changed

src/main/Driver.java

Lines changed: 94 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import models.*;
66
import utils.FoundationClassUtilities;
77
import utils.ScannerInput;
8+
import utils.Utilities;
89

910
/**
1011
* The responsibility for this class is to manage the User Interface (UI) i.e. the menu and user input/output. This class should be the only class that has System.out.println() or Scanner reads in it. This class contains an object of AppStoreAPI and an object of DeveloperAPI.
@@ -13,8 +14,8 @@
1314
* @link <a href="https://github.com/DevExzh/">GitHub Link</a>
1415
*/
1516
public class Driver {
16-
private DeveloperAPI developerAPI = new DeveloperAPI();
17-
private AppStoreAPI appStoreAPI = new AppStoreAPI();
17+
private final DeveloperAPI developerAPI = new DeveloperAPI();
18+
private final AppStoreAPI appStoreAPI = new AppStoreAPI();
1819

1920
public static void main(String[] args) {
2021
new Driver().start();
@@ -52,13 +53,12 @@ private void runMainMenu() {
5253
while (option != 0) {
5354
switch (option) {
5455
case 1 -> runDeveloperMenu();
55-
//case 2 -> // TODO run the App Store Menu and the associated methods (your design here)
56-
//case 3 -> // TODO run the Reports Menu and the associated methods (your design here)
56+
case 2 -> runAppMenu();
57+
case 3 -> runReportMenu();
5758
case 4 -> searchAppsBySpecificCriteria();
5859
case 5 -> appStoreAPI.sortAppsByNameAscending(); // Sort Apps by Name
59-
case 6 -> { // print the recommended apps
60-
System.out.println(appStoreAPI.listAllRecommendedApps());
61-
}
60+
case 6 -> // print the recommended apps
61+
System.out.println(appStoreAPI.listAllRecommendedApps());
6262
case 7 -> {
6363
System.out.println("=== App of the Day ==="); // print the random app of the day
6464
System.out.println(appStoreAPI.randomApp());
@@ -80,6 +80,92 @@ private void exitApp() {
8080
System.exit(0);
8181
}
8282

83+
//---------------------
84+
// App Management
85+
// --------------------
86+
private void runAppMenu() {
87+
switch (FoundationClassUtilities.scanValidInteger("""
88+
|--------App Store Menu--------|
89+
| 1) Add an app |
90+
| 2) Update an app |
91+
| 3) Delete an app |
92+
| 0) RETURN to main menu |
93+
|------------------------------|
94+
""", (choice) -> choice >= 0 && choice <= 5)) {
95+
default:
96+
ScannerInput.validNextLine("Invalid option... Return to main menu.\nPress any key to continue...");
97+
case 0:
98+
return;
99+
case 1: // Add an app
100+
String appType = FoundationClassUtilities.scanValidString(
101+
"Which type of App would you like to add [Education / Game / Productivity]? ",
102+
new String[] {"e", "education", "g", "game", "p", "productivity"});
103+
Developer developer = readValidDeveloperByName();
104+
String appName = ScannerInput.validNextLine("Please enter the name of the App: ");
105+
double appSize = FoundationClassUtilities.scanValidDouble("Please enter the size of the App: ",
106+
(value) -> value >= 1 && value <= 1000); // [1, 1000]
107+
double appVersion = FoundationClassUtilities.scanValidDouble("Please enter the version of the App: ",
108+
(value) -> Utilities.greaterThanOrEqualTo(value, 1.0)); // >= 1.0
109+
double appCost = FoundationClassUtilities.scanValidDouble("Please enter the cost of the App: ",
110+
(value) -> Utilities.greaterThanOrEqualTo(value, 0)); // >= 0
111+
System.out.println(appStoreAPI.addApp(switch (appType.toLowerCase()) {
112+
case "education", "e" ->
113+
new EducationApp(developer, appName, appSize, appVersion, appCost,
114+
ScannerInput.validNextInt("Please enter the level of the education app: "));
115+
case "game", "g" ->
116+
new GameApp(developer, appName, appSize, appVersion, appCost, Utilities.YNtoBoolean(
117+
ScannerInput.validNextChar("Does the game support multiplayer? [Y/n]: ")));
118+
case "productivity", "p" ->
119+
new ProductivityApp(developer, appName, appSize, appVersion, appCost);
120+
default -> // Will never reach here, just to be safe
121+
new ProductivityApp(developer, "", -1, -1, -1);
122+
}) ? "The app has been added successfully." : "There was an error adding the app.");
123+
break;
124+
case 2: // Update an app
125+
appStoreAPI.listAllApps();
126+
App appToBeUpdated = appStoreAPI.getAppByIndex(
127+
FoundationClassUtilities.scanValidInteger(
128+
"Please choose the index of the App you want to update: ",
129+
appStoreAPI::isValidIndex));
130+
switch (FoundationClassUtilities.scanValidString(
131+
"Which property would you like to update? [Name / Description / Size / Cost / Version]",
132+
new String[] {"n", "name", "d", "description", "s", "size", "c", "cost", "v", "version"})) {
133+
case "n", "name" -> appToBeUpdated.setAppName(
134+
ScannerInput.validNextLine("Please enter the name of the App: "));
135+
case "d", "description" -> appToBeUpdated.setDescription(
136+
ScannerInput.validNextLine("Please enter the description of the App: "));
137+
case "s", "size" -> appToBeUpdated.setAppSize(
138+
FoundationClassUtilities.scanValidDouble("Please enter the size of the App: ",
139+
(value) -> value >= 1 && value <= 1000));
140+
case "c", "cost" -> appToBeUpdated.setAppCost(
141+
FoundationClassUtilities.scanValidDouble("Please enter the cost of the App: ",
142+
(value) -> Utilities.greaterThanOrEqualTo(value, 0)));
143+
case "v", "version" -> appToBeUpdated.setAppVersion(
144+
FoundationClassUtilities.scanValidDouble("Please enter the version of the App: ",
145+
(value) -> Utilities.greaterThanOrEqualTo(value, 1.0)));
146+
}
147+
break;
148+
case 3: // Delete an app
149+
appStoreAPI.listAllApps();
150+
System.out.println(
151+
appStoreAPI.deleteAppByIndex(
152+
FoundationClassUtilities.scanValidInteger(
153+
"Please choose the index of the App you want to delete: ",
154+
appStoreAPI::isValidIndex)) == null
155+
? "There was an error deleting the specified App."
156+
: "The App has been deleted successfully.");
157+
break;
158+
159+
}
160+
}
161+
162+
//---------------------
163+
// Reports
164+
// --------------------
165+
private void runReportMenu() {
166+
167+
}
168+
83169
//--------------------------------------------------
84170
// Developer Management - Menu Items
85171
//--------------------------------------------------
@@ -160,7 +246,6 @@ private void searchAppsBySpecificCriteria() {
160246
3) Rating (all apps of that rating or above)""");
161247
int option = ScannerInput.validNextInt("==>> ");
162248
switch (option) {
163-
// TODO Search methods below
164249
case 1 -> searchAppsByName();
165250
case 2 -> searchAppsByDeveloper(readValidDeveloperByName());
166251
case 3 -> searchAppsEqualOrAboveAStarRating();
@@ -185,7 +270,7 @@ private void searchAppsByName() {
185270
}
186271

187272
private void simulateRatings() {
188-
// simulate random ratings for all apps (to give data for recommended apps and reports etc).
273+
// simulate random ratings for all apps (to give data for recommended apps and reports etc.).
189274
if (appStoreAPI.numberOfApps() > 0) {
190275
System.out.println("Simulating ratings...");
191276
appStoreAPI.simulateRatings();

0 commit comments

Comments
 (0)