desislava_topuzakova

Untitled

Mar 1st, 2022
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.52 KB | None | 0 0
  1. //06. Greedy Times
  2. package GreedyTimes_06;
  3.  
  4. import java.util.HashMap;
  5. import java.util.Map;
  6.  
  7. public class Bag {
  8. private long capacity;
  9. private long currentWeight;
  10. private long gold;
  11. private Map<String, Long> gems;
  12. private long totalGems;
  13. private Map<String, Long> cash;
  14. private long totalCash;
  15. private boolean goldIsAdded;
  16.  
  17.  
  18. public Bag(long capacity) {
  19. this.capacity = capacity;
  20. this.currentWeight = 0;
  21. this.gold = 0;
  22. this.gems = new HashMap<>();
  23. this.totalGems = 0;
  24. this.cash = new HashMap<>();
  25. this.totalCash = 0;
  26. this.goldIsAdded = false;
  27. }
  28.  
  29.  
  30. public void addCash(String item, long quantity) {
  31. if(this.hasFreeCapacity(quantity) && this.totalGems >= this.totalCash + quantity){
  32. if(!this.cash.containsKey(item)){
  33. this.cash.put(item, quantity);
  34. }else{
  35. this.cash.put(item, this.cash.get(item) + quantity);
  36. }
  37. this.totalCash += quantity;
  38. this.currentWeight += quantity;
  39. }
  40. }
  41.  
  42. private boolean hasFreeCapacity(long weight) {
  43. return this.currentWeight + weight <= this.capacity;
  44. }
  45.  
  46. public void addGems(String item, long quantity) {
  47. if (this.hasFreeCapacity(quantity) && this.gold >= this.totalGems + quantity){
  48. if(!this.gems.containsKey(item)){
  49. this.gems.put(item, quantity);
  50. }else{
  51. this.gems.put(item, this.gems.get(item) + quantity);
  52. }
  53. this.totalGems += quantity;
  54. this.currentWeight += quantity;
  55. }
  56. }
  57.  
  58. public void addGold(long weight) {
  59. if(this.hasFreeCapacity(weight)){
  60. this.goldIsAdded = true;
  61. this.gold += weight;
  62. this.currentWeight += weight;
  63. }
  64. }
  65.  
  66. @Override
  67. public String toString() {
  68. StringBuilder sb = new StringBuilder();
  69. if(this.goldIsAdded) {
  70. sb.append("<Gold> $").append(this.gold).append(System.lineSeparator());
  71. sb.append("##Gold - ").append(this.gold).append(System.lineSeparator());
  72. }
  73. if(this.gems.size() > 0) {
  74. sb.append("<Gem> $").append(this.totalGems).append(System.lineSeparator());
  75.  
  76. this.gems.entrySet().stream().sorted((f, s) -> {
  77. int result = s.getKey().compareTo(f.getKey());
  78. if (result == 0) {
  79. result = f.getValue().compareTo(s.getValue());
  80. }
  81. return result;
  82. }).forEach(entry -> {
  83. sb.append("##")
  84. .append(entry.getKey())
  85. .append(" - ")
  86. .append(entry.getValue())
  87. .append(System.lineSeparator());
  88. });
  89. }
  90. if(this.cash.size() > 0) {
  91. sb.append("<Cash> $").append(this.totalCash).append(System.lineSeparator());
  92. this.cash.entrySet().stream().sorted((f, s) -> {
  93. int result = s.getKey().compareTo(f.getKey());
  94. if (result == 0) {
  95. result = f.getValue().compareTo(s.getValue());
  96. }
  97. return result;
  98. }).forEach(entry -> {
  99. sb.append("##")
  100. .append(entry.getKey())
  101. .append(" - ")
  102. .append(entry.getValue())
  103. .append(System.lineSeparator());
  104. });
  105. }
  106.  
  107. return sb.toString();
  108. }
  109. }
  110.  
  111.  
  112. package GreedyTimes_06;
  113.  
  114. import java.io.BufferedReader;
  115. import java.io.InputStreamReader;
  116. import java.util.Scanner;
  117.  
  118. public class Main {
  119. public static void main(String[] args) {
  120. Scanner scanner = new Scanner(System.in);
  121.  
  122. long capacity = Long.parseLong(scanner.nextLine());
  123.  
  124. Bag bag = new Bag(capacity);
  125.  
  126. String[] input = scanner.nextLine().split("\\s+");
  127.  
  128. for (int i = 0; i < input.length; i += 2) {
  129. String item = input[i];
  130. long weight = Long.parseLong(input[i + 1]);
  131.  
  132. if (item.length() == 3) {
  133. bag.addCash(item, weight);
  134. } else if (item.toLowerCase().endsWith("gem") && item.length() > 3) {
  135. bag.addGems(item, weight);
  136. } else if (item.equalsIgnoreCase("gold")) {
  137. bag.addGold(weight);
  138. }
  139. }
  140.  
  141. System.out.println(bag.toString());
  142. }
  143. }
  144.  
  145.  
  146.  
  147.  
Advertisement
Add Comment
Please, Sign In to add comment