In this project I'm suppose to use a old project and create a menu for it. Basically "Create a menu system so that the user may choose between the various Roach functions (create roach colonies, breed and spray roaches and get the current number of roaches). Make sure you display what the user requested and the result."

Don't mind that everything may be off for now, i'm just taking my old project and a similar lab assignment code and modifying it for this project. I'm trying to work on the code one statement/case at a time.

Class file:

public class RoachMenu
{
	private double roachCount;

	public RoachMenu(double count) 
	
	
	{
		roachCount = count;
	}
	
    public String roachCount(String roaches) 
    {
        return "You now have " +roaches + " roaches in your colony." ;
    }

	public double getCount() // The roaches initial population.
	{
		return roachCount;
	}

	public void breed()
	{
		roachCount = roachCount * 2; // The roaches initial population times 2. 
	}

	public void spray() // Killing 10% of the roaches population
	{
		roachCount = roachCount * 0.90;
	}
}

Tester:

import java.util.Scanner;

public class RoachMenuTester
{
    public static void main(String args[])
    {       
        Scanner keyboard = new Scanner(System.in);
        int menuOption;
        int total = 0;
        
        System.out.println("This program will allow the user to choose");
        System.out.println( "between the various Roach functions.");
        RoachMenu myRoach = new RoachMenu(total);
        do {
            System.out.println("\n          Roach function Options");
            System.out.println("1. Create a Roach Colony.");
            System.out.println("2. Get Roach Count.");
            System.out.println("3. Breed.");
            System.out.println("4. Spray.");
            System.out.println("5. Exit.");
            System.out.print("What do you want to do? ");
            menuOption = keyboard.nextInt();
            switch (menuOption) 
            {
            case 1:
            	System.out.println("Menu option 1 chosen");
                System.out.println("How much roaches do you want in your colony? ");
                String roaches = keyboard.next();
                System.out.println(myRoach.roachCount(roaches));
                break;
            case 2:
            	System.out.println("Menu option 2 chosen");
                System.out.println("There are " +myRoach.getCount() + " roaches.");
                break;
            case 3:
            	System.out.println("Menu option 3 chosen");
                myRoach.breed();
                break;
            case 4:   
            	System.out.println("Menu option 4 chosen");
            	myRoach.spray();
                break;
            case 5:   
                System.out.println("Program terminating");
                break;
            default:
                System.out.println("Invalid menu option.  Please re-enter.");
                break;
            }
        } while (menuOption != 5);
    }
}

You know how I created the roach colony inside the tester class. How do I get that count? When I did the roach population project, I inputted the starting population instead of asking the user.

My question is how do I get the roach count from what the user inputted because when the user input the number it's not returning any value.. it just "break" after it input the count.

Thanks!

Opps. I wasn't suppose to touch my old project file.

Here's the Class i'm suppose to write a menu for.

public class RoachMenu
{
	private double roachCount;

	public RoachMenu(double count) 
	{
		roachCount = count;
	}

	public double getCount() // The roaches initial population.
	{
		return roachCount;
	}

	public void breed()
	{
		roachCount = roachCount * 2; // The roaches initial population times 2. 
	}

	public void spray() // Killing 10% of the roaches population
	{
		roachCount = roachCount * 0.90;
	}

	public String toString() // Print out string of what's left of the population.
	{
		return "Roach Count = " + roachCount;
	}
}

My question is still the same. Since I have to make a menu program for this class and use the methods from it also.. How would I get what the user input for the roach population to go back to the RoachMenu class and calculate the input?

My professor told me not to touch the class file( not to change anything).

Nevermind .. solved it :D.

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.