0

I am a beginner in coding and I need to write a program in Java where a restaurant admin can input a date (January 10, 2022) for until when customer can book reservation at their restaurant and when a customer wants to book the restaurant after the date input by the admin (February 12, 2022), the customer needs to choose another date (it must be before or at January 10, 2022).

I read about the calendar class and time class, but still cannot figure it out. How do I do it? I am sorry I cannot show even the smallest part of my code about this program because I really don't know what to do.

import java.util.*;
public class BookRestaurant{
    public static void main(String[] args){
        Scanner key = new Scanner(System.in);
        String uName;
        String password;
        String dateByAdmin;
        String dateByCust;

        System.out.print("Enter username: ");
        uName = key.nextLine();
        System.out.print("Enter password: ");
        password = key.nextLine();
        switch (uName){
            case "admin":
                System.out.print("Enter until what date restaurant open(mm/dd/yyyy): ");
                dateByAdmin = key.nextLine();
            break;
            case "user":
                System.out.print("Enter book date(mm/dd/yyyy): ");
                dateByCust = key.nextLine();
                //this is where the customer need to get asked to enter another date
            break;
        }
        System.out.print("Thank You");
    }
}
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
sealuna
  • 1
  • 2
  • 1
    This sounds like homework. If so. I would presume your instructor has gone over the necessary parts of the language to do this. If you really don't know where to start I recommend you discuss this with them. But if you expect to get help here you need to make an attempt at writing the code. – WJS Nov 16 '21 at 02:24
  • I am sorry for not providing the code beforehand. and about the instructor, my instructor is prefer to explaining the lecture directly from powerpoint slide rather than by code example, so I cannot follow it as much, and she is so difficult to contact in this online study – sealuna Nov 16 '21 at 02:39
  • What relevant Java classes were covered on this powerpoint slides? – Basil Bourque Nov 16 '21 at 02:41
  • 1
    Duplicate: [*How to compare two string dates in Java?*](https://stackoverflow.com/q/25963720/642706) – Basil Bourque Nov 16 '21 at 02:42
  • Already on StackOverflow. [Link](https://stackoverflow.com/questions/2592501/how-to-compare-dates-in-java) – shen s Nov 16 '21 at 02:49
  • Don’t use the `Calendar` class you mentioned. It is poorly designed, cumbersome to work with and long outdated. Do use `LocalDate` from java.time, the modern Java date and time API. This is probably what you meant by the *time class* you mentioned. – Ole V.V. Nov 16 '21 at 05:36
  • You are really, though probably not intentionally, asking two questions in one: (1) how to parse a date string into an appropriate date-time object like `LocalDate`? (2) How to compare two such objects to see which one is earlier? – Ole V.V. Nov 16 '21 at 06:00

1 Answers1

1

java.time

I recommend that you use java.time, the modern Java date and time API, for your date work. You will need to read a string from the user the way you are already doing in your code and parse (convert) that string into a LocalDate object. We first declare a formatter to use for parsing:

private static final DateTimeFormatter DATE_FORMATTER 
        = DateTimeFormatter.ofPattern("M/d/u", Locale.US);

Now reading, parsing and comparing may go like this:

    Scanner key = new Scanner(System.in);
    
    System.out.println("Admin: Enter until what date restaurant open (mm/dd/yyyy): ");
    String dateByAdminString = key.nextLine();
    LocalDate lastReservationDate = LocalDate.parse(dateByAdminString, DATE_FORMATTER);

    System.out.println("Customer: Enter book date (mm/dd/yyyy):");
    String dateByCustString = key.nextLine();
    LocalDate bookDate = LocalDate.parse(dateByCustString, DATE_FORMATTER);
    while (bookDate.isAfter(lastReservationDate)) {
        System.out.println("Book date must not be after "
                + lastReservationDate.format(DATE_FORMATTER)
                + ". Enter an earlier date:");
        dateByCustString = key.nextLine();
        bookDate = LocalDate.parse(dateByCustString, DATE_FORMATTER);
    }

    System.out.println("Customer chose to book on " + bookDate);

Example session:

Admin: Enter until what date restaurant open (mm/dd/yyyy): 
01/10/2022
Customer: Enter book date (mm/dd/yyyy):
02/12/2022
Book date must not be after 1/10/2022. Enter an earlier date:
1/10/2022
Customer chose to book on 2022-01-10

It doesn’t make sense to accept a user date until the admin has entered a last booking date. How you will manage this in your code, I don’t know. Since my code is only there for demonstrating parsing and comparison, I have ignored the problem.

Tutorial link: Oracle Tutorial: Date Time

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161