My goal in this project is to be able to ask a user if they want to add a mechanic and bay number to their respective ArrayLists. My code works fine, but there is one annoying problem. When the while loop that starts all of the code over again runs, the prompt for if they would like to edit the schedule prints twice, once without allowing a user response. I realise there is a lot of code to read but I will print it all here, and the bug that I get every time. It would mean a lot if someone could take the time to tell me what I need to fix.
Code:
// Import packages and create main function
import java.util.Scanner;
import java.util.ArrayList;
public class schedule{
public static void main(String []args){
// Creates scanner and ArrayLists
Scanner scanner = new Scanner(System.in);
ArrayList<Integer> mechanics = new ArrayList<Integer>();
ArrayList<Integer> bays = new ArrayList<Integer>();
boolean edit = true;
while(edit == true){
edit = editSchedule(scanner);
// Runs AddorRemove or ends program based on user's previous choice
if(edit == true){
if(AddorRemove(scanner) == true){
add(scanner, mechanics, bays);
}
else{
remove(scanner, mechanics, bays);
}
}
else{
System.out.println("Have a nice day!");
}
}
}
// Asks user if they want to edit the schedule
public static boolean editSchedule(Scanner scanner){
String answer = "b";
while(!(answer.equals("y")) || answer.equals("n")){
System.out.print("Would you like to edit the schedule? (y/n): ");
answer = scanner.nextLine();
if(answer.equals("y") || answer.equals("n")){
break;
}
else{
System.out.println("Invalid response.");
}
}
if(answer.equals("y")){
return true;
}
else{
return false;
}
}
// Asks user if they want to add or remove an element
public static boolean AddorRemove(Scanner scanner){
String aor = "b";
while(!(aor.equals("a")) || aor.equals("r")){
System.out.print("Do you want to add or remove a mechanic and bay? (a/r): ");
aor = scanner.nextLine();
if(aor.equals("a") || aor.equals("r")){
break;
}
}
if(aor.equals("a")){
return true;
}
else{
return false;
}
}
// Checks to see that elements are not present already and adds element to the mechanic and bay ArrayLists
public static void add(Scanner scanner, ArrayList<Integer> mechanics, ArrayList<Integer> bays){
System.out.print("Enter the mechanic's ID number: ");
int mechanic = scanner.nextInt();
System.out.print("Enter the bay number: ");
int bay = scanner.nextInt();
boolean shouldAdd = true;
for(int i=0; i<=mechanics.size()-1; i++){
if(mechanic == mechanics.get(i)){
System.out.println("Mechanic " + mechanic + " is already booked.");
shouldAdd = false;
}
}
for(int j=0; j<=bays.size()-1; j++){
if(bay == bays.get(j)){
System.out.println("Bay " + bay + " is already booked.");
shouldAdd = false;
}
}
if(shouldAdd == true){
mechanics.add(mechanic);
bays.add(bay);
System.out.println("Mechanic " + mechanic + " booked for bay " + bay + ".");
}
else{
System.out.println("Could not book mechanic " + mechanic + " in bay " + bay + ".");
}
}
// Removes an element from the mechanic and bay ArrayLists
public static void remove(Scanner scanner, ArrayList<Integer> mechanics, ArrayList<Integer> bays){
System.out.println("remove");
}
}
That is the code so far. Don't worry about the remove function, as I haven't added anything to it yet. Mainly focus on the main function and the editSchedule function, which is where something goes wrong. I will now paste the problem I am having with the output.
Would you like to edit the schedule? (y/n): y
Do you want to add or remove a mechanic and bay? (a/r): a
Enter the mechanic's ID number: 1
Enter the bay number: 3
Mechanic 1 booked for bay 3.
Would you like to edit the schedule? (y/n): Invalid response.
Would you like to edit the schedule? (y/n):
All I want to know is what is making the code repeat the phrase "Would you like to edit the schedule? (y/n):" and then adding "Invalid response." If any of you need clarification or further explanation, please let me know. Thanks!