Question

Need help debugging. Create an application that keeps track of the items that a wizard can...

Need help debugging.

Create an application that keeps track of the items that a wizard can carry.

console application which has no errors:

import java.util.Scanner;

public class Console {
  
    private static Scanner sc = new Scanner(System.in);

    public static String getString(String prompt) {
        System.out.print(prompt);
        String s = sc.nextLine();
        return s;
    }

    public static int getInt(String prompt) {
        int i = 0;
        boolean isValid = false;
        while (!isValid) {
            System.out.print(prompt);
            if (sc.hasNextInt()) {
                i = sc.nextInt();
                isValid = true;
            } else {
                System.out.println("Error! Invalid integer. Try again.");
            }
            sc.nextLine(); // discard any other data entered on the line
        }
        return i;
    }

    public static int getInt(String prompt, int min, int max) {
        int i = 0;
        boolean isValid = false;
        while (!isValid) {
            i = getInt(prompt);
            if (i <= min) {
                System.out.println(
                        "Error! Number must be greater than " + min + ".");
            } else if (i >= max) {
                System.out.println(
                        "Error! Number must be less than " + max + ".");
            } else {
                isValid = true;
            }
        }
        return i;
    }

    public static double getDouble(String prompt) {
        double d = 0;
        boolean isValid = false;
        while (!isValid) {
            System.out.print(prompt);
            if (sc.hasNextDouble()) {
                d = sc.nextDouble();
                isValid = true;
            } else {
                System.out.println("Error! Invalid number. Try again.");
            }
            sc.nextLine(); // discard any other data entered on the line
        }
        return d;
    }

    public static double getDouble(String prompt, double min, double max) {
        double d = 0;
        boolean isValid = false;
        while (!isValid) {
            d = getDouble(prompt);
            if (d <= min) {
                System.out.println(
                        "Error! Number must be greater than " + min + ".");
            } else if (d >= max) {
                System.out.println(
                        "Error! Number must be less than " + max + ".");
            } else {
                isValid = true;
            }
        }
        return d;
    }
}

//-------------------------------------------------------------------------------------------------------------------------

WizardInventoryApp class which has several errors:

import java.util.ArrayList;
import java.util.List;

public class WizardInventoryApp {

