/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package shopping;
import java.util.ArrayList;
/**
* A simple ShoppingBag
*
* Stores items in an ArrayList
*
* Possible to add items
* Also can "empty" the bag
*
* You should NOT need to amend this code at all!
*
* NOTE: 'Object' is the main base class for everything in Java. This means that everything 'is an' Object
* ANy class you create will work with the shipping bag - you don't need to change this code!
*
* @author Ken
*/
public class ShoppingBag {
private ArrayList<Object> items;
/**
* Create a shopping bag by initialising the items to an empty arraylist
*/
public ShoppingBag(){
items = new ArrayList<Object>();
}
/**
* Add an item to the shopping bag
* @param someItem the item that has been bought
*/
public void buyItem(Object someItem){
this.items.add(someItem);
}
/**
* create a formatted String containing all the items in the ShoppingBag
* @return the formatted String
* Also clears the items list -- 'empties the bag'
*/
public String emptyBag(){
String itemList = "";
for(Object item: this.items){
itemList = itemList + item.toString() + "\n";
}
this.items.clear();
return itemList;
}
}
guys can someone tell me why it is declare "items" to time? one is private ArrayList<Object> items; another is items = new ArrayList<Object>();. first one is just saying data type and second one run to constructer ? if it is we already also declare data type when we run the constructer, why we need to first one ?