0

I have two classes here:

public class Invoice {

    ArrayList<Double> ItemPrices= new ArrayList<Double>();
    ArrayList<Boolean> ItemIsPet = new ArrayList<Boolean>();
    ArrayList<Integer> ItemQuantitys = new ArrayList<Integer>();

    public void add(Item anItem){

    }


    public static void main(String args[]){

        int counter = 0;
        boolean pet;
        Scanner s = new Scanner(System.in);

        do {

            System.out.println();
            System.out.print("Item " + counter+1 + ":");
            System.out.println();

            System.out.print("Please enter the item's price: ");
            double inputprice = s.nextDouble();

            System.out.println();
            System.out.print("Is this item a pet? (Y/N): ");
            String inputIsPet = s.next();
            if (inputIsPet.equals('Y')){
                pet = true;
            }
            else {
                pet = false;
            }
            System.out.println();

            System.out.print("What is the quantity of this item?: ");
            int inputquantity = s.nextInt();


            Item newItem = new Item(inputprice, pet, inputquantity);

            counter += 1;

        } while (true);

    }
}

And here is the second class:

public class Item {

    Invoice Inv = new Invoice();

    public Item(double price, boolean isPet, int quantity){

    }

}

My question is for this line right here:

Item newItem = new Item(inputprice, pet, inputquantity);

So I have those 3 necessary arguments from the user input so that I can make a new object newItem of type item, but my question is once I have object newItem, how can I access those inputted arguments? My assignment is forcing me to implement this particular "add" method:

public void add(Item anItem)

Basically, the way I want to use this method is to take the inputted arguments from here:

Item newItem = new Item(inputprice, pet, inputquantity);

and have the "add" method access them, and from there put them into these arrays:

ArrayList<Double> ItemPrices= new ArrayList<Double>();
ArrayList<Boolean> ItemIsPet = new ArrayList<Boolean>();
ArrayList<Integer> ItemQuantitys = new ArrayList<Integer>();

But how do I access the parts of the object newItem? Do I have to modify my constructor in some way?

2
  • Add instance fields and getters to the Item class and you are good to go Commented Apr 16, 2018 at 7:03
  • Are those arrays part of your assignment? I'd guess not, because they look very clunky and un-OO. Commented Apr 16, 2018 at 7:03

1 Answer 1

3

Do I have to modify my constructor in some way?

Yes, you need to save incoming values into instance fields.

public class Item {

    private final double price;
    private final boolean isPet;
    private final int quantity;

    public Item(double price, boolean isPet, int quantity) {
        this.price = price;
        this.isPet = isPet;
        this.quantity = quantity;
    }

}

How can I access those inputted arguments?

Then you can write a getter for each field you want to access to.

public double getPrice() {
    return price;
}

How do I access the parts of the object newItem?

Now you can.

double price = newItem.getPrice();
Sign up to request clarification or add additional context in comments.

1 Comment

@MatthewButner, you may accept the answer if it was helpful

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.