Hey Guys, I am new here but looking to come here often and obtain some leet skills in java. I am new to programming but do enjoy it. I was just assigned some homework that has me stumped. I would go talk to someone at my school but they are all international students so I don't understand most of what they say.
I uploaded the assignment. I am having trouble with the nested loops and how to get started.. Thanks, I am not expecting someone to do it for me, just a step or two in the right direction.

import java.util.*;

public class HW4 {

	public static void main(String args[]) {

		Scanner keyboard = new Scanner(System.in);
		double budget = 1;

		int categoryControl;
		String receipt = "";
		int itemSelection = 0;
		int itemQuanitity = 0;
		int dvdCount = 0;
		int firstitemQuanitiy = 0;
		int secounditemQuanitiy = 0;
		int thirditemQuanitiy = 0;
		int fourthitemQuanitiy = 0;

		System.out.println("Welcome to Panther Gift Store");

		System.out.println("Please enter shopping budget ($): ");
		budget = keyboard.nextDouble();
		while (budget < 0) {
			System.out.println("Invalid budget");
			System.out.println("Please enter shopping budget ($): ");
			budget = keyboard.nextDouble();

		}

		for (int i = 1; i > 0; i++) {
			System.out
					.println("================================================================");
			System.out.println("");
			System.out
					.println("Panther Giftstore                              Balance: $"
							+ budget);
			System.out.println("------------------");
			System.out.println("1 Panther DVDS");
			System.out.println("2 Panther Figurines");
			System.out.println("3 Panther Foods");
			System.out.println("");
			System.out.println("Please select your category (0 to exit): ");
			categoryControl = keyboard.nextInt();

			if (categoryControl == 1) {
				System.out.println("");
				System.out
						.println("Panther DVDs                               Balance: $"
								+ budget);
				System.out.println("------------");
				System.out.println("1 The Jungle Book ($10.95)");
				System.out.println("2 Monsoon Railway ($8.25");
				System.out.println("3 Great Peaks ($49.95)");
				System.out.println("4 Thomas and Friends ($5.00)");
				System.out.println("");
				System.out
						.println("Please select your DVD (1-4, 0 to main menu):");

				if (itemSelection == 1) {
					budget = budget - 10.95;
					dvdCount++;
				}

				System.out.println("Please enter quantity (0 to cancel):");
				System.out.println("Added to shopping cart");

			}

		}

	}

}

Recommended Answers

All 3 Replies

Well, one thing is for sure - you're going to need to use a try-catch clause inside a conditional while-loop for error checking purposes.

I also wouldn't worry about initializing all those integers for different inputs. Just break your program up into different methods and only worry about the current input value, then call the corresponding method.

Here's an example of everything I talked about:

boolean Running = True;

    While(Running)
    {
        try
        {
             //some code
             int input = scanner.nextInt();
             if(input==0)
             {
                 Running = False;
             }
             else if(input==1)
             {
                 pantherDVDMenu();
             }
        }
        catch (InputMismatchException e)
        {
            System.out.println(e.getMessage());
        }
    }

You can then have the pantherDVDMenu method call itself recursively (A method that calls itself) until the user chooses to exit.

Public static void pantherDVDMenu() throws InputMismatchException
{
  
     System.out.println(" Enter initial text here, etc...");

     int input = scanner.nextInt();

     if(input==1)
     {
          System.out.println("Quantity?");
          int input = scanner.nextInt();
          budget -= price*input;
          pantherDVDMenu();
     }
     [B][I]//...Other Stuff //[/I][/B]
  
}

The easiest way to deal with the budget and scanner in this fashion for a novice at Java is to make the budget and scanner variable a global private variable under the class, so that all methods have access.

Public class Store
{
    static private double budget = 0.0;
    static private Scanner scanner = new Scanner(System.in);

    Public static void main(String[] args)
    {
      [B][I]//...etc...[/I][/B]

This is all pseudo code so you'll have to adapt it on your own. Hope that helped!

I unfortunately can't use methods as the teacher wants use to master the if/for/while stuff. So I don't think I can even use the try argument..

You can complete the problem without using multiple methods, but as your program requires a user to stay on a specific menu until the user requests an exit, it's really a lot messier and I would not encourage it. Any professor that *limits* what you can do would have gotten an earful from me, lol.

Anyway, here's an example:

boolean Running = True;
int input;
    While(Running)
    {
         //some code
         input = scanner.nextInt();
         if(input==0)
         {
             Running = False;
         }
         else if(input==1)
         {

             While(True)
             {
                   //panther menu here.
                   System.out.println(" Enter initial text here, etc...");

                   input = scanner.nextInt();

                   if(input==1)
                   {
                        System.out.println("Quantity?");
                        input = scanner.nextInt();
                        budget -= price*input;
                   } 
                   if(input==0)
                   { break; } //break out of this menu item.
             }
         }
        
    }

That code should put you well on your way, good luck.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.