Advertisement
Guest User

Untitled

a guest
Jan 28th, 2020
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.69 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Collections;
  5. import java.util.Comparator;
  6.  
  7. public class Main {
  8.  
  9.     public static class GasType {
  10.         public String Name;
  11.         public double Price;
  12.  
  13.         public GasType(String name, double price){
  14.             Name = name;
  15.             Price = price;
  16.         }
  17.  
  18.         @Override
  19.         public boolean equals(Object obj) {
  20.             if(obj == this){
  21.                 return true;
  22.             }
  23.  
  24.             if (!(obj instanceof GasType)) {
  25.                 return false;
  26.             }
  27.  
  28.             GasType g = (GasType) obj;
  29.  
  30.             return Name.compareTo(g.Name) == 0
  31.                   && Double.compare(Price, g.Price) == 0;
  32.  
  33.         }
  34.  
  35.         @Override
  36.         public String toString() {
  37.             return Name + " " + Price;
  38.         }
  39.     }
  40.  
  41.     public static class Employee {
  42.         public String FirstName;
  43.         public String LastName;
  44.  
  45.         Employee(String firstName, String lastName){
  46.             FirstName = firstName;
  47.             LastName = lastName;
  48.         }
  49.  
  50.         @Override
  51.         public boolean equals(Object obj) {
  52.             if(obj == this){
  53.                 return true;
  54.             }
  55.  
  56.             if (!(obj instanceof Employee)) {
  57.                 return false;
  58.             }
  59.  
  60.             Employee e = (Employee) obj;
  61.  
  62.             return FirstName.compareTo(e.FirstName) == 0
  63.                   && LastName.compareTo(e.LastName) == 0;
  64.  
  65.         }
  66.  
  67.         @Override
  68.         public String toString() {
  69.             return FirstName + " " + LastName;
  70.         }
  71.     }
  72.  
  73.     public static class GasTypesCustomComparator implements Comparator<GasType> {
  74.  
  75.         @Override
  76.         public int compare(GasType gasType, GasType t1) {
  77.             int priceComparator = Double.compare(gasType.Price, t1.Price);
  78.             int nameComparator = gasType.Name.compareTo(t1.Name);
  79.  
  80.             if(priceComparator == 0){
  81.                 return nameComparator;
  82.             }
  83.  
  84.             return priceComparator;
  85.         }
  86.     }
  87.  
  88.     public static class GasStation {
  89.         public ArrayList<GasType> gasTypes;
  90.         public ArrayList<Employee> employees;
  91.  
  92.         GasStation() {
  93.             this.gasTypes = new ArrayList<>();
  94.             this.employees = new ArrayList<>();
  95.         }
  96.  
  97.         public void addGasType(GasType gasType) {
  98.             if(gasType != null){
  99.                 this.gasTypes.add(gasType);
  100.             }
  101.         }
  102.  
  103.         public void removeGasType(GasType gasType){
  104.             if(gasType != null){
  105.                 this.gasTypes.remove(gasType);
  106.             }
  107.         }
  108.  
  109.         public void addEmployee(Employee empl) {
  110.             if(empl != null){
  111.                 this.employees.add(empl);
  112.             }
  113.         }
  114.  
  115.         public void removeEmployee(Employee empl){
  116.             if(empl != null){
  117.                 this.employees.remove(empl);
  118.             }
  119.         }
  120.  
  121.         public void showGasTypes() {
  122.             ArrayList gasTypes = (ArrayList)this.gasTypes.clone();
  123.             Collections.sort(gasTypes, new GasTypesCustomComparator());
  124.  
  125.             System.out.println(gasTypes.toString());
  126.         }
  127.     }
  128.  
  129.     public static void main(String[] args) {
  130.  
  131.         GasType gasType1 = new GasType("aaab", 2);
  132.         GasType gasType2 = new GasType("bbc", 2);
  133.         GasType gasType3 = new GasType("ab", 2);
  134.  
  135.         Employee empl = new Employee("Ivan", "Ivanov");
  136.  
  137.         GasStation gasStation = new GasStation();
  138.         gasStation.addGasType(gasType1);
  139.         gasStation.addGasType(gasType2);
  140.         gasStation.addGasType(gasType3);
  141.         gasStation.addEmployee(empl);
  142.  
  143.         gasStation.showGasTypes();
  144.     }
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement