我认为我的代码存在一些基本问题。我对java语法不太熟悉,所以我不太确定我哪里出错了。任何帮助将不胜感激。
我尝试在中间使用构造函数,并在底部使用一些访问器,但我认为我自己过于复杂了。
import java.util.Scanner;
public class FerryBooking {
public static void main(String args[]){
class VehicleBooking {
private String booking_ID = new String("");
private String registration = new String("");
private String make_model = new String("");
private int number_passengers = 1;
private boolean insurance_choice = false;
private boolean insurance_flag = false;
public static final int booking_fee= 100;
public static final int extra_passenger = 50;
public static final int insurance_fee = 50;
VehicleBooking() {
Scanner input = new Scanner(System.in);
Scanner scan = new Scanner(System.in);
System.out.print("Enter booking ID");
booking_ID = input.next();
System.out.print("Enter registration number");
registration = input.next();
System.out.print("Enter vehicle make/model");
make_model = input.next();
System.out.print("Enter number of passengers");
number_passengers = scan.nextInt();
}
public String getBookingID(){
return booking_ID;
}
public String getRegistration(){
return registration;
}
public String getMakeModel(){
return make_model;
}
public int getPassengers(){
return number_passengers;
}
public boolean getInsurance(){
return insurance_choice;
}
public boolean addInsurance(){
insurance_choice = true;
if (insurance_flag = false) {
insurance_flag = true;
return true;
} else if (insurance_flag = true) {
return false;
}
return true;
}
public double getBookingFee(){
int final_cost = booking_fee + (getPassengers()*extra_passenger);
if (insurance_choice = true){
final_cost = final_cost + insurance_fee;
}
return final_cost;
}
}
}
}
- 编辑 -
我重写了很多代码,并将其缩小到我的大问题所在;构造函数。但是,我收到与提供给我的代码的构造函数相关的错误。
import java.util.Scanner;
public class VehicleBooking {
private String booking_ID = "";
private String registration = "";
private String make_model = "";
private int number_passengers = 1;
private boolean insurance_choice = false;
public static final int BOOKING_FEE= 100;
public static final int EXTRA_PASSENGER = 50;
public static final int INSURANCE_FEE = 50;
public VehicleBooking(String booking_ID1, String registration1, String make_model1, int number_passengers1) {
/** Initialise the variables **/
booking_ID = booking_ID1;
registration = registration1;
make_model = make_model1;
number_passengers = number_passengers1;
}
public static void main(String args[]) {
VehicleBooking vb = new VehicleBooking(booking_ID1, registration1, make_model1, number_passengers1);
}
}