Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.Comparator;
- public class Main {
- public static class GasType {
- public String Name;
- public double Price;
- public GasType(String name, double price){
- Name = name;
- Price = price;
- }
- @Override
- public boolean equals(Object obj) {
- if(obj == this){
- return true;
- }
- if (!(obj instanceof GasType)) {
- return false;
- }
- GasType g = (GasType) obj;
- return Name.compareTo(g.Name) == 0
- && Double.compare(Price, g.Price) == 0;
- }
- @Override
- public String toString() {
- return Name + " " + Price;
- }
- }
- public static class Employee {
- public String FirstName;
- public String LastName;
- Employee(String firstName, String lastName){
- FirstName = firstName;
- LastName = lastName;
- }
- @Override
- public boolean equals(Object obj) {
- if(obj == this){
- return true;
- }
- if (!(obj instanceof Employee)) {
- return false;
- }
- Employee e = (Employee) obj;
- return FirstName.compareTo(e.FirstName) == 0
- && LastName.compareTo(e.LastName) == 0;
- }
- @Override
- public String toString() {
- return FirstName + " " + LastName;
- }
- }
- public static class GasTypesCustomComparator implements Comparator<GasType> {
- @Override
- public int compare(GasType gasType, GasType t1) {
- int priceComparator = Double.compare(gasType.Price, t1.Price);
- int nameComparator = gasType.Name.compareTo(t1.Name);
- if(priceComparator == 0){
- return nameComparator;
- }
- return priceComparator;
- }
- }
- public static class GasStation {
- public ArrayList<GasType> gasTypes;
- public ArrayList<Employee> employees;
- GasStation() {
- this.gasTypes = new ArrayList<>();
- this.employees = new ArrayList<>();
- }
- public void addGasType(GasType gasType) {
- if(gasType != null){
- this.gasTypes.add(gasType);
- }
- }
- public void removeGasType(GasType gasType){
- if(gasType != null){
- this.gasTypes.remove(gasType);
- }
- }
- public void addEmployee(Employee empl) {
- if(empl != null){
- this.employees.add(empl);
- }
- }
- public void removeEmployee(Employee empl){
- if(empl != null){
- this.employees.remove(empl);
- }
- }
- public void showGasTypes() {
- ArrayList gasTypes = (ArrayList)this.gasTypes.clone();
- Collections.sort(gasTypes, new GasTypesCustomComparator());
- System.out.println(gasTypes.toString());
- }
- }
- public static void main(String[] args) {
- GasType gasType1 = new GasType("aaab", 2);
- GasType gasType2 = new GasType("bbc", 2);
- GasType gasType3 = new GasType("ab", 2);
- Employee empl = new Employee("Ivan", "Ivanov");
- GasStation gasStation = new GasStation();
- gasStation.addGasType(gasType1);
- gasStation.addGasType(gasType2);
- gasStation.addGasType(gasType3);
- gasStation.addEmployee(empl);
- gasStation.showGasTypes();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement