Question: PLEASE HELP Please fix the code bellow in java (eclipse) Why this code do not work? The output the table with name, date, age, country
PLEASE HELP
Please fix the code bellow in java (eclipse)
Why this code do not work?
The output the table with name, date, age, country etc.
How to change it in Java:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
public class Travel {
static ArrayList
public static void main(String[] args) {
InputStreamReader inp = null;
BufferedReader input = null;
int nOption = 0;
try {
inp = new InputStreamReader(System.in);
input = new BufferedReader(inp);
while (true) {
System.out.println("1. Enter Passenger(s)");
System.out.println("2. Modify Passenger");
System.out.println("3. Delete Passenger");
System.out.println("4. Show all Passengers");
System.out.println("5. Exit.");
System.out.println(" Choose an option(1-5) : ");
nOption = Integer.parseInt(input.readLine());
switch (nOption) {
case 1:
addPassenger(input);
break;
case 2:
modifyPassenger(input);
break;
case 3:
deletePassenger(input);
break;
case 4:
showAllPassengers();
break;
case 5:
System.out.println("Exiting program.");
System.exit(0);
break;
}
}
} catch (Exception exp) {
}
}
private static void addPassenger(BufferedReader input) throws IOException {
PassengerTemplate tmpObject = null;
while (true) {
tmpObject = new PassengerTemplate();
System.out.println("Enter the name: ");
tmpObject.Name = input.readLine().toString();
System.out.println("Enter the date of travel: " + tmpObject.Name + "(Sun,Mon,Tue....): ");
tmpObject.Day = input.readLine().toString();
System.out.println("Enter the age: ");
tmpObject.Age = input.readLine().toString();
System.out.println("Do you have any health problems? (yes/no)");
tmpObject.Prob = input.readLine().toString();
System.out.println("Enter the country: ");
tmpObject.Country = input.readLine().toString();
System.out.println("Enter the passport number: ");
tmpObject.Passport = input.readLine().toString();
if (tmpObject != null)
ListPassengers.add(tmpObject);
System.out.println(" Do you want to add another Passenger?(y/n): ");
if (!input.readLine().toLowerCase().equals("y"))
break;
}
}
private static void ModifyPassenger(BufferedReader input) throws IOException
{
PassengerTemplate tmpObject = null;
System.out.println("Passenger's name to modify: ");
String OldName = input.readLine();
int index = getTVShowIndexByName(OldName);
if(index == -1)
{
System.out.println(" Passenger " + OldName+ " not found.");
}
else
{
tmpObject = (PassengerTemplate)ListPassengers.get(index);
showPassenger(tmpObject);
System.out.println("What you want to modify (Name|Day|Country)?: ");
String strOption = input.readLine();
if("name".equals(strOption.toLowerCase()))
{
System.out.println("New Passenger's Name: ");
tmpObject.ShowName = input.readLine().toString();
}
else if("day".equals(strOption.toLowerCase()))
{
System.out.println("New Day "+tmpObject.Name+"(Sun,Mon,Tue....) : ");
tmpObject.Day = input.readLine().toString();
}
else if("age".equals(strOption.toLowerCase()))
{
System.out.println("New Age: ");
tmpObject.Age = input.readLine().toString();
}
else if("country".equals(strOption.toLowerCase()))
{
System.out.println("New Country: ");
tmpObject.Country = input.readLine().toString();
}
else if("passport".equals(strOption.toLowerCase()))
{
System.out.println("New Passport's number: ");
tmpObject.Passport = input.readLine().toString();
}
else
{
System.out.println("Unable to locate the propety entered..");
}
ListPassengers.set(index, tmpObject);
}
}
private static int getTVShowIndexByName(String Name)
{
int index = -1;
PassengerTemplate tmp =null;
for(int i=0;i { tmp = (PassengerTemplate)ListPassengers.get(i); if(tmp.Name.toLowerCase().equals(Name.toLowerCase())) { index = i; break; } } return index; } private static void showName(PassengerTemplate tshow) { System.out.println(tshow.Name+"\t\t"+tshow.Day+"\t"+tshow.Age+"\t"+tshow.Prob+"\t"+tshow.Country+"\t"+tshow.Passport); } private static void DeletePassenger(BufferedReader input) throws IOException { System.out.println("Name of the Passenger to delete: "); String OldName = input.readLine(); int index = getTVShowIndexByName(OldName); if(index == -1) { System.out.println(" Passenger " + OldName+ " not found."); } else { ListPassengers.remove(index); System.out.println(" Passenger " + OldName+ "deleted successfully."); } } private static void showAllPassengers() { System.out.println("** Passenger management **** "); System.out.println("Name\t\tDay\tAge"); for(int i=0;i { showName((PassengerTemplate)ListPassengers.get(i)); } } } class PassengerTemplate { public String Passport; public String Country; public String Prob; public String Age; public String Name; public String ShowName = ""; public String Day = ""; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
