Im trying to write a program to export a file given a text document with informa
ID: 654146 • Letter: I
Question
Im trying to write a program to export a file given a text document with information in it, I keep running into trouble with the file that is being created is unable to be opened according to the junit test so I was hoping someone could help me figure out where it's messing up with that, and also it needs to have the same export as it took in so I need to make sure that the two match if you see anything obviously wrong with my code in that aspect.
the second method is supposed to take file input and get the correct number of flights, passengers, destination time, takeoff time, and alerts.
import java.io.*;
import java.util.*;
public class Manager{
private static ArrayList thePassengers;
private static ArrayList theFlights;
private static File fileName;
private static Formatter makeFiles;
public static void exportData(String filename,
ArrayList passengers, ArrayList flights) {
fileName = new File("./filename.txt");
theFlights = flights;
thePassengers = passengers;
openFile();
newFlight();
newPassenger();
closeFile();
}
// openFile
public static void openFile() {
try {
makeFiles = new Formatter(fileName);
} catch (Exception e) {
System.out.println("Error.");
}
}
// newFlight
public static void newFlight() {
// Flight++
makeFiles.format("%s%d", "#flightCount", theFlights.size());
for (int i = 0; i < theFlights.size(); i++) {
String Src = theFlights.get(i).getSourceAirport();
String Dest = theFlights.get(i).getDestinationAirport();
int ToT = theFlights.get(i).getTakeoffTime();
int LT = theFlights.get(i).getLandingTime();
int Cap = theFlights.get(i).getCapacity();
makeFiles.format("%s %s%s%d%d %d ", "#newFlight", Src, Dest, ToT, LT, Cap);
}
}
// newPassenger
public static void newPassenger() {
makeFiles.format("%s%d", "#passCount", thePassengers.size());
int i = 0;
while (i < thePassengers.size()) {
String first = thePassengers.get(i).getFirstName();
String last = thePassengers.get(i).getLastName();
String Flights = flightLimit(i);
String Standby = sbFlightLimit(i);
String Alerts = Alerts(i);
int O = thePassengers.get(i).getAlerts().size();
int R = thePassengers.get(i).getBookedFlights().size();
int S = thePassengers.get(i).getStandbyFlights().size();
makeFiles.format("%s %s%s%s %d %s %d %s %d %s", "#newPass", first,
" , ", last, O, Alerts, R, Flights, S, Standby);
i++;
}
}
// Flight capacity
public static String flightLimit(int i) {
StringBuilder J = new StringBuilder();
int a = 0;
while (a < thePassengers.get(i).getStandbyFlights().size()) {
J.append(thePassengers.get(i).getStandbyFlights().get(a)
.getSourceAirport()
+ " , "
+ thePassengers.get(i).getStandbyFlights().get(a)
.getDestinationAirport()
+ " , "
+ Integer.toString(thePassengers.get(i).getStandbyFlights()
.get(a).getTakeoffTime())
+ " , "
+ Integer.toString(thePassengers.get(i).getStandbyFlights()
.get(a).getLandingTime()) + " ");
a++;
}
String S = J.toString();
return S;
}
// StandbyFlight Capacity
public static String sbFlightLimit(int i) {
StringBuilder J = new StringBuilder();
int a = 0;
while (a < thePassengers.get(i).getStandbyFlights().size()) {
J.append(thePassengers.get(i).getStandbyFlights().get(a)
.getSourceAirport()
+ " , "
+ thePassengers.get(i).getStandbyFlights().get(a)
.getDestinationAirport()
+ " , "
+ Integer.toString(thePassengers.get(i).getStandbyFlights()
.get(a).getTakeoffTime())
+ " , "
+ Integer.toString(thePassengers.get(i).getStandbyFlights()
.get(a).getLandingTime()) + " ");
a++;
}
String S = J.toString();
return S;
}
// closeFile
public static void closeFile() {
makeFiles.close();
}
// Alerts
public static String Alerts(int i) {
StringBuilder J = new StringBuilder();
while (i < thePassengers.get(i).getAlerts().size()) {
Joey.append(thePassengers.get(i).getAlerts().get(i));
i++;
}
String S = J.toString();
return S;
}
// Alerts++
public static int AlertsCount(int i) {
return thePassengers.get(i).getAlerts().size();
}
// Alerts list
public static String listAlerts(int i) {
StringBuilder stringBuilder = new StringBuilder();
for (int z = 0; z < thePassengers.get(i).getAlerts().size(); z++) {
stringBuilder.append(thePassengers.get(i).getAlerts().get(z)
+ System.getProperty("line.separator"));
}
String alerts = stringBuilder.toString();
return alerts;
}
public static ImportData importData(String filename){
File file = new File("./Test.txt");
try {
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
System.out.println(line);
}
scanner.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
}
}
Explanation / Answer
Below issues need to be taken care of:-
1) The filename is hardcoded even if it receives String filename as a parameter in method exportData()
fileName = new File("./filename.txt");
Please make sure the file path is correct and you have write permission to files in that location.
2) The pogram does not have a main method and hence it will not run.