Advertisement
add1ctus

Прва

Dec 30th, 2015
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.48 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Arrays;
  3. import java.util.Collections;
  4. import java.util.Comparator;
  5. import java.util.List;
  6. import java.util.Random;
  7. import java.util.Scanner;
  8.  
  9. class App implements Comparable<App>
  10. {
  11.     private final String name;
  12.     private final String description;
  13.     private final double price;
  14.     private final int rating;
  15.    
  16.     public App(String n, String d, int r, double p)
  17.     {
  18.         name = n;
  19.         description = d;
  20.         price = p;
  21.         rating = r;
  22.     }
  23.    
  24.     public int compareTo(App rhs)
  25.     {
  26.         return (int)price - (int)rhs.price;
  27.     }
  28.    
  29.     public String getName()
  30.     {
  31.         return name;
  32.     }
  33.    
  34.     public String getDescription()
  35.     {
  36.         return description;
  37.     }
  38.    
  39.     public double getPrice()
  40.     {
  41.         return price;
  42.     }
  43.    
  44.     public int getRating()
  45.     {
  46.         return rating;
  47.     }
  48.    
  49.     public static class NameComparator implements Comparator<App>
  50.     {
  51.         public int compare(App a1, App a2)
  52.         {
  53.             return a1.getName().compareTo(a2.getName());
  54.         }
  55.     }
  56.    
  57.     public static class DescriptionComparator implements Comparator<App>
  58.     {
  59.         public int compare(App a1, App a2)
  60.         {
  61.             return a1.getDescription().compareTo(a2.getDescription());
  62.         }
  63.     }
  64.    
  65.     class PriceComparator implements Comparator<App>
  66.     {
  67.         public int compare(App a1, App a2)
  68.         {
  69.             return new Double(a1.getPrice()).compareTo(new Double(a2.getPrice()));
  70.         }
  71.     }
  72.    
  73.     public static class RatingComparator implements Comparator<App>
  74.     {
  75.         public int compare(App a1, App a2)
  76.         {
  77.             return new Integer(a1.getRating()).compareTo(new Integer(a2.getRating()));
  78.         }
  79.     }
  80. }
  81.  
  82. class AppManager
  83. {
  84.     private static int r;
  85.     private ArrayList<App> AL;
  86.    
  87.     public AppManager(App... apps)
  88.     {
  89.         r = 0;
  90.         AL = new ArrayList<App>();
  91.         for(App app : apps)
  92.         {
  93.             AL.add(app);
  94.         }
  95.     }
  96.    
  97.     public App bestApp()
  98.     {
  99.         int best = 0;
  100.        
  101.         for(int i = 1 ; i < AL.size() ; ++i)
  102.             if(AL.get(i).getRating() > AL.get(best).getRating())
  103.                 best = i;
  104.        
  105.         return AL.get(best);
  106.     }
  107.    
  108.     public App cheapestApp()
  109.     {
  110.         int cheapest = 0;
  111.        
  112.         for(int i = 1 ; i < AL.size(); ++i)
  113.         {
  114.             if(AL.get(i).getPrice() < AL.get(cheapest).getPrice())
  115.                 cheapest = i;
  116.         }
  117.        
  118.         return AL.get(cheapest);
  119.     }
  120.    
  121.    
  122.    
  123.    
  124.     public List<App> randomChoice()
  125.     {
  126.         List<App> result = new ArrayList<App>();
  127.        
  128.         //System.out.println(r);
  129.        
  130.         for(int i = 0 ; i < Math.min(3, AL.size()-r) ; ++i)
  131.         {
  132.             result.add(AL.get(r));
  133.             r = (r+1) % AL.size();
  134.         }
  135.        
  136.         return result;
  137.     }
  138.    
  139.     public List<App> allApps(String attribute, boolean ascending)
  140.     {
  141.         List<App> AL2 = new ArrayList<App>(AL);
  142.         if(attribute.equals("name"))
  143.             Collections.sort(AL2,new App.NameComparator());
  144.         else if(attribute.equals("price"))
  145.             Collections.sort(AL2,new App.DescriptionComparator());
  146.         else if(attribute.equals("rating"))
  147.             Collections.sort(AL2,new App.RatingComparator());
  148.         if(!ascending)
  149.             Collections.reverse(AL2);
  150.         return AL2;
  151.     }
  152.    
  153.     public int[] ratingStats()
  154.     {
  155.         int[] result = new int[5];
  156.         for(int i = 0 ; i < 5 ; ++i)
  157.             result[i] = 0;
  158.        
  159.         for(int i = 0 ; i < AL.size() ; ++i)
  160.             result[AL.get(i).getRating()-1]++;
  161.        
  162.         return result;
  163.     }
  164. }
  165.  
  166. public class AppManagerTest {
  167. public static void main(String[] args) throws Exception {
  168.         Scanner jin = new Scanner(System.in);
  169.         int n = Integer.parseInt(jin.nextLine());
  170.         App[] apps = new App[n];
  171.         for ( int i = 0 ; i < n ; ++i ) {
  172.             apps[i] = new App(jin.nextLine(),jin.nextLine(),jin.nextInt(),jin.nextDouble());
  173.             jin.nextLine();
  174.         }
  175.         int k = jin.nextInt();
  176.         AppManager am = new AppManager(apps);
  177.         if ( k == 0 ) { //test everything but randomChoice
  178.             while ( true ) {
  179.                 System.out.println();
  180.                 String cmd = jin.next();
  181.                 System.out.println(cmd);
  182.                 if ( cmd.equals("stop") ) break;
  183.                 if ( cmd.equals("bestApp") ) {
  184.                     print(am.bestApp());
  185.                 }
  186.                 if ( cmd.equals("cheapestApp") ) {
  187.                     print(am.cheapestApp());
  188.                 }
  189.                 if ( cmd.equals("allApps") ) {
  190.                     print(am.allApps(jin.next(),jin.nextBoolean()));
  191.                 }
  192.                 if ( cmd.equals("ratingStats") ) {
  193.                     System.out.println(Arrays.toString(am.ratingStats()));
  194.                 }
  195.             }
  196.         }
  197.         else { //test randomChoice
  198.             System.out.println("Testing random choice...");
  199.             int w = jin.nextInt();
  200.             for ( int q = 0 ; q < w ; ++q ) {
  201.                 boolean[] flags = new boolean[n];
  202.                 for ( int i = 0 ; i <= n/3 ; ++i ) {
  203.                     List<App> res = am.randomChoice();
  204.                     for ( App a : res ) {
  205.                         int idx = idxOf(apps,a);
  206.                         if ( idx == -1 ) System.out.println("You returned an app that wasn't in the list at all? What are you doing???");
  207.                         if ( flags[idx] ) {
  208.                             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.");
  209.                             throw new Exception("App already returned");
  210.                         }
  211.                         flags[idx] = true;
  212.                     }
  213.                 }
  214.             }
  215.             System.out.println("Great work on implementing randomChoice. That is just what we needed.");
  216.         }
  217.     }
  218.  
  219.     private static void print(App app) {
  220.         System.out.println("Name: "+app.getName());
  221.         System.out.println("Rating: "+app.getRating());
  222.         System.out.printf("Price: %.2f$\n",app.getPrice());
  223.     }
  224.    
  225.     private static void print(List<App> apps) {
  226.         for ( App ap : apps ) {
  227.             print(ap);
  228.             System.out.println();
  229.         }
  230.     }
  231.    
  232.     private static int idxOf(App apps[],App a){
  233.         for ( int i = 0 ; i < apps.length ; ++i )
  234.             if ( equal(apps[i],a) ) return i;
  235.         return -1;
  236.     }
  237.    
  238.     private static boolean equal(App a , App b) {
  239.         return a.getName().equals(b.getName())&&a.getPrice()==b.getPrice()&&a.getRating()==b.getRating();
  240.     }
  241.  
  242. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement