HELP! :D

booking system java vs javascript

Page 1 of 1

3 Replies - 790 Views - Last Post: 03 September 2008 - 06:15 AM

#1 Sanghe  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 02-September 08

HELP! :D

Posted 02 September 2008 - 07:30 AM

Hey guys!

Aight to start this off... I have not had much\any training with Java or Javascripting. But i did find some thing on this site that could help me a lot.

Train / Bus Reservation Example in Java...

import java.util.Scanner;
import java.util.Date;

public class reservation {

	// Create an array of 12 seats, 6 window and 6 aisle.
	private static int[] seats = new int[12];

	public static void main(String args[]) {
		System.out.println("Welcome to the DIC lovin train reservation system!");
		System.out.println("Code ninjas, code newbies, one fabulous DIC ride!");
		System.out.println();
		

		// Lets start by setting all seats equal to 0 (aka Empty)
		for (int i = 0; i < 12; i++) {
			seats[i] = 0;
		}

		// Setup our scanner and default the choice to window.
		Scanner s = new Scanner(System.in);
		int choice = 1;

		// Ask user for a window or an aisle seat and store their coice.
		System.out.print("Please enter 1 for window, 2 for aisle, or 0 to exit: ");
		choice = s.nextInt();


		// While their choice is not the one for exit, execute our booking.
		while (choice != 0) {
			int seatnumber = 0;


			// If they chose a window seat, attempt to book it.
			if (choice == 1) {
				seatnumber = bookWindow();


				// No window seats available, try booking an aisle seat for them instead.
				if (seatnumber == -1) {
					seatnumber = bookAisle();
				
					if (seatnumber != -1) {
						System.out.println("Sorry, we were not able to book a window seat. But do have an aisle seat.");
						printBoardingPass(seatnumber);
					}
				}
				else {
					// Booking a window seat was successful.
					System.out.println("You are in luck, we have a window seat available!");
					printBoardingPass(seatnumber);
				}
			}
			else if (choice == 2) {

				// If they chose booking an isle, check to see if it is available.
				seatnumber = bookAisle();
			
				// If not available, see if we have window seats available.
				if (seatnumber == -1) {
					seatnumber = bookWindow();

					if (seatnumber != -1) {
						System.out.println("Sorry, we were not able to book an aisle seat. But do have a window seat.");
						printBoardingPass(seatnumber);
					}
				}
				else {
					// Booking an aisle seat was successful.
					System.out.println("You are in luck, we have an aisle seat available!");
					printBoardingPass(seatnumber);
				}
			}
			else {
				// Print an error message if they did not choose 1, 2, or 0 for their choice.
				System.out.println("Invalid choice made. Please try again!");
				choice = 0;
			}


			// No window or aisle seats were available.
			if (seatnumber == -1) {
				System.out.println("We are sorry, there are no window or aisle seats available.");
				System.out.println();
			}


			// Reprompt for a choice
			System.out.print("Please enter 1 for window, 2 for aisle, or 0 to exit: ");
			choice = s.nextInt();
		}

			
	}


	// This function checks for window seats and returns seat number or -1 if full.
	private static int bookWindow() {
		for (int i = 0; i < 6; i++) {
			if (seats[i] == 0) {
				seats[i] = 1;
				return i + 1;
			}
		}
		return -1;
	}


	// This function checks to see if aisle seats were available, -1 if full.
	private static int bookAisle() {
		for (int i = 6; i < 12; i++) {
			if (seats[i] == 0) {
				seats[i] = 1;
				return i + 1;
			}
		}
		return -1;

	}


	// This simply prints out a nice little boarding pass message with their seat number and date of issue.
	private static void printBoardingPass(int seatnumber) {
		Date timenow = new Date();
		System.out.println();
		System.out.println("Date: " + timenow.toString());
		System.out.println("Boarding pass for seat number: " + seatnumber);
		System.out.println("This ticket is non-refundable and non-transferable.");
		System.out.println("Please be curteous, do not smoke. Enjoy your trip.");
		System.out.println();
	}



I need something similar but in javascript... where do I start? Can someone plz nudge me in the right direction? :D

Is This A Good Question/Topic? 0
  • +

Replies To: HELP! :D

#2 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1102
  • View blog
  • Posts: 6,909
  • Joined: 07-September 06

Re: HELP! :D

Posted 02 September 2008 - 03:02 PM

I would start by learning Javascript, for instance, in JS ou can't call things by int, double, float, boolean etc. They are all just "var" additionally there is no need to say public static void etc. Javascript only requires that you call your functions function.

Like so:

<script>
function test(){
  alert("TEST");
}
test();
</script>



For more infromation I suggest looking at w3schools.com (I believe that is the website)

HTH
Was This Post Helpful? 0
  • +
  • -

#3 _net  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 146
  • Joined: 23-September 07

Re: HELP! :D

Posted 02 September 2008 - 05:46 PM

Also there is a very big difference from Javascript and Java. :-) This thread should be under Java
Was This Post Helpful? 0
  • +
  • -

#4 Sanghe  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 02-September 08

Re: HELP! :D

Posted 03 September 2008 - 06:15 AM

Thanks for replying so fast guys!

Well I do understand that the previous code was a bit of a stretch, but I was\am a bit desperate :S

I have however found something that is much closer to what I was looking for. I just need to find a way to expand the code to be able to gather the information needed to make a "valid" flight reservation.

<?xml version = "1.0"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">


<html xmlns = "http://www.w3.org/1999/xhtml">

	<head>
		<title>Airline Reservation System</title>

		<script type = "text/javascript">
	  
		
		var input;
		var secondInput;
		var element;
		var secondElement;
		var firstCount = 0;
		var economyCount = 0;
		var seats = [ ,0,0,0,0,0,0,0,0,0,0];			
		
		function startArray()
		{
			

			for(var i=0; i<11; i++)
			{
				
					input = window.prompt("Please type 1 for First Class and Please type 2 for Economy.","0");

					if (input == 1 || input == 2)
					{
						element = linearSearch(seats);
						if (element==-1 && input == 1)
						{
							document.writeln("The First Class is already fully booked<br/>");
							secondQuestion(seats);
						}
						else if (element ==-1 && input == 2)
						{
							document.writeln("The Economy Class is already fully booked<br/>");
							secondQuestion(seats);
						}
						else
							boardingPass(input);	
					}
					
					
					else 
					{
						window.status = "Bye-bye!";
						System.exit(0);
					}
					
					
			}
		}
		
		function linearSearch(theArray)
		{
			if (input == 1)
			{
				for (var n=0; n<6; n++)
					if (theArray [n] == 0)
						return n;
			}
			else if (input == 2)
			{
				for (var n=6; n<11; n++)
					if (theArray [n] == 0)
						return n;
			}
			return -1;
		}

		function boardingPass(theInput)
		{
			if (input ==1)
			{	
				document.writeln("----------BOARDING PASS----------<br/>");
				document.writeln("You are allocated in the First Class<br/>");
				document.writeln("Your seat number is "+ element+"<br/>");
				 document.writeln("-----------------------------------------<br/>");
				seats[element]= 1;
				firstCount++;	
			}
			else if (input ==2)
			{	
				document.writeln("----------BOARDING PASS----------<br/>");
				document.writeln("You are allocated in the Economy Class<br/>");
				document.writeln("Your seat number is "+ element +"<br/>");
				document.writeln("-----------------------------------------<br/>");
				seats[element]= 1;
				economyCount++;	
			}
			
		}

		function secondQuestion(theArray)
		{
			if (input == 1)
			{
				for (var n=6; n<11;n++)
				{
					if (theArray [n] == 0)
					{
						secondInput = window.prompt("Do you want to move to Economy Class? (If YES, please press 1. If NO, please press 2)","0");
							if ( secondInput == 1)
							{
								
								input = 2;
								element=linearSearch(seats);
								document.writeln("You have been allocated to Economy Class<br/>");
								boardingPass(input);
								break;
							}
							else if (secondInput == 2)
							{
								document.writeln("Next flight leaves in 3 hours<br/>");
								break;
							}
					}
				}
			}
			else if (input == 2)
			{
				for (var n=0; n<6;n++)
				{
					if (theArray [n] == 0)
					{
						secondInput = window.prompt("Do you want to move to First Class? (If YES, please press 1. If NO, please press 2)","0");
							if ( secondInput == 1)
							{
								input = 1;
								element=linearSearch(seats);
								document.writeln("You have been allocated to First Class<br/>");
								boardingPass(input);
								break;
							}
							else if (secondInput == 2)
							{
								document.writeln("Next flight leaves in 3 hours<br/>");
								break;
							}
					}
				}
			}
			
		}


	  
		</script>

	</head>
	<body onload = "startArray()"></body>
</html>


any ideas? :D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1