Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

JAVA GUI Swing Problem: A company BHC provides tours during the summer months of each year(open June 1st and close Oct 1st). Three tours options:

JAVA GUI Swing Problem:

A company BHC provides tours during the summer months of each year(open June 1st and close Oct 1st). Three tours options: the Gardiner Lake tour is 3 or 5 days, and the cost is $40/day; the Hellroaring Plateau is 2, 3, or 4 days and is $35/day; the Beaten Path trip is 5 or 7 days and is $45/day. Make a window (a JFrame) that will hold controls for allowing a user to set the begin date and duration of a potential tour. You should have controls for

1.Setting the beginning date... Year Month Day

2.Setting the duration of the hike (note that different hikes have different duration options)

3.Submitting the query (use a JButton)

4.Showing the results (DON'T use a JButton): Total Cost

Given te following code as a guide, finish this GUI. Error check/feedback is necessary.

For exmaple, in the GUI window, user can enter 7/1/2008 for start date and 7/7/2008 for ending date. GUI check the date is valid (between June 1st and Oct 1st).

User choose one of three tour opinions:GARDINER, HELLROARING or BEATEN. GUI check the duration is ok or not(GARDINER is 3 or 5 days, HELLROARING is 2,3 or 4 days, BEATEN is 5 or 7 days). Finally, calcualte the total cost and return the cost to user. The following codes have finished all the requirements, but it needs GUI to allow user to enter information and return a cost.

import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.*;

/** Rates is an object that provides a cost for a booked tour. * There is a base rate and a premium rate that are used to calculate * the cost of the tour. Weekdays use the base rate, weekends use the * premium rate. * * The premium rate is automatically generated as 1.5x the base rate * */ public class Rates { public static enum HIKE {GARDINER, HELLROARING, BEATEN}; private HIKE hike; // flag to indicate total rate and day types need to be recalculated private boolean synched=false; // weekday rate private int baseRate = -1; private int premiumRate = -1; // cached calculated rate value private int rate = 0; // start of trip private BookingDay beginDate = null; // end of trip private BookingDay endDate = null; // Format the date to something readable private final static DateFormat FORMAT = DateFormat.getDateInstance(); // cached number of weekend days int premiumDays = 0; // cached number of weekday days int normalDays = 0;

// we open June 1st private int seasonStartMonth = 6; private int seasonStartDay = 1;

// we close Oct 1st. private int seasonEndMonth = 10; private int seasonEndDay = 1; private String details = "none"; private int[] validDurations = null;

public Rates(HIKE hike ) { this.hike = hike; switch(hike) { case GARDINER: baseRate=4000; validDurations = new int[2]; validDurations[0] = 3; validDurations[1] = 5; break; case HELLROARING: baseRate=3500; validDurations = new int[3]; validDurations[0] = 2; validDurations[1] = 3; validDurations[2] = 4; break; case BEATEN: baseRate=4500; validDurations = new int[2]; validDurations[0] = 5; validDurations[1] = 7; break; } premiumRate = baseRate + (baseRate / 2); }

/** Get the total cost for the trip. Returns -0.01 is something is amiss. * * @return the cost of the trip */ public double getCost() { if (synched) { return rate / 100.0; } else { return calculateCost() / 100.0; } }

/** Get the base rate for a weekday @return the weekend rate */ public double getBaseRate() { return baseRate / 100.0; }

/** Get the base rate for a weekend @return the weekend rate */ public double getPremiumRate() { return premiumRate / 100.0; }

/** Set the starting day for the season. This is used to validate booking * dates. Default is June 1st. * @param month (1-Jan, 12-Dec) * @param day */ public void setSeasonStart(int month, int day) { seasonStartMonth = month; seasonStartDay = day; synched = false; }

/** Set the ending day for the season. This is used to validate booking * dates. Default is October 1st. * @param month (1-Jan, 12-Dec) * @param day */ public void setSeasonEnd(int month, int day) { seasonEndMonth = month; seasonEndDay = day; synched = false; }

/** Determine if the entered dates are valid * * @return true if both days exist and the begin date is before the end date */ public boolean isValidDates() { if (beginDate == null || endDate == null) { details = "One of the dates was not defined"; return false; } else if (beginDate.getYear() != endDate.getYear()) { details = "The begin and end date must be within the same year"; return false; } else if (beginDate.equals(endDate)) { details = "The begin and end date must not be the same date"; return false; } else if (beginDate.isValidDate() && endDate.isValidDate()) { if (!beginDate.after(endDate)) { if ((!beginDate.before(seasonStartMonth, seasonStartDay)) && (!endDate.after(seasonEndMonth, seasonEndDay))) { details = "valid dates"; return true; } else { details = "begin or end date was out of season"; return false; } } else { details = "end date was before begin date"; return false; } } else { details = "One of the dates was not a valid day"; return false; } }

/** Status of validation of data. This is filled in once isValidDates() is called * * @return A String indicating the status of the input data validity */ public String getDetails() { return details; } // calculate the cost, returns -1 if dates are bad // bug fix courtesy of Zachary Schmook! 8/7/2010 private int calculateCost() { if (! isValidDates() ) { return -1; } premiumDays = 0; normalDays=0;

// Okay, if we got to here, the dates are somewhat sane... GregorianCalendar day = beginDate.getDate(); GregorianCalendar endDay = endDate.getDate(); while (day.before(endDay)) { int dayOfWeek = day.get(Calendar.DAY_OF_WEEK); if (dayOfWeek == Calendar.SATURDAY || dayOfWeek == Calendar.SUNDAY ) { premiumDays++; } else { normalDays++; } day.add(Calendar.DAY_OF_WEEK, 1); } if (endDay.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY || endDay.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) { premiumDays++; } else { normalDays++; } // System.out.println("normal days = " + normalDays + ", premium days = " + premiumDays); rate = normalDays * baseRate + premiumDays * premiumRate; synched = true; // System.out.println("baseRate = " + baseRate + ", premiumRate = " + premiumRate + ", rate = " + rate); return rate; }

/** Get the number of weekdays in the reservation * * @return number of weekdays in the reservation */ public int getNormalDays() { if (!synched) { calculateCost(); } return normalDays; }

/** Get the number of weekend days in the reservation * * @return number of weekend days in the reservation */ public int getPremiumDays() { if (!synched) { calculateCost(); } return premiumDays; }

/** Get the beginning date of the reservation * * @return a GregorianCalendar object of the beginning date */ public GregorianCalendar getBeginDate() { if (beginDate == null) { return null; } else { return beginDate.getDate(); } }

/** Get the end date of the reservation * * @return a GregorianCalendar object of the end date */ public GregorianCalendar getEndDate() { if (endDate == null) { return null; } else { return endDate.getDate(); } }

/** Get the beginning date of the reservation * * @return a BookingDay object of the beginning date */ public BookingDay getBeginBookingDay() { return beginDate; }

/** Get the end date of the reservation * * @return a GregorianCalendar object of the end date */ public BookingDay getEndBookingDay() { return endDate; }

/** Set the beginning date of the reservation * * @param beginDate the beginning date */ public void setBeginDate(BookingDay beginDate) { this.beginDate = beginDate; synched = false; }

// Get the valid durations for this hike. public int[] getDurations() { return validDurations; } /** Set the duration of the reservation. One day hikes means no overnight, * a two day hike is two days, one night. * * @param int the duration of the hike */ public boolean setDuration(int days) { boolean valid = false; for (int d : validDurations) { if (days == d) { valid = true; break; } } if (!valid) { return false; } else { // first a quick check to see if this is a valid GregorianCalendar day = beginDate.getDate(); day.add(Calendar.DAY_OF_MONTH, days - 1); endDate = new BookingDay(day.get(Calendar.YEAR), day.get(Calendar.MONTH) + 1, day.get(Calendar.DAY_OF_MONTH)); synched = false; return true; } }

/** Set the end date of the reservation * * @param endDate the end date */ public void setEndDate(BookingDay endDate) { this.endDate = endDate; synched = false; }

/** Quick test of the class */ public static void main(String[] argv) { BookingDay startDay = new BookingDay(2008,7,1); BookingDay endDay = new BookingDay(2008,7, 7); System.out.println("start Day of " + startDay + " " + (startDay.isValidDate()?"is valid":"is not valid")); System.out.println("end Day " + endDay + " " + (endDay.isValidDate()?"is valid":"is not valid"));

Rates rates = new Rates(HIKE.BEATEN);

rates.setBeginDate(startDay); boolean success = rates.setDuration(7); System.out.println("duration was " + (success ? "good" : "bad")); System.out.println("valid Dates = " + rates.isValidDates()); if (rates.isValidDates()) { System.out.println("Cost of trip = " + rates.getCost()); System.out.println("Weekdays: " + rates.getNormalDays()); System.out.println("Weekends: " + rates.getPremiumDays()); } else { System.out.println("Sorry, but " + rates.getDetails()); } } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

PostgreSQL Up And Running A Practical Guide To The Advanced Open Source Database

Authors: Regina Obe, Leo Hsu

3rd Edition

1491963417, 978-1491963418

More Books

Students also viewed these Databases questions

Question

Explain budgetary Control

Answered: 1 week ago

Question

Solve the integral:

Answered: 1 week ago

Question

What is meant by Non-programmed decision?

Answered: 1 week ago

Question

What are the different techniques used in decision making?

Answered: 1 week ago

Question

What is the Definition for Third Normal Form?

Answered: 1 week ago

Question

Provide two examples of a One-To-Many relationship.

Answered: 1 week ago