4 Replies - 461 Views - Last Post: 23 April 2015 - 04:55 PM Rate Topic: -----

#1 jdoe   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 11-April 15

Java code not working

Posted 23 April 2015 - 04:19 PM

I'm doing a grocery app and I'm having some problems with some methods in one of my classes. Here's what I should do:

"The Inventory class’s getItem method retrieves a reference to the most recently found Item or Brand object.

In the two newItem methods, start with a call to the findItem method with the second argument true.
If this returns false, add a new item or new branded item to the inventory.

Make the code tight by chaining the setQuantity and setPrice method calls after an anonymous new Item or new Brand instantiation in the add argument.

In the setQuantity, setPrice, and update methods, if the current item is not null, change the quantity or the price for the current item.
In the getQuantity and getPrice methods, if the current item is not null, get the quantity or the price for the current item. If the current item is null in the getQuantity or getPrice method, return -1 and Double.NaN, respectively.

In the findItem methods, initialize itemsFound at 0.
Then, loop through the entire inventory, and whenever there is a match between the specified type or the specified brand and specified type, assign a reference to the matching item to the item instance variable, and increment itemsFound."

This is my code

public class Inventory {
    
    ArrayList<Inventory> inventory = new ArrayList<>();
    String category;
    Item item = null;
    
    Inventory(String category){
        this.category=category;
    }
    
    public Item getItem(Item item){
        return item;
    }
    
    public void newItem(String type, int quantity, double price){
        inventory.add(type, quantity, price); //This does not work
    }
    
    public void newItem(String brand, String type, int quantity, double price){
        
    }
    
    public void setQuantity(int quantity){
        //edit this
    }
    
    public void setPrice(double price){
        //edit this
    }
    
    public int getQuantity(){
        return item.quantity;
    }
    
    public double getPrice(){
        return item.price;
    }
    
    public void update(int qtyIncrease){
        
    }
    
    public void update(double adjustmentFactor){
        
    }
    
    public boolean findItem(String type, boolean WarningIfFound){
        
        return true;//must change
    }
    
    public boolean findItem(String brand, String type, boolean warningIfFound){
        
        return true;//must change
    }
    
    public void stockReport(){
        
    }




I need help with the methods that I could not figure out how to do.

In the main, there is:

        Inventory store = new Inventory("groceries");
        store.newItem("bread", 15, 9.99);
        store.newItem("SunnyDale", "milk", 2, 2.00);
        store.newItem("eggs", 3, 1.50);
        store.newItem("bread", 2, 1.25); // warning: in stock
        store.stockReport();
        store.update("SunnyDale", "milk", .25); // raise price 25%
        store.update("eggs", -1); // lower quantity by 1
        store.update("beer", 3); // warning: not stocked
        store.newItem("BrookSide", "milk", 4, 1.95);
        store.stockReport();
        store.setPrice("iguanas", 99); // warning: not stocked
        store.setQuantity("SunnyDale", "milk", 3);
        store.setPrice("eggs", 2.00);
        System.out.println("milk quantity: " +
        store.getQuantity("BrookRidge", "milk")); // not stocked
        System.out.println("milk quantity: " +
        store.getQuantity("milk")); // ambiguity
        System.out.println(
        "eggs price: " + store.getPrice("eggs"));
        System.out.println(
        "milk price: " + store.getPrice("milk")); // ambiguity




Any help/tips will be appreciated. Thanks

The original can be found at that link. It's the first question.

Is This A Good Question/Topic? 0
  • +

Replies To: Java code not working

#2 no2pencil   User is online

