Advertisement
Guest User

uproszczone md2

a guest
Dec 5th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.18 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Date;
  5. import java.util.List;
  6. import java.util.Map;
  7. import java.util.TreeMap;
  8. import java.util.stream.Collectors;
  9.  
  10. class Mileage {//odczyty przebiegu
  11. int kilometers;
  12. String vin; // asocjacja z atrybutu
  13. Date date;
  14.  
  15. public Mileage(int kilometers, String vin, Date date) {
  16. this.kilometers = kilometers;
  17. this.vin = vin;
  18. this.date = date;
  19. }
  20.  
  21. static List<Mileage> mileageList = new ArrayList<>();
  22.  
  23. static List<Mileage> getAllValuesByVin(String byVin) {
  24.  
  25. return mileageList.stream().filter(record -> record.vin.equals(byVin)).collect(Collectors.toList());
  26. }
  27.  
  28. static void addMileageRecord(Mileage mileageToAdd) {
  29. mileageList.add(mileageToAdd);
  30. }
  31.  
  32. }
  33.  
  34. class Car implements Comparable<Car> {
  35. String brand;
  36. String model;
  37. String vin;
  38.  
  39. static Map<Car, Client> carOwners = new TreeMap<>(); // asocjacja kwalifikowana
  40.  
  41. public static void addOwnerToCar(Client owner, Car car) {
  42. if (carOwners.containsKey(car)) {
  43. throw new IllegalArgumentException("Car already has his owner! you have to remove it first");
  44. }
  45. carOwners.put(car, owner);
  46. }
  47.  
  48. public static Client getCarOwner(Car car) {
  49. if (carOwners.containsKey(car)) {
  50. return carOwners.get(car);
  51. }
  52. throw new IllegalArgumentException("this car has no owner");
  53. }
  54.  
  55. @Override
  56. public int compareTo(Car o) {
  57. return vin.compareTo(o.vin);
  58. }
  59. }
  60.  
  61. class Purchase { // zakup czyli przyklad asocjacji z atrybutem
  62. Client client;
  63. Car car;
  64. Date purchaseDate;
  65. int price;
  66.  
  67. public Purchase(Client client, Car car, Date purchaseDate, int price) {
  68. this.client = client;
  69. this.car = car;
  70. this.purchaseDate = purchaseDate;
  71. this.price = price;
  72. }
  73. }
  74.  
  75. class Discount {
  76.  
  77. private Discount(int percentage, String name) {
  78. this.percentage = percentage;
  79. this.name = name;
  80. }
  81.  
  82. private int percentage;
  83. private String name;
  84.  
  85. static Discount NewClientDiscount = new Discount(10, "Discount for new clients");
  86. static Discount NextCarDiscount = new Discount(12, "Discount for next car");
  87.  
  88. }
  89.  
  90. public class Client {
  91.  
  92. public static void main(String[] args) {
  93. ArrayList<Discount> discountList = new ArrayList<>();
  94. discountList.add(Discount.NewClientDiscount);
  95. discountList.add(Discount.NextCarDiscount);
  96. Client client = new Client("Jan", 1, discountList, "jan@wp.pl", new Date(), "90121912312");
  97. Client client2 = new Client("Maciej", "Tomasz", 1, discountList, "maciej@wp.pl", new Date(), "14253112670");
  98. Client client3 = new Client("Bogdan", 3, discountList, "bogdan@wp.pl", new Date(), "80121912312");
  99. }
  100.  
  101. private String name;
  102. private String secondName; //atrybut opcjonalny
  103. private long id; //atrybut prosty
  104. private final ArrayList<Discount> discountList = new ArrayList<>(); // asocjacja binarna
  105. private String email; //atrybut wymagany
  106. private Date creationDate; // atrybut zlozony
  107. private String pesel;
  108. private Client customerReferral;// asocjacja rekurencyjna, pole oznacza ze klient zostal polecony przez inego klienta
  109.  
  110. public Client getCustomerReferral() {
  111. return customerReferral;
  112. }
  113.  
  114. public void setCustomerReferral(Client customerReferral) {
  115. this.customerReferral = customerReferral;
  116. }
  117.  
  118. private Client(String name, long id, ArrayList discountList, String email, Date creationDate, String pesel) {
  119. this.name = name;
  120. this.id = id;
  121. this.discountList.addAll(discountList);
  122. this.email = email;
  123. this.creationDate = creationDate;
  124. this.pesel = pesel;
  125. }
  126.  
  127. private Client(String name, String secondName, long id, ArrayList discountList, String email, Date creationDate, String pesel) {
  128. this(name, id, discountList, email, creationDate, pesel);
  129. this.secondName = secondName;
  130. }
  131.  
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement