i'm trying to allow a customer to set the date and time they would want to make a reservation. The code which i have already completed creates lots of text in the text file when saved and crashes when you try to load it again.
This is my code for adding a reservation:
public static List<Reservation> addReservations(List<Reservation> reservations, List<Customer> customers) {
int newReservationId = Reservation.getNumberOfReservations() + 1;
String startString = readString("Enter Reservation date");
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
Date date = null;
try {
date = sdf.parse(startString);
} catch (ParseException ex) {
Logger.getLogger(ProjectIncrement5.class.getName()).log(Level.SEVERE, null, ex);
}
Calendar dateTime = Calendar.getInstance();
dateTime.setTime(date);
listCustomers(customers, reservations);
int reservationCustomerId = readInt("Enter Customer Id", Customer.getNumberOfCustomers(), 1);
Customer reservationCustomer = customers.get(reservationCustomerId - 1);
Reservation res = new Reservation(newReservationId, dateTime, reservationCustomer);
reservations.add(res);
return reservations;
}
Reservation Class:
public class Reservation {
private int reservationId;
private Calendar dateTime;
private Customer customer;
private static int numberOfReservations = 0;
public Reservation() {
this.reservationId = 0;
this.dateTime = null;
this.customer = null;
numberOfReservations++;
}
public Reservation(int reservationId, Calendar dateTime, Customer customer) {
this.reservationId = reservationId;
this.dateTime = dateTime;
this.customer = customer;
numberOfReservations++;
}
public static int getNumberOfReservations() {
return numberOfReservations;
}
public int getReservationId() {
return reservationId;
}
public void setreservationId(int reservationId) {
this.reservationId = reservationId;
}
public Calendar getDateTime() {
return dateTime;
}
public Customer getCustomer()
{
return customer;
}
public void setCustomer(Customer customer)
{
this.customer = customer;
}
public String setDateTime() {
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
String dateString = formatter.format(this.dateTime.getTime());
return dateString;
}
@Override
public String toString() {
return "Reservation id: " + getReservationId() + ", "
+ "Date/Time " + setDateTime() +
"customer: " + getCustomer();
}
}
This is what saves into the text file causing the error:
1:java.util.GregorianCalendar[time=851385600000,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Europe/London",offset=0,dstSavings=3600000,useDaylight=true,transitions=242,lastRule=java.util.SimpleTimeZone[id=Europe/London,offset=0,dstSavings=3600000,useDaylight=true,startYear=0,startMode=2,startMonth=2,startDay=-1,startDayOfWeek=1,startTime=3600000,startTimeMode=2,endMode=2,endMonth=9,endDay=-1,endDayOfWeek=1,endTime=3600000,endTimeMode=2]],firstDayOfWeek=2,minimalDaysInFirstWeek=4,ERA=1,YEAR=1996,MONTH=11,WEEK_OF_YEAR=52,WEEK_OF_MONTH=4,DAY_OF_MONTH=24,DAY_OF_YEAR=359,DAY_OF_WEEK=3,DAY_OF_WEEK_IN_MONTH=4,AM_PM=0,HOUR=0,HOUR_OF_DAY=0,MINUTE=0,SECOND=0,MILLISECOND=0,ZONE_OFFSET=0,DST_OFFSET=0]:4:<
I think the error occurs when getting the instance of the calendar but I'm am unsure of how to do it an easier way and fix this problem. Can anyone help?
java.util.Date,java.util.Calendar, andjava.text.SimpleDateFormatare now legacy, supplanted by the java.time classes. See Tutorial by Oracle. - Basil Bourque