mercredi 30 septembre 2020

while loop defaults to else statement. Java

I was assigned a bike shop assignment, where you would be able to rent or return a bike through this program. You to take into counter stock and such. It was very open ended so you could do what ever you felt would work, I don't have crazy great coding skills so I'm probably making it more complicated than it would be for someone else. My solution was to make do-while loop that would replay the same questions until we ran out of bikes and then display a different question when we had none. not everything is all planned out yet but I've run into a problem, every time the loop starts it defaults to the else statement without allowing user input and then loops again and lets you answer. I don't really know if that's a good explanation but ill leave my code and maybe you can help me.

import java.awt.Choice;
import java.util.Scanner;
public class bikeShop {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        
        Scanner scan = new Scanner(System.in);
        int amount = 13;


        do 
        {
        System.out.println("Welcome to the bike shop. Are you here to rent a bike or return a bike? Please enter rent or return.");
        String choice = scan.nextLine();
        
        if (choice.equals("rent"))
        {
            System.out.println("Would you like to rent as a family (3-5 bike) or as an individual? Please enter family or individual.");
            String plan = scan.nextLine();
            
            if (plan.equals("individual"))
            {
            System.out.println("How long would you like to rent? you can rent by hours, days, and weeks. If none put 0. How any hours?");
            int hours = scan.nextInt();
            System.out.println("How any days?");
            int days = scan.nextInt();
            System.out.println("How any weeks?");
            int weeks = scan.nextInt();
            
            amount --;
            
            int total = (((hours * 5) + (days * 20) + (weeks * 60)));
            System.out.println("The total cost is: $" + total );
            }
            
            else if (plan.equals("family"))
            {
                System.out.println("How many people will you be renting for?");
                int people = scan.nextInt();
                if (people > 2 & people < 6 & people > amount)
                {
                    System.out.println("I'm sorry we don't have that many bikes in stock");
                }
                else if (people > 2 & people < 6)
                {
                System.out.println("How long would you like to rent? you can rent by hours, days, and weeks. If none put 0. How many hours?");
                int hours = scan.nextInt();
                System.out.println("How many days?");
                int days = scan.nextInt();
                System.out.println("How many weeks?");
                int weeks = scan.nextInt();
                
                amount = (amount - people);
                
                int time = ((hours * 5) + (days * 20) + (weeks * 60));
                int total = ( (((time * people)  * 70) / 100 ));
                System.out.println("The total cost is: $" + total);
                }
                else 
                {
                    System.out.println("Error, that is not a valid answer.");
                }
            }
            else
            {
                System.out.println("Error, that is not a valid answer.");
            }
        }
        
        else if (choice.equals("return"))
        {
            System.out.println("Which bike are you returning?");
        }
        else 
        {
            System.out.println("Error, that is not a valid answer.");
        }
        
        }
        while(amount > 0);
        
        
        if (amount == 0)
        {
            System.out.println("We are all out of bikes for now, are you returning a bike?");
            String returnBike = scan.nextLine();
            
            if (returnBike.equals("yes"))
            {
                System.out.println("Which bike are you returning?");
            }
            else if (returnBike.equals("no"))
            {
                System.out.println("We are all out of bikes for now.");
            }
            else
            {
                System.out.println("Error, that is not a valid answer.");
            }
        }
    }
}

Aucun commentaire:

Enregistrer un commentaire