Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- class App{
- private String ime, opis;
- private int ocenka;
- private double cena;
- public App(String ime, String opis, int ocenka, double cena){
- this.ime= ime;
- this.opis = opis;
- this.cena = cena;
- this.ocenka = ocenka;
- }
- public String getName() {
- return ime;
- }
- public String getOpis() {
- return opis;
- }
- public double getPrice() {
- return cena;
- }
- public int getRating() {
- return ocenka;
- }
- }
- class AppManager{
- private LinkedList<App> apps;
- private LinkedList<Integer> random;
- int iskoristeno;
- public AppManager(App[] niza){
- apps = new LinkedList<App>();
- random = new LinkedList<Integer>();
- for(int i=0;i<niza.length;i++){
- apps.add(niza[i]);
- random.add(0);
- }
- iskoristeno = 0;
- }
- public App bestApp(){
- App tmp = apps.get(0);
- for(int i=1;i<apps.size();i++){
- if(apps.get(i).getRating()>tmp.getRating()){
- tmp = apps.get(i);
- }
- }
- return tmp;
- }
- public App cheapestApp(){
- App tmp = apps.get(0);
- for(int i=1;i<apps.size();i++){
- if(apps.get(i).getPrice()<tmp.getPrice()){
- tmp = apps.get(i);
- }
- }
- return tmp;
- }
- public int[] ratingStats(){
- int[] ratings = new int[5];
- for(int i=0;i<5;i++) ratings[i] = 0;
- for(int i=0;i<apps.size();i++){
- ratings[apps.get(i).getRating()-1]++;
- }
- return ratings;
- }
- public List<App> allApps(String attribute, boolean ascending){
- LinkedList<App> lista = new LinkedList<App>();
- for(App ap : apps){
- lista.add(ap);
- }
- if(attribute.equals("name")){
- Collections.sort(lista, new NameComparator());
- }
- else if(attribute.equals("price")){
- Collections.sort(lista, new PriceComparator());
- }
- else if(attribute.equals("rating")){
- Collections.sort(lista, new RatingComparator());
- }
- else return null;
- if(!ascending){
- Collections.reverse(lista);
- }
- return lista;
- }
- public List<App> randomChoice(){
- LinkedList<App> lista = new LinkedList<App>();
- Random randomGenerator = new Random();
- int brojac = 0;
- if(iskoristeno==apps.size()){
- for(int i=0;i<apps.size();i++){
- random.set(i, 0);
- }
- iskoristeno = 0;
- }
- while(iskoristeno<apps.size()){
- if(brojac==3) break;
- int randomInt = randomGenerator.nextInt(apps.size());
- if(random.get(randomInt)==0){
- lista.add(apps.get(randomInt));
- random.set(randomInt, 1);
- brojac++;
- iskoristeno++;
- }
- }
- return lista;
- }
- }
- class NameComparator implements Comparator<App> {
- @Override
- public int compare(App a1 , App a2){
- return a1.getName().compareTo(a2.getName());
- }
- }
- class PriceComparator implements Comparator<App> {
- @Override
- public int compare(App a1 , App a2){
- double rez = a1.getPrice()-a2.getPrice();
- if(rez<0) return -1;
- if(rez>0) return 1;
- return 0;
- }
- }
- class RatingComparator implements Comparator<App> {
- @Override
- public int compare(App a1 , App a2){
- return a1.getRating()-a2.getRating();
- }
- }
- 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