我正在寻找对这个小的,简单的程序,我写的一般反馈。它工作得很完美,但我正在努力实现可读的、干净的代码。
我也有一些具体的问题:
switch语句是编写该类的最有效/最符合逻辑的方法吗?/**
* This AirlineReservation class assigns seats on an airplane and prints boarding passes.
* (1) A boolean array is used to store a seating chart and which seats are available
* (2) The user is asked if they want a first class reservation, second class reservation, or end
* (3) A switch statement case is selected based on the user's input
* (4) The requested class of flight is checked for available seats
* (a) seats 1-5 are first class
* (b) seats 6-10 are economy
* (5) if available, seat is booked and boarding pass is printed
* (6) if not available, user is notified and presented with the original 3 options.
*
* NOTE: I have commented out a method I was using to check my array for accuracy.
*/
import java.util.Scanner;
import java.util.Arrays;
public class AirlineReservations
{
public static void main(String[] args)
{
int totalSeats = 10;
int firstClassSeats = 5;
int economySeats = 5;
int classChoice = 0;
boolean continueInput = true;
Scanner input = new Scanner(System.in);
boolean[] seatingChart = new boolean[totalSeats];
Arrays.fill(seatingChart, false);
while (continueInput == true)
{
System.out.print("Please type 1 for First Class, 2 for Economy Class, or 3 to exit System: ");
classChoice = input.nextInt();
switch (classChoice)
{
case 1://first class requested
if (firstClassSeats > 0)//seat available
{
seatingChart[firstClassSeats-1]=true;
firstClassSeats--;
printBoardingPass((firstClassSeats+1), classChoice);
//printSeatArray(seatingChart);
}
else //seat not available
System.out.print("\nFirst class seats are now SOLD OUT!\n");
break;
case 2://economy class requested
if (economySeats > 0)//seat available
{
seatingChart[economySeats+4]=true;
economySeats--;
printBoardingPass((economySeats+6),classChoice);
//printSeatArray(seatingChart);
}
else//seat not available
System.out.print("\nEconomy seats are now SOLD OUT!\n");
break;
case 3://exit system
continueInput = false;
break;
default://incorrect input
System.out.print("\nIncorrect input. Please enter only a 1, 2, or 3.");
break;
}//end switch classChoice
}//end while continueInput
}//end main method
private static void printBoardingPass(int seat, int classChoice)
{
String seatingClass = (classChoice == 1? "First" : "Economy");
System.out.print("\nBoarding Pass: " + seatingClass + " class, Seat # " + seat +"\n\n");
}//end printBoardingPass
//private static void printSeatArray(boolean[] seatingChart)
//{
//for (boolean element : seatingChart)
//System.out.print(element + "\n");
//System.out.println();
//}
}//end class发布于 2015-10-18 22:07:24
我的主要方法是不是太大了?我应该把它分解成更小的方法吗?你怎么建议把它分开?
您的main方法不太大;它做了太多不同的事情。
在java程序中,更典型的做法是使用main方法设置要执行任务的类的实例,然后将程序的控制权交给该对象。
在这种情况下,main正在初始化您的座椅地图,运行您的输入/输出循环,提供您的业务行为。
“switch”语句是编写该类的最有效/最符合逻辑的方法吗?
可能不是--这里有两个问题。首先是打开一个int,其中int是其他东西的代码。没有什么特别的理由认为"1“应该是头等舱,"2”应该是经济的--这只是这个赋值所要求的任意编码。一个更好的选择是使用enum来表示不同的座位类别。
public enum SeatingClass {
FIRST_CLASS, ECONOMY
}这里的第二个问题是,您正在使用开关来驱动行为的更改。这通常意味着代码中隐藏着一个类,等待您注意到它。在这种情况下,有两个:第一个事实是,您真正要做的是将输入转换为Commands。
另外,请注意,case 1和case 2所做的行为完全相同,但数据不同。这表明应该有一个类知道如何提供该行为,以及两个具有不同数据值的不同实例。因此,在这里,您应该有某种SeatSectionAssigner;每个位置都有若干个座位和一个偏移量到座椅地图中,以确定分配了哪些座位。
任何改进/反馈都是非常感谢的,请记住,我学习java已经有一个多月了。
寻找将程序分解为做一件事的类的方法。
我找到了完全相同的任务(航空公司预订系统),但我真的很难遵循这个计划。也许这是编程经验带来的?轻松地阅读别人的代码,也就是说。但那里没有“主要”方法。
是的,这段代码本身不会做任何事情。隐含的假设是,其他地方的代码看起来像
Airline airline = new Airline();
airline.start();https://codereview.stackexchange.com/questions/107973
复制相似问题