- 分享
- 0
- 人气
- 0
- 主题
- 1
- 帖子
- 26
- UID
- 465709
- 积分
- 3
- 阅读权限
- 10
- 注册时间
- 2011-12-28
- 最后登录
- 2012-9-6
- 在线时间
- 219 小时
|
各位jbtalks 的老大,大家好。。
因为assignment的关系,需要做一个 Java cinema booking system..
以下是我的code。。
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Scanner;
public class CinemaBooking
{
static final int rows = 7;
static final int cols = 4;
static char[][] seats = new char[rows][cols];
static ArrayList<String> reservedSeats = new ArrayList<String>();
public static void main(String[] args)
{
buildSeats();
printSeats();
Scanner scan = new Scanner(System.in);
String s = "";
do
{
if(!hasAvailableSeats())
{
System.out.println("All seats are chosen. Thank you for using this booking system");
s="0";
continue;
}
System.out.println("Enter Seat number to reserve or 0 to quit:");
s = scan.next();
int row = Integer.parseInt(s.toUpperCase().substring(0, 1));
if(0==row)
{
break;
}
char col = s.toUpperCase().charAt(1);
reserveSeat(row, col);
} while(!s.equals("0"));
}
public static boolean hasAvailableSeats()
{
boolean blnHasAvailableSeat = false;
for(int i =0; ((i < seats.length) && (!blnHasAvailableSeat)); i++)
{
for(int j = 0; j < seats[i].length; j++)
{
if('A' == seats[i][j])
{
blnHasAvailableSeat = true;
break;
}
}
}
return blnHasAvailableSeat;
}
public static void buildSeats()
{
char seatLetter = 'A';
for (int i = 0; i < seats.length; i++)
{
for (int j = 0; j < seats[i].length; j++)
seats[i][j] = seatLetter++;
seatLetter = 'A';
}
}
public static void printSeats()
{
System.out.println("Available Seats:");
for (int i = 0; i < seats.length; i++)
{
System.out.print((i + 1) + " ");
for (int j = 0; j < seats[i].length; j++)
System.out.print(seats[i][j] + " ");
System.out.println();
}
}
public static void reserveSeat(int row, char col)
{
String seatNo=String.valueOf(row)+col;
if (checkAvailability(seatNo))
{
reservedSeats.add(seatNo);
for (int i = row - 1; i == row - 1; i++)
{
for (int j = 0; j < seats[i].length; j++)
{
if (seats[i][j] == col)
{
seats[i][j] = 'X';
}
}
}
System.out.println(" Seat " + seatNo + " is Reserved ");
}
else
{
System.out.println("Sorry! The Seat "+seatNo+" is NOT available.Please look up for another seat.");
}
printSeats();
}
public static boolean checkAvailability(String seatNo)
{
boolean available = true;
for(int i=0;i<reservedSeats.size();i++)
{
if(reservedSeats.get(i).equalsIgnoreCase(seatNo))
{
available = false;
}
}
return available;
}
}
问题是,我怎么改以上的coding so that when the user enter '1E' or '8A', 系统会show 'invalid seat input'呢?
谢谢各位的回复。。
小弟感激不尽。。 |
|