Java Программа - введите дважды в консоли - PullRequest
0 голосов
/ 15 апреля 2020

Я пишу программу для школы, в которой просто отслеживать велосипеды для магазина проката велосипедов. Независимо от того, что я делаю, когда решаю, что выбрать ie: A, L, R, C, Q, чтобы получить доступ к различной информации, я должен дважды нажать клавишу ввода, чтобы фактически отобразить информацию. Я изучал эту проблему некоторое время и не могу найти ничего, что решило бы ее. Казалось, что он появился из ниоткуда, и я не знаю, что его вызвало. Если бы вы, ребята, могли бы помочь мне, это было бы здорово!

Вот код (есть другой класс, но это были только методы, которые мне дал проф, поэтому я не думаю, что это уместно):

package lab_10;

import java.util.ArrayList;
import java.util.Scanner;

public class BikeReservations {

    // Fields

    final static int NUMBIKES = 5;

    // Constructor

    public BikeReservations() {

        Scanner input = new Scanner(System.in);
        String menuChoice = "";
        String firstName, lastName;
        double hoursRented;
        int bikeIndex;
        BikeRental newBike;
        ArrayList<BikeRental> bikeListing = new ArrayList<>();

        // Initialize the ArrayList by adding all NUMBIKES bikes
        this.addBikes(bikeListing);

        // Introductory Message
        System.out.println("*** Welcome Valued Employee! ***\n");
        System.out.println("We currently have " + NUMBIKES + " bikes available for rent.");
        System.out.println("Remember, bikes are numbered begining with zero.");
        System.out.println("Please wash hands after every transaction.");
        System.out.println("Safty is everyone's responsibility. --Mgmt");

        // User Menu
        while (!menuChoice.equals("Q")) {
            System.out.println();
            System.out.print("(A)vailability, (L)isting, (R)eserve Bike, (C)heck In, or (Q)uit > ");
            menuChoice = input.next().toUpperCase();
            //input.nextLine();

            if (menuChoice.equals("A"))
                this.bikeSelection(bikeListing);
            else if (menuChoice.equals("L")) {

                // TODO: 1. Implement the printBikeListing() method (below)
                printBikeListing(bikeListing);
            } 
            else if (menuChoice.equals("R")) {
                System.out.print("Bike Number: ");
                //int num = input.nextInt();

                while (!input.hasNextInt()) {
                    input.next();
                    System.out.println("Please enter a valid integer.");
                }
                // TODO: 2. Ensure user enters an integer (See textbook p 40)
                // Hint: There is an example of capturing a double a few line down...
                bikeIndex = input.nextInt();
                try {

                    if (!bikeListing.get(bikeIndex).isAvailable()) {
                        System.out.println("Sorry, that bike is already out.");
                    }

                    else { 

                        System.out.print("First name: ");
                        firstName = input.next();
                        System.out.print("Last name: ");
                        lastName = input.next();
                        System.out.print("Hours rented: ");

                        // Ensure user enters an double
                        while (!input.hasNextDouble()) { 
                            input.next();
                            System.out.println("Enter hours");
                        }
                        hoursRented = input.nextDouble();

                        newBike = new BikeRental();
                        newBike.checkBikeOut(firstName, lastName, hoursRented);
                        bikeListing.set(bikeIndex, newBike);
                        System.out.println("Reservation Complete.");
                    }
                }catch (IndexOutOfBoundsException e) {
                    System.out.println("Choose a number between 0 and 4");
                }
            }


                // TODO: 3. Allow this reservation only a if requested bike is available.
                // Otherwise issue a message that the bike is already checked out.
                // Hint: An if statement should do the trick. 
                // (see ArrayList API: get(), BikeRental API: isAvailable())

                // TODO: 4. Handle IndexOutOfBoundsException (see lab 5, textbook p 82)
                // Hint: what happens if you try to reserve a bike beyond NUMBIKES?

            if (menuChoice.equals("C")) {
                System.out.println("Bike Rented: ");
                while (!input.hasNextInt()) {
                    input.next();
                    System.out.println("Please enter a valid integer.");
                }
                bikeIndex = input.nextInt();
                try {
                    if (bikeListing.get(bikeIndex).isAvailable()) {
                        System.out.println("That bike is already checked in.");
                    }
                    else {
                        bikeListing.get(bikeIndex).checkBikeIn();
                    }
                }catch (IndexOutOfBoundsException e) {
                    System.out.println("Choose a number between 0 and 4");
                }

                // TODO: 5. 
                // a. Ask user for the bike index to be checked in,
                // then check bike in and make it available.
                // b. If no bikes are out, say so, and get out of this option
                // d. Prompt user if the bike they specify is already in.
                // c. Don't accept bad input (anything other than an int)
                // e. Handle exception if user enters an index out of range


            } 
            else if (menuChoice.equals("Q")) {
                System.out.println("<end>");
            } 
            else {
                System.out.println("Choose P, C, R, or Q");
            }
        }
        input.close();
    }


    // Methods

    public void addBikes(ArrayList<BikeRental> bikes) {
        for (int i = 0; i < NUMBIKES; ++i) {
            bikes.add(new BikeRental());
        }
    }

    public void bikeSelection(ArrayList<BikeRental> bikes) {
        for (int i = 0; i < bikes.size(); ++i) {
            if (bikes.get(i).isAvailable())
                System.out.println("Bike " + i + " is available for rent.");
        }
    }

    public void printBikeListing(ArrayList<BikeRental> bikes) {
        System.out.println("Bike\tFirst\tLast\tHours");
        for (int i = 0; i < bikes.size(); ++i) {
            System.out.println(i + "\t" + bikes.get(i).getFirstName() + "\t" + bikes.get(i).getLastName() + "\t" + bikes.get(i).getHoursRented());
        }

        // FIXME: Print the reservation data for each bike
        // Hint: see methods above that take the bike ArrayList as parameters
        // Notice also that there is printReseveration() method in the BikeRental API
    }

    public static void main(String[] args) {
        new BikeReservations();
    }
}
...