Thank you to anyone who has already donated - your generous donations helped make three months of treatment possible.
My brother Nate continues to fight stage IV Hodgkin's lymphoma. He's just 31, with a wife and baby girl. They have no active income (since he's been unable to return to work), no insurance, and cannot afford the treatment he needs. Nate and his family need your help. Please consider a donation, every dollar helps. Thanks.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
package revenueProcessor; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; import java.util.LinkedList; import java.util.List; import java.util.Set; public class Solution{ public static void main(String[] args) throws IOException{ Solution obj = new Solution(); obj.process(); } void process() throws IOException { String fileLine; String delimiter = "\\|"; List<String> linesInFile = new LinkedList<String>(); List<PurchaseRecord> record = new ArrayList<PurchaseRecord>(); Set<String> distinctNames = new HashSet<String>(); Set<String> distinctCategories = new HashSet<String>(); FileReader newFile = new FileReader("c:/test.txt"); BufferedReader buffer = new BufferedReader(newFile); // Store file contents into a String array by Line do { fileLine = buffer.readLine(); if(fileLine != null){ linesInFile.add(fileLine); } } while(fileLine != null); buffer.close(); /** * Very important for loop * Create a new PurchaseRecord object for each line above * Store each Purchase record object in an ArrayList called record * Store each distinct name in a HashSet called distinctNames * Store each distinct category in a Distinct Category called distinctCategories */ for(String line: linesInFile){ String[] temp = line.split(delimiter); PurchaseRecord pr = new PurchaseRecord(); pr.name = temp[0]; pr.category = temp[1]; pr.description = temp[2]; pr.cost = temp[3]; distinctNames.add(pr.name); distinctCategories.add(pr.category); record.add(pr); } //Sort the ArrayList record by PurchaseRecord.name Collections.sort(record, PurchaseRecord.recordNameComparator); calculateRecordByName(record, distinctNames); calculateRecordByNameAndCategory(record, distinctNames, distinctCategories); } void calculateRecordByName(List<PurchaseRecord> record, Set<String> allNames){ System.out.println("Total Revenue By Customer:"); double sum; for(String name : allNames){ sum = 0; for(PurchaseRecord pr2 : record){ if(name.equals(pr2.name)){ sum = sum + pr2.costInt(); } } printRecordByName(name, sum); } System.out.println(""); } void calculateRecordByNameAndCategory(List<PurchaseRecord> record, Set<String> allNames, Set<String> allCategories){ double sum; for(String name : allNames){ System.out.println("Purchases by "+name); for(String category : allCategories){ sum = 0; for(PurchaseRecord pr : record){ if(name.equals(pr.name) && category.equals(pr.category)){ sum = sum + pr.costInt(); } } printRecordByCategory(category, sum); } System.out.println(""); } } void printRecordByName(String name, double revenue){ System.out.println(name+": $"+ revenue); } void printRecordByCategory(String category, double revenue){ if(revenue > 0){ System.out.println(category+": $"+revenue); } } } package revenueProcessor; import java.util.Comparator; public class PurchaseRecord{ String name; String category; String description; String cost; public double costInt(){ return Double.parseDouble(cost); } public static Comparator<PurchaseRecord> recordNameComparator = new Comparator<PurchaseRecord>(){ @Override public int compare(PurchaseRecord r1, PurchaseRecord r2) { String recordName1 = r1.name; String recordName2 = r2.name; return recordName1.compareTo(recordName2); } }; } |