Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.util.LinkedList;
- import java.util.HashMap;
- import java.util.Arrays;
- class Item {
- private String desc;
- private String category;
- private double amount;
- public String getDesc() { return this.desc; }
- public String getCategory() { return this.category; }
- public double getAmount() { return this.amount; }
- public void setDesc(String desc) { this.desc = desc; }
- public void setCategory(String category) { this.category = category; }
- public void setAmount(double amount) { this.amount = amount; }
- public Item(String desc, String category, double amount) {
- this.desc = desc;
- this.category = category;
- this.amount = amount;
- }
- }
- class ExpSystem {
- private LinkedList<Item> data;
- public void addItem(Item item) {
- this.data.add(item);
- }
- public void removeItem(Item item) {
- this.data.remove(item);
- }
- public ExpSystem() {
- this.data = new LinkedList();
- }
- // get a selection from user input
- public static String getMode(LinkedList<String> choices) {
- // Display numbered choices
- HashMap<Integer,String> items = new HashMap();
- for ( int i=0; i<choices.size(); i++ ) {
- items.put(i+1,choices.get(i));
- System.out.println((i+1)+". "+choices.get(i));
- }
- // get choice from input
- System.out.printf(">>> ");
- Scanner sc = new Scanner(System.in);
- int inp = sc.nextInt();
- return choices.get(inp-1);
- }
- public void addTestItems() {
- Item item1 = new Item("Item 1","A",1.5);
- this.addItem(item1);
- Item item2 = new Item("Item 2","A",8);
- this.addItem(item2);
- Item item3 = new Item("Item 3","B",11);
- this.addItem(item3);
- }
- public static void main(String[] args) {
- // create a test ExpSystem
- ExpSystem system = new ExpSystem();
- system.addTestItems();
- while (true) {
- LinkedList<String> modeChoices = new LinkedList(Arrays.asList("Edit","Analyse","Exit"));
- System.out.println("--- Expenditure database ---");
- switch(getMode(modeChoices)) {
- case "Edit":
- System.out.println("--- Edit items ---");
- LinkedList<String> editChoices = new LinkedList(Arrays.asList("Add item","Delete item"));
- switch(getMode(editChoices)) {
- case "Add item":
- System.out.println("--- Add an item ---");
- // Get new item properties
- Scanner newItem = new Scanner(System.in);
- System.out.printf("Description: ");
- String desc = newItem.nextLine();
- System.out.printf("Category: ");
- String category = newItem.nextLine();
- System.out.printf("Amount: ");
- double amount = newItem.nextDouble();
- // Construct & add item
- system.addItem(new Item(desc,category,amount));
- System.out.println("*Item added!*");
- break;
- case "Delete item":
- System.out.println("--- Delete an item ---");
- // get item to delete
- LinkedList<String> itemNames = new LinkedList();
- for (Item item : system.data) {
- itemNames.add(item.getDesc());
- }
- // Delete chosen item
- String toDelete = getMode(itemNames);
- system.data.removeIf(item -> item.getDesc() == toDelete);
- System.out.println("*Item deleted!*");
- break;
- }
- break;
- case "Analyse":
- System.out.println("--- Item analysis ---");
- // Sort by category
- System.out.println("\n*Breakdown by category*");
- HashMap<String,LinkedList> byCat = new HashMap();
- for ( Item item : system.data ) {
- String thisCat = item.getCategory();
- if (byCat.containsKey(thisCat) == false) {
- byCat.put(thisCat,new LinkedList<Item>());
- }
- LinkedList<Item> currList = byCat.get(thisCat);
- currList.add(item);
- byCat.put(thisCat,currList);
- }
- for ( String cat : byCat.keySet() ) {
- System.out.println(cat);
- LinkedList<Item> currList = byCat.get(cat);
- for ( Item item : currList ) {
- System.out.println(item.getDesc()+"\t"+item.getAmount());
- }
- }
- // Average & total exp.
- double totalExp = 0;
- for (Item item : system.data) {
- totalExp += item.getAmount();
- }
- System.out.println("\n*Average spend per item*");
- double avg = totalExp/system.data.size();
- System.out.println(avg);
- System.out.println("\n*Total expenditure*");
- System.out.println(totalExp);
- break;
- case "Exit":
- System.exit(0);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement