Advertisement
Guest User

Book Library

a guest
Dec 6th, 2017
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.38 KB | None | 0 0
  1. import java.text.ParseException;
  2. import java.text.SimpleDateFormat;
  3. import java.util.*;
  4.  
  5. public class _24BookLibrary {
  6.     public static void main(String[] args) {
  7.         Scanner console = new Scanner(System.in);
  8.  
  9.         int n = Integer.parseInt(console.nextLine());
  10.  
  11.         Library newLibrary = new Library("New",
  12.                 new ArrayList<>());
  13.  
  14.         for (int i = 0; i < n; i++) {
  15.             newLibrary.books.add(ReadBook());
  16.         }
  17.  
  18.         PrintBooks(newLibrary);
  19.     }
  20.  
  21.     private static void PrintBooks(Library newLibrary) {
  22.  
  23.         HashMap<String, Double> authorSales = new HashMap<>();
  24.  
  25.         for (Book b : newLibrary.books) {
  26.             authorSales.put(b.author,
  27.                     authorSales.get(b.author) + b.price);
  28.         }
  29.  
  30.         Map<String, Double> sorted = sortHash(authorSales);
  31.  
  32.         for (Map.Entry<String, Double> entry : sorted.entrySet()) {
  33.             String key = entry.getKey();
  34.             Double value = entry.getValue();
  35.  
  36.             System.out.println(key + " -> " + value);
  37.         }
  38.     }
  39.  
  40.     private static Map<String, Double> sortHash(Map<String, Double> map) {
  41.         List<Map.Entry<String, Double>> list = new ArrayList<>(map.entrySet());
  42.  
  43.         // Sort list by integer values then by string keys
  44.         Collections.sort(list, (a, b) -> {
  45.             int cmp1 = a.getValue().compareTo(b.getValue());
  46.             if (cmp1 != 0)
  47.                 return cmp1;
  48.             else
  49.                 return a.getKey().compareTo(b.getKey());
  50.         });
  51.  
  52.         Map<String, Double> result = new HashMap<>();
  53.         for (Map.Entry<String, Double> entry : list)
  54.             result.put(entry.getKey(), entry.getValue());
  55.  
  56.         return result;
  57.     }
  58.  
  59.     private static Book ReadBook() {
  60.         Scanner console = new Scanner(System.in);
  61.  
  62.         String[] input = console.nextLine().split(" ");
  63.  
  64.         String title = input[0];
  65.         String author = input[1];
  66.         String publisher = input[2];
  67.         Date releaseDate = null;
  68.         try {
  69.             releaseDate = new SimpleDateFormat("dd.MM.yyyy").parse(input[3]);
  70.         } catch (ParseException e) {
  71.             e.printStackTrace();
  72.         }
  73.         String ISBN = input[4];
  74.         double price = Double.parseDouble(input[5]);
  75.  
  76.         Book currentBook = new Book(title,
  77.                 author,
  78.                 publisher,
  79.                 releaseDate,
  80.                 ISBN,
  81.                 price);
  82.  
  83.         return currentBook;
  84.     }
  85.  
  86.  
  87.     public static class Library {
  88.         public String name;
  89.         public List<Book> books;
  90.  
  91.         public Library(String name, List<Book> books) {
  92.             this.name = name;
  93.             this.books = books;
  94.         }
  95.     }
  96.  
  97.     public static class Book {
  98.         public String title;
  99.         public String author;
  100.         public String publisher;
  101.         public Date releaseDate;
  102.         public String ISBN;
  103.         public Double price;
  104.  
  105.         public Book(String title,
  106.                     String author,
  107.                     String publisher,
  108.                     Date releaseDate,
  109.                     String ISBN,
  110.                     Double price) {
  111.             this.title = title;
  112.             this.author = author;
  113.             this.publisher = publisher;
  114.             this.releaseDate = releaseDate;
  115.             this.ISBN = ISBN;
  116.             this.price = price;
  117.         }
  118.     }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement