【发布时间】:2017-11-30 21:56:11
【问题描述】:
我正在为我的 CSCI 课做作业,但遇到了一个障碍。
在我们的作业中,我们创建了一个乘客类,然后是我们的航班代码。
我们的 Flight 是从一个文本文件中读取前 20 个人,然后将它们分配到我们数组对象中的一个位置。输入这 20 人后,其余乘客(21-55)将由用户通过对话框插入。
我已经完成了所有这些,但是在此之后,我们需要根据他们拥有的航班舱位打印出我们数组中的每个人,即打印所有头等舱的人,然后是商务舱,然后是经济舱。当我尝试打印我的所有对象时,只打印我的文本文件中的人,而不是用户输入的每个人。
这是我的代码
import javax.swing.JOptionPane;
import java.io.*;
import java.util.Scanner;
public class Flight_EdgarFlores
{
public static void main(String[] args) throws IOException
{
String fullName=null;
String seatClass=null;
String seatNumber=null;
String ff_Number=null;
String points=null;
String line, word;
int seat_Choice, again;
Passengers[] Pass;
final int NUM_OF_PASSENGERS= 55; //size of array
Pass = new Passengers[NUM_OF_PASSENGERS];
Scanner lineScan;
Scanner fileScan = new Scanner(new File("PassengerList.txt"));
int line_counter = 0;
int word_counter = 0;
while(fileScan.hasNext())
{
line = fileScan.nextLine();
lineScan = new Scanner(line); // Scans each line of the file
lineScan.useDelimiter(", ");
word_counter = 0;
while(lineScan.hasNext()) // Reads each word in the line
{
word = lineScan.next();
switch (word_counter)
{
case 0:
fullName = word;
break;
case 1:
seatClass = word;
break;
case 2:
seatNumber = word;
break;
case 3:
ff_Number = word;
break;
case 4:
points = word;
break;
}
word_counter++;
}
if(word_counter==3)
Pass[line_counter] = new Passengers(fullName, seatClass, seatNumber);
if(word_counter==5)
Pass[line_counter] = new Passengers(fullName, seatClass, seatNumber, ff_Number, points);
line_counter++;
}
int counter=20;
do
{
String full_Name = JOptionPane.showInputDialog("Enter Full Name: ");
String[] seatClassChoice = new String[3];
seatClassChoice[2] = "Economy Class";
seatClassChoice[1] = "Business Class";
seatClassChoice[0] = "First Class";
int classChoice = JOptionPane.showOptionDialog(null, "Choose Seat Class", "Seat Class Choice", JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE, null, seatClassChoice, seatClassChoice[0]);
if(classChoice==0)
{
String[] seatChoice = {"1A", "2A", "3A", "4A", "5A", "6A"};
seat_Choice = JOptionPane.showOptionDialog(null, "Choose Seat ", "Seat Choice", JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE, null, seatChoice, seatChoice[0]);
}
else if(classChoice==1)
{
String[] seatChoice = {"7A", "7B", "8A", "8B", "9A", "9B", "10A", "10B", "11A", "11B"};
seat_Choice = JOptionPane.showOptionDialog(null, "Choose Seat ", "Seat Choice", JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE, null, seatChoice, seatChoice[0]);
}
else if(classChoice==2)
{
String[] seatChoice = {"12A", "12B", "12C", "13A", "13B", "13C", "14A", "14B", "14C", "15A", "15B", "15C", "16A", "16B", "16C", "17A", "17B", "17C", "18A", "18B", "18C", "19A", "19B", "19C", "20A", "20B", "20C", "21A", "21B", "21C", "22A", "22B", "22C", "23A", "23B", "23C", "24A", "24B", "24C" };
seat_Choice = JOptionPane.showOptionDialog(null, "Choose Seat ", "Seat Choice", JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE, null, seatChoice, seatChoice[0]);
}
Pass[counter] = new Passengers(full_Name, seatClass, seatNumber);
again = JOptionPane.showConfirmDialog(null, "Add another Passenger?");
counter++;
}while((counter<NUM_OF_PASSENGERS)&& (again==JOptionPane.YES_OPTION));
Pass[0].addFrequentFlyer("#2AZ433", "4455"); //adds flyer number and flyer points to object at index 0
for(int i=0; i<NUM_OF_PASSENGERS; i++)
{
if(Pass[i].getseatClass().equals("First Class"))
System.out.println(Pass[i] + " , " + i);
}
for(int i=0; i<=NUM_OF_PASSENGERS; i++)
{
if(Pass[i].getseatClass().equals("Business Class"))
System.out.println(Pass[i] + " , " + i);
}
for(int i=0; i<=NUM_OF_PASSENGERS; i++)
{
if(Pass[i].getseatClass().equals("Economy Class"))
System.out.println(Pass[i] + " , " + i);
}
}
}
这是我的乘客舱
public class Passengers
{
private String fullName, seatClass, seatNumber, frequentFlyerNumber, flyerPoints;
public Passengers (String full_name, String seat_class, String seat_number, String frequent_flyernumber, String flyer_points)
{
fullName=full_name;
seatClass=seat_class;
seatNumber=seat_number;
frequentFlyerNumber=frequent_flyernumber;
flyerPoints=flyer_points;
}
public Passengers (String full_name, String seat_class, String seat_number)
{
fullName=full_name;
seatClass=seat_class;
seatNumber=seat_number;
}
public void addFrequentFlyer (String frequent_flyer_number, String flyer_Points)
{
frequentFlyerNumber=frequent_flyer_number;
flyerPoints=flyer_Points;
}
/* public String getLastName()
{
String lastName="";
int nameLength= fullName.length();
for(int i =nameLength; i> 0; i--)
{
if(full_name.charAt(i)!=' ')
lastName+=fullName.charAt(i);
else
break;
}
}*/
public String getseatClass()
{
return seatClass;
}
public String toString()
{
return fullName + ", "+ seatClass + ", " + seatNumber;
}
}
【问题讨论】:
-
鉴于这是一项学校作业(感谢您坦诚相告),我认为拼出您尝试过的内容对您的学习很重要。在这种情况下,您的代码中似乎没有证据表明您尝试加载用户提供的其他乘客。请提供您为加载这些客户所做的任何尝试,否则这看起来就像您在要求某人为您做作业。
-
他们每次在 while 循环中使用 Pass[counter] = new Passengers(full_Name, seatClass, seatNumber); @艾伦?
标签: java arrays object arrayobject