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(); } } }