0

Thank you for looking at this. I am making a movie rental software. Where you can put in the information, then you can say how long you want to rent it. Well after you say how long the user needs to insert the current date. I am trying to get it where the user can input the current date in the format mm/dd/yyyy. I am also trying to get it where the output is in mm-dd-yyyy. How do I do this? Below is what I have so far. I currently have the format where it is (Name of Month) dd, yyyy. And the output is yyyy-mm-dd. I have used a few answers to different questions from here and it still did not help.

import java.util.*;
import java.text.*;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.util.Locale;
import java.time.format.FormatStyle;
import java.time.*;
import java.time.chrono.IsoChronology;
import java.text.SimpleDateFormat;

public class MovieRental
{
    private static final Locale defaultFormattingLocale = Locale.getDefault
    (Locale.Category.FORMAT);
    private static String defaultDateFormat = DateTimeFormatterBuilder.getLocalizedDateTimePattern
    (FormatStyle.LONG, null, 
            IsoChronology.INSTANCE, defaultFormattingLocale);
    private static final DateTimeFormatter dateFormatter 
    = DateTimeFormatter.ofPattern(defaultDateFormat, defaultFormattingLocale);
    public static void main(String[ ] args)
    {
        String format = DateTimeFormatterBuilder.getLocalizedDateTimePattern
        (FormatStyle.FULL, null, IsoChronology.INSTANCE, Locale.US);
        LocalDate today = LocalDate.now();
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("h:mm a");
        Scanner in = new Scanner(System.in);
        double rentalPrice = 1.25;
        double federalSalestax = 0.04;
        System.out.println("Hello, and thank you for using us to rent your movie.");
        System.out.println("Please enter your first and last name: ");
        String firstName = in.next();
        String lastName = in.nextLine();
        char firstIn = firstName.charAt(0);
        System.out.println("What movie would you like to rent?");
        String movieTitle = in.nextLine();
        //I used "Halo: The Fall of Reach" as an example. It is a real moviebased on Halo.
        System.out.println("How many days are you wanting to keep \"" + movieTitle + "\"? ");
        String rentalLength = in.nextLine();
        LocalDate dueDate
        = (LocalDate.now().plusDays(Integer.parseInt(rentalLength)));
        
    
        System.out.println("Enter date in " + defaultDateFormat + " format: " );
        //This says Month dd, 2021. I need to change it to mm/dd/yyyy.
        String dateString = in.nextLine();
        try {
        LocalDate inputDate = LocalDate.parse(dateString, dateFormatter);
        System.out.println("Date entered was " + inputDate);
        } catch (Exception dtpe) {
        System.out.println("Invalid date: " + dateString);
        }
        System.out.println("Please enter your card number: ");
        String cardNumber = in.nextLine();
        String censoredCard = cardNumber.substring(10);
        System.out.println("Please enter your PIN number: ");
        String pinNumber = in.nextLine();
        double totalPrice = (rentalPrice * Integer.parseInt(rentalLength));
        double taxedPrice = ((totalPrice * federalSalestax) + totalPrice);
        
        System.out.println("Account Charged: #####-###-" + censoredCard);
        System.out.println("Price: $" + totalPrice);
        System.out.println("Due Date: " + dueDate);
        System.out.println("Due Time: " + sdf.format(date));
        System.out.println("Thank you for your purchase.\nEnjoy your rental!");
    }
}

Below is an example of the output

Hello, and thank you for using us to rent your movie.
Please enter your first and last name: 
Nate Dillard
What movie would you like to rent?
Halo: The Fall of Reach
How many days are you wanting to keep "Halo: The Fall of Reach"? 
4
Enter date in MMMM d, y format: 
November 1, 2021
Date entered was 2021-11-01
Please enter your card number: 
87430-736-3439
Please enter your PIN number: 
1764
Account Charged: #####-###-3439
Price: $5.0
Due Date: 2021-11-05
Due Time: 4:20 PM
Thank you for your purchase.
Enjoy your rental!

This is an unfinished project where the bottom portion(after I get this figured out with your help) will be formatted as an e-receipt.

  • 1
    Since you can use java.time, the modern Java date and time API, don’t also mix in the outdated and troublesome classes `Date` and `SimpleDateFormat` from Java 1.0 and 1.1. They will only add needless complication and unnecessary conversions. I recommend you throw them out of your code immediately. – Ole V.V. Nov 02 '21 at 06:21
  • Is there any point in having the user enter the current date? You are already getting it from `LocalDate.now()`. – Ole V.V. Nov 02 '21 at 06:26
  • 1
    For output in `mm-dd-yyyy` format define a formatter `DateTimeFormatter.ofPattern("MM-dd-uuuu")` and pass it to `LocalDate.format()`. – Ole V.V. Nov 02 '21 at 06:30

0 Answers0