Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- import java.util.List;
- import java.util.Scanner;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.Comparator;
- public class AppManagerTest {
- public static void main(String[] args) throws Exception {
- Scanner jin = new Scanner(System.in);
- int n = Integer.parseInt(jin.nextLine());
- App[] apps = new App[n];
- for ( int i = 0 ; i < n ; ++i ) {
- apps[i] = new App(jin.nextLine(),jin.nextLine(),jin.nextInt(),jin.nextDouble());
- jin.nextLine();
- }
- int k = jin.nextInt();
- AppManager am = new AppManager(apps);
- if ( k == 0 ) { //test everything but randomChoice
- while ( true ) {
- System.out.println();
- String cmd = jin.next();
- System.out.println(cmd);
- if ( cmd.equals("stop") ) break;
- if ( cmd.equals("bestApp") ) {
- print(am.bestApp());
- }
- if ( cmd.equals("cheapestApp") ) {
- print(am.cheapestApp());
- }
- if ( cmd.equals("allApps") ) {
- print(am.allApps(jin.next(),jin.nextBoolean()));
- }
- if ( cmd.equals("ratingStats") ) {
- System.out.println(Arrays.toString(am.ratingStats()));
- }
- }
- }
- else { //test randomChoice
- System.out.println("Testing random choice...");
- int w = jin.nextInt();
- for ( int q = 0 ; q < w ; ++q ) {
- boolean[] flags = new boolean[n];
- for ( int i = 0 ; i <= n/3 ; ++i ) {
- List<App> res = am.randomChoice();
- for ( App a : res ) {
- int idx = idxOf(apps,a);
- if ( idx == -1 ) System.out.println("You returned an app that wasn't in the list at all? What are you doing???");
- if ( flags[idx] ) {
- System.out.println("You returned an app twice, before returning all the apps in the list. I want to see all the apps first then you can give me duplicates.");
- throw new Exception("App already returned");
- }
- flags[idx] = true;
- }
- }
- }
- System.out.println("Great work on implementing randomChoice. That is just what we needed.");
- }
- }
- private static void print(App app) {
- System.out.println("Name: "+app.getName());
- System.out.println("Rating: "+app.getRating());
- System.out.printf("Price: %.2f$\n",app.getPrice());
- }
- private static void print(List<App> apps) {
- for ( App ap : apps ) {
- print(ap);
- System.out.println();
- }
- }
- private static int idxOf(App apps[],App a){
- for ( int i = 0 ; i < apps.length ; ++i )
- if ( equal(apps[i],a) ) return i;
- return -1;
- }
- private static boolean equal(App a , App b) {
- return a.getName().equals(b.getName())&&a.getPrice()==b.getPrice()&&a.getRating()==b.getRating();
- }
- }
- /**
- * @author Damjan
- *
- */
- final class App {
- private String name;
- private String description;
- private double price;
- private int rating;
- public App(String name, String description,int rating,double price) {
- this.name = name;
- this.description = description;
- this.price = price;
- this.rating = rating;
- }
- public String getName() {
- return name;
- }
- public String getDescription() {
- return description;
- }
- public double getPrice() {
- return price;
- }
- public int getRating() {
- return rating;
- }
- }
- /**
- * @author Damjan
- *
- */
- class AppManager {
- private List<App> applications;
- public AppManager(App... apps) {
- applications = new ArrayList<App>();
- for (App a : apps) {
- applications.add(a);
- Collections.shuffle(applications);
- }
- }
- public App bestApp() {
- return Collections.max(applications, new PriceComparator());
- }
- public App cheapestApp() {
- return Collections.min(applications, new PriceComparator());
- }
- public List<App> allApps(String attribute, boolean ascending) {
- List<App> tmp = applications;
- if (attribute.equals("name")) {
- Collections.sort(applications, new NameComparator());
- } else if (attribute.equals("price")) {
- Collections.sort(applications, new PriceComparator());
- } else if (attribute.equals("rating")) {
- Collections.sort(applications, new RatingComparator());
- }
- if (!ascending) {
- Collections.reverse(applications);
- }
- return tmp;
- }
- /* public boolean contains(int rating) {
- for (App a : applications) {
- if (a.getRating() == rating) {
- return true;
- }
- }
- return false;
- }*/
- public int[] ratingStats() {
- int rate[] = new int[5];
- for (int i = 0; i < applications.size(); i++) {
- if (applications.get(i).getRating() == 1) {
- rate[0]++;
- }
- else if (applications.get(i).getRating() == 2) {
- rate[1]++;
- }
- else if (applications.get(i).getRating() == 3) {
- rate[2]++;
- }
- else if (applications.get(i).getRating() == 4) {
- rate[3]++;
- }
- else if (applications.get(i).getRating() == 5) {
- rate[4]++;
- }
- }
- return rate;
- }
- public List<App> randomChoice() {
- List<App> rand = new ArrayList<App>(3);
- return rand;
- }
- }
- class RatingComparator implements Comparator<App> {
- @Override
- public int compare(App arg0, App arg1) {
- return arg0.getRating()-arg1.getRating();
- }
- }
- class PriceComparator implements Comparator<App> {
- @Override
- public int compare(App o1, App o2) {
- return Double.compare(o1.getPrice(), o2.getPrice());
- }
- }
- class NameComparator implements Comparator<App> {
- @Override
- public int compare(App o1, App o2) {
- return o1.getName().compareTo(o2.getName());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment