0

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: ");
        }
    }
  • Please look at [Scanner is skipping nextLine() after using next(), nextInt() or other nextFoo() methods](http://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-nextint-or-other-nextfoo) – Hovercraft Full Of Eels May 28 '21 at 14:18
  • For any other issues, try compressing the code to the smallest program that compiles, runs, and then that demonstrates the problem for us, a [mre] (please read the link), and then posting this in your question. – Hovercraft Full Of Eels May 28 '21 at 14:19
  • I literally did it piece by piece for code that doesn't work for me? – Stefan Jankovic May 28 '21 at 14:20
  • Please clarify that last comment. Also, have you fixed your Scanner issue that I mentioned yet? – Hovercraft Full Of Eels May 28 '21 at 14:23
  • And regarding the [MRE](https://stackoverflow.com/help/minimal-reproducible-example), if we have code that we can run and experience your problem directly, that we can then modify and see if a change fixes it, we often have a much better chance of being able to help. No one wants to see the entire program however, just enough code to compile, run and demonstrate the issue is all. Yes, this requires extra work on your part, but if you don't get a solution soon, it is work that is well worthwhile. – Hovercraft Full Of Eels May 28 '21 at 14:24
  • I made it clear how each piece of code works and emphasized how it should work, I really don't know how to briefly explain my problem. I also didn't solve the problem with the link you left me because I don't know what, how and where to change. – Stefan Jankovic May 28 '21 at 14:25
  • Please read the link's question and answer. The answer explains the issue and how to fix it. – Hovercraft Full Of Eels May 28 '21 at 14:26
  • Regarding "I made it clear how each piece of code works..." -- are you 100% sure that the code that you've posted is responsible for the problem that you're seeing? Do you know *exactly* what is causing the bug? If you did, you'd likely have a fix for your problem by now, but if you don't (most likely the case), then the [MRE](https://stackoverflow.com/help/minimal-reproducible-example) can be a God-send for us – Hovercraft Full Of Eels May 28 '21 at 14:28
  • Yes, the Scanner issue is likely contributing greatly. Be sure to handle the end-of-line tokens appropriately, such as by calling an empty `sc.nextLine();` after every `sc.nextInt()` call. Again, the duplicate will explain the details of why and how this works. – Hovercraft Full Of Eels May 28 '21 at 14:32
  • Yeah, Im 100% sure that the code that I've posted is responsible for the problem Im seeing. Other parts of the code work great. I wrote every piece of code that doesn't work, wrote how it should work and how it actually works. – Stefan Jankovic May 28 '21 at 14:32
  • It seems to have improved the code somewhat, it just still prints the same question to me 4 times after I enter yes or no – Stefan Jankovic May 28 '21 at 14:35
  • Then something else is also going on, possibly occult recursion -- post your [mre] and let's take a look at it. – Hovercraft Full Of Eels May 28 '21 at 14:37
  • I would but `You have reached your question limit` – Stefan Jankovic May 28 '21 at 14:38
  • [edit] this current question and post your [mre] – Hovercraft Full Of Eels May 28 '21 at 14:38
  • I did it. Thanks. – Stefan Jankovic May 28 '21 at 14:43
  • So, you're saying that I can copy your current posted code, paste it into my IDE, and then compile and run it as a complete program? It doesn't look that way to me. Again, please have a look at the [mre] link since based on this post, I don't think that you've read it fully yet. – Hovercraft Full Of Eels May 28 '21 at 14:44
  • Try now. I updated my question – Stefan Jankovic May 28 '21 at 14:51
  • Made it, thank you for helping! – Stefan Jankovic May 28 '21 at 14:54
  • You still are not handling the end-of-line token with your Scanner in your `PassengersControllerImpl` class. – Hovercraft Full Of Eels May 28 '21 at 15:06
  • Side note: The Passengers should probably be renamed `Passenger` since it encapsulates the concept of a single Passenger object. Also, that class should not have or use a Scanner object or have any UI code within it. Instead if invalid parameters are passed into a method, that method should throw an exception, and your UI code class(es) should be wired to handle the exceptions, including re-obtaining valid input, if needed. – Hovercraft Full Of Eels May 28 '21 at 15:13
  • Thanks for suggestions, I will try to update my code – Stefan Jankovic May 28 '21 at 15:17

0 Answers0