Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Having trouble working with Gregorian dates and object date

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Having trouble working with Gregorian dates and object date

    So far, I have two classes - a reservation class & a main class. Essentially, I want the user to enter two dates: an arrival and departure date. From the two dates, I will do a calculation (num days & price). I am having trouble finding a way to let users enter the (preferably in int, but that wasnt working),use getTime() to convert it from a calender to a date object. Then I want to use the dates to do a calculation. In addition, my constructor is being funky.

    I'm new to Java and seem to grasp everything else pretty quickly, but for some reason I cannot wrap my head around this.

    package hotelreservation;
    import java.util.Date;
    import java.util.Calendar;
    import java.math.*;
    //import java.text.*;

    public class Reservation
    {
        //instance variables
        //private ArrayList<>rooms;
        private Date arrivalDate;
        private Date departureDate;
        private int stay;
        private BigDecimal price;
        private final double nightlyRate = 115.00;
     
        //constructors
        Reservation(Date arrivalDate, Date departureDate)
        {
               this.arrivalDate = arrivalDate;
    this.departureDate = departureDate; 
        }
     
        //gets and sets
        public Date getArrivalDate()
        {
            return arrivalDate;
        }
     
        public void setArrivalDate(Date a)
        {
            arrivalDate = a;
        }
     
        public Date getDepartureDate()
        {
            return departureDate;
        }
     
        public void setDepartureDate(Date d)
        {
            departureDate = d;
        }
     
        //Calculate difference in days
        public int calculateStay(int stay)
        {
           long difference = departureDate.getTime() - arrivalDate.getTime();
           stay = (int)difference/(1000*60*60*24);
           return stay;
        }
     
        //calculate price of stay
        public BigDecimal calculatePrice(BigDecimal price)
        {
            BigDecimal nRate = new BigDecimal(Double.toString(nightlyRate));
            price  = nRate.multiply(new BigDecimal(stay));
            return price;
        }
     
         //override the toString method
        @Override
        public String toString ()
        {
            return "Arrival Date: " + arrivalDate + "\n" + "Departure Date: " + 
            departureDate + "\n"  + "Price: " + nightlyRate + " per night \n" + 
            "Total price: " + price + " for " + stay + "nights \n";
        }
     
    }
    package hotelreservation;
    import java.text.*;
    import java.text.*;
    import java.util.Calendar;
    import java.util.Date;
    import java.util.Scanner;
    import java.util.GregorianCalendar;
    import java.util.logging.Level;
    import java.util.logging.Logger;
     
    /**
     *
     * @author 
     */
    public class HotelReservation 
    {
        public static void main(String[] args) 
        {
            //Introduction
            System.out.println("Welcome to the Reservation Calculator");
            System.out.println("-----------------------------------------");
     
            //intialize variable
            GregorianCalendar arrival_date = null;
            GregorianCalendar departure_date;
            String aDate;
            Reservation r = new Reservation();
     
            Scanner sc = new Scanner(System.in);
            String choice = "Y";
            while (choice.equalsIgnoreCase("y"))
            {
                String pattern = "mm/dd/yyyy";
                DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
                Date date = null;
     
                try 
                {
                    System.out.print ("Enter arrival date: ");
                    pattern = sc.nextLine();
                    date = df.parse(pattern);
                }
                catch (ParseException ex) 
                {
                    System.out.println("Could not parse string.");
                    continue;
                }
                Calendar cal = Calendar.getInstance();
                cal.setTime(date);
                r.setArrivalDate(date);
     
                System.out.print("Continue (y/n): ");
                choice = sc.next();
            }
        }
     
    }


  2. #2
    Member
    Join Date
    Feb 2012
    Posts
    173
    Thanks
    6
    Thanked 10 Times in 10 Posts

    Default Re: Having trouble working with Gregorian dates and object date

    Look up the Scanner class and/or JOptionPane

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,718
    My Mood
    Mellow
    Thanks
    231
    Thanked 706 Times in 692 Posts

    Default Re: Having trouble working with Gregorian dates and object date

    I'm having trouble . . . In addition, my constructor is being funky.
    You'll have to describe what those mean.

    As for entering the dates: If you stick with console entry, you can instruct the user how to enter the date, say "MM/DD/YYYY", validate that the entry is correct, and then use String methods and/or parse tools to extract the needed parts.

  4. #4
    Junior Member
    Join Date
    Feb 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Having trouble working with Gregorian dates and object date

    Sorry,

    Here is what I'm trying to do:
    - Have user enter arrival date of their hotel visit and expected departure date
    - Calculate charges based on those dates (fixed $115 per night)
    - format to enter date is mm/dd/yyyy
    - The application will then display the arrival date, the departure date, the room rate, the total price, and the number of nights.

  5. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,718
    My Mood
    Mellow
    Thanks
    231
    Thanked 706 Times in 692 Posts

    Default Re: Having trouble working with Gregorian dates and object date

    And what's happening instead? What do you need help with?

    Are we to run it, find the problems, and fix it for you, or will you describe which parts from the list above your code does NOT accomplish correctly and ask specific questions about how to fix it? The latter approach is more likely to get you help.

    For example, you might say: The user enters an arrival date and departure date, but my program always calculates the length of stay as 0 days. What am I doing wrong?

    Good luck.

Similar Threads

  1. Java Date Format in Date Object
    By Ashr Raza in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 13th, 2012, 09:47 AM
  2. problem Comparing date and month from dates
    By desire7696 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 6th, 2012, 10:08 AM
  3. Working out the day from numeric date (dd/mm/yy format).
    By ShaunB in forum Java Theory & Questions
    Replies: 6
    Last Post: April 23rd, 2011, 08:55 PM
  4. Replies: 9
    Last Post: March 16th, 2011, 11:35 AM
  5. I'm having trouble with date and calendar class
    By kiph in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 7th, 2010, 02:56 AM