Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.text.DecimalFormat;
- import java.text.NumberFormat;
- import java.util.Locale;
- import java.util.Random;
- public class Concert {
- private int day, month, year,
- availableUpperTickets,
- availableLowerTickets,
- availableFloorTickets;
- private String venue, artist;
- private double totalSales;
- /*ticket prices for the different sections */
- private static final double PRICE_UPPER_TICKET = 29.90;
- private static final double PRICE_LOWER_TICKET = 99.0;
- private static final double PRICE_FLOOR_TICKET = 180.0;
- /* total number of tickets per section */
- private static final int TOTAL_NUMBER_UPPER_TICKETS = 300;
- private static final int TOTAL_NUMBER_LOWER_TICKETS = 300;
- private static final int TOTAL_NUMBER_FLOOR_TICKETS = 400;
- /**
- * Instantiates a new Concert.
- */
- public Concert() {
- this.month = 9;
- this.day = 8;
- this.year = 2019;
- this.artist = "Credence Clearwater Revival";
- this.venue = "Van Andel Arena";
- this.totalSales = 0;
- this.availableFloorTickets = TOTAL_NUMBER_FLOOR_TICKETS;
- this.availableUpperTickets = TOTAL_NUMBER_UPPER_TICKETS;
- this.availableLowerTickets = TOTAL_NUMBER_LOWER_TICKETS;
- }
- /**
- * Instantiates a new Concert.
- *
- * @param m the month
- * @param d the day
- * @param y the year
- * @param a the artist's name
- * @param v the venue's name
- */
- public Concert(int m, int d, int y, String a, String v) {
- setDate(m,d,y);
- this.artist = a;
- this.venue = v;
- this.totalSales = 0;
- this.availableFloorTickets = TOTAL_NUMBER_FLOOR_TICKETS;
- this.availableUpperTickets = TOTAL_NUMBER_UPPER_TICKETS;
- this.availableLowerTickets = TOTAL_NUMBER_LOWER_TICKETS;
- }
- /**
- * Instantiates a new Concert.
- *
- * @param date the date
- * @param a the artist's name
- * @param v the venue's name
- */
- public Concert(String date, String a, String v) {
- this.artist = a;
- this.venue = v;
- parseDate(date);
- this.totalSales = 0;
- this.availableFloorTickets = TOTAL_NUMBER_FLOOR_TICKETS;
- this.availableUpperTickets = TOTAL_NUMBER_UPPER_TICKETS;
- this.availableLowerTickets = TOTAL_NUMBER_LOWER_TICKETS;
- }
- /**
- * Sets artist.
- *
- * @param n the name of the artist
- */
- /* Setters */
- public void setArtist(String n) {
- this.artist = n;
- }
- /**
- * Sets venue.
- *
- * @param n the name of the venue
- */
- public void setVenue(String n) {
- this.venue = n;
- }
- /**
- * Sets date.
- *
- * @param m the month
- * @param d the day
- * @param year the year
- */
- public void setDate(int m, int d, int year) {
- if (isValidDate(m, d, year)) {
- this.month = m;
- this.day = d;
- this.year = year;
- } else {
- System.out.println("Invalid date.");
- }
- }
- /**
- * Gets artist.
- *
- * @return the artist
- */
- /* Getters */
- public String getArtist() {
- return this.artist;
- // or return artist;
- }
- /**
- * Gets venue.
- *
- * @return the venue
- */
- public String getVenue() {
- return this.venue;
- // or return venue;
- }
- /**
- * Gets ticket price.
- *
- * @param ticketType the ticket type
- *
- * @return the ticket price
- */
- public double getTicketPrice(char ticketType) {
- if (ticketType == 'F') {
- return PRICE_FLOOR_TICKET;
- } else if (ticketType == 'L') {
- return PRICE_LOWER_TICKET;
- } else {
- return PRICE_UPPER_TICKET;
- }
- }
- /**
- * Gets available upper tickets.
- *
- * @return the available upper tickets
- */
- public int getAvailableUpperTickets() {
- return availableUpperTickets;
- }
- /**
- * Gets available lower tickets.
- *
- * @return the available lower tickets
- */
- public int getAvailableLowerTickets() {
- return availableLowerTickets;
- }
- /**
- * Gets available floor tickets.
- *
- * @return the available floor tickets
- */
- public int getAvailableFloorTickets() {
- return availableFloorTickets;
- }
- /**
- * Gets total sales.
- *
- * @return the total sales
- */
- public double getTotalSales() {
- return this.totalSales;
- }
- /**
- * Gets month.
- *
- * @return the month
- */
- public int getMonth() {
- return this.month;
- }
- /**
- * Gets day.
- *
- * @return the day
- */
- public int getDay() {
- return this.day;
- }
- /**
- * Gets year.
- *
- * @return the year
- */
- public int getYear() {
- return this.year;
- }
- /**
- * Accepts date in String type then parses it
- * into its corresponding integer equivalent.
- * Uses substring(), not split().
- *
- * @param date the date
- */
- private void parseDate(String date) {
- int firstSlash = date.indexOf("/");
- int secondSlash = date.lastIndexOf("/");
- int dateMonth = Integer.parseInt(date.substring(0, firstSlash));
- int dateDay = Integer.parseInt(date.substring(firstSlash + 1, secondSlash));
- int dateYear = Integer.parseInt(date.substring(secondSlash + 1));
- if (isValidDate(dateMonth, dateDay, dateYear)) {
- this.month = dateMonth;
- this.day = dateDay;
- this.year = dateYear;
- } else {
- System.out.println("Invalid date provided.");
- }
- }
- /**
- * Checks if a given year is a leap year or not.
- *
- * @param y the year
- * @return True if year is a leap year, False otherwise.
- */
- private boolean isLeapYear(int y) {
- // Using ternary operator...
- return ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)));
- }
- /**
- * Checks if the given date is valid.
- *
- * @param m the month
- * @param d the day
- * @param y the year
- * @return True if year is a valid year, False otherwise.
- */
- private boolean isValidDate(int m, int d, int y) {
- final int[] DAYS = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
- if (m < 1 || m > 12) {
- return false;
- }
- if (d < 1 || d > DAYS[m]) {
- return false;
- }
- return m != 2 || d != 29 || isLeapYear(y);
- }
- /**
- * Edits given date string into readable format.
- *
- * @param format ranges from 1 to 4, each representing a valid date format
- *
- * @return the date String in given format
- */
- public String formatDate(int format) {
- String formattedDate = "";
- if (format == 1) {
- formattedDate = String.format("%d/%d/%d", this.getMonth(), this.getDay(),
- this.getYear());
- } else if (format == 2) {
- DecimalFormat df = new DecimalFormat("00");
- formattedDate = String.format("%s/%s/%s",df.format(this.getMonth()), df.format(day),
- df.format(year));
- } else if (format == 3) {
- final String months = "JanFebMarAprMayJunJulAugSepOctNovDec";
- formattedDate = "";
- } else { // if format == 4
- switch(this.getMonth()) {
- case 1:
- formattedDate = "January " + this.getDay() + ", " + this.getYear();
- break;
- case 2:
- formattedDate = "February " + this.getDay() + ", " + this.getYear();
- break;
- case 3:
- formattedDate = "March " + this.getDay() + ", " + this.getYear();
- break;
- case 4:
- formattedDate = "April " + this.getDay() + ", " + this.getYear();
- break;
- case 5:
- formattedDate = "May " + this.getDay() + ", " + this.getYear();
- break;
- case 6:
- formattedDate = "June " + this.getDay() + ", " + this.getYear();
- break;
- case 7:
- formattedDate = "July " + this.getDay() + ", " + this.getYear();
- break;
- case 8:
- formattedDate = "August " + this.getDay() + ", " + this.getYear();
- break;
- case 9:
- formattedDate ="September " + this.getDay() + ", " + this.getYear();
- break;
- case 10:
- formattedDate = "October " + this.getDay() + ", " + this.getYear();
- break;
- case 11:
- formattedDate = "November " + this.getDay() + ", " + this.getYear();
- break;
- default:
- formattedDate = "December " + this.getDay() + ", " + this.getYear();
- }
- }
- return formattedDate;
- }
- /**
- * Method that allows buying functionality.
- *
- * @param ticketType the type of ticket, either Floor, Lower, or Upper type
- * @param numTickets the number tickets to be bought
- * @param pmt the payment for the tickets bought
- */
- public void buyTickets(char ticketType, int numTickets, double pmt) {
- if (numTickets < 0 || pmt < 0) {
- System.out.println("Error - invalid (negative) number of tickets or payment");
- } else if (ticketType != 'F' && ticketType != 'L' && ticketType != 'U') {
- System.out.println("Invalid ticket type.");
- } else {
- double amountDue;
- if ((numTickets <= availableFloorTickets)
- || (numTickets <= availableLowerTickets)
- || (numTickets <= availableUpperTickets)) {
- amountDue = numTickets * getTicketPrice(ticketType);
- if (pmt >= amountDue) {
- if (ticketType == 'F') {
- availableFloorTickets -= numTickets;
- System.out.printf("Transaction: Number tickets Floor section : %d, total: $%," +
- ".2f\n", numTickets, amountDue);
- } else if (ticketType == 'L') {
- availableLowerTickets -= numTickets;
- System.out.printf("Transaction: Number tickets Lower section : %d, total: $%," +
- ".2f\n", numTickets, amountDue);
- } else {
- availableUpperTickets -= numTickets;
- System.out.printf("Transaction: Number tickets Upper section : %d, total: $%," +
- ".2f\n", numTickets, amountDue);
- }
- totalSales += amountDue;
- } else {
- System.out.println("Error – payment is not enough to buy the tickets.");
- } // end innermost else
- } else {
- if (ticketType == 'F') {
- System.out.println("Tickets not available in floor section.");
- } else if (ticketType == 'L') {
- System.out.println("Tickets not available in lower section.");
- } else {
- System.out.println("Tickets not available in upper section.");
- }
- } // end inner else
- } // end outer else
- }
- /**
- * Simulates a company buying a total number of individual tickets (numberTickets)
- * in different sections.
- *
- * Invokes the buyTickets() method.
- *
- * @param numberTickets the number tickets bought
- */
- public void simulateCompanyBuyingTickets(int numberTickets) {
- Random rand = new Random();
- char[] ticketSymbols = new char[] {'F','L', 'U'};
- char ticketType;
- for (int i = 0; i < numberTickets; i++) {
- ticketType = ticketSymbols[rand.nextInt(3)];
- buyTickets(ticketType, 1, this.getTicketPrice(ticketType));
- }
- }
- public void printReport() {
- NumberFormat currency = NumberFormat.getCurrencyInstance(Locale.US);
- int totalFloorTicketsSold = TOTAL_NUMBER_FLOOR_TICKETS - this.getAvailableFloorTickets();
- int totalUpperTicketsSold = TOTAL_NUMBER_UPPER_TICKETS - this.getAvailableUpperTickets();
- int totalLowerTicketsSold = TOTAL_NUMBER_LOWER_TICKETS - this.getAvailableLowerTickets();
- System.out.println("Concert Report");
- System.out.println("======================");
- System.out.println("Artist: \t\t" + this.getArtist());
- System.out.println("Venue: \t\t\t" + this.getVenue());
- System.out.println("Date: \t\t\t" + this.formatDate(4));
- System.out.println(); // prints whitespace, equivalent to println("\n")
- System.out.println("Tickets sold:");
- System.out.println("=============");
- System.out.println("Upper: " + totalUpperTicketsSold + "\t" +
- currency.format(totalUpperTicketsSold * PRICE_UPPER_TICKET));
- System.out.println("Lower: " + totalLowerTicketsSold + "\t" +
- currency.format(totalLowerTicketsSold * this.getTicketPrice('L')));
- System.out.println("Floor: " + totalFloorTicketsSold + "\t" +
- currency.format(totalFloorTicketsSold * PRICE_FLOOR_TICKET));
- System.out.println("=============");
- System.out.println("Total Sales: " + currency.format(this.getTotalSales()));
- } // end printReport()
- }
Advertisement
Add Comment
Please, Sign In to add comment