Program not getting the expected output
using System;
using System.Threading;
using System.Collections;
using System.Collections.Generic;
namespace ThreadSample
{
class FunWithVehicles
{
public void VehicleThread1()
{
Console.WriteLine("Type Of Vehicle Is Car\nRegisteration
Number Is: TN0098 1234\nLicense Number Is
APK32456\nVehicle Number Is 1");
}
public void VehicleThread2()
{
Console.WriteLine("Type Of Vehicle Is Van\nRegisteration
Number Is: TN0012 2385\nLicense Number Is
UKL37899\nVehicle Number Is 2");
}
public void VehicleThread3()
{
Console.WriteLine("Type Of Vehicle Is
Truck\nRegisteration Number Is: TN1798 8907\nLicense
Number Is MNT59863\nVehicle Number Is 3");
}
public void VehicleThread4()
{
Console.WriteLine("Type Of Vehicle Is
Tanker\nRegisteration Number Is: TN3987 5357\nLicense
Number Is RTJ23498\nVehicle Number Is 4");
}
public void VehicleThread5()
{
Console.WriteLine("Type Of Vehicle Is Bus\nRegisteration
Number Is: TN9768 3212\nLicense Number Is
RTJ98734\nVehicle Number Is 5");
}
}
class TollGate
{
public void Check(int []numbers, int Token)
{
for (int i = 0; i < numbers.Length; i++)
{
if ( Token > numbers[i])
{
Console.WriteLine("You Got To Wait");
return;
}
}
}
}
class Simulate
{
public static void Main()
{
FunWithVehicles f = new FunWithVehicles();
int[] numbers = new int[5] {1, 2, 3, 4, 5 };
ArrayList list = new ArrayList(numbers);
Random rnd = new Random();
int r = rnd.Next(numbers.Length);
int Token = (numbers[r]);
TollGate T = new TollGate();
if ( Token == 1)
{
ThreadStart Ref1 = new
ThreadStart(f.VehicleThread1);
Thread Th1 = new Thread(Ref1);
Th1.Start();
}
if ( Token == 2 )
{
ThreadStart Ref2 = new
ThreadStart(f.VehicleThread2);
Thread Th2 = new Thread(Ref2);
Th2.Start();
}
if ( Token == 3)
{
ThreadStart Ref3 = new
ThreadStart(f.VehicleThread3);
Thread Th3 = new Thread(Ref3);
Th3.Start();
}
if ( Token == 4)
{
ThreadStart Ref4 = new
ThreadStart(f.VehicleThread4);
Thread Th4 = new Thread(Ref4);
Th4.Start();
}
if ( Token == 5)
{
ThreadStart Ref5 = new
ThreadStart(f.VehicleThread5);
Thread Th5 = new Thread(Ref5);
Th5.Start();
}
T.Check(numbers, Token);
if (Token == 1 )
{
Console.WriteLine("Printing Toll Pass\n\"Happy
Journey\"");
list.RemoveAt(0);
numbers = list.ToArray(typeof(int)) as int[];
r = rnd.Next(numbers.Length);
Token = (numbers[r]);
T.Check(numbers, Token);
if ( Token == 2)
Console.WriteLine("Printing Toll Pass\n\"Happy
Journey\"");
list.RemoveAt(1);
numbers = list.ToArray(typeof(int)) as int[];
r = rnd.Next(numbers.Length);
Token = (numbers[r]);
T.Check(numbers, Token);
if ( Token == 3)
Console.WriteLine("Printing Toll
Pass\n\"Happy Journey\"");
list.RemoveAt(2);
numbers = list.ToArray(typeof(int)) as int[];
r = rnd.Next(numbers.Length);
Token = (numbers[r]);
T.Check(numbers, Token);
if ( Token == 4)
Console.WriteLine("Printing Toll
Pass\n\"Happy Journey\"");
list.RemoveAt(3);
numbers = list.ToArray(typeof(int)) as
int[];
r = rnd.Next(numbers.Length);
Token = (numbers[r]);
T.Check(numbers, Token);
if ( Token == 5)
Console.WriteLine("Printing Toll
Pass\n\"Happy Journey\"");
list.RemoveAt(4);
numbers =
list.ToArray(typeof(int)) as
int[];
r = rnd.Next(numbers.Length);
Token = (numbers[r]);
T.Check(numbers, Token);
}
}
}
}
The following program has been created for the following purpose. 1, 5
different vehicle threads has been created. 2, 1 - 5 number has been
allotted for each thread and stored in array 3, Out of this any one number
is randomly extracted 4, A condition which checks for the availability of
preceding numbers if no preceding numbers are available then the message "
Happy Journey" gets displayed if any preceding number exists then this
message " You got to wait" gets displayed. 5, If no preceding number
exists then it gets decremented from the array, this keeps happening until
all the numbers are exhausted leaving behind an empty array
I don't know where i went wrong
No comments:
Post a Comment