Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
import java.io.*;
import java.nio.charset.StandardCharsets;
public class TravProfDB {
public int numTravelers, currentTravelerIndex;
public String fileName;
private TravProf[] travelerList = new TravProf[10];
/* old solution that did not work well
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class TravProfDB {
private int numTravelers;
private int currentTravelerIndex;
private String fileName;
public TravProf[] travelerList;
//This is a constructor method which accepts the name of the file in which the traveler
//profiles will be stored. Thus, for the ITS system in its current version, the database of TravProf is
//implemented using a file.``
public TravProfDB(String fileName){
this.fileName = fileName;
numTravelers = 0;
currentTravelerIndex = 0;
travelerList = new TravProf[0];
}
//This method accepts a TravProf as input and inserts it into the array travelerList
public void insertNewProfile(TravProf travProf){
TravProf[] newTravelerList = new TravProf[travelerList.length+1];
if(travelerList.length == 0){
newTravelerList[0] = travProf;
} else {
for (int i = 0; i < newTravelerList.length-1; i++) {
newTravelerList[i] = travelerList[i];
}
newTravelerList[travelerList.length] = travProf;
}
travelerList = newTravelerList;
numTravelers = travelerList.length;
}
//This method accepts the travAgentID and lastName as inputs and returns the
//corresponding traveler profile as output.
public TravProf findProfile(String travAgentID, String lastName){
TravProf travProf = null;
for (int i = 0; i < travelerList.length; i++) {
if(travelerList[i].getLastName() == lastName && travelerList[i].getTravAgentID() == travAgentID){
travProf = travelerList[i];
}
}
return travProf;
}
//This method accepts the travAgentID and lastName as inputs and deletes
//the corresponding traveler profile. It returns a boolean value to indicate whether the delete operation
//was successful.
public boolean deleteProfile(String travAgentID, String lastName){
if(travelerList.length == 0){
return false;
} else if (travelerList.length == 1){
numTravelers = 0;
travelerList = new TravProf[0];
return true;
}
TravProf[] newTravelerList = new TravProf[travelerList.length-1];
boolean found = false;
int i;
for (i = 0; i < newTravelerList.length; i++) {
if (travelerList[i].getLastName() == lastName && travelerList[i].getTravAgentID() == travAgentID) {
i++;
for (int j = i-1; j < newTravelerList.length; j++) {
newTravelerList[j] = travelerList[i];
i++;
}
travelerList = newTravelerList;
numTravelers = travelerList.length;
currentTravelerIndex = 0;
return true;
}
newTravelerList[i] = travelerList[i];
}
return false;
}
//Returns first profile in the DB.
public TravProf findFirstProfile(){
return travelerList[0];
}
//This method writes all TravProf stored in the array travelerList to a
//file. Argument is a filename. E.x. "ExampleDB.txt".
public void writeAllTravProf(String fileName){
try {
FileOutputStream outputStream = new FileOutputStream(fileName);
for (int i = 0; i < travelerList.length; i++) {
String line = travelerList[i].toString() + "\n";
byte[] strToBytes = line.getBytes();
outputStream.write(strToBytes);
}
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
*/
public TravProfDB(String newfileName){
//new DB
fileName = newfileName;
File file = new File(fileName);
}
//new profile
public void insertNewProfile(TravProf newProfile){
TravProf newarr[] = new TravProf[numTravelers + 1];
for(int i = 0; i < numTravelers; i++){
newarr[i] = travelerList[i];
}
newarr[numTravelers] = newProfile;
travelerList = newarr;
numTravelers++;
}
/*find profile
//Iterates through the profiles in the DB using the travelerIndex as an index
//and the numTravelers as a max to loop to 0.
public TravProf findNextProfile(){
if(numTravelers == 0){
System.out.println("No travelers in the database!");
return null;
} else if(currentTravelerIndex >= numTravelers) {
currentTravelerIndex = 1;
return travelerList[0];
} else {
return travelerList[currentTravelerIndex++];
}
*/
public TravProf findProfile(String travAgentID, String lastName){
for(int i = 0; i < numTravelers; i++){
TravProf profile = travelerList[i];
if(profile.gettravAgentID().equals(travAgentID) && profile.getLastName().equals(lastName)){
return profile;
}
}
return null;
}
//delete profile
public boolean deleteProfile(String travAgentID, String lastname) {
TravProf temp = findProfile(travAgentID, lastname);
if(temp == null)
return false;
else {
TravProf[] temptravelerList = new TravProf[travelerList.length - 1];
//loop throughf
for (int i = 0, j = 0; i < numTravelers; i++)
{
//if last name is the same
if (!travelerList[i].getLastName().equals(lastname))
{
temptravelerList[j++] = travelerList[i];
}
}
travelerList = temptravelerList;
numTravelers--; //less travelers
return true; //return true
}
}
//find first profile
public TravProf findFirstProfile()
{
if(travelerList.length > 0)
{
currentTravelerIndex = 0;
//return the first profile index 0
return travelerList[0];
}
else
return null;
}
public TravProf findNextProfile(){
//find the next profile
if(numTravelers > 0 && currentTravelerIndex < numTravelers - 1)
{
currentTravelerIndex++;
return travelerList[currentTravelerIndex];
}
else
return null;
}
//increase size of list
private void increase(){
TravProf[] newarr = new TravProf[travelerList.length + 10];
for(int i = 0; i < numTravelers; i++){
newarr[i] = travelerList[i];
}
travelerList = newarr; //new array
}
public void WriteAllTravProf(String fileName) {
try {
FileOutputStream outputStream = new FileOutputStream(fileName);
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream, StandardCharsets.UTF_8);
BufferedWriter buff = new BufferedWriter(outputStreamWriter);
for(int i = 0; i < numTravelers; i++){
buff.write(travelerList[i].gettravAgentID());
buff.newLine();
buff.write(travelerList[i].getFirstName());
buff.newLine();
buff.write(travelerList[i].getLastName());
buff.newLine();
buff.write(travelerList[i].getAddress());
buff.newLine();
buff.write(travelerList[i].getPhone());
buff.newLine();
buff.write(travelerList[i].getTravelType());
buff.newLine();
buff.write(travelerList[i].getPaymentType());
buff.newLine();
buff.write(String.valueOf(travelerList[i].getTripCost()));
buff.newLine();
buff.write(travelerList[i].getMedCondInfo().getAlgType());
buff.newLine();
buff.write(travelerList[i].getMedCondInfo().getIllType());
buff.newLine();
buff.write(travelerList[i].getMedCondInfo().getMdContact());
buff.newLine();
buff.write(travelerList[i].getMedCondInfo().getMdPhone());
buff.newLine();
buff.newLine();
}
buff.write("~\n");
buff.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
/*
//This function should read traveler profiles from a file. The string argument might be filename or filepath.
//This method is used to read in the existing traveler profiles placed in the file. Example Argument: "TestDB.txt".
public void initializeDataBase(String fileName){
try {
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(fileName), StandardCharsets.UTF_8));
String line;
while ((line = br.readLine()) != null) {
String[] attributes = line.split("\t");
insertNewProfile(new TravProf(attributes[0],attributes[1],attributes[2],attributes[3],attributes[4],Float.parseFloat(attributes[5]),attributes[6],attributes[7],new MedCond(attributes[8],attributes[9],attributes[10],attributes[11])));
}
} catch (Exception e) {
e.printStackTrace();
}
}
*/
public void initializeDatabase(String fileName){
try{
BufferedReader buff = new BufferedReader(new FileReader(fileName));
numTravelers = 0;
String line;
while(!((line = buff.readLine()).equals("~")))
{
if(numTravelers == travelerList.length) {
increase();
}
String travAgentID, FirstName, LastName, Address, Phone, TravelType, PaymentType,
AlgType, IllType, MdContact, MdPhone;
float TripCost;
travAgentID = line;
line = buff.readLine();
FirstName = line;
line = buff.readLine();
LastName = line;
line = buff.readLine();
Address = line;
line = buff.readLine();
Phone = line;
line = buff.readLine();
TravelType = line;
line = buff.readLine();
PaymentType = line;
line = buff.readLine();
TripCost = Float.parseFloat(line);
line = buff.readLine();
AlgType = line;
line = buff.readLine();
IllType = line;
line = buff.readLine();
MdContact = line;
line = buff.readLine();
MdPhone = line;
line = buff.readLine();
//create new instances for the database
MedCond medprofile = new MedCond(AlgType, IllType, MdContact, MdPhone);
TravProf profile = new TravProf(travAgentID, FirstName, LastName, Address, Phone, TravelType,
PaymentType, TripCost, medprofile);
travelerList[numTravelers] = profile;
numTravelers++;
}
buff.close();
}catch(IOException e) {
e.printStackTrace();
}
currentTravelerIndex = 0;
}
}