Advertisement
jovanovski

НЛ Лаб6 Зад1

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