Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9249889
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T10:18:38+00:00 2026-06-18T10:18:38+00:00

I have two classes. The first is complete and it is making an Item

  • 0

I have two classes. The first is complete and it is making an Item object:

public class Item {

private String name; // The Name of the item.
private int unitPrice; // The price of each unit of this item, in cents.
private int quantity; // The number of units of this item.

// CONSTRUCTOR

/*
 * Constructs an item with specified name, price, and quantity.
 */
public Item(String name, int unitPrice, int quantity) {
    this.name = name;
    this.unitPrice = unitPrice;
    this.quantity = quantity;
}

// METHODS

/*
 * Gets the name for the item. Returns the name for this item.
 */
public String getName() {
    return name;
}

/*
 * Gets the price of each unit of a given item, in cents. Return the price.
 */
public int getUnitPrice() {
    return unitPrice;
}

/*
 * Gets and returns the number of units of this item.
 */
public int getQuantity() {
    return quantity;
}

/*
 * Increases or decreases the quantity of this item. If value is positive,
 * quantity increases. If negative, quantity decreases.
 */
public void changeQuantity(int value) {
    quantity = quantity + value;
}

/*
 * Gets the total price, in cents. Returns the productof quantity and
 * unitPrice as the total price of this item.
 */
public int getTotalPrice() {
    return quantity * unitPrice;
}

/*
 * Returns a string of this item, including name, price($)
 */
public String toString() {
    return name + ": " + quantity + " ($" + unitPrice / 100 + "."
            + unitPrice % 100 + " per unit)";
}

}

Now, I am trying to do a findItem method, where i search through. Not exactly sure how to do it. I would like to go through all of the inventory and try to match the name. I believe I should use getName() but not sure how to go through each name.

public class Store {
private ArrayList<Item> inventory;

//MOTHODS

/*
 * Finds an item by its name if it is part of the store's inventory
 * Name is case-insensitive
 * Returns the Item object corresponding to the given name if the item was found. If an item with the given name was not found, then this method returns null.
 */
public Item findItem(String name){
    for(int i=0; i<inventory.size();i++){
        if(name.toUpperCase().equals(?????))
    }
}

}

Thanks for the help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T10:18:39+00:00Added an answer on June 18, 2026 at 10:18 am

    Why not use a for-each loop?

    public Item findItem(String name)
    {
        for(Item item : inventory)
        {
            if(item.getName().equalsIgnoreCase(name))
            {
                return item;
            }
        }
    
        return null;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

my code is like this: i have two classes first class: public class Box<E>
I have two classes Car and Drivers : public class Car { public string
I have two classes, first: public class A { public delegate void Function(); public
i have made two view classes in first view class i am making more
I have two (unrelated) classes. The first one is Point: typedef std::complex<double> complex_number; class
Let's say I have two classes. Each class has one parameter. Parameter of first
I have two classes like so: public class SentEmailAttachment : ISentEmailAttachment { public SentEmailAttachment();
I have two classes. First class has TabPage control. I want to change layout
Suppose that I have two classes, first a class without any properties, fields or
Scenario 1: I have two classes: Post : class Post { String content Date

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.