Untitled

mail@pastecode.io avatarunknown
plain_text
8 days ago
6.2 kB
5
Indexable
Never
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

class Node {
    int bookingID;
    int vID;
    int vDate;
    int pID;
    String vName;
    Node next;
    Node prev;

    Node(int bookingID, int vID, int vDate, int pID, String vName) {
        this.bookingID = bookingID;
        this.vID = vID;
        this.vDate = vDate;
        this.pID = pID;
        this.vName = vName;
        this.next = null;
        this.prev = null;
    }

    @Override
    public String toString() {
        return bookingID + " " + vID + " " + vDate + " " + pID + " " + vName;
    }
}

// CList.java
class CList {
    Node Cursor;
    int size;

    CList() {
        Cursor = null;
        size = 0;
    }

    void insert(Node N) {
        if (Cursor == null) {
            Cursor = N;
            N.next = N;
            N.prev = N;
        } else {
            Node current = Cursor;
            while (current.next != Cursor && current.next.vDate < N.vDate) {
                current = current.next;
            }
            N.next = current.next;
            current.next.prev = N;
            current.next = N;
            N.prev = current;
            if (N.vDate < Cursor.vDate) {
                Cursor = N;
            }
        }
        size++;
    }

    void remove(int vID) {
        Node current = Cursor;
        int count = size;
        do {
            if (current.vID == vID) {
                current.prev.next = current.next;
                current.next.prev = current.prev;
                if (current == Cursor) {
                    Cursor = current.next;
                }
                size--;
                return;
            }
            current = current.next;
            count--;
        } while (count != 0);
    }

    int earning(int pID) {
        int totalEarning = 0;
        Node current = Cursor;
        int count = size;
        do {
            if (current.pID == pID) {
                totalEarning += getPackagePrice(pID, current.vDate);
            }
            current = current.next;
            count--;
        } while (count != 0);
        return totalEarning;
    }

    void print1(int vDate) {
        Node current = Cursor;
        int count = size;
        do {
            if (current.vDate == vDate) {
                System.out.println(current.toString());
            }
            current = current.next;
            count--;
        } while (count != 0);
    }

    void print2(int pID) {
        Node current = Cursor;
        int count = size;
        do {
            if (current.pID == pID) {
                System.out.println(current.toString());
            }
            current = current.next;
            count--;
        } while (count != 0);
    }

    void print3(int bookingID) {
        Node current = Cursor;
        int count = size;
        do {
            if (current.bookingID == bookingID) {
                System.out.println(current.toString());
                return; // Print only one node with matching bookingID
            }
            current = current.next;
            count--;
        } while (count != 0);
        System.out.println("0"); // If no matching bookingID is found, print 0
    }

    private int getPackagePrice(int pID, int vDate) {
        // Implement logic to get the package price based on pID and vDate
        // You can use a lookup table or calculations based on the provided Table 1
        // For simplicity, I will assume a fixed lookup table for package prices
        int[][] packagePrices = {
            // pID 1    2    3    4    5    6    7
            {1000, 100, 50, 800, 100, 100, 100}, // Weekend Pass
            {700, 50, 20, 500, 50, 50, 50},     // Weekday Pass
            {500, 0, 0, 500, 50, 100, 50}      // Special Holidays
        };
        return packagePrices[vDateIsSpecial(vDate) ? 2 : (vDateIsWeekend(vDate) ? 0 : 1)][pID - 1];
    }

    private boolean vDateIsWeekend(int vDate) {
        int day = vDate % 100;
        return (day == 6 || day == 7 || day == 13 || day == 14 || day == 20 || day == 21 || day == 27 || day == 28);
    }

    private boolean vDateIsSpecial(int vDate) {
        return (vDate == 220 || vDate == 2309);
    }
}

// Solution.java


public class Solution {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        CList circularList = new CList();

        while (scanner.hasNextLine()) {
            String[] input = scanner.nextLine().split(" ");
            int command = Integer.parseInt(input[0]);

            switch (command) {
                case 1:
                    int bookingID = Integer.parseInt(input[1]);
                    int vID = Integer.parseInt(input[2]);
                    int vDate = Integer.parseInt(input[3]);
                    int pID = Integer.parseInt(input[4]);
                    String vName = input[5];
                    Node newNode = new Node(bookingID, vID, vDate, pID, vName);
                    circularList.insert(newNode);
                    break;
                case 2:
                    int removeVID = Integer.parseInt(input[1]);
                    circularList.remove(removeVID);
                    break;
                case 3:
                    int packageID = Integer.parseInt(input[1]);
                    int earning = circularList.earning(packageID);
                    System.out.println(earning);
                    break;
                case 4:
                    int vDateToPrint = Integer.parseInt(input[1]);
                    circularList.print1(vDateToPrint);
                    break;
                case 5:
                    int pIDToPrint = Integer.parseInt(input[1]);
                    circularList.print2(pIDToPrint);
                    break;
                case 6:
                    int bookingIDToPrint = Integer.parseInt(input[1]);
                    circularList.print3(bookingIDToPrint);
                    break;
            }
        }
    }
}