Need help with the java code!

Thread Solved
Reply

Join Date: May 2008
Posts: 2
Reputation: zantex is an unknown quantity at this point 
Solved Threads: 0
zantex zantex is offline Offline
Newbie Poster

Need help with the java code!

 
0
  #1
May 30th, 2008
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.
  1. package Solution;
  2. import java.io.*;
  3. import java.util.*;
  4.  
  5. public class TourBookingSystem
  6. {
  7. private static final Scanner console = new Scanner (System.in);
  8.  
  9. private static Tour [] tours = new Tour[10];
  10. private static EquipmentHire [] eHires = new EquipmentHire[50];
  11.  
  12. private static int tourCount = 0;
  13. private static int hireCount = 0;
  14.  
  15. public static void main (String [] args)
  16. {
  17. char selection;
  18.  
  19. loadData();
  20.  
  21. do
  22. {
  23. printMenu();
  24. selection = readChar();
  25. selection = Character.toUpperCase(selection);
  26.  
  27. switch (selection)
  28. {
  29. case 'A':
  30. addTour();
  31. break;
  32. case 'B':
  33. listTours();
  34. break;
  35.  
  36. case 'C':
  37. addTourBooking();
  38. break;
  39.  
  40. case 'D':
  41. listTourBookings();
  42. break;
  43.  
  44. case 'E':
  45. hireEquipment();
  46. break;
  47.  
  48. case 'F':
  49. listEquipmentHires();
  50. break;
  51.  
  52. case 'G':
  53. cancelBooking();
  54. break;
  55.  
  56. case 'X':
  57. System.out.println("Writing data out to file");
  58. saveData();
  59.  
  60. default:
  61. System.out.println("Error - invalid option selected!");
  62. }
  63.  
  64. System.out.println();
  65.  
  66. } while (selection != 'X');
  67.  
  68. System.out.println("Program Terminating - goodbye!");
  69. System.out.println();
  70. }
  71.  
  72. public static void printMenu()
  73. {
  74. System.out.println("****** Tour Booking System Menu ******\n");
  75. System.out.println(" A - Add Tour");
  76. System.out.println(" B - List All Available Tours");
  77. System.out.println(" C - Add Tour Booking");
  78. System.out.println(" D - List All Tour Bookings");
  79. System.out.println(" E - Add Equipment Hire Booking");
  80. System.out.println(" F - List All Equipment Hire Bookings");
  81. System.out.println(" G - Cancel Tour Booking");
  82. System.out.println(" X - Exit");
  83. System.out.println();
  84. System.out.print("Enter your selection: ");
  85. }
  86.  
  87. // this method can be used to read any single-character input values
  88. // that your program requires from the keyboard
  89. public static char readChar()
  90. {
  91. String input;
  92. do
  93. {
  94. try
  95. {
  96. input = console.nextLine();
  97.  
  98. if (input.length() != 1)
  99. throw new Exception();
  100.  
  101. return Character.toUpperCase(input.charAt(0));
  102.  
  103.  
  104. }
  105. catch (Exception e)
  106. {
  107. System.out.println("Input must be a single character only - " +
  108. "please try again.");
  109. }
  110. }
  111. while (true);
  112. }
  113.  
  114. // this method can be used to read any integer values that
  115. // your program requires from the keyboard
  116. public static int readInt()
  117. {
  118. int input;
  119. do
  120. {
  121. try
  122. {
  123. input = console.nextInt();
  124.  
  125. // clear trailing newline from buffer
  126. if (console.hasNextLine())
  127. console.nextLine();
  128.  
  129. return input;
  130. }
  131. catch (InputMismatchException e)
  132. {
  133. System.out.println("Input must be a integer value - " +
  134. "please try again.");
  135. System.out.println();
  136.  
  137. // clear erroneous input from buffer
  138. if (console.hasNextLine())
  139. console.nextLine();
  140. }
  141. }
  142. while (true);
  143. }
  144.  
  145. public static double readDouble()
  146. {
  147. double input;
  148. do
  149. {
  150. try
  151. {
  152. input = console.nextDouble();
  153.  
  154. // clear trailing newline from buffer
  155. if (console.hasNextLine())
  156. console.nextLine();
  157.  
  158. return input;
  159. }
  160. catch (InputMismatchException e)
  161. {
  162. System.out.println("Input must be a double value - " +
  163. "please try again.");
  164. System.out.println();
  165.  
  166. // clear erroneous input from buffer
  167. if (console.hasNextLine())
  168. console.nextLine();
  169. }
  170. }
  171. while (true);
  172. }
  173. public static void addTour()
  174. {
  175. System.out.println("Add Tour Option Selected");
  176. {
  177. Tour temp;
  178.  
  179. String type;
  180. String tourID, tourDesc, date, guide;
  181. double tourFee;
  182. int size;
  183.  
  184. if (tourCount == tours.length)
  185. {
  186. System.out.println("Error - account list is full!");
  187. return;
  188. }
  189.  
  190. System.out.print("Enter Tour ID: ");
  191. tourID = console.nextLine();
  192.  
  193. System.out.print("Enter Tour Description: ");
  194. tourDesc = console.nextLine();
  195.  
  196. System.out.print("Enter Tour Fee: ");
  197. tourFee = console.nextDouble();
  198.  
  199. do
  200. {
  201. System.out.print("Enter Tour Type " +
  202. "(G for guide tour, U for no guide tour): ");
  203.  
  204. type = console.nextLine().toUpperCase();
  205.  
  206. System.out.println();
  207.  
  208. } while (type.equals("G") == false && type.equals("U") == false);
  209.  
  210. if (type.equals("U"))
  211. temp = new Tour(tourID, tourDesc, tourFee);
  212. else
  213. {
  214. System.out.print("Enter tour date: ");
  215. date = console.nextLine();
  216.  
  217. System.out.print("Enter tour size: ");
  218. size = console.nextInt();
  219.  
  220. System.out.print("Enter tour guide name: ");
  221. guide = console.nextLine();
  222.  
  223. temp = new GuidedTour(tourID, tourDesc, tourFee, date, size, guide);
  224. }
  225.  
  226. // clear the trailing newline left in the input buffer
  227.  
  228. if (console.hasNextLine())
  229. console.nextLine();
  230.  
  231.  
  232. // add the new object into the next empty spot in the array
  233. tours[tourCount] = temp;
  234. tourCount++;
  235.  
  236. }
  237. }
  238.  
  239. public static void listTours()
  240. {
  241. System.out.println("List All Tours Option Selected");
  242. if (tourCount == 0)
  243. {
  244. System.out.println("Tour List is Empty!");
  245. }
  246. else
  247. {
  248.  
  249. for (int i = 0; i < tourCount; i++)
  250. {
  251. tours[i].display();
  252. System.out.println();
  253. }
  254. }
  255. }
  256. public static void addTourBooking()
  257. {
  258.  
  259. }
  260. public static void listTourBookings()
  261. {
  262.  
  263. }
  264.  
  265. public static void hireEquipment()
  266. {
  267.  
  268. }
  269. public static void listEquipmentHires()
  270. {
  271.  
  272. }
  273. public static void cancelBooking()
  274. {
  275.  
  276. }
  277.  
  278. public static void loadData()
  279. {
  280.  
  281. }
  282.  
  283. public static void saveData()
  284. {
  285.  
  286. }
  287. }

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.
Attached Files
File Type: zip Solution.zip (4.2 KB, 5 views)
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 973
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: Need help with the java code!

 
0
  #2
