I want to only change the int value of a specific item in a LinkedList, to a new int value. I would also like to understand the logic of how it is done, not only the code.
p.s I can not change anything, I can only add to the inside of the methods.
Thank you.
For example: [Green tea, 1] to [Green tea, 2]
The method where this needs to happen
public void changeItemCount(int k, int m){
}
Class where the question is
import java.util.LinkedList;
import java.util.ListIterator;
public class GroceryList {
//this class will create an object that contains a list of Grocery Items
//variable
private LinkedList<GroceryItem> glist; //holds GroceryItems
//constructor: instantiates glist to an empty groceryList
public GroceryList() {
glist = new LinkedList<GroceryItem>();
}
// return number of GroceryItems in glist
public int getSize() {
return glist.size();
}
//return GroceryItem at requested index
public GroceryItem getItem(int k) {
GroceryItem m = glist.get(k);
return m;
}
//add GroceryItem x to end of glist
public void addItem(GroceryItem x) {
glist.add(x);
}
//Remove item at index k from glist
public GroceryItem removeItem(int k ) {
return glist.remove(k);
}
//change item count for k item in glist to m
public void changeItemCount(int k, int m) {
//need help here
}
//Should work as is
public void sortList( ) {
glist.sort(null);
System.out.println("Sorted GroceryList: " glist);
}
}
other class in relation:
public class GroceryItem implements Comparable<GroceryItem> {
//this class will create items from a Grocery Store and the desired quantity
//variables
private String name; //grocery item name
private int count; // number needed; make sure count is always >= 0
//set count = 1; set name = s in following constructor
//if only a string is passed, default count is set to 1
public GroceryItem ( String s) {
count = 1;
name = s;
}
//constructor
// set name = s and set count = initCount in following constructor
//if a string and an int are passed then we set them to the passed values
public GroceryItem ( String s , int initCount) {
name = s;
count = initCount;
}
//returns the current count
public int getCount() {
return count;
}
//returns a fixed/known amount
public void setCount(int m) {
count = m;
}
public String getName() {
return name;
}
//@Override
public int compareTo(GroceryItem o) {
if( name.compareTo(o.getName() ) < 0) //less than compared item
return -1;
else if (name.compareTo(o.getName()) > 0) //greater than compared item
return 1;
else
return( count - o.count); //duplicated item?
}
//toString method: formatting: [bananas, 3]
public String toString() {
name.toString();
String temp = Integer.toString(count);
String formatt = name ", " temp ;
return formatt;
}
}
main driver:
public class GroceryListDriver {
//this class tests the methods of the two other classes
public static void main(String[] args) {
// TODO Auto-generated method stub
//creating 10 items in GroceryItem
GroceryItem items1 = new GroceryItem("banana", 5);
GroceryItem items2 = new GroceryItem("apples", 5);
GroceryItem items3 = new GroceryItem("pea soup", 2);
GroceryItem items4 = new GroceryItem("apples", 3);
GroceryItem items5 = new GroceryItem("wheat bread", 2);
GroceryItem items6 = new GroceryItem("tuna fish", 5);
GroceryItem items7 = new GroceryItem("potatoes", 4);
GroceryItem items8 = new GroceryItem("sourdough bread", 1);
GroceryItem items9 = new GroceryItem("chedadar cheese", 1);
GroceryItem items10 = new GroceryItem("green tea", 1);
//creating groceryList called groceryList
GroceryList groceryList = new GroceryList();
//adding to the list
groceryList.addItem(items1);
groceryList.addItem(items2);
groceryList.addItem(items3);
groceryList.addItem(items4);
groceryList.addItem(items5);
groceryList.addItem(items6);
groceryList.addItem(items7);
groceryList.addItem(items8);
groceryList.addItem(items9);
groceryList.addItem(items10);
//print current list
//groceryList.printGroceryList(null);
//sorting groceryList
//groceryList.sortList();
//change count of green tea to 2
System.out.println(groceryList.getItem(9));
}
}
CodePudding user response:
Try this one:
public void changeItemCount(int k, int m){
GroceryItem item = getItem(k); //just get the item from list
item.setCount(m); //and change the count value
}