    public static void main(String[] args) {
        System.out.println("The Wizard Inventory application\n");
      
        displayMenu();

        // all players start with these 3 items
        ArrayList inventory = new ArrayList<>();
        inventory("wooden staff");
        inventory("wizard hat");
        inventory("cloth shoes");

        String command = Console.getString("Command: ");
        while (command.equals("exit") {      
          
            if (command.equals("show")) {
                show(inventory);
            } else if (command.equals("grab")) {
                grabItem(inventory);
            } else if (command.equals("edit")) {
                editItem(inventory);
            } else {
                System.out.println("Not a valid command. Please try again.\n");
            }
        }
        System.out.println("Bye!");
    }
  
    private static void displayMenu() {
        System.out.println("COMMAND MENU");
        System.out.println("show - Show all items");
        System.out.println("grab - Grab an item");
        System.out.println("edit - Edit an item");
        System.out.println("exit - Exit program");
        System.out.println();
    }

    private static void show(List inventory) {
        for (int i = 0; i < inventory; i++) {
            String item = inventory(i);
            int number = i + 1;
            System.out.println(number + ". " + item);
        }
        System.out.println();
    }

    private static void grabItem(List inventory) {
        if (inventory >= 4) {
            System.out.println("You can't carry any more items. " +
                  "Drop something first.\n");
        } else {
            String item = Console.getString("Name: ");
            inventory(item);
            System.out.println(item + " was added.\n");
        }
    }

    private static void editItem(List inventory) {
        int number = Console.getInt("Number: ");
        if (number < 1 || number > inventory {
            System.out.println("Invalid item number.\n");
        } else {
            String item = Console.getString("Updated name: ");
            inventory(number-1, item);
            System.out.println("Item number " + number + " was updated.\n");
        }
    }

    private static void dropItem(List inventory) {
        int number = Console.getInt("Number: ");
        if (number < 1 || number > inventory {
            System.out.println("Invalid item number.\n");
        } else {
            String item = inventory(number-1);
            System.out.println(item + " was dropped.\n");
        }
    }
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.util.ArrayList;
import java.util.List;

public class WizardInventoryApp {

    public static void main(String[] args) {
        System.out.println("The Wizard Inventory application\n");

        displayMenu();

        // all players start with these 3 items
        ArrayList inventory = new ArrayList<>();//specify type
        inventory.add("wooden staff");//use add method to insert into array list
        inventory.add("wizard hat");
        inventory.add("cloth shoes");

        String command = Console.getString("Command: ");
        while (!command.equals("exit")) {//it should be while command not equals to exit

            if (command.equals("show")) {
                show(inventory);
            } else if (command.equals("grab")) {
                grabItem(inventory);
            } else if (command.equals("edit")) {
                editItem(inventory);
            } else if (command.equals("drop")) {
                dropItem(inventory);
            } else {
                System.out.println("Not a valid command. Please try again.\n");
            }
            command = Console.getString("Command: ");//ask again
        }
        System.out.println("Bye!");
    }

    private static void displayMenu() {
        System.out.println("COMMAND MENU");
        System.out.println("show - Show all items");
        System.out.println("grab - Grab an item");
        System.out.println("edit - Edit an item");
        System.out.println("drop - Drop an item");
        System.out.println("exit - Exit program");
        System.out.println();
    }

    private static void show(List inventory) {
        for (int i = 0; i < inventory.size(); i++) {//use size to get length
            String item = (String) inventory.get(i);//cast to string
            int number = i + 1;
            System.out.println(number + ". " + item);
        }
        System.out.println();
    }

    private static void grabItem(List inventory) {
        if (inventory.size() >= 4) {//use size
            System.out.println("You can't carry any more items. " +
                    "Drop something first.\n");
        } else {
            String item = Console.getString("Name: ");
            inventory.add(item);//use add method
            System.out.println(item + " was added.\n");
        }
    }

    private static void editItem(List inventory) {
        int number = Console.getInt("Number: ");
        if (number < 1 || number > inventory.size()) {//use size
            System.out.println("Invalid item number.\n");
        } else {
            String item = Console.getString("Updated name: ");
            inventory.set(number - 1, item);//use set to update an index
            System.out.println("Item number " + number + " was updated.\n");
        }
    }

    private static void dropItem(List inventory) {
        int number = Console.getInt("Number: ");
        if (number < 1 || number > inventory.size()) {//use size
            System.out.println("Invalid item number.\n");
        } else {
            String item = (String) inventory.remove(number - 1);
            System.out.println(item + " was dropped.\n");
        }
    }
}

Add a comment
Know the answer?
Add Answer to:
Need help debugging. Create an application that keeps track of the items that a wizard can...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Need help debugging. first class seems fine. second class is shooting an error on s =...

    Need help debugging. first class seems fine. second class is shooting an error on s = super.getString(prompt);   third class is giving me an error in the while loop where int num = console.getInt("Enter an integer:"); //-------------------------------------------------------------------------- import java.util.Scanner; public class Console {     private Scanner sc;     boolean isValid;     int i;     double d;        public Console()     {         sc = new Scanner(System.in);     }     public String getString(String prompt)     {         System.out.print(prompt);         return sc.nextLine();...

  • public static void main(String[] args) {         System.out.println("Welcome to the Future Value Calculator\n");         Scanner sc...

    public static void main(String[] args) {         System.out.println("Welcome to the Future Value Calculator\n");         Scanner sc = new Scanner(System.in);         String choice = "y";         while (choice.equalsIgnoreCase("y")) {             // get the input from the user             System.out.println("DATA ENTRY");             double monthlyInvestment = getDoubleWithinRange(sc,                     "Enter monthly investment: ", 0, 1000);             double interestRate = getDoubleWithinRange(sc,                     "Enter yearly interest rate: ", 0, 30);             int years = getIntWithinRange(sc,                     "Enter number of years: ", 0, 100);             System.out.println();            ...

  • Project 7-3 Guessing Game import java.util.Scanner; public class GuessNumberApp {    public static void main(String[] args)...

    Project 7-3 Guessing Game import java.util.Scanner; public class GuessNumberApp {    public static void main(String[] args) { displayWelcomeMessage(); // create the Scanner object Scanner sc = new Scanner(System.in); String choice = "y"; while (choice.equalsIgnoreCase("y")) { // generate the random number and invite user to guess it int number = getRandomNumber(); displayPleaseGuessMessage(); // continue until the user guesses the number int guessNumber = 0; int counter = 1; while (guessNumber != number) { // get a valid int from user guessNumber...

  • Step 4: Add code that discards any extra entries at the propmt that asks if you...

    Step 4: Add code that discards any extra entries at the propmt that asks if you want to enter another score. Notes from professor: For Step 4, add a loop that will validate the response for the prompt question:       "Enter another test score? (y/n): " This loop must only accept the single letters of ‘y’ or ‘n’ (upper case is okay). I suggest that you put this loop inside the loop that already determines if the program should collect...

  • I need to create a code for this prompt: In this project we will build a...

    I need to create a code for this prompt: In this project we will build a generic UserInput class for getting keyboard input from the user. Implementation: The class UserInput is a 'Methods only' class, and all the methods should be declared static. Look at the TestScanner.java program at the bottom of this page that inputs a string, int and double. It shows you how to use Scanner class to get input from the keyboard. Write FOUR simple methods, one...

  • Java -Create an interface and implement it In the murach.db package, create an interface named IProductDB....

    Java -Create an interface and implement it In the murach.db package, create an interface named IProductDB. This interface should specify this abstract method: public abstract Product get(String productCode); Modify the ProductDB class so it implements the IProductDB interface. Write the code for the new ‘get’ method. Then remove the getProductByCode method. In the Main class, modify the code so it works with the new ProductDB class. This code should create an instance of the IProductDB interface like this: IProductDB db...

  • I need to change the following code so that it results in the sample output below...

    I need to change the following code so that it results in the sample output below but also imports and utilizes the code from the GradeCalculator, MaxMin, and Student classes in the com.csc123 package. If you need to change anything in the any of the classes that's fine but there needs to be all 4 classes. I've included the sample input and what I've done so far: package lab03; import java.util.ArrayList; import java.util.Scanner; import com.csc241.*; public class Lab03 { public...

  • Below, you can find the description of your labwork for today. You can also find the...

    Below, you can find the description of your labwork for today. You can also find the expected output of this code in the Application Walkthrough section. You are going to improve your existing Money & Stock Trading Platform on previous week’s labwork by incorporating Collections. In previous labworks, you have used arrays for holding Customer and Item objects. For this labwork you need to use ArrayList for holding these objects. So, rather than defining Customer[] array, you need to define...

  • I have this program that works but not for the correct input file. I need the...

    I have this program that works but not for the correct input file. I need the program to detect the commas Input looks like: first_name,last_name,grade1,grade2,grade3,grade4,grade5 Dylan,Kelly,97,99,95,88,94 Tom,Brady,100,90,54,91,77 Adam,Sandler,90,87,78,66,55 Michael,Jordan,80,95,100,89,79 Elon,Musk,80,58,76,100,95 output needs to look like: Tom Brady -------------------------- Assignment 1: A Assignment 2: A Assignment 3: E Assignment 4: A Assignment 5: C Final Grade: 82.4 = B The current program: import java.io.File; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.util.Scanner; public class main {    public static void main(String[]...

  • Java Double Max Function I need help with this top problem. The bottom is the assignment...

    Java Double Max Function I need help with this top problem. The bottom is the assignment // return Double .NEGATIVE-INFINİTY if the linked list is empty public double max return max (first); h private static double max (Node x) f e I TODO 1.3.27 return 0; 1 package algs13; 2 import stdlib.*; 4 public class MyLinked f static class Node public Node() t 1 public double item; public Node next; 10 int N; Node first; 12 13 14 public MyLinked...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT