public class Reservation
{
private String firstname;
private String lastname;
private String address;
private String name;
private int licenseN;
private String date;
private int daysN;
private String type;
public Reservation(String f,String l,String a,int lpn,String dt,int nd,String tp)
{
firstname = f;
lastname = l;
address = a;
licenseN = lpn;
date = dt;
daysN = nd;
type = tp;
}
public String getName()
{
return firstname + lastname;
}
public String getAddress()
{
return address;
}
public int getLicenseN()
{
return licenseN;
}
public String getDate()
{
return date;
}
public int getDaysN()
{
return daysN;
}
public String getType()
{
return type;
}
public void setFirstname(String f)
{
firstname = f;
}
public void setLastname(String l)
{
lastname = l;
}
public void setAddress(String add)
{
address= add;
}
public void setLicenseN(int ln)
{
licenseN = ln;
}
public void setDate(String dt)
{
date = dt;
}
public void setDaysN(int dn)
{
daysN = dn;
}
public void setType(String tp)
{
type = tp;
}
public String toString()
{
return "Name: "+ name + "\n" + "Address: " + address + "\n" + "License Number: " + licenseN + "\n" + "Arrival Date: " + date + "\n" + "Number of days: " + daysN + "\n" + "type of camping site : " + type +"\n";
}
}
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
import java.util.Arrays;
public class ReservationBook
{
private Reservation[] data;
public Reservation[] readInfo(String filename) throws IOException
{
FileReader reader = new FileReader(filename);
try
{
Scanner in = new Scanner(reader);
int numberofR = in.nextInt();
data = new Reservation[numberofR];
for(int i =0;i < numberofR;i++)
{
String f = in.nextLine();
data[i].setFirstname(f);
String l = in.nextLine();
data[i].setLastname(l);
String ad = in.nextLine();
data[i].setAddress(ad);
int ls = in.nextInt();
data[i].setLicenseN(ls);
String dt = in.nextLine();
data[i].setDate(dt);
int d = in.nextInt();
data[i].setDaysN(d);
String tp = in.nextLine();
data[i].setType(tp);
}
}
finally
{
reader.close();
}
return data;
}
//readInfo(in,i);
/*private void readInfo(Scanner in,int i)throws IOException
{
}
*/
}
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;
public class ReservationReader
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
ReservationBook book = new ReservationBook();
try
{
System.out.println("Please enter the file name: ");
String filename = in.next();
Reservation[] data = book.readInfo(filename);
for(int i =0;i<data.length;i++)
System.out.println(data[i].toString());
}
catch(FileNotFoundException exception)
{
System.out.println("File not found");
}
catch(IOException exception)
{
exception.printStackTrace();
}
//for(int i =0;i<data.length;i++)
//data[i].sort();
/*System.out.println("Enter the name you want to search: ");
String n = in.nextLine();
data.search(n);
*/
}
}
This is the reservationlist file:
12
liu
yan
elizabeth
111
2001
1
service
yu
xi
halley
222
2002
2
unservice
jia
tianyang
allandale
333
2003
3
service
wu
junxi
newfoundland
444
2004
4
unservice
Thanks very much.