Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- *
- * @author Tołpa Łukasz S15525
- *
- */
- package KOL2;
- import java.io.FileNotFoundException;
- public class Main {
- public static void main(String[] args) {
- CustomersPurchaseSortFind cpsf = new CustomersPurchaseSortFind();
- String fname = System.getProperty("user.home") + "/customers.txt";
- try {
- cpsf.readFile(fname);
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }
- cpsf.showSortedBy("Nazwiska");
- cpsf.showSortedBy("Koszty");
- String[] custSearch = { "c00001", "c00002" };
- for (String id : custSearch) {
- cpsf.showPurchaseFor(id);
- }
- }
- }
- /**
- * @author Tołpa Łukasz S15525
- */
- package KOL2;
- import java.util.HashMap;
- import java.util.Map;
- public class Purchase {
- String m_id;
- String m_name;
- String m_article;
- double m_price;
- double m_quantity;
- double m_overallPrice;
- public Map<String, String> sortingData = new HashMap<String, String>();
- public Purchase(String id, String name, String article, double price, double quantity) {
- m_id = id;
- m_name = name;
- m_article = article;
- m_price = price;
- m_quantity = quantity;
- m_overallPrice = m_price * m_quantity;
- sortingData.put("Name", m_name.split(" ")[0]);
- sortingData.put("Price", String.valueOf(m_overallPrice));
- sortingData.put("ID", m_id);
- }
- public String toString() {
- return m_id + ";" + m_name + ";" + m_article + ";" + m_price + ";" + m_quantity;
- }
- }
- /**
- * @author Tołpa Łukasz S15525
- */
- package KOL2;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.util.*;
- public class CustomersPurchaseSortFind {
- List<Purchase> m_list = new ArrayList<>();
- public void readFile(String fname) throws FileNotFoundException {
- String id_klienta;
- String imieNazwisko;
- String nazwaTowaru;
- double cena;
- double ilosc;
- File file = new File(fname);
- Scanner in = new Scanner(file);
- while (in.hasNextLine()) {
- // id_klienta = in.next();
- // imieNazwisko = in.next();
- // nazwaTowaru = in.next();
- // cena = in.next();
- // ilosc = in.next();
- // m_list.add(new Purchase(id_klienta,imieNazwisko,nazwaTowaru,cena,ilosc));
- String line = in.nextLine();
- String[] parts = line.split(";");
- id_klienta = parts[0];
- imieNazwisko = parts[1];
- nazwaTowaru = parts[2];
- cena = Double.parseDouble(parts[3]);
- ilosc = Double.parseDouble(parts[4]);
- m_list.add(new Purchase(id_klienta,imieNazwisko,nazwaTowaru,cena,ilosc));
- }
- }
- public void showSortedBy(String objectSort) {
- String sortBy = null;
- if (objectSort.equals("Nazwiska")) {
- sortBy = "Name";
- } else if (objectSort.equals("Koszty")) {
- sortBy = "Price";
- }
- PurchaseComparator pc = new PurchaseComparator();
- pc.sortBy(sortBy);
- Collections.sort(m_list, pc);
- System.out.println(objectSort);
- for (Purchase p : m_list) {
- System.out.println(p.m_id + ";" + p.m_name + ";" + p.m_article + ";" + p.m_price + ";" + p.m_quantity + (sortBy == "Price" ? (" (koszt: " + p.m_overallPrice + ")") : ""));
- }
- System.out.println();
- }
- public void showPurchaseFor(String id) {
- System.out.println("Klient " + id);
- for (Purchase p : m_list) {
- if (p.m_id.equals(id)) {
- System.out.println(p.m_id + ";" + p.m_name + ";" + p.m_article + ";" + p.m_price + ";" + p.m_quantity);
- }
- }
- System.out.println();
- }
- }
- package KOL2;
- import java.util.Comparator;
- class PurchaseComparator implements Comparator<Purchase>
- {
- String m_sortBy;
- public void sortBy(String sortBy) {
- m_sortBy = sortBy;
- }
- public int compare(Purchase a, Purchase b)
- {
- String av = a.sortingData.get(m_sortBy);
- String bv = b.sortingData.get(m_sortBy);
- int comparison = m_sortBy == "Price" ? (new Double(bv)).compareTo(new Double(av)) : av.compareTo(bv);
- if(m_sortBy == "Name" && comparison == 0) {
- return a.sortingData.get("ID").compareTo(b.sortingData.get("ID"));
- }
- return comparison;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement