Advertisement
Guest User

Untitled

a guest
Dec 9th, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.22 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.LinkedList;
  3. import java.util.HashMap;
  4. import java.util.Arrays;
  5.  
  6. class Item {
  7.  
  8. private String desc;
  9. private String category;
  10. private double amount;
  11.  
  12. public String getDesc() { return this.desc; }
  13. public String getCategory() { return this.category; }
  14. public double getAmount() { return this.amount; }
  15.  
  16. public void setDesc(String desc) { this.desc = desc; }
  17. public void setCategory(String category) { this.category = category; }
  18. public void setAmount(double amount) { this.amount = amount; }
  19.  
  20. public Item(String desc, String category, double amount) {
  21. this.desc = desc;
  22. this.category = category;
  23. this.amount = amount;
  24. }
  25.  
  26. }
  27.  
  28. class ExpSystem {
  29.  
  30. private LinkedList<Item> data;
  31.  
  32. public void addItem(Item item) {
  33. this.data.add(item);
  34. }
  35. public void removeItem(Item item) {
  36. this.data.remove(item);
  37. }
  38. public ExpSystem() {
  39. this.data = new LinkedList();
  40. }
  41.  
  42. // get a selection from user input
  43. public static String getMode(LinkedList<String> choices) {
  44.  
  45. // Display numbered choices
  46. HashMap<Integer,String> items = new HashMap();
  47. for ( int i=0; i<choices.size(); i++ ) {
  48. items.put(i+1,choices.get(i));
  49. System.out.println((i+1)+". "+choices.get(i));
  50. }
  51. // get choice from input
  52. System.out.printf(">>> ");
  53. Scanner sc = new Scanner(System.in);
  54. int inp = sc.nextInt();
  55. return choices.get(inp-1);
  56. }
  57.  
  58. public void addTestItems() {
  59. Item item1 = new Item("Item 1","A",1.5);
  60. this.addItem(item1);
  61. Item item2 = new Item("Item 2","A",8);
  62. this.addItem(item2);
  63. Item item3 = new Item("Item 3","B",11);
  64. this.addItem(item3);
  65. }
  66.  
  67. public static void main(String[] args) {
  68.  
  69. // create a test ExpSystem
  70. ExpSystem system = new ExpSystem();
  71. system.addTestItems();
  72.  
  73. while (true) {
  74.  
  75. LinkedList<String> modeChoices = new LinkedList(Arrays.asList("Edit","Analyse","Exit"));
  76. System.out.println("--- Expenditure database ---");
  77. switch(getMode(modeChoices)) {
  78.  
  79. case "Edit":
  80. System.out.println("--- Edit items ---");
  81. LinkedList<String> editChoices = new LinkedList(Arrays.asList("Add item","Delete item"));
  82. switch(getMode(editChoices)) {
  83.  
  84. case "Add item":
  85. System.out.println("--- Add an item ---");
  86.  
  87. // Get new item properties
  88. Scanner newItem = new Scanner(System.in);
  89. System.out.printf("Description: ");
  90. String desc = newItem.nextLine();
  91. System.out.printf("Category: ");
  92. String category = newItem.nextLine();
  93. System.out.printf("Amount: ");
  94. double amount = newItem.nextDouble();
  95.  
  96. // Construct & add item
  97. system.addItem(new Item(desc,category,amount));
  98. System.out.println("*Item added!*");
  99. break;
  100.  
  101. case "Delete item":
  102. System.out.println("--- Delete an item ---");
  103.  
  104. // get item to delete
  105. LinkedList<String> itemNames = new LinkedList();
  106. for (Item item : system.data) {
  107. itemNames.add(item.getDesc());
  108. }
  109.  
  110. // Delete chosen item
  111. String toDelete = getMode(itemNames);
  112. system.data.removeIf(item -> item.getDesc() == toDelete);
  113. System.out.println("*Item deleted!*");
  114. break;
  115.  
  116. }
  117. break;
  118. case "Analyse":
  119. System.out.println("--- Item analysis ---");
  120.  
  121. // Sort by category
  122. System.out.println("\n*Breakdown by category*");
  123. HashMap<String,LinkedList> byCat = new HashMap();
  124. for ( Item item : system.data ) {
  125. String thisCat = item.getCategory();
  126. if (byCat.containsKey(thisCat) == false) {
  127. byCat.put(thisCat,new LinkedList<Item>());
  128. }
  129.  
  130. LinkedList<Item> currList = byCat.get(thisCat);
  131. currList.add(item);
  132. byCat.put(thisCat,currList);
  133. }
  134. for ( String cat : byCat.keySet() ) {
  135. System.out.println(cat);
  136. LinkedList<Item> currList = byCat.get(cat);
  137. for ( Item item : currList ) {
  138. System.out.println(item.getDesc()+"\t"+item.getAmount());
  139. }
  140. }
  141.  
  142. // Average & total exp.
  143. double totalExp = 0;
  144. for (Item item : system.data) {
  145. totalExp += item.getAmount();
  146. }
  147. System.out.println("\n*Average spend per item*");
  148. double avg = totalExp/system.data.size();
  149. System.out.println(avg);
  150.  
  151. System.out.println("\n*Total expenditure*");
  152. System.out.println(totalExp);
  153. break;
  154.  
  155. case "Exit":
  156. System.exit(0);
  157. }
  158. }
  159. }
  160.  
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement