banner



How To Create Train Ticketing System In Java Netbeans

#1

  • New D.I.C Head

Reputation: 0

  • View blog
  • Posts: 6
  • Joined: 24-June 09

Railway Reservation System

Posted 24 June 2009 - 01:15 PM

Quote

Write a program that simulates the movement of 4 trains A,B,C,D on a single straight railway with 100km length. On this railway there are 5 stations stA, stB, stC, stD, stE in 0, 25, 50, 75, 100 km distances. When time t=0 train A begins from stA to stE, train B from stB to stE and in the opposite direction train C from stE to stA and train D from stD to stB. Trains A,B,C,D have random velocities from a uniform distribution between 0-180km/h.In whole journey each one has constant velocity, with negligible spaces of acceleration and deceleration nearby stations. In every station each train must wait at least 5minutes and the train leaves only when the part of line till the next station is free.

We suppose that we know the position of each train at any time but not its velocity, and we should not make a priori the whole movement programming. This open-loop solution wouldn't need threads, but it is more complicated. You can assume if you want that in case of a deadlock we choose the trains with this priority A,B,C,D. The program ends when all trains go to their final destination(station).
The program must print the time-table that every train has ran, which is arrival/departure from/to every station.

Here is my code:

import java.awt.*; import java.lang.Math; import java.lang.Thread;  public class Lines { 	public boolean[] lineStatus = new boolean[5];  	public synchronized void setRed (int lineNo) 	{ 		lineStatus[lineNo]=true;	 //true means 'under use' 	}   	public synchronized void setGreen (int lineNo) 	{ 		lineStatus[lineNo]=false; 	}  	// the following method is never called (for the moment) 	public synchronized void waitGreen(int lineNo)	// wait until a green light appears at lineNo 	{ 		 if(lineStatus[lineNo]=true) 		 { 			  System.out.println("Line " + lineNo+ " is available, NO NEED TO WAIT"); 			  //return; 		 } 		 else{ 			 System.out.println("Line " + lineNo+ " is  not available!!!!! You have TO WAIT"); 		 } 	 }  }            
              import java.awt.*; import java.lang.Math; import java.lang.Thread;  public class Train2 extends Thread { 	public String trainName; 	public float speed; 	public int distanceRun=0; 	public float timeRun=0; 	public int distanceMax=0; 	public Lines line; 	public int startStation; 	public int currStation;    	public Train2(String aName, Lines theLine,int stStation, int finalDistance,int currentStation ) 	{ 		trainName= aName; 		line= theLine; 		startStation= stStation; 		distanceMax= finalDistance; 		speed=(float)Math.random()*180.0f; 		currStation=currentStation; 	}   	public void run() 	{ 		System.out.println(trainName + " begins from Station no " + startStation + " with travelling " + distanceRun + "km.");   		while(distanceRun < distanceMax) 		{ 			try{ 				sleep(2000); 			} 			catch(InterruptedException e) {}  			if( startStation<2) 			{  				System.out.println(trainName + " attempts to use line " + currStation); 				line.waitGreen(currStation);  				System.out.println("the line no " + currStation + " is free? " + line.lineStatus[currStation]);  				if(line.lineStatus[currStation]= true) 				{ 					//line.setRed(currStation); 					System.out.println(trainName + " can pass from line " + currStation);  					distanceRun= distanceRun + 25; 					try{ 						sleep( (int)(distanceRun/speed)*1000); 					} 					catch(InterruptedException e) {}  					line.setGreen(currStation); 					currStation= currStation+ 1; 					System.out.println("ouaou " + trainName + " has arrived at Station " + currStation + " add has made " + distanceRun + "km"); 				} 				else 				{	System.out.println("what the fuck"); 					try{ 						sleep(5000); 					} 					catch(InterruptedException e) {} 				} 			} 		} 		System.out.println(trainName + " has arrived at last at his lastStation witch is " + currStation + " and has made " + distanceRun + " \n"); 	}  }            
import java.awt.*; import java.lang.Math; import java.lang.Thread;  public class TrainTest2 {      public static void main(String[] args)    { 	 Lines line = new Lines(); 	 line.lineStatus[0]= false; 	 line.lineStatus[1]= false; 	 line.lineStatus[2]= false; 	 line.lineStatus[3]= false;  	Train2 trA = new Train2("Train A", line, 0, 100, 0);  //String aName, Lines theLine,int stStation, int finalDistance 	Train2 trB = new Train2("Train B", line, 1, 75, 1); 	Train3 trC = new Train3("Train C", line, 4, 100, 4); 	Train3 trD = new Train3("Train D", line, 3, 50, 3);  	System.out.println(trA.trainName + " starts at " + trA.currStation + " and has speed "+ trA.speed + " km/h"); 	System.out.println(trB.trainName + " starts at " + trB.currStation + " and has speed "+ trB.speed + " km/h"); 	System.out.println(trC.trainName + " starts at " + trC.currStation + " and has speed "+ trC.speed + " km/h"); 	System.out.println(trD.trainName + " starts at " + trD.currStation + " and has speed "+ trD.speed + " km/h");  	trA.start(); 	trB.start(); 	trC.start(); 	trD.start(); 	} }            
import java.awt.*; import java.lang.Math; import java.lang.Thread;  public class Train3 extends Thread { 	public String trainName; 	public float speed; 	public int distanceRun=0; 	public float timeRun=0; 	public int distanceMax=0; 	public Lines line; 	public int startStation; 	public int currStation;    	public Train3(String aName, Lines theLine,int stStation, int finalDistance,int currentStation ) 	{ 		trainName= aName; 		line= theLine; 		startStation= stStation; 		distanceMax= finalDistance; 		speed=(float)Math.random()*180.0f; 		currStation=currentStation; 	}   	public void run() 	{ 		System.out.println(trainName + " begins from Station no " + startStation + " with travelling " + distanceRun + "km.");   		while(distanceRun < distanceMax) 		{ 			try{ 				sleep(2000); 			} 			catch(InterruptedException e) {}     				System.out.println(trainName + " attempts to use line " + (currStation-1)); 				line.waitGreen(currStation-1);  				System.out.println("the line no " + currStation + " is free? " + line.lineStatus[currStation]);  				if(line.lineStatus[currStation-1]= true) 				{ 					//line.setRed(currStation-1); 					System.out.println(trainName + " can pass from line " + (currStation-1)); 					distanceRun= distanceRun + 25; 					timeRun= (float)(timeRun+ (distanceRun/speed)*60 + 5); 					try{ 						sleep( (int)(distanceRun/speed)*1000); 						} 						catch(InterruptedException e) {} 						line.setGreen(currStation-1); 						currStation= currStation- 1; 						System.out.println("ouaou " + trainName + " has arrived at Station " + (currStation) + " add has made " + distanceRun + "km and time " + timeRun + " min\n"); 				} 				else 					{ 					try{ 						sleep(5000); 						} 						catch(InterruptedException e) {} 					}   		} 		System.out.println(trainName + " has arrived at last at his lastStation witch is " + currStation + " and has made " + distanceRun+ " km and time " + timeRun +  "min\n"); 	} }            

This post has been edited by maikol: 24 June 2009 - 01:23 PM


Is This A Good Question/Topic? 0

  • +

#2 maikol User is offline

  • New D.I.C Head

Reputation: 0

  • View blog
  • Posts: 6
  • Joined: 24-June 09

Re: Railway Reservation System

Posted 24 June 2009 - 01:21 PM

Well i made two classes Train2 and Train3 because when i tried to use 1 class and put a for syntax with if...else, but the program didn't enter the else part(don't know why...).Anyway i want you to take a look at it and tell me what else i should write to my program because i think that something is missing...Is it necessary to use wait() and notifyAll() methods???And if yes, what exactly should i write???I have to give this paper tomorrow, so if anyone can answer me I would appreciate it if he/she did this as soon as possible...

Thank you for your time...

This post has been edited by maikol: 24 June 2009 - 01:26 PM

#3 nick2price User is offline

Reputation: 565

  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Railway Reservation System

Posted 24 June 2009 - 01:34 PM

Arnt you a different poster to the original poster?

How To Create Train Ticketing System In Java Netbeans

Source: https://www.dreamincode.net/forums/topic/111674-railway-reservation-system/

Posted by: mclaughlinfragend.blogspot.com

0 Response to "How To Create Train Ticketing System In Java Netbeans"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel