Results 1 to 4 of 4

Thread: Searching Arrays Using A Loop

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2008
    Posts
    1

    Searching Arrays Using A Loop

    Hi all, im new to all this java stuff so please go easy on me!

    Im doing business computing at college, my first assignment is to write a basic ticketing system for an airline company that involves booking a luxury seat, an executive seat and searching for a booked ticket.

    I have almost completed it, i have the booking parts working with arrays but I have no idea on how to search the arrays and print out the results.

    My code so for is as follows:
    java Code:
    1. import java.util.Scanner;
    2.  
    3. public class TicketSystem
    4. {
    5.  
    6. public static void menu()
    7.  
    8. {
    9. System.out.println("Welcome To CCN Jets");
    10. System.out.println("Please Choose One Of The Following Options By Typing A Number Between 1 & 4 (Inclusive) And Then Pressing The Enter Key");
    11. System.out.println("1 - Book a Luxury Seat");
    12. System.out.println("2 - Book an Executive Seat");
    13. System.out.println("3 - Display Booking Details");
    14. System.out.println("4 - Exit");
    15. }
    16.  
    17. public static void main (String [] args)
    18.  
    19. {
    20.  
    21. Scanner input = new Scanner(System.in);
    22. input.useDelimiter("\n"); //ignores spaces in strings
    23.  
    24. String [] title = new String [10];
    25. String [] firstName = new String [10];
    26. String [] surname = new String [10];
    27. String [] address = new String [10];
    28. String [] postcode = new String [10];
    29. String [] telephoneNumber = new String [10];
    30.  
    31. int details=0;
    32. int bookingReference=100;
    33. int seatNumber=0;
    34. int howMany=10;
    35. int luxuryAvailable=6;
    36. int executiveAvailable=4;
    37. int option;
    38.  
    39. menu();
    40. option = input.nextInt();
    41.  
    42.  
    43. if (option == 1);
    44.  
    45. {
    46. System.out.println("Thank You For Choosing To Book A Luxury Seat");
    47. System.out.println("We Now Need to Take A Few Details From You");
    48. System.out.println("========================================");
    49.  
    50. System.out.println("How Many Tickets Would You Like To Book? ");
    51. howMany = input.nextInt();
    52.  
    53.  
    54. {
    55. if ((howMany < 1 || (howMany > luxuryAvailable)))
    56. System.out.print("There Is Not Enough Tickets Available");
    57.  
    58. else
    59. luxuryAvailable = luxuryAvailable - howMany;
    60. System.out.println("Luxury Seat Tickets Available: " +luxuryAvailable );
    61.  
    62. }
    63.  
    64.  
    65. System.out.println("========================================");
    66.  
    67. System.out.println("Mr/Mrs/Miss/Ms/Dr?");
    68. title[details] = input.next();
    69.  
    70. System.out.println("What Is Your First Name?");
    71. firstName[details] = input.next();
    72.  
    73. System.out.println("What Is Your Surname?");
    74. surname[details] = input.next();
    75.  
    76. System.out.println("What Is Your Address?");
    77. address[details] = input.next();
    78.  
    79. System.out.println("What Is Your Postcode?");
    80. postcode[details] = input.next();
    81.  
    82. System.out.println("What Is Your Phone Number?");
    83. telephoneNumber[details] = input.next();
    84.  
    85. System.out.println("========================================");
    86. System.out.println("Thank You. You Have Successfully Booked A Ticket For A Luxury Seat");
    87. bookingReference ++; //booking reference incremented
    88. System.out.println("Your Booking Reference Is:" +bookingReference);
    89.  
    90. seatNumber ++;
    91. System.out.println("Your Seat Number Is: " +seatNumber);
    92.  
    93. System.out.println("Your Seat Class Is Luxury");
    94. System.out.println("You Have Booked Your Tickets Under The Name: ");
    95. System.out.println(firstName[details]);
    96.  
    97. System.out.println("========================================");
    98.  
    99. menu();
    100. option = input.nextInt();
    101.  
    102. }
    103.  
    104. if (option == 2);
    105.  
    106. {
    107. System.out.println("Thank You For Choosing To Book An Executive Seat");
    108. System.out.println("We Now Need to Take A Few Details From You");
    109. System.out.println("========================================");
    110.  
    111.  
    112. System.out.println("How Many Tickets Would You Like To Book? ");
    113. howMany = input.nextInt();
    114.  
    115.  
    116. {
    117. if ((howMany < 1 || (howMany > luxuryAvailable)))
    118. System.out.print("There Is Not Enough Tickets Available");
    119.  
    120. else
    121. luxuryAvailable = luxuryAvailable - howMany;
    122. System.out.println("Luxury Seat Tickets Available: " +luxuryAvailable );
    123.  
    124. }
    125.  
    126. System.out.println("========================================");
    127.  
    128. System.out.println("Mr/Mrs/Miss/Ms/Dr?");
    129. title[details] = input.next();
    130.  
    131. System.out.println("What Is Your First Name?");
    132. firstName[details] = input.next();
    133.  
    134. System.out.println("What Is Your Surname?");
    135. surname[details] = input.next();
    136.  
    137. System.out.println("What Is Your Address?");
    138. address[details] = input.next();
    139.  
    140. System.out.println("What Is Your Postcode?");
    141. postcode[details] = input.next();
    142.  
    143. System.out.println("What Is Your Phone Number?");
    144. telephoneNumber[details] = input.next();
    145.  
    146. System.out.println("========================================");
    147.  
    148. System.out.println("Thank You. You Have Successfully Booked A Ticket For An Executive Seat");
    149. bookingReference ++; //booking reference incremented
    150. System.out.println("Your Booking Reference Is:" +bookingReference);
    151.  
    152. seatNumber ++;
    153. System.out.println("Your Seat Number Is: " +seatNumber);
    154.  
    155. System.out.println("Your Seat Class Is Executive");
    156. System.out.println("You Have Booked Tickets Under The Name: ");
    157. System.out.print(firstName[details]);
    158.  
    159. System.out.println("========================================");
    160.  
    161. menu();
    162. option = input.nextInt();
    163.  
    164.  
    165. }
    166.  
    167. if (option == 3)
    168.  
    169.  
    170. System.out.println("You have Chosen To View Your Booking Details");
    171. System.out.println("Please Enter Your Name To Continue:");
    172.  
    173.  
    174.  
    175. System.out.print(title[details]);
    176. System.out.print(surname[details]);
    177. System.out.print(address[details]);
    178. System.out.print(postcode[details]);
    179. System.out.print(telephoneNumber[details]);
    180.  
    181. if (option == 4)
    182.  
    183. System.out.println("Thank You For Choosing CCN Jets");
    184. System.out.println("Goodbye!");
    185.  
    186.  
    187.  
    188.  
    189. }
    190.  
    191. }
    Somebody in my class suggested using classes but i have looked at his and although it is working it is very complicated way of doing it. I know it can be done using a loop but im not quite sure on how to write the code.

    Ive included my code as i thought it would help anyone into coming up with a solution to my problem.

    Any help would be greatly appreciated!
    Thanks in advance, Jamie.
    Last edited by Hack; Dec 30th, 2008 at 07:32 AM. Reason: Added Highlight Tags

  2. #2
    Frenzied Member oceanebelle's Avatar
    Join Date
    Jun 2005
    Location
    my n00k.
    Posts
    1,064

    Re: Searching Arrays Using A Loop

    Well, there are lots of things going wrong here.

    First, your array will only ever have 1 data in. The one in index 0. That means when someone new books a ticket the old one will get overwritten.

    Next the reference Number. The reference number should be the "key" to extract the data here but right now. I don't think it's tied to any record at all.

    You could do very well with using a Booking class and then use a HashMap.
    A hash map has key and value. the key will be your booking reference number and the value will be the booking object that you instantiate everytime someone registers.

    searching a map would just be a matter of getting it, not looping through it.

  3. #3
    Frenzied Member oceanebelle's Avatar
    Join Date
    Jun 2005
    Location
    my n00k.
    Posts
    1,064

    Re: Searching Arrays Using A Loop

    Also, using classes should not hinder you from using java properly. Arrays are old school.

  4. #4
    Frenzied Member oceanebelle's Avatar
    Join Date
    Jun 2005
    Location
    my n00k.
    Posts
    1,064

    Re: Searching Arrays Using A Loop

    I'm demostrating here using a class and hashmap.
    Very simple. The Bookings class does not have the proper setter and getter class because I didn't want to put too much code. this is just to demostrate things.

    Code:
    import java.util.Map;
    import java.util.HashMap;
    
    public class BookingMain {
       static int referenceNos = 100;
       static Map<Integer, Booking> bookings = new HashMap<Integer, Booking>();
    
        public static void main(String... args){
            // call create booking to create the booking and add to the bookings.
            createBooking();
    
            // search the bookings given a reference no.
            Booking b = bookings.get(100);
            if(b != null)
            {
                // print the details
                System.out.println(b.title);
                System.out.println(b.firstName);
                System.out.println(b.surName);
            }
            else
            {
                System.out.println("no booking found.");
            }
        }
    
        private static Booking createBooking(){
            Booking b = new Booking();
            b.title = "title input";
            b.firstName = "first name input";
            b.surName = "surname input";
            b.address = "address input";
            b.postCode = "postcode input";
            b.phoneNumber = "phone number input";
            b.referenceNo = referenceNos++;
    
            // add the booking into the list of all bookings
            bookings.put(b.referenceNo, b);
    
            return b;
        }
    }
    
    class Booking
    {
        String title;
        String firstName;
        String surName;
        String address;
        String postCode;
        String phoneNumber;
        Integer referenceNo;
        String seatNo;
        String seatClass;
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width