Assignment4.java
import java.util.Scanner;
import java.text.DecimalFormat;
import java.text.NumberFormat;
public class Assignment4 {
public static void main(String[] args){
int flightNum = 2399;
String deptCity = "Phoenix";
String arriCity = "Boston";
double price = 473.50;
Scanner in = new Scanner(System.in);
DecimalFormat fmt = new DecimalFormat("$0.00");
Flight flightOne = new Flight(flightNum, deptCity, arriCity, price);
char choice = ' ';
String caseD = new String("D");
String caseA = new String("A");
String caseI = new String("I");
String caseR = new String("R");
String caseS = new String("S");
String caseM = new String("?");
String caseQ = new String("Q");
String letter;
do{
System.out.println("\tChoose an Action\n----------------------------------");
System.out.println("D\tChange Departure City.");
System.out.println("A\tChange Arrival City.");
System.out.println("I\tIncrease The Price.");
System.out.println("R\tReduce The Price.");
System.out.println("S\tShow Flight Info.");
System.out.println("?\tDisplay The Menu.");
System.out.println("Q\tQuit.");
letter = in.nextLine();
if(letter.equals(caseD)){
choice = 'D';
}
if(letter.equals(caseA)){
choice = 'A';
}
if(letter.equals(caseI)){
choice = 'I';
}
if(letter.equals(caseR)){
choice = 'R';
}
if(letter.equals(caseS)){
choice = 'S';
}
if(letter.equals(caseM)){
choice = '?';
}
if(letter.equals(caseQ)){
choice = 'Q';
}
// switch statement runs one of the choices based on
// the user input, specifically the value of int choice
switch(choice)
{
case 'D':
System.out.println("Enter the new departure city");
String newDeptCity = in.nextLine();
flightOne.setDeptCity(newDeptCity);
System.out.println("The departure city has been changed from Phoenix to " + deptCity + ".");
break;
case 'A':
System.out.println("Enter the new arrival city");
String newArriCity = in.nextLine();
flightOne.setArriCity(newArriCity);
System.out.println("The departure city has been changed from Boston to " + arriCity + ".");
break;
case 'I':
System.out.println("Enter the increase rate (0.08):");
double raiseRate = in.nextDouble();
flightOne.raisePrice(raiseRate);
System.out.println("Price was increased by " + (raiseRate * 100) + "%, the new price is " + fmt.format(price) + ".");
break;
case 'R':
System.out.println("Enter the increase rate (0.05):");
double reduceRate = in.nextDouble();
flightOne.raisePrice(reduceRate);
System.out.println("Price was increased by " + (reduceRate * 100) + "%, the new price is " + fmt.format(price) + ".");
break;
case 'S':
flightOne.toString();
break;
case '?':
break;
}
// exits the loop when the user enters '4'
// (exit program) for their choice
}while(choice != 'Q');
in.close();
}
}
Flight.java
public class Flight {
private int flightNum = 2399;
private String deptCity = "Phoenix";
private String arriCity = "Boston";
private double price = 473.50;
public Flight(int newId, String newDepCity, String newArriCity, double newPrice){
flightNum = newId;
deptCity = newDepCity;
arriCity = newArriCity;
price = newPrice;
}
public int getFlightNum(){
return flightNum;
}
public String getDeptCity(){
return deptCity;
}
public String getArriCity(){
return arriCity;
}
public double getPrice(){
return price;
}
public void setFlightNum(int newFlightNum){
flightNum = newFlightNum;
}
public void setDeptCity(String newCity){
deptCity = newCity;
}
public void setArriCity(String newCity){
arriCity = newCity;
}
public void raisePrice(double raiseRate){
double newPrice = (price * raiseRate) + price;
price = newPrice;
}
public void reducePrice(double reduceRate){
double newPrice = price - (price * reduceRate);
price = newPrice;
}
public String toString(){
String result;
result = "\nFlight No:\t" + flightNum + "\nDeparture:\t" + deptCity + "\nArrival:\t\t" + arriCity + "\nPrice:\t\t" + price + "\n\n";
return result;
}
}
我在Assignment4.java第12行尝试编码构造函数时遇到错误:
Flight flightOne = new Flight(flightNum, deptCity, arriCity, price);
错误状态"航班无法解析为某种类型。"为什么我的Assignment4.java文件找不到我的Flight.java文件?我一直在谷歌上搜索这个问题并且在过去的3个小时里盯着我的电脑找不到任何帮助我的东西!我认为这可能是一个关于文件保存位置的问题,但我真的不确定。任何帮助将不胜感激!