DamSi

Untitled

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