binibiningtinamoran

Concert

Oct 16th, 2019
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 13.83 KB | None | 0 0
  1. import java.text.DecimalFormat;
  2. import java.text.NumberFormat;
  3. import java.util.Locale;
  4. import java.util.Random;
  5.  
  6. public class Concert {
  7.  
  8.     private int day, month, year,
  9.             availableUpperTickets,
  10.             availableLowerTickets,
  11.             availableFloorTickets;
  12.     private String venue, artist;
  13.     private double totalSales;
  14.  
  15.     /*ticket prices for the different sections */
  16.     private static final double PRICE_UPPER_TICKET = 29.90;
  17.     private static final double PRICE_LOWER_TICKET = 99.0;
  18.     private static final double PRICE_FLOOR_TICKET = 180.0;
  19.  
  20.     /* total number of tickets per section */
  21.     private static final int TOTAL_NUMBER_UPPER_TICKETS = 300;
  22.     private static final int TOTAL_NUMBER_LOWER_TICKETS = 300;
  23.     private static final int TOTAL_NUMBER_FLOOR_TICKETS = 400;
  24.  
  25.     /**
  26.      * Instantiates a new Concert.
  27.      */
  28.     public Concert() {
  29.         this.month = 9;
  30.         this.day = 8;
  31.         this.year = 2019;
  32.         this.artist = "Credence Clearwater Revival";
  33.         this.venue = "Van Andel Arena";
  34.         this.totalSales = 0;
  35.         this.availableFloorTickets = TOTAL_NUMBER_FLOOR_TICKETS;
  36.         this.availableUpperTickets = TOTAL_NUMBER_UPPER_TICKETS;
  37.         this.availableLowerTickets = TOTAL_NUMBER_LOWER_TICKETS;
  38.     }
  39.  
  40.     /**
  41.      * Instantiates a new Concert.
  42.      *
  43.      * @param m the month
  44.      * @param d the day
  45.      * @param y the year
  46.      * @param a the artist's name
  47.      * @param v the venue's name
  48.      */
  49.     public Concert(int m, int d, int y, String a, String v) {
  50.         setDate(m,d,y);
  51.         this.artist = a;
  52.         this.venue = v;
  53.         this.totalSales = 0;
  54.         this.availableFloorTickets = TOTAL_NUMBER_FLOOR_TICKETS;
  55.         this.availableUpperTickets = TOTAL_NUMBER_UPPER_TICKETS;
  56.         this.availableLowerTickets = TOTAL_NUMBER_LOWER_TICKETS;
  57.     }
  58.  
  59.     /**
  60.      * Instantiates a new Concert.
  61.      *
  62.      * @param date the date
  63.      * @param a    the artist's name
  64.      * @param v    the venue's name
  65.      */
  66.     public Concert(String date, String a, String v) {
  67.         this.artist = a;
  68.         this.venue = v;
  69.         parseDate(date);
  70.         this.totalSales = 0;
  71.         this.availableFloorTickets = TOTAL_NUMBER_FLOOR_TICKETS;
  72.         this.availableUpperTickets = TOTAL_NUMBER_UPPER_TICKETS;
  73.         this.availableLowerTickets = TOTAL_NUMBER_LOWER_TICKETS;
  74.     }
  75.  
  76.     /**
  77.      * Sets artist.
  78.      *
  79.      * @param n the name of the artist
  80.      */
  81.     /* Setters */
  82.     public void setArtist(String n) {
  83.         this.artist = n;
  84.     }
  85.  
  86.     /**
  87.      * Sets venue.
  88.      *
  89.      * @param n the name of the venue
  90.      */
  91.     public void setVenue(String n) {
  92.         this.venue = n;
  93.     }
  94.  
  95.     /**
  96.      * Sets date.
  97.      *
  98.      * @param m    the month
  99.      * @param d    the day
  100.      * @param year the year
  101.      */
  102.     public void setDate(int m, int d, int year) {
  103.         if (isValidDate(m, d, year)) {
  104.             this.month = m;
  105.             this.day = d;
  106.             this.year = year;
  107.         } else {
  108.             System.out.println("Invalid date.");
  109.         }
  110.     }
  111.  
  112.     /**
  113.      * Gets artist.
  114.      *
  115.      * @return the artist
  116.      */
  117.     /* Getters */
  118.     public String getArtist() {
  119.         return this.artist;
  120.         // or return artist;
  121.     }
  122.  
  123.     /**
  124.      * Gets venue.
  125.      *
  126.      * @return the venue
  127.      */
  128.     public String getVenue() {
  129.         return this.venue;
  130.         // or return venue;
  131.     }
  132.  
  133.     /**
  134.      * Gets ticket price.
  135.      *
  136.      * @param ticketType the ticket type
  137.      *
  138.      * @return the ticket price
  139.      */
  140.     public double getTicketPrice(char ticketType) {
  141.         if (ticketType == 'F') {
  142.             return PRICE_FLOOR_TICKET;
  143.         } else if (ticketType == 'L') {
  144.             return PRICE_LOWER_TICKET;
  145.         } else {
  146.             return PRICE_UPPER_TICKET;
  147.         }
  148.     }
  149.  
  150.     /**
  151.      * Gets available upper tickets.
  152.      *
  153.      * @return the available upper tickets
  154.      */
  155.     public int getAvailableUpperTickets() {
  156.         return availableUpperTickets;
  157.     }
  158.  
  159.  
  160.     /**
  161.      * Gets available lower tickets.
  162.      *
  163.      * @return the available lower tickets
  164.      */
  165.     public int getAvailableLowerTickets() {
  166.         return availableLowerTickets;
  167.     }
  168.  
  169.     /**
  170.      * Gets available floor tickets.
  171.      *
  172.      * @return the available floor tickets
  173.      */
  174.     public int getAvailableFloorTickets() {
  175.         return availableFloorTickets;
  176.     }
  177.  
  178.     /**
  179.      * Gets total sales.
  180.      *
  181.      * @return the total sales
  182.      */
  183.     public double getTotalSales() {
  184.         return this.totalSales;
  185.     }
  186.  
  187.     /**
  188.      * Gets month.
  189.      *
  190.      * @return the month
  191.      */
  192.     public int getMonth() {
  193.         return this.month;
  194.     }
  195.  
  196.     /**
  197.      * Gets day.
  198.      *
  199.      * @return the day
  200.      */
  201.     public int getDay() {
  202.         return this.day;
  203.     }
  204.  
  205.     /**
  206.      * Gets year.
  207.      *
  208.      * @return the year
  209.      */
  210.     public int getYear() {
  211.         return this.year;
  212.     }
  213.  
  214.  
  215.     /**
  216.      * Accepts date in String type then parses it
  217.      * into its corresponding integer equivalent.
  218.      * Uses substring(), not split().
  219.      *
  220.      * @param date the date
  221.      */
  222.     private void parseDate(String date) {
  223.         int firstSlash = date.indexOf("/");
  224.         int secondSlash = date.lastIndexOf("/");
  225.  
  226.         int dateMonth = Integer.parseInt(date.substring(0, firstSlash));
  227.         int dateDay = Integer.parseInt(date.substring(firstSlash + 1, secondSlash));
  228.         int dateYear = Integer.parseInt(date.substring(secondSlash + 1));
  229.  
  230.         if (isValidDate(dateMonth, dateDay, dateYear)) {
  231.             this.month = dateMonth;
  232.             this.day = dateDay;
  233.             this.year = dateYear;
  234.         } else {
  235.             System.out.println("Invalid date provided.");
  236.         }
  237.     }
  238.  
  239.     /**
  240.      * Checks if a given year is a leap year or not.
  241.      *
  242.      * @param y the year
  243.      * @return  True if year is a leap year, False otherwise.
  244.      */
  245.     private boolean isLeapYear(int y) {
  246.         // Using ternary operator...
  247.         return ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)));
  248.     }
  249.  
  250.     /**
  251.      * Checks if the given date is valid.
  252.      *
  253.      * @param m the month
  254.      * @param d the day
  255.      * @param y the year
  256.      * @return  True if year is a valid year, False otherwise.
  257.      */
  258.     private boolean isValidDate(int m, int d, int y) {
  259.         final int[] DAYS = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  260.         if (m < 1 || m > 12) {
  261.             return false;
  262.         }
  263.         if (d < 1 || d > DAYS[m]) {
  264.             return false;
  265.         }
  266.         return m != 2 || d != 29 || isLeapYear(y);
  267.     }
  268.  
  269.  
  270.     /**
  271.      * Edits given date string into readable format.
  272.      *
  273.      * @param format ranges from 1 to 4, each representing a valid date format
  274.      *
  275.      * @return       the date String in given format
  276.      */
  277.     public String formatDate(int format) {
  278.         String formattedDate = "";
  279.         if (format == 1) {
  280.             formattedDate = String.format("%d/%d/%d", this.getMonth(), this.getDay(),
  281.                     this.getYear());
  282.         } else if (format == 2) {
  283.             DecimalFormat df = new DecimalFormat("00");
  284.             formattedDate = String.format("%s/%s/%s",df.format(this.getMonth()), df.format(day),
  285.                     df.format(year));
  286.         } else if (format == 3) {
  287.             final String months = "JanFebMarAprMayJunJulAugSepOctNovDec";
  288.             formattedDate = "";
  289.         } else { // if format == 4
  290.             switch(this.getMonth()) {
  291.                 case 1:
  292.                     formattedDate = "January " + this.getDay() + ", " + this.getYear();
  293.                     break;
  294.                 case 2:
  295.                     formattedDate = "February " + this.getDay() + ", " + this.getYear();
  296.                     break;
  297.                 case 3:
  298.                     formattedDate = "March " + this.getDay() + ", " + this.getYear();
  299.                     break;
  300.                 case 4:
  301.                     formattedDate = "April " + this.getDay() + ", " + this.getYear();
  302.                     break;
  303.                 case 5:
  304.                     formattedDate = "May " + this.getDay() + ", " + this.getYear();
  305.                     break;
  306.                 case 6:
  307.                     formattedDate = "June " + this.getDay() + ", " + this.getYear();
  308.                     break;
  309.                 case 7:
  310.                     formattedDate = "July " + this.getDay() + ", " + this.getYear();
  311.                     break;
  312.                 case 8:
  313.                     formattedDate = "August " + this.getDay() + ", " + this.getYear();
  314.                     break;
  315.                 case 9:
  316.                     formattedDate ="September " + this.getDay() + ", " + this.getYear();
  317.                     break;
  318.                 case 10:
  319.                     formattedDate = "October " + this.getDay() + ", " + this.getYear();
  320.                     break;
  321.                 case 11:
  322.                     formattedDate = "November " + this.getDay() + ", " + this.getYear();
  323.                     break;
  324.                 default:
  325.                     formattedDate = "December " + this.getDay() + ", " + this.getYear();
  326.             }
  327.         }
  328.         return formattedDate;
  329.     }
  330.  
  331.     /**
  332.      * Method that allows buying functionality.
  333.      *
  334.      * @param ticketType the type of ticket, either Floor, Lower, or Upper type
  335.      * @param numTickets the number tickets to be bought
  336.      * @param pmt        the payment for the tickets bought
  337.      */
  338.     public void buyTickets(char ticketType, int numTickets, double pmt) {
  339.         if (numTickets < 0 || pmt < 0) {
  340.             System.out.println("Error - invalid (negative) number of tickets or payment");
  341.         } else if (ticketType != 'F' && ticketType != 'L' && ticketType != 'U') {
  342.             System.out.println("Invalid ticket type.");
  343.         } else {
  344.             double amountDue;
  345.             if ((numTickets <= availableFloorTickets)
  346.                     || (numTickets <= availableLowerTickets)
  347.                     || (numTickets <= availableUpperTickets)) {
  348.  
  349.                 amountDue = numTickets * getTicketPrice(ticketType);
  350.                 if (pmt >= amountDue) {
  351.                     if (ticketType == 'F') {
  352.                         availableFloorTickets -= numTickets;
  353.                         System.out.printf("Transaction: Number tickets Floor section : %d, total: $%," +
  354.                                 ".2f\n", numTickets, amountDue);
  355.                     } else if (ticketType == 'L') {
  356.                         availableLowerTickets -= numTickets;
  357.                         System.out.printf("Transaction: Number tickets Lower section : %d, total: $%," +
  358.                                 ".2f\n", numTickets, amountDue);
  359.                     } else {
  360.                         availableUpperTickets -= numTickets;
  361.                         System.out.printf("Transaction: Number tickets Upper section : %d, total: $%," +
  362.                                 ".2f\n", numTickets, amountDue);
  363.                     }
  364.                     totalSales += amountDue;
  365.                 } else {
  366.                     System.out.println("Error – payment is not enough to buy the tickets.");
  367.                 } // end innermost else
  368.             } else {
  369.                 if (ticketType == 'F') {
  370.                     System.out.println("Tickets not available in floor section.");
  371.                 } else if (ticketType == 'L') {
  372.                     System.out.println("Tickets not available in lower section.");
  373.                 } else {
  374.                     System.out.println("Tickets not available in upper section.");
  375.                 }
  376.             } // end inner else
  377.         } // end outer else
  378.     }
  379.  
  380.     /**
  381.      * Simulates a company buying a total number of individual tickets (numberTickets)
  382.      * in different sections.
  383.      *
  384.      * Invokes the buyTickets() method.
  385.      *
  386.      * @param numberTickets the number tickets bought
  387.      */
  388.     public void simulateCompanyBuyingTickets(int numberTickets) {
  389.         Random rand = new Random();
  390.         char[] ticketSymbols = new char[] {'F','L', 'U'};
  391.         char ticketType;
  392.  
  393.         for (int i = 0; i < numberTickets; i++) {
  394.             ticketType = ticketSymbols[rand.nextInt(3)];
  395.                 buyTickets(ticketType, 1, this.getTicketPrice(ticketType));
  396.         }
  397.     }
  398.  
  399.     public void printReport() {
  400.         NumberFormat currency = NumberFormat.getCurrencyInstance(Locale.US);
  401.  
  402.         int totalFloorTicketsSold = TOTAL_NUMBER_FLOOR_TICKETS - this.getAvailableFloorTickets();
  403.         int totalUpperTicketsSold = TOTAL_NUMBER_UPPER_TICKETS - this.getAvailableUpperTickets();
  404.         int totalLowerTicketsSold = TOTAL_NUMBER_LOWER_TICKETS - this.getAvailableLowerTickets();
  405.  
  406.         System.out.println("Concert Report");
  407.         System.out.println("======================");
  408.         System.out.println("Artist: \t\t" + this.getArtist());
  409.         System.out.println("Venue: \t\t\t" + this.getVenue());
  410.         System.out.println("Date: \t\t\t" + this.formatDate(4));
  411.  
  412.         System.out.println(); // prints whitespace, equivalent to println("\n")
  413.  
  414.         System.out.println("Tickets sold:");
  415.         System.out.println("=============");
  416.         System.out.println("Upper: " + totalUpperTicketsSold + "\t" +
  417.                 currency.format(totalUpperTicketsSold * PRICE_UPPER_TICKET));
  418.         System.out.println("Lower: " + totalLowerTicketsSold + "\t" +
  419.                 currency.format(totalLowerTicketsSold * this.getTicketPrice('L')));
  420.         System.out.println("Floor: " + totalFloorTicketsSold + "\t" +
  421.                 currency.format(totalFloorTicketsSold * PRICE_FLOOR_TICKET));
  422.  
  423.         System.out.println("=============");
  424.         System.out.println("Total Sales: " + currency.format(this.getTotalSales()));
  425.     } // end printReport()
  426.  
  427. }
Advertisement
Add Comment
Please, Sign In to add comment