这是一个学校项目。我已经完成了大部分工作,但我仍然坚持不懈。
这是我迄今为止所做的事情:
import java.io.*;
import java.util.*;
class train1
{
public static void main() throws IOException
{
InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader x=new BufferedReader(isr);
int tp=0, c=0, no=0, r, o;
boolean flag = true;
boolean ar[][]=new boolean[10][4];
System.out.print("***********WELCOME TO TICKET RESERVATION SYSTEM***********");
System.out.println();
System.out.println();
System.out.println(" TICKET RESERVATION");
while(no<10 && flag==true)
{
System.out.println(" Main Menu");
System.out.println("1. Book Ticket");
System.out.println("2. Exit");
System.out.print("Enter Choice :");
int choice = Integer.parseInt(x.readLine());
switch (choice)
{
case 1:
System.out.println();
System.out.println();
System.out.print("*********TRAIN TICKET RESERVATION COUNTER***********");
System.out.println();
System.out.println("Enter the number for the station as given in the menu below");
System.out.println("1 for Station A");
System.out.println("2 for Station B");
System.out.println("3 for Station C");
System.out.println("4 for Station D");
System.out.println("5 for Station E");
System.out.print("START STATION: ");
int s=Integer.parseInt(x.readLine());
System.out.print("DESTINATION STATION: ");
int d=Integer.parseInt(x.readLine());
s=--s;
d=--d;
**//Here how do I check if seats are available or not?**
int f=0;
String starttime="", reaching="";
if(s==0 && d==1)
{
f = 50;
starttime = "08:00";
reaching = "10:00";
}
if(s==0 && d==2)
{
int a =0;
int b =2;
f = 100;
starttime = "08:00";
reaching = "12:00";
}
if(s==0 && d==3)
{
f = 150;
starttime = "08:00";
reaching = "14:00";
}
if(s==0 && d==4)
{
int a =0;
int b =4;
f = 200;
starttime = "08:00";
reaching = "16:00";
}
if(s==1 && d==2)
{
f = 50;
starttime = "10:00";
reaching = "12:00";
}
if(s==1 && d==3)
{
f = 100;
starttime = "10:00";
reaching = "14:00";
}
if(s==1 && d==4)
{
f = 150;
starttime = "10:00";
reaching = "16:00";
}
if(s==2 && d==3)
{
f = 50;
starttime = "12:00";
reaching = "14:00";
}
if(s==2 && d==4)
{
f = 100;
starttime = "12:00";
reaching = "16:00";
}
if(s==3 && d==4)
{
f = 50;
starttime = "14:00";
reaching = "16:00";
}
if (s==4 || d==0)
{
System.out.println ("Wrong Choice. Your ticket was not booked");
}
for (int i=0; i<no; i++)
{
for (int j=s; j<=d; j++)
{
ar[i][j]=true;
}
}
/*
Now what I have been unable to do is. Suppose a person
is going from station A to B. Then row 1 column 1 is
true, and if the next passenger wants to book from B-D
then how do I get the seat booked in the row 1 if there
is space available. And is seat is not available then
it should go to row 2. How do I do that? What I am
doing is "no++" so it goes to row next every time
*/
System.out.print("ENTER THE NAME: ");
String people=x.readLine();
System.out.print("ENTER THE AGE: ");
int age=Integer.parseInt(x.readLine());
System.out.print("GENDER OF THE PERSON: ");
String gender=x.readLine();
System.out.print("DATE OF JOURNRY: ");
String date2=x.readLine();
System.out.println();
System.out.println();
System.out.println(" *****RESERVATION TICKET*****");
System.out.println("Name :"+people+" "+"Age :"+age+" "+"Gender :"+gender+" ");
System.out.println();
System.out.println("START STATION: "+s);
System.out.println("Boarding time: " + starttime);
System.out.println("DESTINATION STATION: "+d);
System.out.println("Expected time of Arrival:" + reaching);
System.out.println();
System.out.println("TOTAL AMOUNT: "+ f);
System.out.println();
System.out.println(" ***WISH YOU A HAPPY AND SAFE JOURNEY***");
System.out.println("COURTESY: ROMIT RAILWAY STATION");
break;
case 2:
flag=false;
System.out.println ("You have exited");
break;
default: System.out.println("Choice Does not Exit");
}
no=no++;
}
}
让我告诉它必须如何运作。主菜单必须包含2个选项,一个用于预订,另一个用于退出。我做到了。现在,如果选择预订,我们需要输入登机和目的地。
现在程序必须检查座位是否可用。这是我被困的地方。我创建了一个2D数组,其中我需要10行4列。每当预订机票时,我将其存储为真,并且最初所有地点都是假的。现在每当预订新票时,阵列状态都会发生变化。我也是这样做的。
我无法做的是:假设一个人从A站转到B.然后第1行第1列为真,如果下一位乘客想要从BD预订,那我该如何预订座位?如果有可用空间,则为第1行。并且座位不可用,那么应该去第2行。我该怎么做?
如果所有座位都已填满,那么必须告诉我座位不可用。我怎么做?我不太了解Java,因为我使用的是BlueJ。因此,如果有人可以对BlueJ提出建议,那就更好了。
我是10年级的学生,所以请不要为SQL系统烦恼。
答案 0 :(得分:3)
关于A-B和B-D的第一个问题无法用您的数据结构来回答,因为您按车站(开始,中间和目的地)存储票证。要解决这个问题,您可以切换到不使用工作站的数据结构,而是使用轨迹(即AB,BC,CD等)。然后,您需要翻译用户输入(&#39;从A到B&#39;将变为&#39;轨迹1(AB)&#39;和&#39;从B到D&#39;变为&#39;轨迹2(BC)和3(CD)&#39;,现在没有重叠)。
要解决您的其他问题,您必须重新考虑使用“不”的问题。变量。首先,在第126行的循环每次从0开始运行是没有意义的(从我所知道的),你只预订一张票,并在你当前的代码中预订存储在{{{ 1}}。因此,您只需要设置数组的索引(当然,但是对于每个站/轨迹)。
不是简单地在每个循环中递增no
,而是必须在用户输入后检查数据结构,以确定是否存在仍有空间的座位索引(数组的第一个维度)在用户请求的所有站(或轨迹)上。如果是这样,请预订,如果没有:没有可用的座位。
(编辑:提示:你不应该用另一个巨大的if语句来做这个,而是用no
循环解决这个问题,一些简单的for
语句,可能还有一个或多个布尔变量来跟踪结果。您也可以查看if
甚至break
,它们是在continue
循环中使用的有用语句。)
(或者,如果有可用座位,您可以检查每个轨迹:用户可以预订机票的机会更多,但他/她可能需要换座位....)。
作为旁注,对于奖励积分:代码的重要部分是if语句将开始和停止站(for
和s
)转换为d
(票价),starttime和停止时间。如果你有10个电台,你的代码长度会怎样?它爆炸,因为你需要81 if语句!有一种更简单的方法可以做到这一点。提示:将每个案例f
与费用进行比较。你看到一个模式吗?现在将d - s
与开始时间进行比较。你看到一个模式吗? d
也是如此。看看你是否可以简化你的代码,以便(未来&#39;)可以扩展到10个工作站,而无需编写很多代码。
答案 1 :(得分:0)
这是我在收到建议后所做的工作....
import java.io.*;
import java.util.*;
class Train
{
public static void main() throws IOException
{
InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader x=new BufferedReader(isr);
int tp=0, c=0, no=0, r, o, i, count=0, position=0;
boolean flag = true;
boolean ar[][]=new boolean[10][4];
for (int row=0; row<10; row++)
{
for (int column=0; column<4; column++)
{
ar[row][column]=false;
}
}
System.out.print("***********WELCOME TO TICKET RESERVATION SYSTEM***********");
System.out.println();
System.out.println();
System.out.println(" TICKET RESERVATION");
while(flag==true)
{
System.out.println(" Main Menu");
System.out.println("1. Book Ticket");
System.out.println("2. Exit");
System.out.print("Enter Choice :");
int choice = Integer.parseInt(x.readLine());
switch (choice)
{
case 1:
System.out.println();
System.out.println();
System.out.print("*********TRAIN TICKET RESERVATION COUNTER***********");
System.out.println();
System.out.println("Enter the number for the station as given in the menu below");
System.out.println("1 for Station A");
System.out.println("2 for Station B");
System.out.println("3 for Station C");
System.out.println("4 for Station D");
System.out.println("5 for Station E");
System.out.print("START STATION: ");
int s=Integer.parseInt(x.readLine());
System.out.print("DESTINATION STATION: ");
int d=Integer.parseInt(x.readLine());
int stat=d-s;
s=--s;
d=--d;
while (no<10)
{
for (i=0; i<5; i++)
{
if (ar[no][i]==false)
{
count=count+1;
position=i;
}
}
if (count>=stat)
{
for (int j=position-stat+1; j<position; j++)
{
ar[no][j]=true;
}
}
if (count<stat)
{
no=no+1;
}
count=0;
no=0;
}
if (no>=10)
{
break;
}
else
{
int f=0;
String starttime="", reaching="";
if(s==0 && d==1)
{
f = 50;
starttime = "08:00";
reaching = "10:00";
}
if(s==0 && d==2)
{
int a =0;
int b =2;
f = 100;
starttime = "08:00";
reaching = "12:00";
}
if(s==0 && d==3)
{
f = 150;
starttime = "08:00";
reaching = "14:00";
}
if(s==0 && d==4)
{
int a =0;
int b =4;
f = 200;
starttime = "08:00";
reaching = "16:00";
}
if(s==1 && d==2)
{
f = 50;
starttime = "10:00";
reaching = "12:00";
}
if(s==1 && d==3)
{
f = 100;
starttime = "10:00";
reaching = "14:00";
}
if(s==1 && d==4)
{
f = 150;
starttime = "10:00";
reaching = "16:00";
}
if(s==2 && d==3)
{
f = 50;
starttime = "12:00";
reaching = "14:00";
}
if(s==2 && d==4)
{
f = 100;
starttime = "12:00";
reaching = "16:00";
}
if(s==3 && d==4)
{
f = 50;
starttime = "14:00";
reaching = "16:00";
}
if (s==4 || d==0)
{
System.out.println ("Wrong Choice. Your ticket was not booked");
}
System.out.print("ENTER THE NAME: ");
String people=x.readLine();
System.out.print("ENTER THE AGE: ");
int age=Integer.parseInt(x.readLine());
System.out.print("GENDER OF THE PERSON: ");
String gender=x.readLine();
System.out.print("DATE OF JOURNRY: ");
String date2=x.readLine();
System.out.println();
System.out.println();
System.out.println(" *****RESERVATION TICKET*****");
System.out.println("Name :"+people+" "+"Age :"+age+" "+"Gender :"+gender+" ");
System.out.println();
System.out.println("START STATION: "+s);
System.out.println("Boarding time: " + starttime);
System.out.println("DESTINATION STATION: "+d);
System.out.println("Expected time of Arrival:" + reaching);
System.out.println();
System.out.println("TOTAL AMOUNT: "+ f);
System.out.println();
System.out.println(" ***WISH YOU A HAPPY AND SAFE JOURNEY***");
System.out.println("COURTESY: ROMIT RAILWAY STATION");
}
break;
case 2:
flag=false;
System.out.println ("You have exited");
break;
default: System.out.println("Choice Does not Exit");
}
}
}
}