I have this part of method here:
System.out.println("Do you have promo code (yes / no ): ");
String yesNo = scanner.nextLine();
if (yesNo.contains("yes")) {
System.out.println("Enter promo code: ");
String promoCode = scanner.nextLine();
for (Flights flight : flights) {
if (promoCode.equals(flight.getPromoCode())) {
System.out.println("Your promo code is valid!");
} else if (yesNo.contains("no")) {
System.out.println("Your dont have promo code!");
} else {
System.out.println("Your promo code is not valid, 6 for options");
}
}
}
}
}
Program ask me this, that is fine:
System.out.println("Do you have promo code (yes / no ): ");
String yesNo = scanner.nextLine();
If i type yes program print me this:
if (yesNo.contains("yes")) {
System.out.println("Enter promo code: ");
String promoCode = scanner.nextLine();
That is fine, but if I enter valid promo code, program print me this:
Your promo code is valid!
Your promo code is not valid, 6 for options
Your promo code is not valid, 6 for options
Your promo code is not valid, 6 for options
Also if i type no, this part of code isnt working:
} else if (yesNo.contains("no")) {
System.out.println("Your dont have promo code!");
I guess it's up to the forr loop, but I can't think of a way to fix this part of the code.
Requires code to run it in your development environment.
Class flights:
package com.cv.entity.flights;
public class Flights {
private String from;
private String to;
private double economyClassPrice;
private double businessClassPrice;
private double firstCassPrice;
private int flightId;
private String promoCode;
public Flights() {
}
public Flights(String from, String to, double economyClassPrice, double businessClassPrice, double firstCassPrice, int flightId, String promoCode) {
this.from = from;
this.to = to;
this.economyClassPrice = economyClassPrice;
this.businessClassPrice = businessClassPrice;
this.firstCassPrice = firstCassPrice;
this.flightId = flightId;
this.promoCode = promoCode;
}
public String getFrom() {
return from;
}
public void setFrom(String from) {
this.from = from;
}
public String getTo() {
return to;
}
public void setTo(String to) {
this.to = to;
}
public double getEconomyClassPrice() {
return economyClassPrice;
}
public void setEconomyClassPrice(double economyClassPrice) {
this.economyClassPrice = economyClassPrice;
}
public double getBusinessClassPrice() {
return businessClassPrice;
}
public void setBusinessClassPrice(double businessClassPrice) {
this.businessClassPrice = businessClassPrice;
}
public double getFirstCassPrice() {
return firstCassPrice;
}
public void setFirstCassPrice(double firstCassPrice) {
this.firstCassPrice = firstCassPrice;
}
public int getFlightId() {
return flightId;
}
public void setFlightId(int flightId) {
this.flightId = flightId;
}
public String getPromoCode() {
return promoCode;
}
public void setPromoCode(String promoCode) {
this.promoCode = promoCode;
}
@Override
public String toString() {
return "*FLIGHTS* " +
"Flight ID: " + flightId +
", From: " + from + '\'' +
", To: " + to + '\'' +
", Economy class price: " + economyClassPrice + "$" +
", Business class price: " + businessClassPrice + "$" +
", First class price: " + firstCassPrice + "$" +
", Promo code: " + promoCode;
}
}
Class passengers:
package com.cv.entity.passengers;
import java.util.Scanner;
public class Passengers {
private final Scanner scanner = new Scanner(System.in);
private String name;
private String lastName;
private String email;
private double balance;
private boolean isVip;
private String promoCode;
public Passengers() {
}
public Passengers(String name, String lastName, String email, double balance, boolean isVip, String promoCode) {
this.name = name;
this.lastName = lastName;
this.email = email;
this.balance = balance;
this.isVip = isVip;
this.promoCode = promoCode;
}
public String getName() {
return name;
}
//setter validation for name
public void setName(String name) {
while (!name.matches("[a-zA-Z]+")) {
System.err.println("Invalid Entry (" + name + ")! Only letters for name!");
name = scanner.nextLine();
}
this.name = name;
}
public String getLastName() {
return lastName;
}
//setter validation for last name
public void setLastName(String lastName) {
while (!lastName.matches("[a-zA-Z]+")) {
System.err.println("Invalid Entry (" + lastName + ")! Only letters for last name!");
lastName = scanner.nextLine();
}
this.lastName = lastName;
}
public String getEmail() {
return email;
}
//setter validation for email
public void setEmail(String email) {
while (!email.matches("^(.+)@(.+)$")) {
System.err.println("Invalid Entry (" + email + ")! Try again!");
email = scanner.nextLine();
}
this.email = email;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public boolean isVip() {
return isVip;
}
public void setVip(boolean vip) {
isVip = vip;
}
public String getPromoCode() {
return promoCode;
}
public void setPromoCode(String promoCode) {
this.promoCode = promoCode;
}
@Override
public String toString() {
return "*PASSENGERS* " +
"Name: " + name + '\'' +
", Last name: " + lastName + '\'' +
", Email: " + email + '\'' +
", Balance: " + balance +
", Is vip: " + isVip;
}
}
Interface PassengerController
package com.cv.entity.passengers;
import com.cv.entity.flights.Flights;
import java.lang.reflect.Array;
import java.util.ArrayList;
public interface PassengersController {
public void addNewPassenger(ArrayList<Passengers> passengersList);
public void reserveFlight(ArrayList<Flights> flights);
}
Class PassengerControllerImpl:
package com.cv.entity.passengers;
import com.cv.entity.flights.Flights;
import com.sun.org.apache.xerces.internal.impl.xpath.regex.BMPattern;
import java.util.ArrayList;
import java.util.Scanner;
public class PassengersControllerImpl implements PassengersController {
final Scanner scanner = new Scanner(System.in);
final Passengers passengers = new Passengers();
//method for adding new passengers in array list while program is running
@Override
public void addNewPassenger(ArrayList<Passengers> passengersList) {
System.out.println("Creating new passenger!");
System.out.print("Add name of passenger: ");
passengers.setName(scanner.nextLine());
System.out.print("Add last name of passenger: ");
passengers.setLastName(scanner.nextLine());
System.out.print("Add email of passenger: ");
passengers.setEmail(scanner.nextLine());
System.out.print("Add balance of passenger: ");
passengers.setBalance(scanner.nextDouble());
System.out.print("Is passenger vip? (true or false): ");
passengers.setVip(scanner.nextBoolean());
System.out.println("You added a new passenger!");
//adding passengers into array list
passengersList.add(passengers);
}
//the method by which the user reserves a flight
@Override
public void reserveFlight(ArrayList<Flights> flights) {
System.out.println("Enter ID of flight to reserve it");
int userChoice = scanner.nextInt();
scanner.nextLine();
for (Flights flight : flights) {
if (userChoice == flight.getFlightId()) {
System.out.println("You reserved flight to: " + flight.getTo()
+ " from " + flight.getFrom());
}
}
System.out.println("Do you have promo code (yes / no ): ");
String yesNo = scanner.nextLine();
if (yesNo.contains("yes")) {
System.out.println("Enter promo code: ");
String promoCode = scanner.nextLine();
for (Flights flight : flights) {
if (promoCode.equals(flight.getPromoCode())) {
System.out.println("Your promo code is valid!");
} else if (yesNo.contains("no")) {
System.out.println("Your dont have promo code!");
} else {
System.out.println("Your promo code is not valid, 6 for options");
}
}
}
}
}
Main method:
package com.cv.demo;
import com.cv.entity.flights.Flights;
import com.cv.entity.flights.FlightsControllerImpl;
import com.cv.entity.passengers.Passengers;
import com.cv.entity.passengers.PassengersControllerImpl;
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
//adding classes
public static PassengersControllerImpl passengersControllerImpl = new PassengersControllerImpl();
public static FlightsControllerImpl flightsControllerImpl = new FlightsControllerImpl();
//importing scanner for user input
final static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
//manually adding passengers
Passengers passenger1 = new Passengers("John", "Slavic", "john@gmail.com", 3000, true, "gC49");
Passengers passenger2 = new Passengers("Ivan", "Ivanovic", "ivan@gmail.com", 2000, true, "asdf");
Passengers passenger3 = new Passengers("Marko", "Markovic", "marko@gmail.com", 1800, false, "sad3");
Passengers passenger4 = new Passengers("Jovan", "Jovanovic", "jovan@gmail.com", 100, true, "xb43");
//manually adding flihts
Flights flight1 = new Flights("Moscow", "Belgrade", 300, 600, 850, 1, "gC49");
Flights flight2 = new Flights("Paris", "Dortmund", 250, 290, 400, 2, "soe4");
Flights flight3 = new Flights("Podgorica", "Nis", 25, 40, 80, 3, "sx33");
Flights flight4 = new Flights("London", "Miami", 600, 1500, 2500, 4, "zcl3");
//creating array list for passengers
ArrayList<Passengers> passengersArrayList = new ArrayList<>();
passengersArrayList.add(passenger1);
passengersArrayList.add(passenger2);
passengersArrayList.add(passenger3);
passengersArrayList.add(passenger4);
//creating array list for flights
ArrayList<Flights> flightsArrayList = new ArrayList<>();
flightsArrayList.add(flight1);
flightsArrayList.add(flight2);
flightsArrayList.add(flight3);
flightsArrayList.add(flight4);
//program at first show menu to user
showMenu();
//switch case to allow the user to select an action
while (true) {
int choice = scanner.nextInt();
scanner.nextLine();
switch (choice) {
case 1:
for (Passengers passengers : passengersArrayList) {//printing array elements one below the other
System.out.println(passengers);
}
break;
case 2:
for (Flights flights : flightsArrayList) { //printing array elements one below the other
System.out.println(flights);
}
break;
case 3:
passengersControllerImpl.addNewPassenger(passengersArrayList); //adding a new passenger
break;
case 4:
flightsControllerImpl.addNewFlights(flightsArrayList); //adding a new flight
break;
case 5:
System.out.println("--- Available flights ---");
for (Flights flights : flightsArrayList) { //printing list of flight
System.out.println(flights);
}
passengersControllerImpl.reserveFlight(flightsArrayList);//calling method for reservation
break;
case 6:
showMenu(); //showing options to the user
break;
case 7:
return; //program shutdown
default:
System.out.println("Invalid input!");
}
}
}
public static void showMenu() { //list of user options
System.out.println("1\t Passenger list");
System.out.println("2\t Flight list");
System.out.println("3\t Add new passenger");
System.out.println("4\t Add new flight");
System.out.println("5\t Reserve flight");
System.out.println("6\t Show menu again");
System.out.println("7\t Exit program");
System.out.println("Please enter your choice: ");
}
}