Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Collections;
- import java.util.Comparator;
- import java.util.List;
- import java.util.Random;
- import java.util.Scanner;
- class App implements Comparable<App>
- {
- private final String name;
- private final String description;
- private final double price;
- private final int rating;
- public App(String n, String d, int r, double p)
- {
- name = n;
- description = d;
- price = p;
- rating = r;
- }
- public int compareTo(App rhs)
- {
- return (int)price - (int)rhs.price;
- }
- public String getName()
- {
- return name;
- }
- public String getDescription()
- {
- return description;
- }
- public double getPrice()
- {
- return price;
- }
- public int getRating()
- {
- return rating;
- }
- public static class NameComparator implements Comparator<App>
- {
- public int compare(App a1, App a2)
- {
- return a1.getName().compareTo(a2.getName());
- }
- }
- public static class DescriptionComparator implements Comparator<App>
- {
- public int compare(App a1, App a2)
- {
- return a1.getDescription().compareTo(a2.getDescription());
- }
- }
- class PriceComparator implements Comparator<App>
- {
- public int compare(App a1, App a2)
- {
- return new Double(a1.getPrice()).compareTo(new Double(a2.getPrice()));
- }
- }
- public static class RatingComparator implements Comparator<App>
- {
- public int compare(App a1, App a2)
- {
- return new Integer(a1.getRating()).compareTo(new Integer(a2.getRating()));
- }
- }
- }
- class AppManager
- {
- private static int r;
- private ArrayList<App> AL;
- public AppManager(App... apps)
- {
- r = 0;
- AL = new ArrayList<App>();
- for(App app : apps)
- {
- AL.add(app);
- }
- }
- public App bestApp()
- {
- int best = 0;
- for(int i = 1 ; i < AL.size() ; ++i)
- if(AL.get(i).getRating() > AL.get(best).getRating())
- best = i;
- return AL.get(best);
- }
- public App cheapestApp()
- {
- int cheapest = 0;
- for(int i = 1 ; i < AL.size(); ++i)
- {
- if(AL.get(i).getPrice() < AL.get(cheapest).getPrice())
- cheapest = i;
- }
- return AL.get(cheapest);
- }
- public List<App> randomChoice()
- {
- List<App> result = new ArrayList<App>();
- //System.out.println(r);
- for(int i = 0 ; i < Math.min(3, AL.size()-r) ; ++i)
- {
- result.add(AL.get(r));
- r = (r+1) % AL.size();
- }
- return result;
- }
- public List<App> allApps(String attribute, boolean ascending)
- {
- List<App> AL2 = new ArrayList<App>(AL);
- if(attribute.equals("name"))
- Collections.sort(AL2,new App.NameComparator());
- else if(attribute.equals("price"))
- Collections.sort(AL2,new App.DescriptionComparator());
- else if(attribute.equals("rating"))
- Collections.sort(AL2,new App.RatingComparator());
- if(!ascending)
- Collections.reverse(AL2);
- return AL2;
- }
- public int[] ratingStats()
- {
- int[] result = new int[5];
- for(int i = 0 ; i < 5 ; ++i)
- result[i] = 0;
- for(int i = 0 ; i < AL.size() ; ++i)
- result[AL.get(i).getRating()-1]++;
- return result;
- }
- }
- 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();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement