Hi, I am trying to create a reserve console app using Java. Any Help is appreciated.

The Params are:

Calculate reservation totals

Welcome to the Reservation Calculator

Enter the arrival month (1-12): 5

Enter the arrival day (1-31): 16

Enter the arrival year: 2016

Enter the departure month (1-12): 5

Enter the departure day (1-31): 18

Enter the departure year: 2016

Arrival Date: May 16, 2016

Departure Date: May 18, 2016

Price: $105.00 per night

Total price: $210.00 for 2 nights

Continue? (y/n): n

· This application calculates the charges for a stay at a hotel based on the arrival and departure dates.

· The application prompts the user for the month, day, and year of the arrival and the departure. Then, the application displays the arrival date, the departure date, the room rate, the total price, and the number of nights.

Specifications

· Create a class named Reservation that defines a reservation. This class should contain instance variables for the arrival date and departure date. It should also contain a constant initialized to the nightly rate of $105.00.

· The Reservation class should include the following methods:

setArrivalDate(LocalDate arrivalDate)

LocalDate getArrivalDate()

String getArrivalDateFormatted()

setDepartureDate(LocalDate departureDate)

LocalDate getDepartureDate()

String getDepartureDateFormatted()

int getNumberOfNights()

String getPricePerNightFormatted()

double getTotalPrice()

String getTotalPriceFormatted()

I have tried to get this to work but I am stuck:

CURRENT CODE:

import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.util.Scanner;
import java.time.LocalDateTime;
import java.time.LocalDate;    
import java.time.Month;
import java.text.DateFormat;

public class ResCalc {

private static Scanner sc = new Scanner(System.in);

public static void main(String[] args) {
    System.out.println("Welcome!");

    getArrivalDate();

    }

     public static void getArrivalDate(){
        String choice = "y";

  while (choice.equalsIgnoreCase("y")) {

        System.out.print("Enter the arrival month (1-12): ");
        int month = sc.nextInt();
        System.out.print("Enter the arrival day (1-31):  ");
        int day = sc.nextInt();
        System.out.print("Input arrival year: ");
        int year = sc.nextInt();
        System.out.println();

        System.out.println(setArrivalDate(day, month, year));

        choice = sc.nextLine();

        }
    } 

 public static void getDepartureDate(){
    String choice = "y";

    while (choice.equalsIgnoreCase("y")) {

        System.out.print("Enter the departure month (1-12): ");
        int month = sc.nextInt();
        System.out.print("Enter the departure day (1-31):  ");
        int day = sc.nextInt();
        System.out.print("Input departure year: ");
        int year = sc.nextInt();
        System.out.println();

        //Users choice
        choice = sc.nextLine();

        }

    }   

    public static String setArrivalDate(int month, int day, int year) {
    String monthStr = "";

    switch (month) {
        case 1:
            monthStr = "January";
            break;

        case 2:
            monthStr = "February";
            break;

        case 3:
            monthStr = "March";
            break;

        case 4:
            monthStr = "April";
            break;

        case 5:
            monthStr = "May";
            break;

        case 6:
            monthStr = "June";
            break;

        case 7:
            monthStr = "July";
            break;

        case 8:
            monthStr = "August";
            break;

        case 9:
            monthStr = "September";
            break;

        case 10:
            monthStr = "October";
            break;

        case 11:
            monthStr = "November";
            break;

        case 12:
            monthStr = "December";
            break;
    }

    return "Arrival Date: " + monthStr + day + year;
    }

     public static String getArrivalDateFormatted()
     {
    DateTimeFormatter df = DateTimeFormatter.ofLocalizedDate(
             FormatStyle.LONG);
     return df.format(setArrivalDate());
     }

    public static String setDepartureDate(int month, int day, int year) {
        String monthStr = "";
        switch (month) {
        case 1:
        monthStr = "January";
        break;

    case 2:
        monthStr = "February";
        break;

    case 3:
        monthStr = "March";
        break;

    case 4:
        monthStr = "April";
        break;

    case 5:
        monthStr = "May";
        break;

    case 6:
        monthStr = "June";
        break;

    case 7:
        monthStr = "July";
        break;

    case 8:
        monthStr = "August";
        break;

    case 9:
        monthStr = "September";
        break;

    case 10:
        monthStr = "October";
        break;

    case 11:
        monthStr = "November";
        break;

    case 12:
        monthStr = "December";
        break;
}

        return  monthStr + day +  year;
    } 

Not sure where to go from here. Been stuck for 4 hours.

Recommended Answers

All 3 Replies

It looks like you stopped reading the instructions about half way down. That's always a fatal mistake.

Go back to where it says "Specifications" and do what it says there.
Then write a main method that just does the user I/O and creates/uses an instance of the Reservation class.

The instructions also tell you to use LocalDate for the dates, so there's no reason for you to process month numbers/names yourself.
Hint:LocalDate.of(int year, int month, int dayOfMonth)

commented: Hi, James, I responded to your post below. Thank you for responding! +0

Hi, James, I scratched all the code above and have started over working my way down from the top. I have created the arrivaDate methods required just to start. Where does my actual user input and while loop go....which method do I put it in and how do I invoke the arrivalDate methods?

CURRENT CODE:

import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.util.Scanner;
import java.time.LocalDateTime;
import java.time.LocalDate;
import java.time.Month;
import java.text.DateFormat;
import java.text.NumberFormat;

public class ReservationCalculator {

private static Scanner sc = new Scanner(System.in);

public static void main(String[] args) {
     // display a welcome message
    System.out.println("Welcome to the Reservation Calculator");
    System.out.println();
}

private LocalDate arrivalDate;

//1st METHOD
public void setArrivalDate(LocalDate arrivalDate) {
    this.arrivalDate = arrivalDate;
}

//2ND METHOD
public LocalDate getArrivalDate() {
    return arrivalDate;
}

public String getArrivalDatFormatted() {

    DateTimeFormatter df = DateTimeFormatter.ofLocalizedDate(
             FormatStyle.LONG);
     return df.format(arrivalDate);
}

That's a lot better!
I don't want to give you your homework, so here's an outline that illustrates the sort of structure you can use.
Its an arithmetic app that just adds two numbers ;) but, like yours it needs some values to be set and a result to be queried.
The user input loop goes in the main method because that's what you want o see when the app starts.

class Adder {
   int a,b; 
   getters and setters for a and b
   public int getSum() {return a+b;}

   public static void main() {
      do {
         Adder myAdder = new Adder();
         ask user for value of first number
         myAdder.setA(first number);
         ditto for second number
         print myAdder.getSum()   
         ask user: more?
      } while(more);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.