Advertisement
John_IV

Untitled

Nov 14th, 2019
326
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.01 KB | None | 0 0
  1. public class E06CottageScraper {
  2.     public static void main(String[] args) {
  3.         Scanner sc = new Scanner(System.in);
  4.  
  5.         List<Tree> list = new ArrayList<>();
  6.         String command = sc.nextLine();
  7.  
  8.         while (!command.equals("chop chop")) {
  9.             String[] input = command.split(" -> ");
  10.             String type = input[0];
  11.             int meters = Integer.parseInt(input[1]);
  12.  
  13.             Tree tree = new Tree(type, meters);
  14.             list.add(tree);
  15.  
  16.             command = sc.nextLine();
  17.         }
  18.  
  19.         String neededType = sc.nextLine();
  20.         double minMeters = Double.parseDouble(sc.nextLine());
  21.  
  22.         double usedLogsSum = list.stream().filter(tree -> tree.getType().equals(neededType) && tree.getLength() >= minMeters).mapToDouble(Tree::getLength).sum();
  23.         double unusedLogsSum = list.stream().filter(tree -> !tree.getType().equals(neededType) || tree.getLength() < minMeters).mapToDouble(Tree::getLength).sum();
  24.  
  25.         double priceToRound = list.stream().mapToDouble(Tree::getLength).average().orElse(0.0);
  26.         double price = Math.round(priceToRound * 100.0) / 100.0;
  27.  
  28.         double usedLogsPrice = Math.round(usedLogsSum * price * 100.0) / 100.0;
  29.         double unusedLogsPrice = Math.round((unusedLogsSum * price * 0.25) * 100.0) / 100.0;
  30.         double total = Math.round((usedLogsPrice + unusedLogsPrice) * 100.0) / 100.0;
  31.  
  32.         System.out.printf("Price per meter: $%.2f%n", price);
  33.         System.out.printf("Used logs price: $%.2f%n", usedLogsPrice);
  34.         System.out.printf("Unused logs price: $%.2f%n", unusedLogsPrice);
  35.         System.out.printf("CottageScraper subtotal: $%.2f", total);
  36.     }
  37.  
  38.     private static class Tree {
  39.  
  40.         String type;
  41.         int length;
  42.  
  43.         private Tree(String type, int length) {
  44.             this.type = type;
  45.             this.length = length;
  46.         }
  47.  
  48.         public String getType() {
  49.             return type;
  50.         }
  51.  
  52.         public int getLength() {
  53.             return length;
  54.         }
  55.     }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement