import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Program {
public static void main(String[] args) {
FlightService service = new FlightServiceImpl();
Flight[] flights = service.getAllFlights();
display(flights);
List
printBoardingPass(service, tickets);
}
private static void display(Flight[] flights) {
System.out.println("Welcome to Delta Airlines");
System.out.format(
"There are %d available flights to San Juan, Puerto Rico:%n",
flights.length);
for (Flight f : flights) {
System.out.println( f.getFlightDetail());
System.out.println( f.getDescription());
for (String seat : f.getAvailableSeats()) {
System.out.format("Seat: %s%n", seat);
}
}
}
private static List
List
boolean done = false;
do {
Ticket ticket = new Ticket();
ticket.setFlightNo( read("Please enter the 3- or 4-digit Flight number of your preferred flight in the following line: "));
ticket.setSeatNo( read("Please write the seat (exactly as it is written) of your choice"));
tickets.add(ticket);
String nextTicket = read("Next ticket (Y/N)? ");
if ("n".equalsIgnoreCase( nextTicket)) {
done = true;
}
} while (!done);
return tickets;
}
private static String read(String message) {
String result = "";
System.out.println(message);
Scanner keyboard = new Scanner(System.in);
result = keyboard.nextLine();
return result;
}
private static void printBoardingPass(FlightService service,
List
for (Ticket ticket : tickets) {
Flight flight = service.getFlightByNo( ticket.getFlightNo());
StringBuilder sb = new StringBuilder();
sb.append("*** Boarding Pass ***\n");
sb.append( flight.getFlightDetail() ).append("\n");
sb.append( flight.getDescription() ).append("\n");
sb.append( String.format("Seat: %s%n", ticket.getSeatNo()));
System.out.println(sb.toString());
}
}
}
interface FlightService {
Flight[] getAllFlights();
Flight getFlightByNo(String flightNo);
}
class FlightServiceImpl implements FlightService {
private Flight[] flights = new Flight[3];
public FlightServiceImpl() {
buildFlights();
}
@Override
public Flight[] getAllFlights() {
return flights;
}
@Override
public Flight getFlightByNo(String flightNo) {
Flight flight = null;
for (Flight f : flights) {
if (f.getFlightNo( ).equalsIgnoreCase( flightNo)) {
flight = f;
break;
}
}
return flight;
}
private void buildFlights() {
flights[0] = new Flight("2453", "Boeing 747",
"DEPARTS: ATL 7:56PM, ARRIVES PR 10:47AM GATE 33");
flights[0].addSeats("3D", "4F", "34F", "20A", "23B", "12B");
flights[1] = new Flight("345", "Airbus A330",
"DEPARTS: ATL 12:45PM, ARRIVES PR 3:32AM GATE 22");
flights[1].addSeats("30B", "25A", "22C", "10E", "10F", "35F");
flights[2] = new Flight("1566", "Fokker 100",
"DEPARTS: ATL 3:45PM, ARRIVES PR 6:24PM GATE 12");
flights[2].addSeats("2B", "2C", "5C", "10B", "10A", "4B");
}
}
class Flight {
private String flightNo;
private String type;
private String description;
private List
public Flight(String flightNo, String type, String description) {
setFlightNo(flightNo);
setType(type);
setDescription(description);
}
public String getFlightNo() {
return flightNo;
}
public void setFlightNo(String flightNo) {
this.flightNo = flightNo;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getFlightDetail() {
return String.format("Flight %s-%s", getFlightNo(), getType());
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public void addSeats(String... seats) {
for (String seat : seats) {
this.seats.add(seat);
}
}
public List
return this.seats;
}
}
class Ticket {
private String flightNo;
private String seatNo;
public String getFlightNo() {
return flightNo;
}
public void setFlightNo(String flightNo) {
this.flightNo = flightNo;
}
public String getSeatNo() {
return seatNo;
}
public void setSeatNo(String seatNo) {
this.seatNo = seatNo;
}
}
Thank you for your interest in my question!
I have to write a program that "prints" a boarding pass. I initially provide the user with a list of 3 flights, each with 6 seats available. I then use the Scanner class to receive the flight and seat of choice by the user. The next part is where I'm stuck! I have to then provide the user with the total price of their chosen flight and seat, WITHOUT using conditionals. How can I do this?
Thank you!