  • Professor Snuggly Pants
  • member icon

Reputation: 6642
  • View blog
  • Posts: 30,932
  • Joined: 10-May 07

Re: Java code not working

Posted 23 April 2015 - 04:26 PM

"code not working" : Code does exactly what you tell it to.
Was This Post Helpful? 0
  • +
  • -

#3 jdoe   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 11-April 15

Re: Java code not working

Posted 23 April 2015 - 04:39 PM

View Postno2pencil, on 23 April 2015 - 04:26 PM, said:

"code not working" : Code does exactly what you tell it to.


What?
Was This Post Helpful? 0
  • +
  • -

#4 jdoe   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 11-April 15

Re: Java code not working

Posted 23 April 2015 - 04:50 PM

I'm doing a grocery app. There are 4 classes for the program: Inventory, Item, Brand, GroceryInventory(main). I'm having some problems to figure out some methods in one of my classes Inventory. Here's what I should do:

"The Inventory class’s getItem method retrieves a reference to the most recently found Item or Brand object.

In the two newItem methods, start with a call to the findItem method with the second argument true.
If this returns false, add a new item or new branded item to the inventory.

Make the code tight by chaining the setQuantity and setPrice method calls after an anonymous new Item or new Brand instantiation in the add argument.

In the setQuantity, setPrice, and update methods, if the current item is not null, change the quantity or the price for the current item.
In the getQuantity and getPrice methods, if the current item is not null, get the quantity or the price for the current item. If the current item is null in the getQuantity or getPrice method, return -1 and Double.NaN, respectively.

In the findItem methods, initialize itemsFound at 0.
Then, loop through the entire inventory, and whenever there is a match between the specified type or the specified brand and specified type, assign a reference to the matching item to the item instance variable, and increment itemsFound."

This is my code

Brand:
public class Brand extends Item {
    
    String brand;
    
    //Constructor
    public Brand(){   
    }
    
    Brand(String Brand){
        this.brand=brand;
    }
    
    public String getBrand(){
        return brand;
    }
}



Item:
public class Item{
    
    String type;
    int quantity;
    double price;
    
    //Constructor
    public Item(){
    }
    
    public Item(String type){
        this.type=type;
    }
    
    public void setQuantity(int qty){
        this.quantity=qty;
    }
    
    public void setPrice(double price){
        this.price=price;
    }
    
    public String getType(){
        return type;
    }
    
    public int getQuantity(){
        return quantity;
    }
    
    public double getPrice(){
        return price;
    }
    
    public void update(int qtyIncrease){
        quantity = quantity + qtyIncrease;
    }
    
    public void update(double adjustmentFactor){
        price = price * (1 + adjustmentFactor);
    }
}




Inventory (where I need help)
public class Inventory {
    
    ArrayList<Inventory> inventory = new ArrayList<>();
    String category;
    Item item = null;
    
    Inventory(String category){
        this.category=category;
    }
    
    public Item getItem(Item item){
        return item;
    }
    
    public void newItem(String type, int quantity, double price){
        inventory.add();
    }
    
    public void newItem(String brand, String type, int quantity, double price){
        
    }
    
    public void setQuantity(int quantity){
        //edit this
    }
    
    public void setPrice(double price){
        //edit this
    }
    
    public int getQuantity(){
        return item.quantity;
    }
    
    public double getPrice(){
        return item.price;
    }
    
    public void update(int qtyIncrease){
        
    }
    
    public void update(double adjustmentFactor){
        
    }
    
    public boolean findItem(String type, boolean WarningIfFound){
        
        return true;//must change
    }
    
    public boolean findItem(String brand, String type, boolean warningIfFound){
        
        return true;//must change
    }
    
    public void stockReport(){
        
    }
    
}



And the main method:

public class GroceryInventory {

    public static void main(String[] args) {

        Inventory store = new Inventory("groceries");
        store.newItem("bread", 15, 9.99);
        store.newItem("SunnyDale", "milk", 2, 2.00);
        store.newItem("eggs", 3, 1.50);
        store.newItem("bread", 2, 1.25); // warning: in stock
        store.stockReport();
        store.update("SunnyDale", "milk", .25); // raise price 25%
        store.update("eggs", -1); // lower quantity by 1
        store.update("beer", 3); // warning: not stocked
        store.newItem("BrookSide", "milk", 4, 1.95);
        store.stockReport();
        store.setPrice("iguanas", 99); // warning: not stocked
        store.setQuantity("SunnyDale", "milk", 3);
        store.setPrice("eggs", 2.00);
        System.out.println("milk quantity: " +
        store.getQuantity("BrookRidge", "milk")); // not stocked
        System.out.println("milk quantity: " +
        store.getQuantity("milk")); // ambiguity
        System.out.println(
        "eggs price: " + store.getPrice("eggs"));
        System.out.println(
        "milk price: " + store.getPrice("milk")); // ambiguity


    }
    
}



Any help/tips will be appreciated. Thanks
Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12316
  • View blog
  • Posts: 45,416
  • Joined: 27-December 08

Re: Java code not working

Posted 23 April 2015 - 04:55 PM

Please don't duplicate post.

Edit: Merged duplicate threads per our PM discussion.

I would advise you to ask specific questions about what you need help with. You've dumped a bunch of code with an "I don't know, please help" statement. That's not enough details for specific problems/errors you are encountering.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1