Advertisement
Guest User

Book Library

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