From 19ae8b49a58dbb2764b38ee954212eb2a4229ca0 Mon Sep 17 00:00:00 2001 From: Adam P Skawinski Date: Wed, 29 Apr 2020 20:07:33 -0400 Subject: [PATCH] Java Classes TravProfInterface --- MedCond.java | 63 +++ TravProf.java | 176 +++++++ TravProfDB.java | 375 +++++++++++++++ TravProfInterface.java | 1038 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 1652 insertions(+) create mode 100644 MedCond.java create mode 100644 TravProf.java create mode 100644 TravProfDB.java create mode 100644 TravProfInterface.java diff --git a/MedCond.java b/MedCond.java new file mode 100644 index 0000000..f0c1560 --- /dev/null +++ b/MedCond.java @@ -0,0 +1,63 @@ +import java.io.Serializable; + +public class MedCond implements Serializable +{ + // Variables + String algType; + String mdPhone; + String mdContact; + String illType; + + + //Constructor + public MedCond(String mdContact, String mdPhone, String algType, String illType) + { + this.mdContact=mdContact; //medical contact + this.mdPhone=mdPhone; //Medical Phone # + this.algType=algType; // Allergy Type + this.illType=illType; // Illness type + } + + @Override + public String toString() { + return mdContact + "\t" + mdPhone + "\t" + algType + "\t" + illType; + } + + //Get Methods + public String getIllType() + { + return illType; + } + public String getMdPhone() + { + return mdPhone; + } + public String getMdContact() + { + return mdContact; + } + public String getAlgType() + { + return algType; + } + + + //Update Methods + public void updateIllType(String name) + { + this.illType=name; + } + public void updateMdPhone(String name) + { + this.mdPhone=name; + } + public void updateMdContact(String name) + { + this.mdContact=name; + } + public void updateAlgType(String name) + { + this.algType=name; + } + +} diff --git a/TravProf.java b/TravProf.java new file mode 100644 index 0000000..5108106 --- /dev/null +++ b/TravProf.java @@ -0,0 +1,176 @@ +public class TravProf { + private String travAgentID = "a", + firstName = "a", + lastName = "a", + address = "a", + phone = "a", + travelType = "a", + paymentType = "a"; + private float tripCost = 0.0f; + private MedCond medcond; +//TravProf +/* old solution +class TravProf { + private String travAgentID; //ID number of employee that created the profile. + private String firstName; //Traveler’s first name + private String lastName; //Traveler’s last name + private String address; //Traveler’s home address + private String phone; //Traveler’s home phone number + private float tripCost; //Total cost of the trip + private String travelType; //Type of travel, must be either "Pleasure" or "Business" + private String paymentType; //Method of payment, must be either “Credit”, “Check”, “Debit”, or “Invoice” + public MedCond medCondInfo; + + public TravProf(String travAgentID, String firstName, String lastName, String address, String phone, float tripCost, String travelType, String paymentType, MedCond medCondInfo){ + this.travAgentID = travAgentID; + this.firstName = firstName; + this.lastName = lastName; + this.address = address; + this.phone = phone; + this.tripCost = tripCost; + this.travelType = travelType; + this.paymentType = paymentType; + this.medCondInfo = medCondInfo; + } + + @Override + public String toString() { + return travAgentID + "\t" + firstName + "\t" + lastName + "\t" + address + "\t" +phone + "\t" +tripCost + "\t" +travelType + "\t" +paymentType + "\t" +medCondInfo.toString(); + + } + + public String getTravAgentID(){ + return travAgentID; + } + + public String getFirstName(){ + return firstName; + } + + public String getLastName(){ + return lastName; + } + + public String getAddress(){ + return address; + } + + public String getPhone(){ + return phone; + } + + public float getTripCost(){ + return tripCost; + } + + public String getTravelType(){ + return travelType; + } + + public String getPaymentType(){ + return paymentType; + } + + public MedCond getMedCondInfo(){ + return medCondInfo; + } + + public void updateFirstName(String firstName) { + this.firstName = firstName; + } + + public void updateLastName(String lastName) { + this.lastName = lastName; + } + + public void updateAddress(String address) { + this.address = address; + } + + public void updatePhone(String phone) { + this.phone = phone; + } + + public void updateTripCost(float tripCost) { + this.tripCost = tripCost; + } + + public void updateTravelType(String travelType) { + this.travelType = travelType; + } + + public void updatePaymentType(String paymentType) { + this.paymentType = paymentType; + } + + public void updateMedCondInfo(MedCond medCondInfo) { + this.medCondInfo = medCondInfo; + } +} */ + public TravProf(String newtravAgentID, String newfirstName, String newlastName, String newaddress, String newphone, + String newtravelType, String newpaymentType, float newtripCost, MedCond newmedCondInfo){ + travAgentID = newtravAgentID; + firstName = newfirstName; + lastName = newlastName; + address = newaddress; + phone = newphone; + travelType = newtravelType; + paymentType = newpaymentType; + tripCost = newtripCost; + medcond = newmedCondInfo; + } + + //implementing the get functions of the class + public String gettravAgentID(){ + return travAgentID; + } + public String getFirstName(){ + return firstName; + } + public String getLastName(){ + return lastName; + } + public String getAddress(){ + return address; + } + public String getPhone(){ + return phone; + } + public float getTripCost(){ + return tripCost; + } + public String getTravelType(){ + return travelType; + } + public String getPaymentType(){ + return paymentType; + } + public MedCond getMedCondInfo(){ + return medcond; + } + //implement the update functions of the class + public void updateFirstName(String name){ + firstName = name; + } + public void updateLastName(String name){ + lastName = name; + } + public void updateAddress(String addy){ + address = addy; + } + public void updatePhone(String Phone){ + phone = Phone; + } + public void updateTripCost(String cost){ + tripCost = Float.parseFloat(cost); + } + public void updateTravelType(String travtype){ + travelType = travtype; + } + public void updatePaymentType(String paytype){ + paymentType = paytype; + } + public void updateMedCondInfo(MedCond cond){ + medcond = cond; + } +} diff --git a/TravProfDB.java b/TravProfDB.java new file mode 100644 index 0000000..6e837fe --- /dev/null +++ b/TravProfDB.java @@ -0,0 +1,375 @@ +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; + } +} diff --git a/TravProfInterface.java b/TravProfInterface.java new file mode 100644 index 0000000..8492c92 --- /dev/null +++ b/TravProfInterface.java @@ -0,0 +1,1038 @@ +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import javax.swing.*; + +public class TravProfInterface { + private TravProfDB database; + private JFrame mainFrame; + + //DB Object + public TravProfInterface(String newfileName){ + database = new TravProfDB(newfileName); + initDB(); + getUserChoice(); + } + + //get choice from the user + private void getUserChoice(){ + mainFrame = new JFrame("Integrated Travel System"); + mainFrame.setSize(500,500); + mainFrame.setLayout(null); + mainFrame.setBackground(Color.RED); + //initialize + JLabel Header; + JRadioButton rbtn1, rbtn2, rbtn3, rbtn4, rbtn5; + JButton btn; + //Header + Header = new JLabel("ITS"); + Header.setBounds(100,10,150,20); + mainFrame.add(Header); + //Options + //Create Profile + rbtn1 = new JRadioButton("Create a Profile"); + rbtn1.setBounds(100,50,120,25); + mainFrame.add(rbtn1); + //Delete Profile + rbtn2 = new JRadioButton("Delete a Profile"); + rbtn2.setBounds(100,80,120,25); + mainFrame.add(rbtn2); + //Update + rbtn3 = new JRadioButton("Update a Profile"); + rbtn3.setBounds(100,110,120,25); + mainFrame.add(rbtn3); + //Find/Display + rbtn4 = new JRadioButton("Find/Display a Profile"); + rbtn4.setBounds(100,140,150,25); + mainFrame.add(rbtn4); + //Display + rbtn5 = new JRadioButton("Display All Profiles"); + rbtn5.setBounds(100,170,150,25); + mainFrame.add(rbtn5); + //Add the buttons we just selected + ButtonGroup group = new ButtonGroup(); + group.add(rbtn1); + group.add(rbtn2); + group.add(rbtn3); + group.add(rbtn4); + group.add(rbtn5); + //search + btn = new JButton("Choose"); + btn.setBounds(110,200,100,30); + mainFrame.add(btn); + mainFrame.getContentPane().setBackground( Color.lightGray ); + mainFrame.setVisible(true); + //Action listener + btn.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + mainFrame.getContentPane().removeAll(); + mainFrame.revalidate(); + mainFrame.repaint(); + //Depending on what is selected go to that function + if(rbtn1.isSelected()) + createNewTravProf(); + + else if(rbtn2.isSelected()) + deleteTravProf(); + + else if(rbtn3.isSelected()) + updateTravProf(); + + else if(rbtn4.isSelected()) + findTravProf(); + + else if(rbtn5.isSelected()) + displayAllTravProf(); + } + }); + } + +//create a profile + public void createNewTravProf(){ + final String[] travAgentID = new String[1]; + final String[] FirstName = new String[1]; + final String[] LastName = new String[1]; + final String[] Address = new String[1]; + final String[] Phone = new String[1]; + final String[] TravelType = new String[1]; + final String[] PaymentType = new String[1]; + final String[] AlgType = new String[1]; + final String[] IllType = new String[1]; + final String[] MdContact = new String[1]; + final String[] MdPhone = new String[1]; + final float[] TripCost = new float[1]; + + //Jlabel and field creation + JLabel Header, lb1, lb2, lb3, lb4, lb5, lb6, lb7, lb8, lb9, lb10, lb11, lb12; + JComboBox box6, box7; + JTextField txt1, txt2, txt3, txt4, txt5, txt8, txt9, txt10, txt11, txt12; + JButton btn; + + //Options + Header = new JLabel("Create a Profile"); + Header.setBounds(100,10,140,20); + mainFrame.add(Header); + lb1 = new JLabel("Traveler ID:"); + lb1.setBounds(10,50,110,20); + mainFrame.add(lb1); + txt1 = new JTextField(); + txt1.setBounds(120,50,140,20); + mainFrame.add(txt1); + lb2 = new JLabel("First Name:"); + lb2.setBounds(10,70,110,20); + mainFrame.add(lb2); + txt2 = new JTextField(); + txt2.setBounds(120,70,140,20); + mainFrame.add(txt2); + lb3 = new JLabel("Last Name:"); + lb3.setBounds(10,90,110,20); + mainFrame.add(lb3); + txt3 = new JTextField(); + txt3.setBounds(120,90,140,20); + mainFrame.add(txt3); + lb4 = new JLabel("Address:"); + lb4.setBounds(10,110,110,20); + mainFrame.add(lb4); + txt4 = new JTextField(); + txt4.setBounds(120,110,140,20); + mainFrame.add(txt4); + lb5 = new JLabel("Phone:"); + lb5.setBounds(10,130,110,20); + mainFrame.add(lb5); + txt5 = new JTextField(); + txt5.setBounds(120,130,140,20); + mainFrame.add(txt5); + lb6 = new JLabel("Travel Type:"); + lb6.setBounds(10,150,110,20); + mainFrame.add(lb6); + String[] list1 = {"Select", "Personal", "Business"}; + box6 = new JComboBox<>(list1); + box6.setBounds(120,150,140,20); + mainFrame.add(box6); + lb7 = new JLabel("Payment Type: "); + lb7.setBounds(10,170,110,20); + mainFrame.add(lb7); + String[] list2 = {"Select", "Credit", "Check", "Debit", "Invoice"}; + box7 = new JComboBox<>(list2); + box7.setBounds(120,170,140,20); + mainFrame.add(box7); + lb8 = new JLabel("Trip Cost:"); + lb8.setBounds(10,190,110,20); + mainFrame.add(lb8); + txt8 = new JTextField(); + txt8.setBounds(120,190,140,20); + mainFrame.add(txt8); + lb9 = new JLabel("Allergies:"); + lb9.setBounds(10,210,110,20); + mainFrame.add(lb9); + txt9 = new JTextField(); + txt9.setBounds(120,210,140,20); + mainFrame.add(txt9); + lb10 = new JLabel("Illnesses:"); + lb10.setBounds(10,230,110,20); + mainFrame.add(lb10); + + txt10 = new JTextField(); + txt10.setBounds(120,230,140,20); + mainFrame.add(txt10); + + lb11 = new JLabel("Medical Contact:"); + lb11.setBounds(10,250,110,20); + mainFrame.add(lb11); + + txt11 = new JTextField(); + txt11.setBounds(120,250,140,20); + mainFrame.add(txt11); + + lb12 = new JLabel("Contact Phone:"); + lb12.setBounds(10,270,110,20); + mainFrame.add(lb12); + + txt12 = new JTextField(); + txt12.setBounds(120,270,140,20); + mainFrame.add(txt12); + + btn = new JButton("Submit"); + btn.setBounds(100, 300, 100, 30); + mainFrame.add(btn); + + btn.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + travAgentID[0] = txt1.getText(); + FirstName[0] = txt2.getText(); + LastName[0] = txt3.getText(); + Address[0] = txt4.getText(); + Phone[0] = txt5.getText(); + TravelType[0] = (String) box6.getSelectedItem(); + PaymentType[0] = (String) box7.getSelectedItem(); + TripCost[0] = Float.parseFloat(txt8.getText()); + AlgType[0] = txt9.getText(); + IllType[0] = txt10.getText(); + MdContact[0] = txt11.getText(); + MdPhone[0] = txt12.getText(); + MedCond medcondinfo = new MedCond(AlgType[0], IllType[0], MdContact[0], MdPhone[0]); + TravProf profile = new TravProf(travAgentID[0], FirstName[0], LastName[0], Address[0], Phone[0], TravelType[0], PaymentType[0], TripCost[0], medcondinfo); + database.insertNewProfile(profile); +// write to the database + writeToDB(); + + mainFrame.getContentPane().removeAll(); + mainFrame.revalidate(); + mainFrame.repaint(); + } + }); + } + + /* + 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(); + } + } + + //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++]; + } + } + + //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(); + } + } + +} + + */ + //delete + public void deleteTravProf(){ + JLabel Header, lb1, lb2; + JTextField txt1, txt2; + JButton btn1, btn2; + + Header = new JLabel("Delete a Profile"); + Header.setBounds(100,10,150,25); + mainFrame.add(Header); + + lb1 = new JLabel("Traveler ID: "); + lb1.setBounds(10,50,120,25); + mainFrame.add(lb1); + + txt1 = new JTextField(); + txt1.setBounds(120,50,150,25); + mainFrame.add(txt1); + + lb2 = new JLabel("Last Name: "); + lb2.setBounds(10,70,120,25); + mainFrame.add(lb2); + + txt2 = new JTextField(); + txt2.setBounds(120,70,150,25); + mainFrame.add(txt2); + + btn1 = new JButton("Delete"); + btn1.setBounds(100, 100, 100, 35); + mainFrame.add(btn1); + + btn2 = new JButton("OK"); + btn2.setBounds(100, 100, 100, 35); + + btn1.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + String ID = txt1.getText(); + String lastName = txt2.getText(); + + mainFrame.getContentPane().remove(txt1); + mainFrame.getContentPane().remove(lb2); + mainFrame.getContentPane().remove(txt2); + mainFrame.getContentPane().remove(btn1); + mainFrame.revalidate(); + mainFrame.repaint(); + + lb1.setBounds(100,50,100,30); + if(database.deleteProfile(ID,lastName)){ + lb1.setText("Profile Deleted."); + } + else{ + lb1.setText("Profile not found."); + } + + writeToDB(); + + mainFrame.add(btn2); + } + }); + + btn2.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + mainFrame.getContentPane().removeAll(); + mainFrame.revalidate(); + mainFrame.repaint(); + } + }); + } + + public void updateTravProf(){ + JLabel Header, lb1, lb2, lb3; + JTextField txt1; + JTextField txt2; + final JTextField[] txt3 = new JTextField[1]; + JComboBox box; + JButton btn1, btn2; + final TravProf[] profile = new TravProf[1]; + Header = new JLabel("Update Profile"); + Header.setBounds(100,10,150,25); + mainFrame.add(Header); + lb1 = new JLabel("Traveler ID: "); + lb1.setBounds(10,50,120,25); + mainFrame.add(lb1); + txt1 = new JTextField(); + txt1.setBounds(120,50,150,25); + mainFrame.add(txt1); + lb2 = new JLabel("Last Name: "); + lb2.setBounds(10,70,120,25); + mainFrame.add(lb2); + txt2 = new JTextField(); + txt2.setBounds(120,70,150,25); + mainFrame.add(txt2); + lb3 = new JLabel("Update Field: "); + lb3.setBounds(10,100,150,25); + mainFrame.add(lb3); + String[] list = {"Select","Address", "Phone number", "Travel type", "Payment type", "Trip cost", "Allergies", "Illnesses","Medical Contact", "Medical contact phone"}; + box = new JComboBox<>(list); + box.setBounds(120,100,150,25); + mainFrame.add(box); + btn1 = new JButton("Find"); + btn1.setBounds(100,130,100,35); + mainFrame.add(btn1); + btn2 = new JButton("Submit"); + btn2.setBounds(100,130,100,35); + btn1.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + String ID = txt1.getText(); + String lastName = txt2.getText(); + + profile[0] = database.findProfile(ID,lastName); + + mainFrame.getContentPane().remove(txt1); + mainFrame.getContentPane().remove(txt2); + mainFrame.getContentPane().remove(box); + mainFrame.getContentPane().remove(btn1); + mainFrame.revalidate(); + mainFrame.repaint(); + + Header.setText("Update"); + Header.setBounds(150,10,150,25); + + lb1.setText("Traveler ID - " + ID); + lb2.setText("Last Name - " + lastName); + + lb3.setText((String) box.getSelectedItem() + ": "); + txt3[0] = new JTextField(); + txt3[0].setBounds(120,100,150,25); + mainFrame.add(txt3[0]); + + mainFrame.add(btn2); + } + }); + + btn2.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + String value = txt3[0].getText(); + + int picked = box.getSelectedIndex(); + if(picked == 1) + profile[0].updateAddress(value); + if(picked == 2) + profile[0].updatePhone(value); + if(picked == 3) + profile[0].updateTravelType(value); + if(picked == 4) + profile[0].updatePaymentType(value); + if(picked == 5) + profile[0].updateTripCost(value); + if(picked == 6) + profile[0].getMedCondInfo().updateAlgType(value); + if(picked == 7) + profile[0].getMedCondInfo().updateIllType(value); + if(picked == 8) + profile[0].getMedCondInfo().updateMdContact(value); + if(picked == 9) + profile[0].getMedCondInfo().updateMdPhone(value); + + writeToDB(); + + mainFrame.getContentPane().removeAll(); + mainFrame.revalidate(); + mainFrame.repaint(); + } + }); + } + + public void findTravProf(){ + + + JLabel Header, lb1, lb2; + JTextField txt1, txt2; + JButton btn; + + Header = new JLabel("Find Profile"); + Header.setBounds(100,10,150,20); + mainFrame.add(Header); + + lb1 = new JLabel("Traveler ID: "); + lb1.setBounds(10,50,120,20); + mainFrame.add(lb1); + + txt1 = new JTextField(); + txt1.setBounds(120,50,150,20); + mainFrame.add(txt1); + + lb2 = new JLabel("Last Name: "); + lb2.setBounds(10,70,120,20); + mainFrame.add(lb2); + + txt2 = new JTextField(); + txt2.setBounds(120,70,150,20); + mainFrame.add(txt2); + + btn = new JButton("Find"); + btn.setBounds(100,100,100,30); + mainFrame.add(btn); + + btn.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + String ID = txt1.getText(); + String lastName = txt2.getText(); + + mainFrame.getContentPane().removeAll(); + mainFrame.revalidate(); + mainFrame.repaint(); + + displayTravProf(database.findProfile(ID, lastName)); + } + }); + } +/* +for reference + + +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(); + } + } + + //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++]; + } + } + + //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 displayTravProf(TravProf traveler){ + + + JLabel Header, lb1, lb2, lb3, lb4, lb5, lb6, lb7, lb8, lb9, lb10, lb11, lb12; + JButton btn; + + Header = new JLabel("Traveler Profile"); + Header.setBounds(100,10,150,20); + mainFrame.add(Header); + + btn = new JButton("Close"); + + if(traveler == null){ + lb1 = new JLabel("Profile not found."); + lb1.setBounds(100,50,150,20); + mainFrame.add(lb1); + + btn.setBounds(100,90,100,30); + mainFrame.add(btn); + + mainFrame.getContentPane().removeAll(); + mainFrame.revalidate(); + mainFrame.repaint(); + } + else{ + lb1 = new JLabel("Traveler ID: " + traveler.gettravAgentID()); + lb1.setBounds(10,50,300,20); + mainFrame.add(lb1); + + lb2 = new JLabel("First Name: " + traveler.getFirstName()); + lb2.setBounds(10,70,300,20); + mainFrame.add(lb2); + + lb3 = new JLabel("Last Name: " + traveler.getLastName()); + lb3.setBounds(10,90,300,20); + mainFrame.add(lb3); + + lb4 = new JLabel("Address: " + traveler.getAddress()); + lb4.setBounds(10,110,300,20); + mainFrame.add(lb4); + + lb5 = new JLabel("Phone: " + traveler.getPhone()); + lb5.setBounds(10,130,300,20); + mainFrame.add(lb5); + + lb6 = new JLabel("Travel type: " + traveler.getTravelType()); + lb6.setBounds(10,150,300,20); + mainFrame.add(lb6); + + lb7 = new JLabel("Payment type: " + traveler.getPaymentType()); + lb7.setBounds(10,170,300,20); + mainFrame.add(lb7); + + lb8 = new JLabel("Trip cost: " + Float.toString(traveler.getTripCost())); + lb8.setBounds(10,190,300,20); + mainFrame.add(lb8); + + lb9 = new JLabel("Allergies: " + traveler.getMedCondInfo().getAlgType()); + lb9.setBounds(10,210,300,20); + mainFrame.add(lb9); + + lb10 = new JLabel("Illnesses: " + traveler.getMedCondInfo().getIllType()); + lb10.setBounds(10,230,300,20); + mainFrame.add(lb10); + + lb11 = new JLabel("Medical contact: " + traveler.getMedCondInfo().getMdContact()); + lb11.setBounds(10,250,300,20); + mainFrame.add(lb11); + + lb12 = new JLabel("Medical Contact Phone: " + traveler.getMedCondInfo().getMdPhone()); + lb12.setBounds(10,270,300,20); + mainFrame.add(lb12); + + btn.setBounds(100,300,100,30); + mainFrame.add(btn); + + mainFrame.getContentPane().removeAll(); + mainFrame.revalidate(); + mainFrame.repaint(); + } + } + + public void displayAllTravProf(){ + + + JLabel Header; + final JLabel[] lb1 = new JLabel[1]; + final JLabel[] lb2 = new JLabel[1]; + final JLabel[] lb3 = new JLabel[1]; + final JLabel[] lb4 = new JLabel[1]; + final JLabel[] lb5 = new JLabel[1]; + final JLabel[] lb6 = new JLabel[1]; + final JLabel[] lb7 = new JLabel[1]; + final JLabel[] lb8 = new JLabel[1]; + final JLabel[] lb9 = new JLabel[1]; + final JLabel[] lb10 = new JLabel[1]; + final JLabel[] lb11 = new JLabel[1]; + final JLabel[] lb12 = new JLabel[1]; + JTextField txt1; + JButton btn1; + final JButton[] btn2 = new JButton[1]; + TravProf[] profiles = new TravProf[database.numTravelers]; + final int[] j = {0}, i = {0}; + + Header = new JLabel("Display All Profiles"); + Header.setBounds(100,10,150,20); + mainFrame.add(Header); + + lb1[0] = new JLabel("Traveler ID: "); + lb1[0].setBounds(10,50,120,20); + mainFrame.add(lb1[0]); + txt1 = new JTextField(); + txt1.setBounds(120,50,150,20); + mainFrame.add(txt1); + btn1 = new JButton("Find"); + btn1.setBounds(100,80,100,30); + mainFrame.add(btn1); + btn2[0] = new JButton("Next"); + btn2[0].setBounds(270,300,100,30); + + //Create Profile + btn1.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + TravProf profile = database.findFirstProfile(); //create a temporary profile to iterate TravelerList + + String ID = txt1.getText(); + + mainFrame.getContentPane().removeAll(); + mainFrame.revalidate(); + mainFrame.repaint(); + + + for(int i = 0; i < database.numTravelers; i++){ + if(profile.gettravAgentID().equals(ID)) { + profiles[j[0]] = profile; + j[0]++; + } + profile = database.findNextProfile(); + } + + mainFrame.add(Header); + + lb1[0] = new JLabel("Traveler ID: " + profiles[i[0]].gettravAgentID()); + lb1[0].setBounds(10,50,300,20); + mainFrame.add(lb1[0]); + + lb2[0] = new JLabel("First Name: " + profiles[i[0]].getFirstName()); + lb2[0].setBounds(10,70,300,20); + mainFrame.add(lb2[0]); + + lb3[0] = new JLabel("Last Name: " + profiles[i[0]].getLastName()); + lb3[0].setBounds(10,90,300,20); + mainFrame.add(lb3[0]); + + lb4[0] = new JLabel("Address: " + profiles[i[0]].getAddress()); + lb4[0].setBounds(10,110,300,20); + mainFrame.add(lb4[0]); + + lb5[0] = new JLabel("Phone: " + profiles[i[0]].getPhone()); + lb5[0].setBounds(10,130,300,20); + mainFrame.add(lb5[0]); + + lb6[0] = new JLabel("Travel type: " + profiles[i[0]].getTravelType()); + lb6[0].setBounds(10,150,300,20); + mainFrame.add(lb6[0]); + + lb7[0] = new JLabel("Payment type: " + profiles[i[0]].getPaymentType()); + lb7[0].setBounds(10,170,300,20); + mainFrame.add(lb7[0]); + + lb8[0] = new JLabel("Trip cost: " + Float.toString(profiles[i[0]].getTripCost())); + lb8[0].setBounds(10,190,300,20); + mainFrame.add(lb8[0]); + + lb9[0] = new JLabel("Allergies: " + profiles[i[0]].getMedCondInfo().getAlgType()); + lb9[0].setBounds(10,210,300,20); + mainFrame.add(lb9[0]); + + lb10[0] = new JLabel("Illnesses: " + profiles[i[0]].getMedCondInfo().getIllType()); + lb10[0].setBounds(10,230,300,20); + mainFrame.add(lb10[0]); + + lb12[0] = new JLabel("Medical Contact Phone: " + profiles[i[0]].getMedCondInfo().getMdPhone()); + lb12[0].setBounds(10,270,300,20); + mainFrame.add(lb12[0]); + + lb11[0] = new JLabel("Medical contact: " + profiles[i[0]].getMedCondInfo().getMdContact()); + lb11[0].setBounds(10,250,300,20); + mainFrame.add(lb11[0]); + + + + mainFrame.add(btn2[0]); + + i[0]++; + } + }); + + //Create Profile + btn2[0].addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + mainFrame.getContentPane().removeAll(); + mainFrame.revalidate(); + mainFrame.repaint(); + + if(i[0] != j[0]){ + mainFrame.add(Header); + + //ID + lb1[0] = new JLabel("Traveler ID:" + profiles[i[0]].gettravAgentID()); + lb1[0].setBounds(10,50,350,20); + mainFrame.add(lb1[0]); + + //First Name + lb2[0] = new JLabel("First Name:" + profiles[i[0]].getFirstName()); + lb2[0].setBounds(10,70,350,20); + mainFrame.add(lb2[0]); + + //Last Name + lb3[0] = new JLabel("Last Name:" + profiles[i[0]].getLastName()); + lb3[0].setBounds(10,90,350,20); + mainFrame.add(lb3[0]); + + //Phone # + lb5[0] = new JLabel("Phone:" + profiles[i[0]].getPhone()); + lb5[0].setBounds(10,130,350,20); + mainFrame.add(lb5[0]); + + //Address + lb4[0] = new JLabel("Address:" + profiles[i[0]].getAddress()); + lb4[0].setBounds(10,110,350,20); + mainFrame.add(lb4[0]); + + //Travel Type + lb6[0] = new JLabel("Travel Type:" + profiles[i[0]].getTravelType()); + lb6[0].setBounds(10,150,350,20); + mainFrame.add(lb6[0]); + + //Payment Type + lb7[0] = new JLabel("Payment Type:" + profiles[i[0]].getPaymentType()); + lb7[0].setBounds(10,170,350,20); + mainFrame.add(lb7[0]); + + //Trip Cost + lb8[0] = new JLabel("Trip Cost:" + Float.toString(profiles[i[0]].getTripCost())); + lb8[0].setBounds(10,190,350,20); + mainFrame.add(lb8[0]); + + //Illnesses + lb10[0] = new JLabel("Illnesses:" + profiles[i[0]].getMedCondInfo().getIllType()); + lb10[0].setBounds(10,230,350,20); + mainFrame.add(lb10[0]); + + //Allergies + lb9[0] = new JLabel("Allergies:" + profiles[i[0]].getMedCondInfo().getAlgType()); + lb9[0].setBounds(10,210,350,20); + mainFrame.add(lb9[0]); + + + + //Medicak Contact + lb11[0] = new JLabel("Medical contact:" + profiles[i[0]].getMedCondInfo().getMdContact()); + lb11[0].setBounds(10,250,350,20); + mainFrame.add(lb11[0]); + + //Medical Phone Num + lb12[0] = new JLabel("Medical Contact Phone:" + profiles[i[0]].getMedCondInfo().getMdPhone()); + lb12[0].setBounds(10,270,350,20); + mainFrame.add(lb12[0]); + + mainFrame.add(btn2[0]); + + i[0]++; + } + } + }); + } + //Database functions + public void initDB(){ + database.initializeDatabase(database.fileName); + } + public void writeToDB(){ + database.WriteAllTravProf(database.fileName); + } + + + + //Main loading screen and what stays open + public static void main(String[] args){ + JFrame fileframe = new JFrame("Integrated Travel System"); + fileframe.setSize(400,200); + fileframe.setLayout(null); + + //Header Setup + JLabel Header = new JLabel("Integrated Travel System"); + Header.setBounds(125,10,150,20); + fileframe.add(Header); + + //Text Field + JTextField filetxt = new JTextField(); + filetxt.setBounds(145,55,150,20); + fileframe.add(filetxt); + + //Prompt setup + JLabel lb = new JLabel("Database filename: "); + lb.setBounds(20,55,200,20); + fileframe.add(lb); + + //Search Button + JButton btn = new JButton("Search"); + btn.setBounds(120,100,150,20); + fileframe.add(btn); + + //make the frame visible + fileframe.getContentPane().setBackground( Color.lightGray ); + fileframe.setVisible(true); + + //Action Listener + btn.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + String fileName = filetxt.getText(); + TravProfInterface face = new TravProfInterface(fileName); + } + }); + } +}