Advertisement
John_IV

Untitled

Nov 16th, 2019
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.04 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.  
  11.             list.add(new Tree(input[0], Integer.parseInt(input[1])));
  12.  
  13.             command = sc.nextLine();
  14.         }
  15.  
  16.         String neededType = sc.nextLine();
  17.         double minMeters = Double.parseDouble(sc.nextLine());
  18.         BigDecimal usedSum = new BigDecimal(0);
  19.         BigDecimal unusedSum = new BigDecimal(0);
  20.  
  21.         for (Tree tree : list) {
  22.             if (tree.getType().equals(neededType) && tree.getLength() >= minMeters) {
  23.                 usedSum = usedSum.add(BigDecimal.valueOf(tree.length));
  24.  
  25.             } else {
  26.                 unusedSum = unusedSum.add(BigDecimal.valueOf(tree.length));
  27.             }
  28.         }
  29.  
  30.         BigDecimal price = usedSum.add(unusedSum).divide(BigDecimal.valueOf(list.size()), 2, RoundingMode.HALF_EVEN);
  31.         BigDecimal usedLogsPrice = price.multiply(usedSum).setScale(2, RoundingMode.HALF_EVEN);
  32.         BigDecimal unusedLogsPrice = price.multiply(unusedSum).multiply(BigDecimal.valueOf(0.25)).setScale(2, BigDecimal.ROUND_HALF_EVEN);
  33.         BigDecimal total = unusedLogsPrice.add(usedLogsPrice).setScale(2, BigDecimal.ROUND_HALF_EVEN);
  34.  
  35.         System.out.printf("Price per meter: $%.2f%n", price);
  36.         System.out.printf("Used logs price: $%.2f%n", usedLogsPrice);
  37.         System.out.printf("Unused logs price: $%.2f%n", unusedLogsPrice);
  38.         System.out.printf("CottageScraper subtotal: $%.2f", total);
  39.     }
  40.  
  41.     private static class Tree {
  42.  
  43.         String type;
  44.         int length;
  45.  
  46.         private Tree(String type, int length) {
  47.             this.type = type;
  48.             this.length = length;
  49.         }
  50.  
  51.         public String getType() {
  52.             return type;
  53.         }
  54.  
  55.         public int getLength() {
  56.             return length;
  57.         }
  58.     }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement