Does anyone know why I am getting a "null" output?
What is the correct code for addRoute() and addAircraft()?
This is what I get when I run Assign1.java:
----Hit any key to start.
Route Costs
HNK : $0.00
SNG : $0.00
AUC : $0.00
PER : $0.00
Dingo Air
Hong Kong, 9.5 hours, 300 passengers
null
Singapore, 7.5 hours, 500 passengers
null
Auckland, 4.0 hours, 400 passengers
null
Perth, 3.5 hours, 270 passengers
null
Daily profit: $0.00
----Hit any key to continue.
This is what the output is supposed to look like:
Output of Assign1.java
Route Costs
HNK : $180,500.00
SNG : $170,250.00
AUC : $84,800.00
PER : $58,800.00
Dingo Air
Adelaide - Hong Kong, 9.5 hours, 300 passengers
Airbus A330, 220
Boeing 737, 150
Adelaide - Singapore, 7.5 hours, 500 passengers
Boeing 747, 350
Boeing 737, 150
Adelaide - Auckland, 4.0 hours, 400 passengers
Airbus A330, 220
Airbus A330, 220
Adelaide - Perth, 3.5 hours, 270 passengers
Boeing 737, 150
Boeing 737, 150
Daily profit: $537,150.00
And this is the code I have done so far (some methods not finished)
import java.text.*;
public class Assign1
{
public static void main (String [] args)
{
Assign1 a1 = new Assign1();
a1.runAirline();
}
public void runAirline()
{
NumberFormat cf= NumberFormat.getCurrencyInstance();
//--- Create Aircraft objects ---------------
Aircraft A330 = new Aircraft ("Airbus A330", 220,14,600,4.0);
Aircraft B747 = new Aircraft ("Boeing 747", 350,20,1000,7.0);
Aircraft B737 = new Aircraft ("Boeing 737", 150,7,450,2.5);
Aircraft BAe464 = new Aircraft ("BAe 464", 60,4,300,1.3);
//--- Create Route objects ---------------
Route HNK = new Route("Hong Kong", 9.5, 300);
Route SNG = new Route("Singapore", 7.5, 500);
Route AUC = new Route("Auckland", 4.0, 400);
Route PER = new Route("Perth", 3.5, 270);
//--- Add Aircraft to Routes ---------------
HNK.addAircraft(A330);
HNK.addAircraft(B737);
SNG.addAircraft(B747);
SNG.addAircraft(B737);
AUC.addAircraft(A330);
AUC.addAircraft(A330);
PER.addAircraft(B737);
PER.addAircraft(B737);
//--- Display Route costs ---------------
System.out.println("\nRoute Costs");
HNK.setFare(1100);
System.out.println("HNK : "+cf.format(HNK.breakEven()));
SNG.setFare(800);
System.out.println("SNG : "+cf.format(SNG.breakEven()));
AUC.setFare(450);
System.out.println("AUC : "+cf.format(AUC.breakEven()));
PER.setFare(450);
System.out.println("PER : "+cf.format(PER.breakEven()));
//--- Create an Airline ---------------
Airline da = new Airline("Dingo Air");
//--- Add Routes to Airline ---------------
da.addRoute(HNK);
da.addRoute(SNG);
da.addRoute(AUC);
da.addRoute(PER);
//--- Display Airline details -------------
System.out.println("\n"+da);
//--- Display Airline daily profit -------------
System.out.print("\nDaily profit: ");
System.out.println(cf.format(da.calcDailyProfit()));
}
}
public class Airline {
private String name;
private final int MAX_ROUTE = 10;
private int rcount = 0;
private Route[] routes = new Route[MAX_ROUTE];
public Airline(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void addRoute(Route r) {
routes[rcount++] = r;
if (rcount > MAX_ROUTE)
System.out.println("That is too many routes");
}
public double calcDailyProfit() {
double dailyProfit = 0;
for (int i = 0; i < rcount; i++)
dailyProfit += routes[i].income()
- routes[i].breakEven();
return dailyProfit;
}
public String toString() {
String str = name;
for (int i =0; i < rcount; i++) {
str += "\n\t" + routes[i];
}
return str;
}
}
public class Aircraft {
private String type;
private int passengers;
private int crew;
private double maintCost;
private double fuelEffic;
public Aircraft(String type, int passengers, int crew,
double maintCost, double fuelEffic) {
this.type = type;
this.passengers = passengers;
this.crew = crew;
this.maintCost = maintCost;
this.fuelEffic = fuelEffic;
}
public String toString() {
return getType() + ", " + getPassengers();
}
public String getType() {
return type;
}
public int getPassengers() {
return passengers;
}
public int getCrew() {
return crew;
}
public double getMaintCost() {
return maintCost;
}
public double getFuelEffic() {
return fuelEffic;
}
public double calcRunningCosts() {
return (crew * 100) + (fuelEffic * 900)
+ maintCost + 5000;
}
}
public class Route {
private String destination;
private double flyTime;
private int passengerVol;
private double fare;
private int acount;
private final int MAX_AIRCRAFT = 10;
private Aircraft [] planes = new Aircraft[MAX_AIRCRAFT];
public Route(String destination, double flyTime, int passengerVol) {
this.destination = destination;
this.flyTime = flyTime;
this.passengerVol = passengerVol;
}
public String getDestination() {
return destination;
}
public double getFlyTime() {
return flyTime;
}
public int getPassengerVol() {
return passengerVol;
}
public void setFare(double fare) {
this.fare = fare;
}
public String toString() {
return getDestination() + ", " + getFlyTime()+ " hours, "
+ getPassengerVol() + " passengers" + "\n\t" + planes[acount++];
}
public double breakEven() {
return 0;
}
public double income() {
return 0;
}
public void addAircraft(Aircraft a) {
int acount = 0;
for (int i = 0; i < acount; i++)
if (acount != MAX_AIRCRAFT) {
planes[acount] = a;
acount++;
}
else
System.out.println("That is too many aircraft");
}
} |