Advertisement
dodoidwow1

Untitled

Jan 27th, 2023
1,079
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.68 KB | Source Code | 0 0
  1. //COMPANY CLASS
  2. public class Company
  3. {
  4.     private RentNode _head;
  5.    
  6.     public Company() {
  7.         _head = null;
  8.     }
  9.    
  10.     public Company(RentNode rent) {
  11.         _head = rent;
  12.     }
  13.    
  14.     private boolean empty() {
  15.         return _head == null;
  16.     }
  17.    
  18.     private boolean RentalExists(Rent r) {
  19.         RentNode ptr = _head;
  20.        
  21.         while (ptr != null) {
  22.             if (ptr.getRent().equals(r)) {
  23.                 return true;
  24.             }
  25.         }
  26.         return false;
  27.     }
  28.    
  29.     public boolean addRent(String renterName, Car rentedCar, Date pickDate, Date returnDate) {
  30.         Rent rent = new Rent(renterName, rentedCar, pickDate, returnDate);
  31.         if (empty()) {
  32.             _head = new RentNode(rent);
  33.             return true;
  34.         }
  35.         if (RentalExists(rent)) {
  36.             return false;
  37.         }
  38.         RentNode ptr = _head;
  39.        
  40.         while (ptr.getNext() != null && (ptr.getRent().getPickDate().before(rent.getPickDate())) || (ptr.getRent().getPickDate().equals(rent.getPickDate())) &&
  41.         (ptr.getRent().howManyDays() > rent.howManyDays())) {
  42.             ptr = ptr.getNext();
  43.         }
  44.         RentNode rentNode = new RentNode(rent, ptr.getNext());
  45.         ptr.setNext(rentNode);
  46.        
  47.         return true;
  48.     }
  49.    
  50.     public boolean removeRent(Date d) {
  51.         if (empty()) {
  52.             return false;
  53.         } else {
  54.             if (_head.getRent().getPickDate().equals(d)) {
  55.                 _head = _head.getNext();
  56.                 return true;
  57.             }
  58.          RentNode behind = _head;
  59.          while (behind.getNext() != null) {
  60.              if (behind.getNext().getRent().getPickDate().equals(d)) {
  61.                  behind.setNext(behind.getNext().getNext());
  62.                  return true;
  63.              }
  64.              behind = behind.getNext();
  65.          }
  66.          return false;
  67.         }
  68.     }
  69.    
  70.     public int getNumOfRents() {
  71.         return getNumOfRents(_head);
  72.     }
  73.    
  74.     private int getNumOfRents(RentNode r) {
  75.         if (r == null) {
  76.             return 0;
  77.         }
  78.         return 1 + getNumOfRents(r.getNext());
  79.     }
  80.    
  81.     public int getSumOfPrices() {
  82.         if (empty()) {
  83.             return 0;
  84.         } else {
  85.             RentNode ptr = _head;
  86.             int sum = 0;
  87.            
  88.             while (ptr.getNext() != null) {
  89.                 sum += ptr.getRent().getPrice();
  90.                 ptr = ptr.getNext();
  91.             }
  92.             return sum;
  93.         }
  94.        
  95.     }
  96.    
  97.     public int getSumOfDays() {
  98.         if (empty()) {
  99.             return 0;
  100.         } else {
  101.             RentNode ptr = _head;
  102.             int totalDays = 0;
  103.            
  104.             while (ptr.getNext() != null) {
  105.                 totalDays += ptr.getRent().howManyDays();
  106.                 ptr = ptr.getNext();
  107.             }
  108.             return totalDays;
  109.         }
  110.     }
  111.    
  112.     public int averageRent() {
  113.         if (empty()) {
  114.             return 0;
  115.         } else {
  116.             return getSumOfDays() / getNumOfRents();
  117.         }
  118.     }
  119.    
  120.     public Car lastCarRent() {
  121.         if (empty()) {
  122.             return null;
  123.         } else {
  124.             RentNode ptr = _head;
  125.            
  126.             while (ptr.getNext() != null) {
  127.                 ptr = ptr.getNext();
  128.             }
  129.            
  130.             Car lastCar = new Car(ptr.getRent().getCar());
  131.             return lastCar;
  132.         }
  133.     }
  134.    
  135.     public Rent longestRent() {
  136.         if (empty()) {
  137.             return null;
  138.         } else {
  139.             Rent longestRent;
  140.             RentNode max = _head;
  141.             RentNode ptr = _head;
  142.             while (ptr.getNext() != null) {
  143.                 if (ptr.getRent().howManyDays() > max.getRent().howManyDays()) {
  144.                     max = ptr;
  145.                 }
  146.                 ptr = ptr.getNext();
  147.                
  148.                 }
  149.             longestRent = new Rent(max.getRent());
  150.             return longestRent;
  151.     }
  152. }
  153.  
  154.    public char mostCommonRate() {
  155.        if (empty()) {
  156.            return 'N';
  157.        }
  158.        int[] types = new int[4];
  159.        int aCounter = 0, bCounter = 0, cCounter = 0, dCounter = 0;
  160.        RentNode ptr = _head;
  161.        
  162.        while (ptr.getNext() != null) {
  163.            if (ptr.getRent().getCar().getType() == 'D') {
  164.                dCounter++;
  165.                types[0] = dCounter;
  166.            }
  167.            if (ptr.getRent().getCar().getType() == 'C') {
  168.                cCounter++;
  169.                types[1] = cCounter;
  170.            }
  171.            if (ptr.getRent().getCar().getType() == 'B') {
  172.                bCounter++;
  173.                types[2] = bCounter;
  174.            }
  175.            if (ptr.getRent().getCar().getType() == 'A') {
  176.                aCounter++;
  177.                types[3] = aCounter;
  178.            }
  179.            
  180.            ptr = ptr.getNext();
  181.        }
  182.        int max = types[0];
  183.        for (int i = 1; i < types.length - 1; i++) {
  184.            if (max < types[i]) {
  185.                max = types[i];
  186.            }
  187.        }
  188.        
  189.        if (max == types[0]) {
  190.            return 'D';
  191.        } else if (max == types[1]) {
  192.            return 'C';
  193.        } else if (max == types[2]) {
  194.            return 'B';
  195.        } else {
  196.            return 'A';
  197.        }
  198.    }
  199.    
  200.    public boolean includes(Company c) {
  201.        if (c.empty() || empty()) {
  202.            return false;
  203.        }
  204.        
  205.        int cLen = c.getNumOfRents();
  206.        boolean[] equalElements = new boolean[cLen];
  207.        RentNode ptr = _head;
  208.        RentNode cPtr = c._head;
  209.        
  210.        for (int i = 0; i < cLen; i++) {
  211.            while (ptr.getNext() != null) {
  212.                if (ptr.getRent().equals(cPtr.getRent())) {
  213.                   equalElements[i] = true;
  214.                   break;
  215.                }
  216.                ptr = ptr.getNext();
  217.            }
  218.            cPtr = cPtr.getNext();
  219.        }
  220.        
  221.        for (int i = 0; i < equalElements.length; i++) {
  222.            if (!equalElements[i]) {
  223.                return false;
  224.            }
  225.        }
  226.        return true;
  227.    }
  228.    
  229.    public Company merge(Company c) {
  230.        Company newRental;
  231.        RentNode ptr = _head;
  232.        RentNode cPtr = c._head;
  233.        
  234.        newRental = new Company(merge(ptr, cPtr));
  235.        return newRental;
  236.    }
  237.    
  238.    private RentNode merge(RentNode r1, RentNode r2) {
  239.        if (r1 == null) {
  240.            return r2;
  241.        }
  242.        if (r2 == null) {
  243.            return r1;
  244.        }
  245.        
  246.        if (r1.getRent().getPickDate().before(r2.getRent().getPickDate())) {
  247.            r1.setNext(merge(r1.getNext(),r2));
  248.            return r1;
  249.        } else {
  250.            r2.setNext(merge(r1, r2.getNext()));
  251.            return r2;
  252.        }
  253.    }
  254.    
  255.    public void unifyRents() {
  256.        if (empty()) {
  257.            return;
  258.        }
  259.        int nodeLen = getNumOfRents();
  260.        RentNode behind = _head;
  261.        RentNode ptr = _head.getNext();
  262.        Rent overlapped;
  263.        
  264.        for (int i = 0; i < nodeLen; i++) {
  265.            while (ptr.getNext() != null) {
  266.                overlapped = behind.getRent().overlap(ptr.getRent());
  267.                if (overlapped == null) {
  268.                    break;
  269.                } else {
  270.                    removeRent(overlapped.getPickDate());
  271.                }
  272.                ptr = ptr.getNext();
  273.            }
  274.            behind = behind.getNext();
  275.            ptr = behind.getNext();
  276.        }
  277.        
  278.    }
  279.    
  280.    public String toString() {
  281.        if (empty()) {
  282.            return "The company has 0 rents.";
  283.        } else {
  284.            String opening = "The company has " + getNumOfRents() + " rents:\n";
  285.            String rents = "";
  286.            while (_head != null) {
  287.                rents += _head.getRent().toString() + "\n";
  288.                _head = _head.getNext();
  289.            }
  290.            return opening + rents;
  291.        }
  292.    }
  293.  
  294. }
  295.  
  296. //RentNode Class
  297. public class RentNode
  298. {
  299.     protected Rent _rent;
  300.     protected RentNode _next;
  301.    
  302.     public RentNode (Rent r) {
  303.         if (r != null) {
  304.             _rent = r;
  305.             _next = null;
  306.         }
  307.     }
  308.    
  309.     public RentNode (Rent r, RentNode next) {
  310.         if (r != null && next != null) {
  311.             _rent = r;
  312.             _next = next;
  313.         }
  314.     }
  315.    
  316.     public RentNode (RentNode other) {
  317.         _rent = other.getRent();
  318.         _next = other.getNext();
  319.     }
  320.    
  321.     public Rent getRent() {
  322.         return _rent;
  323.     }
  324.    
  325.     public void setRent(Rent r) {
  326.         if (_rent.equals(r) || r == null) {
  327.             return;
  328.         }
  329.         _rent = r;
  330.     }
  331.    
  332.     public RentNode getNext() {
  333.         return _next;
  334.     }
  335.    
  336.     public void setNext(RentNode next) {
  337.         _next = next;
  338.     }
  339. }
  340.  
  341.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement