Untitled
unknown
plain_text
a year ago
5.8 kB
2
Indexable
Never
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; } } 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) { int[][] packagePrices = { {1000, 100, 50, 800, 100, 100, 100}, // for Weekend {700, 50, 20, 500, 50, 50, 50}, // for Weekday {500, 0, 0, 500, 50, 100, 50} // for Special }; 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); } } import java.util.Scanner; 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] + " " + input[6]; 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; } } } }