Please need help with pointer program. Please and thank you. For this program yo
ID: 664248 • Letter: P
Question
Please need help with pointer program. Please and thank you.
For this program you'll be writing a class with dynamically allocated data. This is useful in situations where we don’t know at compile time how much memory we’ll need; either it may become very large, or it may not be needed at all.
Consider an airline’s database of customers. Most of its “regular” customers will only fly once or twice a year; on any given day, fewer than 2% of its customers have reservations to fly. Thus, there is little point in setting aside room in the database to store the reservation with the customer record. On the other hand, there are some people who fly weekly, and may have reservations made for several trips. So the number of reservations that need to be stored, while usually 0, is potentially larger. This is a classic situation in which dynamic allocation is needed; there’s no obviously correct size for the record that we can determine at compile time.
For this program you’ll be consolidating data from two input files into a single output file. The first file lists customers. For each customer there’s an identifier, followed by a first and last name. All of the data in this file should be treated as strings. The second file contains reservation information. Your output file will be a file listing only the passengers who have reservations, identifying the frequent flyers in the group.
Make a Passenger class consisting of an identifier (string) and first and last names, and a variable number of reservations.
A reservation is a struct consisting of a date, flight number, and departure and arrival airports (all strings). Define this struct in the header file for the Passenger class. Also define overloaded operators for input and output; the input operator assumes all items are separated by whitespace, and the output operator prints all items for the reservation, in the order given above, on one line, with a tab character (‘ ’) between items.
The Passenger class should have the following methods:
• A constructor that can take 0 parameters. This can be a separate constructor, or a constructor with default values for all parameters.
• A constructor that takes the passenger’s identifier and first and last names.
• Because this class uses dynamic memory, the class will need an overloaded assignment operator, a copy constructor, and a destructor. • Getters and setters for the ID, first name, last name, and a GetFullName() function. Getters should all be const functions, of course.
• A void AddReservation() method that takes a reservation, passed by const reference, and adds it to the list of reserations unless there is already a reservation for the same flight on the same date, in which case the reservation is not added; there is no error message in this case.
• A void ListReservations() method that takes an ostream passed by reference and prints a list of the reservations for that passenger. • A bool HasReservation() method that takes a flight number and date (both strings) and returns true if the Passenger has a reservation for the specified flight on the specified date, false otherwise. Your program has 2 input files: • Passengers.txt. This file contains (oddly enough) passengers, giving their identifiers and first and last names, all separated by whitespace. There is no reservation information in this file.
• Reservations.txt. This file contains the list of flight reservation. Each line begins with a passenger ID, then a Reservation as defined above, formatted so it can be read in with the input operator. For the output file, list only those passengers holding reservations. This file should be sorted by passenger last name, with ties broken on passenger first name. (Hint: Overload comparison operators for the Passenger class to handle this sort.) Passengers with 5 or more reservations should be flagged by printing the line: ***FREQUENT FLYER*** before the passenger information. Print at least one line blank space, or a clear dividing marker of some type, between each passenger’s information. Development notes:
• Your main program should store passenger information in a dynamic array. This means your main program should have a pointer to Passenger as a variable, not an array of Passengers. When you open the file for input, start by allocating a small array, maybe 5 to 10 items. As the array fills, you will need to re-allocate a new dynamic array and copy data into it. (An old programmer’s rule of thumb is that if a dynamic data structure fills up, make the new one twice as big as the old one.)
• Read all the passengers into a single array, and then search the array as needed to find the passenger with a given ID number. (This may or may not be the most efficient way to solve the problem, but the point of the assignment is to give you practice with overloading operators, dynamic memory, and searching and sorting.)
• Your program will be tested against a different data file with the same format.
Developer notes: Passenger.txt has a ton of ID numbers containing 20 numbers with a tab space then Last name,Firstname.
EX:03444249137123814150 Jacobs, Britney
Reservations.txt has 27 reservations with ID numbers containg 20 number with a tab space then the date tab then another 3 number ID number then another tab and 3 letter airport and another tab then 3 letter airport.
EX: 33135695988395136288 08/25/2016 126 LTL RTX
Explanation / Answer
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class InternationalFlight extends JFrame
{
JComboBox CBFrom, CBTo, CBClass, CBAdult, CBChildren, CBInfant;
JLabel LFrom, LTo, LBookingDate, LClass, LAdult, LChildren, LInfant, LBookingDetails, LPassengerDetails, LDate, LImg1, LImg2, LNotes;
JTextField TFBookingDate;
Icon img1, img2;
JButton BFindFlight;
JPanel PPanel1, PPanel2;
LoginPage type1;
public InternationalFlight(LoginPage type1)
{
Container c =getContentPane();
c.setLayout(new BorderLayout());
String[] sItem1={"Trivandrum"};
String[] sItem2={"Bali","Bangkok","Cairo","CapeTown","Chicago","Dubai","Frankfurt","HongKong","Istanbul","London","LosAngeles","Melbourne","New York","Paris","Rome","SanFrancisco","shanghai","Singapore","Sydney","Toronto"};
String[] sItem3={"Economic","Business"};
this.type1 = type1;
PPanel1 = new JPanel(null);
PPanel1.setPreferredSize(new Dimension(500,200));
LBookingDetails = new JLabel("<html><b><font color="#C71585">Booking Details</font></b></html>");
LFrom = new JLabel("From :");
LTo = new JLabel("To :");
LBookingDate = new JLabel("Booking Date:");
LClass = new JLabel("Class :");
CBFrom = new JComboBox(sItem1);
CBTo = new JComboBox(sItem2);
CBClass = new JComboBox(sItem3);
TFBookingDate = new JTextField(10);
LDate = new JLabel("(DD/MM/YYYY)");
LDate.setForeground(Color.red);
img1=new ImageIcon("map1.jpg");
LImg1 = new JLabel(img1);
BFindFlight = new JButton("Find Flight");
LBookingDetails.setBounds(20,3,100,20);
LFrom.setBounds(20,40,100,20);
CBFrom.setBounds(100,40,100,20);
LTo.setBounds(20,100,100,20);
CBTo.setBounds(100,100,100,20);
LBookingDate.setBounds(14,160,100,20);
TFBookingDate.setBounds(100,160,100,20);
LDate.setBounds(210,160,100,20);
LClass.setBounds(20,220,100,20);
CBClass.setBounds(100,220,100,20);
BFindFlight.setBounds(50,270,100,25);
LImg1.setBounds(0,290,495,260);
PPanel1.add(LBookingDetails);
PPanel1.add(LFrom);
PPanel1.add(CBFrom);
PPanel1.add(LTo);
PPanel1.add(CBTo);
PPanel1.add(LBookingDate);
PPanel1.add(TFBookingDate);
PPanel1.add(LDate);
PPanel1.add(LClass);
PPanel1.add(CBClass);
PPanel1.add(BFindFlight);
PPanel1.add(LImg1);
PPanel1.setBackground(Color.white);
c.add(PPanel1,BorderLayout.WEST);
PPanel2 = new JPanel(null);
PPanel2.setPreferredSize(new Dimension(320,160));
LPassengerDetails=new JLabel("<html><b><font color="#C71585">PassengerDetails</font></b></html>");
LAdult = new JLabel("Adults(12+)");
LChildren = new JLabel("Children(2-11)");
LInfant = new JLabel("Infants(under 2)");
String[] item4={"1","2","3","4","5","6"};
CBAdult = new JComboBox(item4);
String[] item5={"0","1","2","3","4"};
CBChildren = new JComboBox(item5);
String[] item6={"0","1","2","3"};
CBInfant = new JComboBox(item6);
img2 = new ImageIcon("note_bg.gif");
LImg2 = new JLabel(img2);
LNotes = new JLabel("<html><body><p>NOTE: Bookings with International Credit Cards <p> have temporarily been suspended.This Service<p> will resume shortly and we will have a notice<p> posted on our website.We regret any <p>inconvenience caused to our passengers.</body></html>");
LPassengerDetails.setBounds(40,3,100,20);
LAdult.setBounds(40,40,100,20);
CBAdult.setBounds(140,40,100,20);
LChildren.setBounds(40,105,100,20);
CBChildren.setBounds(140,105,100,20);
LInfant.setBounds(40,170,100,20);
CBInfant.setBounds(140,170,100,20);
LImg2.setBounds(16,220,320,200);
LNotes.setBounds(55,240,380,180);
PPanel2.add(LPassengerDetails);
PPanel2.add(LAdult);
PPanel2.add(LChildren);
PPanel2.add(LInfant);
PPanel2.add(CBAdult);
PPanel2.add(CBChildren);
PPanel2.add(CBInfant);
PPanel2.add(LNotes);
PPanel2.add(LImg2);
PPanel2.setBackground(Color.white);
c.add(PPanel2,BorderLayout.EAST);
setSize(795,580);
setVisible(true);
BFindFlight.addActionListener(new button2(this, type1));
}
public static void main(String args[])
{
LoginPage type1=null;
new InternationalFlight(type1);
}
}
class button2 implements ActionListener
{
InternationalFlight type;
LoginPage type1;
button2(InternationalFlight type, LoginPage type1)
{
this.type = type;
this.type1 = type1;
}
public void actionPerformed(ActionEvent e)
{
String sFrom = (String)type.CBFrom.getSelectedItem();
String sTo = (String)type.CBTo.getSelectedItem();
String sClass = (String)type.CBClass.getSelectedItem();
String sBookingDate = type.TFBookingDate.getText();
Integer iPrice=0;
String sTime="";
Integer iAdult = Integer.parseInt((String)type.CBAdult.getSelectedItem());
Integer iChildren = Integer.parseInt((String)type.CBChildren.getSelectedItem());
Integer iInfant = Integer.parseInt((String)type.CBInfant.getSelectedItem());
int i = 0;
if(sClass.equals("Economic"))
{
try{
while(i<20)
{
if(type1.row2[i][1].equals(sTo))
{
iPrice = Integer.parseInt((String)type1.row2[i][2]);
sTime = (String)type1.row2[i][3];
break;
}
i++;
}
}catch(Exception e1)
{
JOptionPane.showMessageDialog(null, "You have no rights to access");
System.exit(0);
}
}
else
{
try
{
while(i<20)
{
if(type1.row2[i][1].equals(sTo))
{
iPrice = Integer.parseInt((String)type1.row4[i][2]);
sTime = (String)type1.row4[i][3];
break;
}
i++;
}
}catch(Exception e1)
{
JOptionPane.showMessageDialog(null, "You have no rights to access it");
System.exit(0);
}
}
type.setTitle(iPrice + " " + sTime);
iPrice = (iPrice*iAdult)+(iPrice*(iChildren/2));
int iCount=0;
int iSeatCount=0;
String[] sTempFrom=new String[1250];
String[] sTempTo=new String[1250];
String[] sTempClass=new String[1250];
String[] sTempBookingDate=new String[1250];
String[] sTempTime=new String[1250];
Integer[] iTempAdult=new Integer[1250];
Integer[] iTempChildren=new Integer[1250];
Integer[] iTempInfant=new Integer[1250];
Integer[] iTempPrice=new Integer[1250];
try
{
//read from data
Save1 save1;
ObjectInputStream OIS1 = new ObjectInputStream(new FileInputStream("save1"));
do
{
save1 = (Save1)OIS1.readObject();
sTempFrom[iCount] = save1.sFrom;
sTempTo[iCount] = save1.sTo;
sTempClass[iCount] = save1.sClass;
sTempBookingDate[iCount] = save1.sBookingDate;
sTempTime[iCount] = save1.sTime;
iTempAdult[iCount] = save1.iAdult;
iTempChildren[iCount] = save1.iChildren;
iTempInfant[iCount] = save1.iInfant;
iTempPrice[iCount] = save1.iPrice;
iCount++;
if(save1.sBookingDate.equals(sBookingDate))
if(save1.sTo.equals(sTo))
iSeatCount=iSeatCount + save1.iAdult + save1.iChildren + save1.iInfant;
}while(save1!=null);
OIS1.close();
}
catch(Exception e1)
{
}
iSeatCount = iSeatCount + iAdult + iChildren + iInfant;
if(iSeatCount > 60)
{
JOptionPane.showMessageDialog(null,"Seats are full. Sorry!");
}
else
{
int iChoice = JOptionPane.showConfirmDialog(null,"Seats available. Do you want to Book now?");
if(iChoice == JOptionPane.YES_OPTION)
{
new PrintTicket1(sFrom, sTo, sClass, iAdult, iChildren, iInfant, sBookingDate, iPrice, sTime);
try
{
//write into data
Save1 save2=new Save1(sFrom, sTo, sClass, iAdult, iChildren, iInfant, sBookingDate, iPrice, sTime);
ObjectOutputStream OOS1 = new ObjectOutputStream(new FileOutputStream("save1"));
for(i=0;i<iCount;i++)
{
Save1 temp1=new Save1(sTempFrom[i], sTempTo[i], sTempClass[i], iTempAdult[i], iTempChildren[i], iTempInfant[i], sTempBookingDate[i], iTempPrice[i], sTempTime[i]);
OOS1.writeObject(temp1);
System.out.println(temp1);
}
OOS1.writeObject(save2);
OOS1.close();
}catch(Exception e1)
{
System.out.println(e1);
}
}
else
{
}
}
}
}
class Save1 implements Serializable
{
String sFrom, sTo, sClass, sBookingDate, sTime;
Integer iPrice, iAdult, iChildren, iInfant;
// int iCount;
public Save1(String sFrom, String sTo, String sClass, Integer iAdult, Integer iChildren, Integer iInfant, String sBookingDate, Integer iPrice, String sTime)
{
this.sFrom=sFrom;
this.sTo=sTo;
this.sClass=sClass;
this.iAdult=iAdult;
this.iChildren=iChildren;
this.iInfant=iInfant;
this.sBookingDate=sBookingDate;
this.iPrice=iPrice;
this.sTime=sTime;
// this.iCount = iCount;
}
public String toString()
{
return sFrom+" "+sTo+" "+sClass+" "+iAdult+" "+iChildren+" "+iInfant+" "+sBookingDate+" "+iPrice+" "+sTime;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.