I think there are a few fundamental problems with my code here. I’m not too familiar with the java syntax, so I’m not too sure where I’ve gone wrong. Any help would be greatly appreciated.
I have tried to use a constructor in the middle, and a few accessors towards the bottom, but I think I have over-complicated it for myself.
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;
}
}
}
}
–EDIT–
I have re-written a lot of the code, and sized it down to where my big issue was; the constructor. However I am getting an error related to the constructor with the code provided to me.
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);
}
}
First don’t initialize a String like the one below: –
Rather use
String.valueOf()or just assign an empty string to variable: –Second, make a habit to declare your
CONSTANTSwithUPPER_CASEletter: –Third, take a look at your constructor: –
You should never do
I/Ooperation in your constructor..Constructorsare used to initialize the state of the object. It’s sole purpose is toinitialize.For I/O purpose, make different method
readInput()and call it after your object is created..Another thing: – Here’s your
if-else-ifblock you have used..In this code, your
ifis alwaysfalseand yourelse ifis alwaystrue.. Because you are actually assigning thesevaluesto yourinsurance_flag.. You should use==for comparison purpose..So, use
if (insurance_flag == false).. In fact you don’t need to compare with a boolean literal…just use: –
if (!insurance_flag).. They are equivalent..Ideally you should change that method containing your above if to the one below: –
Because that is what your method is doing, but in an odd way..
You can also replace
final_cost = final_cost + insurance_fee;with the below code: –By using it this way,
final_costwill not be evaluatedtwice..EDIT ** : –
Your
constructorshould look like this: –So, you’re actually passing those values you read, into the constructor as parameter, and initializing your instance attributes, with those parameters..
I think this would clear your doubt..