Provide the appropriate methods to set and get data for each of these class vari
ID: 3856570 • Letter: P
Question
Provide the appropriate methods to set and get data for each of these class variables. For example setScheduleDate(string scheduleDate) and string getScheduleDate().
The main program must provide the following functionality:
1. When the program is first started, it must read a data file called schedule.dat. The program will not prompt the user for the name of the data file. The name of the file must be hard-coded in the program. If the file exists, the program will load the data for each schedule into the global linked list. If the file does not exist, the program will start with an empty linked list.
2. The program will provide a simple text-based user interface that manages the all of the train schedules within a linked list. Each schedule must be placed in the linked list as an object that holds all of the attributes associated with it as mentioned above. The user interface will allow the user to perform the following:
. (a) EnterSchedule – allows the user to enter all of the fields associated with a given train schedule, except for the scheduleId, which will be automatically generated by the program as previously mentioned. After the fields are entered, the program will place the schedule object in the global linked list.
. (b) DisplayAllSchedules – displays all of the schedules within the linked list along with their associated fields. In addition, this option must print the total number of schedules in the linked list.
. (c) SearchforSchedule – allows the user to find a schedule by its schedule identifier. The program will prompt the user to enter the scheduleId and will display all of the fields associated with the given schedule, if it is found. The program must display an error message if the schedule is not found in the linked list.
. (d) EditSchedule – allows the user to edit the fields for a given schedule that is in the linked list. The program must prompt the user to enter the scheduleId as the key to find the schedule to edit. Print a message if the schedule is not found in the linked list. For simplicity, the program may re-prompt the user to re-enter all of the fields associated with the given schedule; however, it must reuse the scheduleId value.
. (e) DeleteSchedule–allows the user to delete a schedule from the linked list using the scheduleId as the key. The program must display a message if the provided scheduleId does not find an associated schedule in the linked list.
. (f) ExitSystem – before the program exits, it must save all of the data in the linked list to the data file. I recommend using a standard text file with one field in the object per line. At this point, if the file does not exist, the program will create it.
Explanation / Answer
Schedule.java
package car;
public class Schedule {
String tname;
int tnumber;
String arivaldate;
String dispdate;
Schedule(){
}
Schedule(String tname,int tnumber,String arivaldate,String dispdate){
this.tname=tname;
this.tnumber=tnumber;
this.arivaldate=arivaldate;
this.dispdate=dispdate;
}
public void settname(String tname){
this.tname=tname;
}
public void settnumber(int tnumber){
this.tnumber=tnumber;
}
public void setarivaldate(String arivaldate){
this.arivaldate=arivaldate;
}
public void setdispdate(String dispdate){
this.dispdate=dispdate;
}
public String tostring(){
return this.tname+" "+this.tnumber+" "+this.arivaldate+" "+this.dispdate;
}
public String gettname(){
return this.tname;
}
public int gettnumber(){
return this.tnumber;
}
public String getarivaldate(){
return this.arivaldate;
}
public String getdispaturedate(){
return this.dispdate;
}
}
Schedulemain.java
package car;
import java.text.SimpleDateFormat;
import java.util.*;
import java.io.*;
public class Schedulemain {
static List<Schedule>slist=new LinkedList<Schedule>();
static Scanner sc=new Scanner(System.in);
public static void main(String[] args) {
try{
String sss="scheduless";
BufferedReader br=new BufferedReader(new FileReader(new File("src/car/"+sss)));
String line="";
while((line=br.readLine())!=null){
String sp[]=line.split(" ");
String name=sp[0];
int number=Integer.parseInt(sp[1]);
String arvdate=sp[2];
String dispdate=sp[3];
Schedule s=new Schedule(name,number,arvdate,dispdate);
slist.add(s);
}
br.close();
}catch(Exception e){
e.printStackTrace();
}
while(true){
System.out.println("1.enterSchedule 2.DisplayallSchedules 3.editSchedules 4.searchSchedule 5.deleteScedule 6.exitschdule ");
int c=sc.nextInt();
switch(c){
case 1:enterSchedule();
break;
case 2:DisplayAllSchedules();
break;
case 3:editSchedule();
break;
case 4:searchforSchedule();
break;
case 5:deleteSchedule();
break;
case 6:ExitSystem();
System.exit(0);
break;
}
}
}
public static void enterSchedule(){
System.out.println("enter train name:");
String name=sc.next();
System.out.println("enter train number:");
int num=sc.nextInt();
System.out.println("enter arival date:");
String avdate=sc.next();
System.out.println("enter dispature date");
String dispdate=sc.next();
Schedule ss=new Schedule(name,num,avdate,dispdate);
boolean b=slist.add(ss);
if(b==true)
System.out.println("succefully object stored....");
else
System.out.println("unsuccessfull in storing object....");
}
public static void DisplayAllSchedules(){
for(Schedule s:slist)
System.out.println(s.tostring());
System.out.println("total number of schedules are"+slist.size());
}
public static void searchforSchedule(){
int count=0;
System.out.println("enter train number to serch:");
int num=sc.nextInt();
for(Schedule s:slist){
if(s.gettnumber()==num){
System.out.println(s.tostring());
count++;
}
if(count==0)
System.out.println("schedule is not there in list");
}
}
public static void editSchedule(){
System.out.println("what you want to edit 1.arival date 2. dipature time");
int ch=sc.nextInt();
System.out.println("enter train number to edit:");
int num=sc.nextInt();
if(ch==1){
for(Schedule z:slist){
if(z.gettnumber()==num){
System.out.println("enter new arival date:");
String atime=sc.next();
z.setarivaldate(atime);
break;
}
}
}else if(ch==2){
for(Schedule z:slist){
if(z.gettnumber()==num){
System.out.println("enter new dipature date:");
String dtime=sc.next();
z.setdispdate(dtime);
break;
}
}
}else
System.out.println("invalid choice:");
}
public static void deleteSchedule(){
System.out.println("enter train number to delete :");
int num=sc.nextInt();
for(int i=0;i<slist.size();i++){
Schedule z=(Schedule)slist.get(i);
if(z.gettnumber()==num){
slist.remove(i);
System.out.println("succefully deleted");
break;
}
}
}
public static void ExitSystem(){
try{
String filess="sout";
BufferedWriter bw=new BufferedWriter(new FileWriter(new File("src/car/"+filess)));
for(Schedule s:slist)
{
bw.write(s.tostring());
System.out.println(s.tostring());
bw.newLine();
}
bw.close();
}catch(Exception e)
{
e.printStackTrace();
}
}
}
output:
1.enterSchedule
2.DisplayallSchedules
3.editSchedules
4.searchSchedule
5.deleteScedule
6.exitschdule
1
enter train name:
hango
enter train number:
1009
enter arival date:
1-1-17
enter dispature date
2-1-17
succefully object stored....
1.enterSchedule
2.DisplayallSchedules
3.editSchedules
4.searchSchedule
5.deleteScedule
6.exitschdule
2
cono 1001 12-11-17 1-12-17
logor 1002 11-11-17 12-12-17
difer 1003 9-12-17 10-112-17
hango 1009 1-1-17 2-1-17
total number of schedules are4
1.enterSchedule
2.DisplayallSchedules
3.editSchedules
4.searchSchedule
5.deleteScedule
6.exitschdule
3
what you want to edit
1.arival date
2. dipature time
2
enter train number to edit:
1002
enter new dipature date:
1-1-17
1.enterSchedule
2.DisplayallSchedules
3.editSchedules
4.searchSchedule
5.deleteScedule
6.exitschdule
2
cono 1001 12-11-17 1-12-17
logor 1002 11-11-17 1-1-17
difer 1003 9-12-17 10-112-17
hango 1009 1-1-17 2-1-17
total number of schedules are4
1.enterSchedule
2.DisplayallSchedules
3.editSchedules
4.searchSchedule
5.deleteScedule
6.exitschdule
5
enter train number to delete :
1002
succefully deleted
1.enterSchedule
2.DisplayallSchedules
3.editSchedules
4.searchSchedule
5.deleteScedule
6.exitschdule
2
cono 1001 12-11-17 1-12-17
difer 1003 9-12-17 10-112-17
hango 1009 1-1-17 2-1-17
total number of schedules are3
1.enterSchedule
2.DisplayallSchedules
3.editSchedules
4.searchSchedule
5.deleteScedule
6.exitschdule
4
enter train number to serch:
1001
cono 1001 12-11-17 1-12-17
1.enterSchedule
2.DisplayallSchedules
3.editSchedules
4.searchSchedule
5.deleteScedule
6.exitschdule
6
cono 1001 12-11-17 1-12-17
difer 1003 9-12-17 10-112-17
hango 1009 1-1-17 2-1-17
inputfile:scheduless.txt
cono 1001 12-11-17 1-12-17
logor 1002 11-11-17 12-12-17
difer 1003 9-12-17 10-112-17
output file:sout.txt
cono 1001 12-11-17 1-12-17
difer 1003 9-12-17 10-112-17
hango 1009 1-1-17 2-1-17
comments
every thing is done ;
choice is taken in whilelop
after exiting the data is writen into soutfile.txt
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.