#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    6
    Rep Power
    0

    Travelling Ticket Booth


    The idea of this project that I'm working on is sort of like creating an online airplane ticket purchasing after user filling out all the information and type of ticket choice. The final text area will display all the information that use has been inputted and compute the total price.

    I got really stuck on the part that where I need to code to compute that total price and then pass it on to the confirmation text area. Could somebody help or could shine me out with some clear hints, please?

    Thanks In Advanced.
    Code:
    import java.applet.Applet;
    import java.awt.*;
    import java.awt.event.*;
    
    public class Part2 extends Applet implements ActionListener
    {
        String    itemDept="", itemArrive="",  nameStr="", phoneStr="",numTicket="", ticketType="";
        float totalFirst=0, totalBus=0, total;
        int type=0, ticket=0;
            Font         fontBold= new Font("Times New Roman", Font.BOLD, 15),
                         fontBoldItalic= new Font("Times New Roman", Font.BOLD+Font.ITALIC, 15);
            TextField    txtName= new TextField(15),
                         txtPhone=new TextField(15);
            Label        lblTheap = new Label("Theap Thickets Agency"),
                         lblDepart = new Label("Dept. City/Airport"),
                         lblArrive = new Label("Arrival City/Airport"),
                         lblName = new Label("Name"),
                         lblPhone= new Label("Phone No."),
                         lblNumTicket= new Label("Number of tickets"),
                         lblConfirm= new Label("Confirmation"),
                         lblTicketType= new Label ("Type of Tickets");
                         
            Button       btnBuyTicket = new Button("Buy Ticket(s)");
                         
            List         lstDept = new List(4),
                         lstArrive = new List(6);
            Choice choptions= new Choice();
            CheckboxGroup chkType=new CheckboxGroup();             
            Checkbox     chkFirstClass= new Checkbox("First Class (400 extra)", chkType, false),
                         chkEconClass= new Checkbox ("Economy Class", chkType, false),
                         chkBusClass= new Checkbox ("Business Class (200 extra)", chkType, false);
            TextArea     txta= new TextArea(30,10);
        
        public void init() 
        {
            add(lblTheap); add(lstDept); add(lblDepart); add(lblArrive);
            add(lblName); add(lblPhone); add(lblNumTicket); add(lblConfirm);
            add(lblTicketType); add(lstDept); add(lstArrive); add(btnBuyTicket);
            add(choptions); add(chkFirstClass); add(chkEconClass); add(chkBusClass);
            add(txta); add(txtName); add(txtPhone);
            lstDept.add("Ontario");
            lstDept.add("LAX");
            lstDept.add("John Wayne");
            lstDept.add("Ontario");
            lstDept.add("Verne");
            lstArrive.add("London       $400.00");
            lstArrive.add("Paris         $450.00");
            lstArrive.add("Las Vegas    $65.00");
            lstArrive.add("Hawaii       $550.00");
            lstArrive.add("New York      $325.00");
            lstArrive.add("Dallas       $275.00");
            choptions.addItem("1");
            choptions.addItem("2");
            choptions.addItem("3");
            choptions.addItem("4");
            choptions.addItem("5");
            
            
            btnBuyTicket.addActionListener(this);
            
            setLayout(null);
            
            lblTheap.setLocation(180,10); lblTheap.setSize(180,20); lblTheap.setFont(fontBoldItalic);  //Title
            
            lblDepart.setLocation(20,50); lblDepart.setSize(150,20); lblDepart.setFont(fontBold); //Depart
            
            lstDept.setLocation(25,80); lstDept.setSize(110,70);
            
            lblArrive.setLocation(170,50); lblArrive.setSize(150,20); lblArrive.setFont(fontBold); //Arrive
            
            lstArrive.setLocation(170,80); lstArrive.setSize(140,70);
            
            lblName.setLocation(340,50); lblName.setSize(100,20); lblName.setFont(fontBold); //Name Text
            
            txtName.setLocation(340,80); txtName.setSize(130,20);
            
            lblPhone.setLocation(340,110); lblPhone.setSize(100,20); lblPhone.setFont(fontBold); //Phone Text
            
            txtPhone.setLocation(340,140); txtPhone.setSize(130,20);
            
            lblNumTicket.setLocation(340,170); lblNumTicket.setSize(120,20); lblNumTicket.setFont(fontBold);  //Number Tickets
            
            choptions.setLocation(400,200); choptions.setSize(40,20);    //Option
            
            lblTicketType.setLocation(23,220); lblTicketType.setSize(200,25); lblTicketType.setFont(fontBold);
            
            chkFirstClass.setLocation(25,240); chkFirstClass.setSize(200,25);  //First Class
            
            chkEconClass.setLocation(23,260); chkEconClass.setSize(200,25);   //Econ Class
            
            chkBusClass.setLocation(23,280); chkBusClass.setSize(200,25);  //Business Class
            
            btnBuyTicket.setLocation(23,305); btnBuyTicket.setSize(100,35);
            
            lblConfirm.setLocation(250,220); lblConfirm.setSize(100,20); lblConfirm.setFont(fontBold);
            
            txta.setLocation(250,240); txta.setSize(150,100);
        }
        
        public void actionPerformed(ActionEvent e)
        {
            nameStr=txtName.getText();
            phoneStr=txtPhone.getText();
            ticket=choptions.getItemCount();
            if ( e.getSource() == btnBuyTicket )
            { 
                itemDept = lstDept.getSelectedItem();
                
                itemDept = lstDept.getSelectedItem();
                itemArrive = lstArrive.getSelectedItem();
                numTicket = choptions.getSelectedItem();
                for (int i=0; i<lstArrive.getItemCount(); ++i )
                {  
                    String item=lstArrive.getItem(i);
                    int pos=item.indexOf("$");
                    String s = item.substring(pos+1);
                    total = (total + Float.parseFloat(s));
                }
                if (chkFirstClass.getState()== true)
                {ticketType += ("First Class");
                totalFirst += (400*ticket);}
                if (chkEconClass.getState()== true)
                {ticketType += ("Economy Class");}
                if (chkBusClass.getState()== true)
                {ticketType += ("Business Class");
                totalBus += (200*ticket);}
                
                
               
                txta.setText("Name: "+nameStr+"\n"+"Phone No. "+phoneStr+"\n"+
                        "Dept City: "+lstDept+"\n")+"Arrive City: "+lstArrive+"\n"+
                        "No. of Tickets: "+numTicket+"\n"+
                        "Ticket type: "+ticketType+"\n"+"Total "+total;
            }
        }
    }
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    74
    Rep Power
    18
    Code:
                txta.setText("Name: "+nameStr+"\n"+"Phone No. "+phoneStr+"\n"+
                        "Dept City: "+lstDept+"\n")+"Arrive City: "+lstArrive+"\n"+
                        "No. of Tickets: "+numTicket+"\n"+
                        "Ticket type: "+ticketType+"\n"+"Total "+total;
            }
        }
    }
    This code does not compile: The parenthesis should be after total, not after lstDept.
    Are you sure that you should output lstDept and lstArrive, instead of itemDept and itemArrive?
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    6
    Rep Power
    0
    I Really Don't See That Mistake, Thank You For Pointing That Out For Me.

    The Real Frustrating Part For Me Is Getting That Final Total To Be Compute.

    For Example:
    1. If I Pick My Arrival Destination To Be London. The Cost Of Each Ticket Will Be $400.00
    2. I Would Like To Purchase 3 Tickets. So It Would Be (400 * 3);
    3. And Each Type Of Ticket Will be First Class So First Class Ticket Will be $400 Extra.(400 /*First Class Extra*/ * 3 /*Number Of Ticket*/);
    4. Total = ((400*3) + (400*3));
    5. Final Output Total Price: $ 2400

    But I Am Totally Clueless Of How To Code These Lines Inside Those <If> Statements.
  6. #4
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    How To Code These Lines Inside Those <If> Statements.
    What if statement?

    One nasty coding technique is to have code on the same line following a { which makes it hard to see the {
    and code on the same line as the pairing } which makes it hard to see the }.
    There shouldn't be any code on the same line following a {
    or on the same line before the }.
    Hiding the {s and the }s makes this code hard to read and understand.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2005
    Posts
    74
    Rep Power
    32
    What behaviour are you getting currently?

    Are you getting some total (even if it is incorrect) or are you getting nothing, or something else??

    Please provide a full picture of the problem.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    14
    Rep Power
    0
    Why do tou don't take it simple, using String class and do some like this

    METHOD USERSTUFF
    Read NAME
    Read CONTACT
    READ DESTINATION()


    METHOD DESTINATION ()
    PRINT DEST-A
    PRINT DEST-B
    PRINT DEST-C
    PRINT DEST-D
    PRINT DEST-E
    READ NUMBER OF DEST
    SWITCH (DEST)
    CASE 1 :
    etc...


    MAIN ()
    USERSTUFF()
    PRINTTICKET()

    the error probability decrease

IMN logo majestic logo threadwatch logo seochat tools logo