Jun 4th, 2008
Line 203 is the problem--

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

Change it to--

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

--and you should be golden.
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 137
Reputation: PoovenM is on a distinguished road 
Solved Threads: 11
PoovenM PoovenM is offline Offline
Junior Poster

Re: Need help with the java code!

 
0
  #3
Jun 4th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 973
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: Need help with the java code!

 
0
  #4
Jun 4th, 2008
Originally Posted by PoovenM View Post
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.
Last edited by Alex Edwards; Jun 4th, 2008 at 4:11 pm.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 973
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: Need help with the java code!

 
0
  #5
Jun 4th, 2008
  1.  
  2. do
  3. {
  4. System.out.print("Enter Tour Type (G for guide tour, U for no guide tour): ");
  5.  
  6. String temp2 = console.nextLine();//apparantly there was still data in the stream that needed to be flushed?
  7.  
  8. type = console.nextLine().toUpperCase();
  9.  
  10. System.out.print("");
  11.  
  12. } while (!type.equalsIgnoreCase("G") && !type.equalsIgnoreCase("U"));

Sorry for the multi-posts, unfortunately you can't edit your previous post after 30 minutes.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 2
Reputation: zantex is an unknown quantity at this point 
Solved Threads: 0
zantex zantex is offline Offline
Newbie Poster

Re: Need help with the java code!

 
0
  #6
Jun 6th, 2008
thx for helping guy
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC