Hi there guys!
Can someone help me with the part add tour,when I run it, its always print the same line 2 times and then when I enter the data for guide tour which should be store in the memory, but when using the display method it didn't appear like i want.

package Solution;
import java.io.*;
import java.util.*;

public class TourBookingSystem
{
   private static final Scanner console = new Scanner (System.in);
     
   private static Tour [] tours = new Tour[10];
   private static EquipmentHire [] eHires = new EquipmentHire[50];
       
   private static int tourCount = 0;
   private static int hireCount = 0;
   
   public static void main (String [] args)
   {
      char selection;
      
      loadData();
      
      do
      {
         printMenu();
         selection = readChar();
         selection = Character.toUpperCase(selection);
            
         switch (selection)
         {
            case 'A':
               addTour();
               break;
            case 'B':
               listTours();
               break;
               
            case 'C':
               addTourBooking();
               break;

            case 'D':
               listTourBookings();
               break;
               
            case 'E':
               hireEquipment();
               break;

            case 'F':
               listEquipmentHires();
               break;

            case 'G':
               cancelBooking();
               break;

            case 'X':
              System.out.println("Writing data out to file");
              saveData();

            default:
               System.out.println("Error - invalid option selected!");
         }
         
         System.out.println();

      } while (selection != 'X');
      
      System.out.println("Program Terminating - goodbye!");
      System.out.println();
   }
   
   public static void printMenu()
   {
      System.out.println("****** Tour Booking System Menu ******\n");
      System.out.println("   A   -   Add Tour");
      System.out.println("   B   -   List All Available Tours");
      System.out.println("   C   -   Add Tour Booking");
      System.out.println("   D   -   List All Tour Bookings");
      System.out.println("   E   -   Add Equipment Hire Booking");
      System.out.println("   F   -   List All Equipment Hire Bookings");
      System.out.println("   G   -   Cancel Tour Booking");
      System.out.println("   X   -   Exit");
      System.out.println();
      System.out.print("Enter your selection: ");
   }
   
   // this method can be used to read any single-character input values 
   // that your program requires from the keyboard
   public static char readChar()
   {
      String input;
      do
      {
         try
         {     
            input = console.nextLine();
            
            if (input.length() != 1)
               throw new Exception();
               
            return Character.toUpperCase(input.charAt(0));
            

         }
         catch (Exception  e)
         {
            System.out.println("Input must be a single character only - " +
                               "please try again.");
         }
      }
      while (true);
   }
   
   // this method can be used to read any integer values that
   // your program requires from the keyboard
   public static int readInt()
   {
      int input;
      do
      {
         try
         {     
            input = console.nextInt();
            
            // clear trailing newline from buffer
            if (console.hasNextLine())
               console.nextLine();
               
            return input; 
         }
         catch (InputMismatchException e)
         {
            System.out.println("Input must be a integer value - " +
                               "please try again.");
            System.out.println();
            
            // clear erroneous input from buffer
            if (console.hasNextLine())
               console.nextLine();
         }
      }
      while (true);
   }
   
   public static double readDouble()
   {
      double input;
      do
      {
         try
         {     
            input = console.nextDouble();
            
            // clear trailing newline from buffer
            if (console.hasNextLine())
               console.nextLine();
               
            return input; 
         }
         catch (InputMismatchException e)
         {
            System.out.println("Input must be a double value - " +
                               "please try again.");
            System.out.println();
            
            // clear erroneous input from buffer
            if (console.hasNextLine())
               console.nextLine();
         }
      }
      while (true);
   }
   public static void addTour()
   {
       System.out.println("Add Tour Option Selected");
      {
         Tour temp;

         String type;
         String tourID, tourDesc, date, guide;
         double tourFee;
         int size;

         if (tourCount == tours.length)
         {
            System.out.println("Error - account list is full!");
            return;
         }
             
         System.out.print("Enter Tour ID: ");
         tourID = console.nextLine();
         
         System.out.print("Enter Tour Description: ");
         tourDesc = console.nextLine();
         
         System.out.print("Enter Tour Fee: ");
         tourFee = console.nextDouble();
        
         do
         {
            System.out.print("Enter Tour Type " +
                             "(G for guide tour, U for no guide tour): ");

            type = console.nextLine().toUpperCase();
            
            System.out.println();
            
         } while (type.equals("G") == false && type.equals("U") == false);
        
         if (type.equals("U"))
            temp = new Tour(tourID, tourDesc, tourFee);
         else
         {
            System.out.print("Enter tour date: ");
            date = console.nextLine();
            
            System.out.print("Enter tour size: ");
            size = console.nextInt();
            
            System.out.print("Enter tour guide name: ");
            guide = console.nextLine();
            
            temp = new GuidedTour(tourID, tourDesc, tourFee, date, size, guide);
         }

         // clear the trailing newline left in the input buffer

         if (console.hasNextLine())
            console.nextLine();


         // add the new object into the next empty spot in the array
         tours[tourCount] = temp;
         tourCount++;
          
      }
   }
   
   public static void listTours()
   {
      System.out.println("List All Tours Option Selected");
      if (tourCount == 0)
      {
         System.out.println("Tour List is Empty!");
      }
      else
      {

         for (int i = 0; i < tourCount; i++)
         {
            tours[i].display();
            System.out.println();
         }
      }
   }
   public static void addTourBooking()
   {
      
   }
   public static void listTourBookings()
   {
      
   }

   public static void hireEquipment()
   {
      
   }
   public static void listEquipmentHires()
   {
     
   }
   public static void cancelBooking()
   {
   
   }
   
   public static void loadData()
   {
     
   }
 
   public static void saveData()
   {
     
   }
}

And 1 more thing that how can i save the data in array into a file?
I attached the rest of the class so if someone want to run it.

shresthanewa commented: Very nice +0

Recommended Answers

All 5 Replies

Line 203 is the problem--

203 type = console.nextLine().toUpperCase();

Change it to--

type = console.next().toUpperCase();

--and you should be golden.

I've not checked if Alex is correct, but as per your question regarding the storage of your array information. It can so easily be done by writing the entire array object to a file.

The is a ObjectOutputStream class and if you check out the API you'd see an example of how this would be done. Alternatively you can write the contents of the array in a text file using a looping structure that reads each element of the array. You can then read the contents back with the Scanner class.

I've not checked if Alex is correct, but as per your question regarding the storage of your array information. It can so easily be done by writing the entire array object to a file.

The is a ObjectOutputStream class and if you check out the API you'd see an example of how this would be done. Alternatively you can write the contents of the array in a text file using a looping structure that reads each element of the array. You can then read the contents back with the Scanner class.

Whoops i was tired when I replied, it's definitely not the solution.

I was trying to figure out if it was the do-while loop repeating or if it was the information on the screen reprinted and it's obviously not the 2nd option. My apologies.

do
         {
            System.out.print("Enter Tour Type (G for guide tour, U for no guide tour): ");

			String temp2 = console.nextLine();//apparantly there was still data in the stream that needed to be flushed?

            type = console.nextLine().toUpperCase();

            System.out.print("");

         } while (!type.equalsIgnoreCase("G") && !type.equalsIgnoreCase("U"));

Sorry for the multi-posts, unfortunately you can't edit your previous post after 30 minutes.

thx for helping guy

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.