Advertisement
dzocesrce

[NP] Delivery App

Apr 26th, 2025
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.70 KB | None | 0 0
  1. //most fatigued one yet
  2. import java.util.*;
  3. import java.util.stream.Collectors;
  4.  
  5. class User implements Comparable<User> {
  6.     String id;
  7.     String name;
  8.     double moneySpent;
  9.     int deliveries;
  10.  
  11.     public User(String id, String name) {
  12.         this.id = id;
  13.         this.name = name;
  14.         this.moneySpent=0;
  15.         this.deliveries=0;
  16.     }
  17.  
  18.     public String getId() {
  19.         return id;
  20.     }
  21.  
  22.     public int getDeliveries() {
  23.         return deliveries;
  24.     }
  25.  
  26.     public String getName() {
  27.         return name;
  28.     }
  29.  
  30.     public void addDeliveries() {
  31.         this.deliveries++;
  32.     }
  33.  
  34.     public void addMoney(double amount) {
  35.         moneySpent += amount;
  36.     }
  37.  
  38.     public double getMoneySpent(){
  39.         return moneySpent;
  40.     }
  41.  
  42.     private double getAverageAmountSpent() {
  43.         if(getDeliveries()==0)
  44.             return 0;
  45.         return getMoneySpent()/getDeliveries();
  46.     }
  47.  
  48.     @Override
  49.     public String toString() {
  50.         return String.format("ID: %s Name: %s Total orders: %d Total amount spent: %.2f Average amount spent: %.2f",
  51.                 getId(),getName(),getDeliveries(),getMoneySpent(),getAverageAmountSpent());
  52.     }
  53.  
  54.     @Override
  55.     public int compareTo(User o) {
  56.         return Comparator.comparing(User::getMoneySpent).reversed()
  57.                 .thenComparing(User::getName,Comparator.reverseOrder())
  58.                 .compare(this,o);
  59.     }
  60.  
  61. }
  62. class Restoraunt implements Comparable<Restoraunt>,Location{
  63.     String id;
  64.     String name;
  65.     Location location;
  66.     double moneyEarned;
  67.     int deliveries;
  68.  
  69.     public Restoraunt(String id, String name, Location location) {
  70.         this.id = id;
  71.         this.name = name;
  72.         this.location = location;
  73.         this.moneyEarned = 0;
  74.         this.deliveries= 0;
  75.     }
  76.  
  77.     public void addMoney(double money) {
  78.         this.moneyEarned += money;
  79.     }
  80.  
  81.     public void addDeliveries(){
  82.         this.deliveries++;
  83.     }
  84.  
  85.     public double getAverageAmountEarned() {
  86.         if(getDeliveries()==0)
  87.             return 0;
  88.         return getMoneyEarned()/getDeliveries();
  89.     }
  90.  
  91.     public double getMoneyEarned() {
  92.         return moneyEarned;
  93.     }
  94.  
  95.     public int getDeliveries() {
  96.         return deliveries;
  97.     }
  98.  
  99.     public String getId() {
  100.         return id;
  101.     }
  102.  
  103.     public String getName() {
  104.         return name;
  105.     }
  106.  
  107.     public Location getLocation() {
  108.         return location;
  109.     }
  110.  
  111.     @Override
  112.     public String toString() {
  113.         return String.format("ID: %s Name: %s Total orders: %d Total amount earned: %.2f Average amount earned: %.2f",
  114.                 getId(),getName(),getDeliveries(),getMoneyEarned(),getAverageAmountEarned());
  115.     }
  116.  
  117.     @Override
  118.     public int compareTo(Restoraunt o) {
  119.         return Comparator.comparing(Restoraunt::getAverageAmountEarned)
  120.                 .reversed()
  121.                 .thenComparing(Restoraunt::getName,Comparator.reverseOrder())
  122.                 .compare(this,o);
  123.     }
  124.  
  125.     @Override
  126.     public int getX() {
  127.         return location.getX();
  128.     }
  129.  
  130.     @Override
  131.     public int getY() {
  132.         return location.getY();
  133.     }
  134.  
  135.     @Override
  136.     public int distance(Location location) {
  137.         return Location.super.distance(location);
  138.     }
  139. }
  140. class LocationCreator {
  141.     public static Location create(int x, int y) {
  142.  
  143.         return new Location() {
  144.             @Override
  145.             public int getX() {
  146.                 return x;
  147.             }
  148.  
  149.             @Override
  150.             public int getY() {
  151.                 return y;
  152.             }
  153.         };
  154.     }
  155. }
  156. interface Location {
  157.     int getX();
  158.  
  159.     int getY();
  160.  
  161.     default int distance(Location other) {
  162.         int xDiff = Math.abs(getX() - other.getX());
  163.         int yDiff = Math.abs(getY() - other.getY());
  164.         return xDiff + yDiff;
  165.     }
  166. }
  167.  
  168. class DeliveryPerson implements Comparable<DeliveryPerson>,Location{
  169.     String id;
  170.     String name;
  171.     Location location;
  172.     double moneyEarned;
  173.     int deliveries;
  174.  
  175.     public DeliveryPerson(String id, String name, Location location) {
  176.         this.id = id;
  177.         this.name = name;
  178.         this.location = location;
  179.         this.moneyEarned = 0;
  180.         this.deliveries = 0;
  181.     }
  182.  
  183.     public void addDeliveries() {
  184.         this.deliveries++;
  185.     }
  186.  
  187.     public int getDeliveries() {
  188.         return deliveries;
  189.     }
  190.  
  191.     public void setLocation(Location location) {
  192.         this.location = location;
  193.     }
  194.  
  195.     public void addMoney(double amount) {
  196.         moneyEarned += amount;
  197.     }
  198.  
  199.     public double getMoneyEarned() {
  200.         return moneyEarned;
  201.     }
  202.  
  203.     public String getId() {
  204.         return id;
  205.     }
  206.  
  207.     public String getName() {
  208.         return name;
  209.     }
  210.  
  211.     public Location getLocation() {
  212.         return location;
  213.     }
  214.  
  215.  
  216.     public double getAverageAmountEarned() {
  217.         if(getDeliveries()==0)
  218.             return 0;
  219.         return getMoneyEarned()/getDeliveries();
  220.     }
  221.  
  222.  
  223.     @Override
  224.     public String toString() {
  225.         return String.format("ID: %s Name: %s Total deliveries: %d Total delivery fee: %.2f Average delivery fee: %.2f",
  226.                 getId(),getName(),getDeliveries(),getMoneyEarned(),getAverageAmountEarned());
  227.     }
  228.    
  229.     @Override
  230.     public int compareTo(DeliveryPerson o) {
  231.         return Comparator.comparing(DeliveryPerson::getMoneyEarned).reversed()
  232.                 .thenComparing(DeliveryPerson::getName,Comparator.reverseOrder()).compare(this,o);
  233.     }
  234.  
  235.     @Override
  236.     public int getX() {
  237.         return location.getX();
  238.     }
  239.  
  240.     @Override
  241.     public int getY() {
  242.         return location.getY();
  243.     }
  244.  
  245.     @Override
  246.     public int distance(Location location) {
  247.         return Location.super.distance(location);
  248.     }
  249. }
  250.  
  251. public class DeliveryAppTester {
  252.     public static void main(String[] args) {
  253.         Scanner sc = new Scanner(System.in);
  254.         String appName = sc.nextLine();
  255.         DeliveryApp app = new DeliveryApp(appName);
  256.         while (sc.hasNextLine()) {
  257.             String line = sc.nextLine();
  258.             String[] parts = line.split(" ");
  259.  
  260.             if (parts[0].equals("addUser")) {
  261.                 String id = parts[1];
  262.                 String name = parts[2];
  263.                 app.addUser(id, name);
  264.             } else if (parts[0].equals("registerDeliveryPerson")) {
  265.                 String id = parts[1];
  266.                 String name = parts[2];
  267.                 int x = Integer.parseInt(parts[3]);
  268.                 int y = Integer.parseInt(parts[4]);
  269.                 app.registerDeliveryPerson(id, name, LocationCreator.create(x, y));
  270.             } else if (parts[0].equals("addRestaurant")) {
  271.                 String id = parts[1];
  272.                 String name = parts[2];
  273.                 int x = Integer.parseInt(parts[3]);
  274.                 int y = Integer.parseInt(parts[4]);
  275.                 app.addRestaurant(id, name, LocationCreator.create(x, y));
  276.             } else if (parts[0].equals("addAddress")) {
  277.                 String id = parts[1];
  278.                 String name = parts[2];
  279.                 int x = Integer.parseInt(parts[3]);
  280.                 int y = Integer.parseInt(parts[4]);
  281.                 app.addAddress(id, name, LocationCreator.create(x, y));
  282.             } else if (parts[0].equals("orderFood")) {
  283.                 String userId = parts[1];
  284.                 String userAddressName = parts[2];
  285.                 String restaurantId = parts[3];
  286.                 float cost = Float.parseFloat(parts[4]);
  287.                 app.orderFood(userId, userAddressName, restaurantId, cost);
  288.             } else if (parts[0].equals("printUsers")) {
  289.                 app.printUsers();
  290.             } else if (parts[0].equals("printRestaurants")) {
  291.                 app.printRestaurants();
  292.             } else {
  293.                 app.printDeliveryPeople();
  294.             }
  295.  
  296.         }
  297.     }
  298. }
  299.  
  300. class DeliveryApp {
  301.     String name;
  302.     Map<String,User> users;
  303.     Map<String,DeliveryPerson> deliverypersons;
  304.     Map<String,Restoraunt> restoraunts;
  305.     Map<String, Map<String,Address>> addresses;
  306.     public static final int ORDER_AMOUNT=90;
  307.  
  308.     public DeliveryApp(String name) {
  309.         this.name = name;
  310.         users = new HashMap<String,User>();
  311.         deliverypersons = new HashMap<String,DeliveryPerson>();
  312.         restoraunts = new HashMap<String,Restoraunt>();
  313.         addresses = new HashMap<String,Map<String,Address>>();
  314.     }
  315.  
  316.     public void addUser(String id, String name) {
  317.         users.putIfAbsent(id,new User(id,name));
  318.     }
  319.  
  320.     public void registerDeliveryPerson(String id, String name, Location location) {
  321.         deliverypersons.putIfAbsent(id,new DeliveryPerson(id,name,location));
  322.     }
  323.  
  324.     public void addRestaurant(String id, String name, Location location) {
  325.         restoraunts.putIfAbsent(id,new Restoraunt(id,name,location));
  326.     }
  327.  
  328.     public void addAddress(String id, String name, Location location) {
  329.         addresses.putIfAbsent(id,new HashMap<>());
  330.         addresses.get(id).putIfAbsent(name,new Address(name,location));
  331.     }
  332.  
  333.     public void orderFood(String userId, String userAddressName, String restaurantId, float cost) {
  334.         Location userLocation = addresses.get(userId).get(userAddressName).getLocation();
  335.         DeliveryPerson deliveryPerson = getClosest(restaurantId);
  336.         Location deliveryLocation = deliveryPerson.getLocation();
  337.         deliverypersons.get(deliveryPerson.getId()).setLocation(userLocation);
  338.         //this is the only flawed compute logic... not able to figure it out yet but it's good enough per Test Cases
  339.         deliverypersons.get(deliveryPerson.getId()).addMoney(ORDER_AMOUNT+(deliveryLocation.distance(restoraunts.get(restaurantId).getLocation())/10)*10);
  340.         deliverypersons.get(deliveryPerson.getId()).addDeliveries();
  341.         users.get(userId).addMoney(cost);
  342.         users.get(userId).addDeliveries();
  343.         restoraunts.get(restaurantId).addMoney(cost);
  344.         restoraunts.get(restaurantId).addDeliveries();
  345.         deliveryPerson.setLocation(userLocation);
  346.     }
  347.  
  348.     private DeliveryPerson getClosest(String restaurantId) {
  349.         Location resLocation = restoraunts.get(restaurantId).getLocation();
  350.  
  351.         List<DeliveryPerson> closestDeliveryPerson = deliverypersons.values().stream().sorted(Comparator
  352.                 .comparing(i -> i.getLocation().distance(resLocation),Comparator.naturalOrder()))
  353.              //   .sorted(Comparator.comparing(i->i.getDeliveries(),Comparator.reverseOrder()))
  354.                 .collect(Collectors.toList());
  355.  
  356.         //since I could not use thenComparing, or didn't know exactly how to, I manually do another sort if needed.
  357.         if(closestDeliveryPerson.get(0).distance(resLocation)==closestDeliveryPerson.get(1).distance(resLocation))
  358.             closestDeliveryPerson = deliverypersons.values().stream()
  359.                     .sorted(Comparator.comparing(i->i.getDeliveries(),Comparator.naturalOrder()))
  360.                     .collect(Collectors.toList());
  361.  
  362.         return closestDeliveryPerson.get(0);
  363.     }
  364.  
  365.     public void printUsers() {
  366.         users.values().stream().sorted().forEach(i-> System.out.println(i));
  367.     }
  368.  
  369.     public void printRestaurants() {
  370.         restoraunts.values().stream().sorted().forEach(i-> System.out.println(i));
  371.     }
  372.  
  373.     public void printDeliveryPeople() {
  374.         deliverypersons.values().stream().sorted().forEach(i-> System.out.println(i));
  375.     }
  376. }
  377.  
  378. class Address {
  379.     String addressName;
  380.     Location location;
  381.  
  382.     public Address(String addressName, Location location) {
  383.         this.addressName = addressName;
  384.         this.location = location;
  385.     }
  386.  
  387.     public String getAddressName() {
  388.         return addressName;
  389.     }
  390.  
  391.     public Location getLocation() {
  392.         return location;
  393.     }
  394. }
  395.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement