Advertisement
Guest User

Untitled

a guest
Nov 16th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.43 KB | None | 0 0
  1. /**
  2.  *
  3.  *  @author Tołpa Łukasz S15525
  4.  *
  5.  */
  6.  
  7. package KOL2;
  8.  
  9.  
  10. import java.io.FileNotFoundException;
  11.  
  12. public class Main {
  13.  
  14.   public static void main(String[] args)  {
  15.     CustomersPurchaseSortFind cpsf = new CustomersPurchaseSortFind();
  16.     String fname = System.getProperty("user.home") + "/customers.txt";
  17.     try {
  18.       cpsf.readFile(fname);
  19.     } catch (FileNotFoundException e) {
  20.       e.printStackTrace();
  21.     }
  22.     cpsf.showSortedBy("Nazwiska");
  23.     cpsf.showSortedBy("Koszty");
  24.  
  25.     String[] custSearch = { "c00001", "c00002" };
  26.  
  27.     for (String id : custSearch) {
  28.       cpsf.showPurchaseFor(id);
  29.     }
  30.   }
  31.  
  32. }
  33. /**
  34.  * @author Tołpa Łukasz S15525
  35.  */
  36.  
  37. package KOL2;
  38.  
  39.  
  40. import java.util.HashMap;
  41. import java.util.Map;
  42.  
  43. public class Purchase {
  44.     String m_id;
  45.     String m_name;
  46.     String m_article;
  47.     double m_price;
  48.     double m_quantity;
  49.     double m_overallPrice;
  50.     public Map<String, String> sortingData = new HashMap<String, String>();
  51.  
  52.     public Purchase(String id, String name, String article, double price, double quantity) {
  53.         m_id = id;
  54.         m_name = name;
  55.         m_article = article;
  56.         m_price = price;
  57.         m_quantity = quantity;
  58.         m_overallPrice = m_price * m_quantity;
  59.         sortingData.put("Name", m_name.split(" ")[0]);
  60.         sortingData.put("Price", String.valueOf(m_overallPrice));
  61.         sortingData.put("ID", m_id);
  62.     }
  63.  
  64.     public String toString() {
  65.         return m_id + ";" + m_name + ";" + m_article + ";" + m_price + ";" + m_quantity;
  66.     }
  67. }
  68. /**
  69.  * @author Tołpa Łukasz S15525
  70.  */
  71.  
  72. package KOL2;
  73.  
  74.  
  75. import java.io.File;
  76. import java.io.FileNotFoundException;
  77. import java.util.*;
  78.  
  79. public class CustomersPurchaseSortFind {
  80.     List<Purchase> m_list = new ArrayList<>();
  81.  
  82.     public void readFile(String fname) throws FileNotFoundException {
  83.         String id_klienta;
  84.         String imieNazwisko;
  85.         String nazwaTowaru;
  86.         double cena;
  87.         double ilosc;
  88.         File file = new File(fname);
  89.         Scanner in = new Scanner(file);
  90.         while (in.hasNextLine()) {
  91. //            id_klienta = in.next();
  92. //            imieNazwisko = in.next();
  93. //            nazwaTowaru = in.next();
  94. //            cena = in.next();
  95. //            ilosc = in.next();
  96. //            m_list.add(new Purchase(id_klienta,imieNazwisko,nazwaTowaru,cena,ilosc));
  97.             String line = in.nextLine();
  98.             String[] parts = line.split(";");
  99.             id_klienta = parts[0];
  100.             imieNazwisko = parts[1];
  101.             nazwaTowaru = parts[2];
  102.             cena = Double.parseDouble(parts[3]);
  103.             ilosc = Double.parseDouble(parts[4]);
  104.             m_list.add(new Purchase(id_klienta,imieNazwisko,nazwaTowaru,cena,ilosc));
  105.         }
  106.     }
  107.  
  108.     public void showSortedBy(String objectSort) {
  109.         String sortBy = null;
  110.         if (objectSort.equals("Nazwiska")) {
  111.             sortBy = "Name";
  112.         } else if (objectSort.equals("Koszty")) {
  113.             sortBy = "Price";
  114.         }
  115.  
  116.         PurchaseComparator pc = new PurchaseComparator();
  117.         pc.sortBy(sortBy);
  118.  
  119.         Collections.sort(m_list, pc);
  120.  
  121.         System.out.println(objectSort);
  122.         for (Purchase p : m_list) {
  123.             System.out.println(p.m_id + ";" + p.m_name + ";" + p.m_article + ";" + p.m_price + ";" + p.m_quantity + (sortBy == "Price" ? (" (koszt: " + p.m_overallPrice + ")") : ""));
  124.         }
  125.         System.out.println();
  126.     }
  127.  
  128.     public void showPurchaseFor(String id) {
  129.         System.out.println("Klient " + id);
  130.         for (Purchase p : m_list) {
  131.             if (p.m_id.equals(id)) {
  132.                 System.out.println(p.m_id + ";" + p.m_name + ";" + p.m_article + ";" + p.m_price + ";" + p.m_quantity);
  133.             }
  134.         }
  135.         System.out.println();
  136.     }
  137. }
  138. package KOL2;
  139.  
  140. import java.util.Comparator;
  141.  
  142. class PurchaseComparator implements Comparator<Purchase>
  143. {
  144.     String m_sortBy;
  145.  
  146.     public void sortBy(String sortBy) {
  147.         m_sortBy = sortBy;
  148.     }
  149.  
  150.     public int compare(Purchase a, Purchase b)
  151.     {
  152.         String av = a.sortingData.get(m_sortBy);
  153.         String bv = b.sortingData.get(m_sortBy);
  154.         int comparison = m_sortBy == "Price" ? (new Double(bv)).compareTo(new Double(av)) : av.compareTo(bv);
  155.  
  156.         if(m_sortBy == "Name" && comparison == 0) {
  157.             return a.sortingData.get("ID").compareTo(b.sortingData.get("ID"));
  158.         }
  159.         return comparison;
  160.     }
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement