I have an array list holding booking information : customer number, date of booking, guests number and table number. I need to find a way how to edit an instance of a specific element from the array list. That is for example if I have the following bookings in the array list:
Customer Number........Date of Booking.........Guests Number........Table Number
..........002...................02/05/2009..........................4........................6
..........008...................10/05/2009..........................2........................4
..........011...................17/05/2009..........................3........................2
Now let's say I need to change the date of customer 008. How can I do that?
This is what I have managed to do up till now.
import java.io.*;
class NextBooking implements Serializable{
private String cust_num;
private String table_num;
private String date;
private int guests;
//constructor
public NextBooking(String cust_numb, String dateb, String tableb, int guestsb){
cust_num = cust_numb;
date = dateb;
table_num = tableb;
guests = guestsb;
}
public void setCust_Num(String cn){
cust_num = cn;
}
public String getCust_Num(){
return cust_num;
}
public void setTable_Num(String tn){
table_num = tn;
}
public String getTable_Num(){
return table_num;
}
public void setGuests(int g){
guests = g;
}
public int getGuests(){
return guests;
}
public void setDate(String d){
date = d;
}
public String getDate(){
return date;
}
public boolean equals( Object other){
return (getCust_Num().equals( ((NextBooking)other).getCust_Num()));
}
public boolean equals2( Object other){
return (getTable_Num().equals( ((NextBooking)other).getTable_Num()));
}
}
//modifying a booking
public static void modifyBooking(ArrayList<NextBooking>nextListIn){
int pos, spot3= -1;
boolean found = true;
int i;
int comparison2;
Scanner scanner = new Scanner(System.in);
scanner.useDelimiter("\n");
System.out.println("Enter Customer number to be modified or E to exit");
String tempnum = scanner.next();
comparison2 = tempnum.compareTo("E");
do
{
found = true;
spot3 = nextListIn.indexOf(new NextBooking(tempnum,null,null, 0));
if ((spot3 < 0) & (comparison2 != 0))
{
System.out.println("Customer with cust no. "+tempnum+ " does not exist ");
System.out.println("Enter customer number to be modified: ");
tempnum = scanner.next();
comparison2 = tempnum.compareTo("E");
found = false;
}
} while (found != true);
//display item to be modified
for (i=spot3; i < (spot3 + 1); i++) {
System.out.print(nextListIn.get(i).getCust_Num());
System.out.print(" ");
System.out.print(nextListIn.get(i).getDate());
System.out.print(" ");
System.out.print(nextListIn.get(i).getTable_Num());
System.out.print(" ");
System.out.println(nextListIn.get(i).getGuests());
}
System.out.println("What would you like to modify");
System.out.println("1.Date");
System.out.println("2.Guests");
int choice = scanner.nextInt();
}
Customer Number........Date of Booking.........Guests Number........Table Number
..........002...................02/05/2009..........................4........................6
..........008...................10/05/2009..........................2........................4
..........011...................17/05/2009..........................3........................2
Now let's say I need to change the date of customer 008. How can I do that?
This is what I have managed to do up till now.
import java.io.*;
class NextBooking implements Serializable{
private String cust_num;
private String table_num;
private String date;
private int guests;
//constructor
public NextBooking(String cust_numb, String dateb, String tableb, int guestsb){
cust_num = cust_numb;
date = dateb;
table_num = tableb;
guests = guestsb;
}
public void setCust_Num(String cn){
cust_num = cn;
}
public String getCust_Num(){
return cust_num;
}
public void setTable_Num(String tn){
table_num = tn;
}
public String getTable_Num(){
return table_num;
}
public void setGuests(int g){
guests = g;
}
public int getGuests(){
return guests;
}
public void setDate(String d){
date = d;
}
public String getDate(){
return date;
}
public boolean equals( Object other){
return (getCust_Num().equals( ((NextBooking)other).getCust_Num()));
}
public boolean equals2( Object other){
return (getTable_Num().equals( ((NextBooking)other).getTable_Num()));
}
}
//modifying a booking
public static void modifyBooking(ArrayList<NextBooking>nextListIn){
int pos, spot3= -1;
boolean found = true;
int i;
int comparison2;
Scanner scanner = new Scanner(System.in);
scanner.useDelimiter("\n");
System.out.println("Enter Customer number to be modified or E to exit");
String tempnum = scanner.next();
comparison2 = tempnum.compareTo("E");
do
{
found = true;
spot3 = nextListIn.indexOf(new NextBooking(tempnum,null,null, 0));
if ((spot3 < 0) & (comparison2 != 0))
{
System.out.println("Customer with cust no. "+tempnum+ " does not exist ");
System.out.println("Enter customer number to be modified: ");
tempnum = scanner.next();
comparison2 = tempnum.compareTo("E");
found = false;
}
} while (found != true);
//display item to be modified
for (i=spot3; i < (spot3 + 1); i++) {
System.out.print(nextListIn.get(i).getCust_Num());
System.out.print(" ");
System.out.print(nextListIn.get(i).getDate());
System.out.print(" ");
System.out.print(nextListIn.get(i).getTable_Num());
System.out.print(" ");
System.out.println(nextListIn.get(i).getGuests());
}
System.out.println("What would you like to modify");
System.out.println("1.Date");
System.out.println("2.Guests");
int choice = scanner.nextInt();
}
Last edited: