Question: 1. Add a Main class with a main method. In the main method, create objects and call methods on the objects to demonstrate their behavior.
1. Add a Main class with a main method. In the main method, create objects and call methods on the objects to demonstrate their behavior. Be sure to print something or include methods that print something.
2. using the Lab 8 App Store project, you could create a AppStore object and call printAppStoreSummaryStats and/or printAppList.
import java.util.ArrayList;
/** * An app store containing apps * * Modifications: * CT: Create AppStore with a list of apps and basic methods to add, clear, and print apps * * @author * @version 2018.05.28 */ public class AppStore { private String appStoreName; private ArrayList
/** * Find an app based on its name * @param name the name of the app to search for * @return the app with the given name * or null if there is no app with that name */ public App findApp(String name) { if(name != null && name.length() != 0) { for(App thisApp: appList) { if(thisApp.getName().equalsIgnoreCase(name)) { return thisApp; } } } return null; } /** * Remove an app based on its name. * @param name remove the name of the app * @return the app with the given name or null if there is no app with that name */ public int removeApp(String name) { if(name != null && name.length() != 0) { App appToRemove = findApp(name); if(appToRemove != null) { appList.remove(appToRemove); } } return 0; } /** * Returns all apps by author's name * @param name the name of the app to remove * @return the app with the given name or null if there is * no app with that name */ public ArrayList
/** * Returns the number of apps in the appstore with the given rating * @param rating integer * @return the count of apps in the appstore with the given rating */ public int getNumberAppsWithRating(int rating) { int countOfAppsWithGivenRating = 0; if(rating >= 0 && rating <= 5) { for(App x : appList) { if(x.getRating() == rating) { countOfAppsWithGivenRating++; } } } return countOfAppsWithGivenRating; }
/** * pritns the name of the app store, the total number of apps * the number o apps of each rating * and the number of unrated apps * @param rating integer * @return the count of apps in the app store with the given rating */ public void printAppStoreSummaryStats() { System.out.println("======== SUMMARY STATS for Joe's App Store ========"); int countRating5 = 0; int countRating4 = 0; int countRating3 = 0; int countRating2 = 0; int countRating1 = 0; int countNoRating = 0; System.out.println("Total # of apps: " + getNumberOfApps()); for(App x : appList) { if(x.getRating() == 5)countRating5++; else if(x.getRating() == 4)countRating4++; else if(x.getRating() == 3)countRating3++; else if(x.getRating() == 2)countRating2++; else if(x.getRating() == 1)countRating1++; else countNoRating++; } System.out.println("# apps rated 5: " + countRating5); System.out.println("# apps rated 4: " + countRating4); System.out.println("# apps rated 3: " + countRating3); System.out.println("# apps rated 2: " + countRating2); System.out.println("# apps rated 1: " + countRating1); System.out.println(" # of unrated apps: " + countNoRating); System.out.println("==========================================="); } }
/** * An app for a mobile device * * @author * @version 2018.06.10 */ public class App { private String name; private String author; private double price; private int rating; // valid ratings are 1-4; 0 means not rated /** * Create an app with the given name, author, price, and rating * @param appName the app name * @param appAuthor the app author * @param appPrice the price of the app (0 if the app is free) * @param appRating the app's rating */ public App(String appName, String appAuthor, double appPrice, int appRating) { name = appName; author = appAuthor; price = appPrice; setRating(appRating); } /** * Create an app with the given name, author, and price that is not rated * @param appName the app name * @param appAuthor the app author * @param appPrice the price of the app (0 if the app is free) */ public App(String appName, String appAuthor, double appPrice) { name = appName; author = appAuthor; price = appPrice; rating = 0; }
/** * @return the app name */ public String getName() { return name; } /** * @return the app author */ public String getAuthor() { return author; } /** * @return the app price */ public double getPrice() { return price; } /** * Set the price of this app to the value given * @param newPrice new price for this app */ public void setPrice(double newPrice) { if (newPrice >= 0) { price = newPrice; } else { System.out.println("Error: Price must be greater than or equal to 0"); } } /** * @return true if this app is free, false otherwise */ public boolean isFree() { return price == 0; } /** * @return the app rating */ public int getRating() { return rating; } /** * Set the rating of this app to the value given * @param newRating new rating for this app */ public void setRating(int newRating) { if (newRating >= 1 && newRating <= 4) { rating = newRating; } else { System.out.println("Error: Valid range for rating is 1 to 4"); } } /** * Reset the rating of this app to not rated */ public void resetRating() { rating = 0; } /** * Increase the rating of this app by 1 */ public void increaseRating() { if (rating < 4) { rating = rating + 1; } }
/** * Decrease the rating of this app by 1 */ public void decreaseRating() { if (rating > 1) { rating = rating - 1; } }
/** * Print information on this app */ public void printAppInfo() { System.out.println("---------------------------------"); System.out.println("App: " + name); System.out.println("Author: " + author); if (price == 0) { System.out.println("Price: FREE"); } else { System.out.println("Price: $" + price); } if (rating == 0) { System.out.println("Rating: (not rated)"); } else { System.out.println("Rating: " + rating); } System.out.println("---------------------------------"); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
