Here is the code
import java.util.*;
class GroceryItem
{
String name;
double value;
GroceryItem(String n, double v)
{
name=n;
value=v;
}
void setName(String n)
{
name=n;
}
void setValue(double n)
{
if(n>0)
value=n;
else value=0;
}
String getName()
{
return name;
}
double getValue()
{
return value;
}
public String toString()
{
return "Name: "+name+", Price: "+value;
}
boolean equals(GroceryItem m)
{
if(this.name.equals(m.name) && this.value==m.value) return true;
else return false;
}
}
public class GroceryList
{
class ListNode
{
GroceryItem data;
ListNode link;
ListNode(GroceryItem i)
{
data=i;
link=null;
}
ListNode()
{
data=null;
link=null;
}
}
ListNode head, current, previous;
GroceryList()
{
head=null;
current=head;
previous=head;
}
void addItem(String s, double v)
{
ListNode n=new ListNode(new GroceryItem(s,v));
if(head==null)
{
head=n;
}
else if(head.link==null)
{
head.link=n;
}
else
{
ListNode t=head;
while(t.link!=null)
{
t=t.link;
}
t.link=n;
}
}
void goToNext()
{
if(current.link!=null)
{
previous=current;
current=current.link;
}
}
String getDataAtCurrent()
{
return current.data.toString();
}
void setDataAtCurrent(GroceryItem n)
{
if(current!=null)
{
current.data.setName(n.getName());
current.data.setValue(n.getValue());
}
}
void insertAfterCurrent(GroceryItem m)
{
if(current!=null)
{
ListNode n= new ListNode(m);
n.link=current.link;
current.link=n;
System.out.println("Node Added");
}
}
void deleteCurrentNode()
{
if(current!=null)
{
previous.link=current.link;
}
}
void showList()
{
ListNode t=head;
while(t!=null)
{
System.out.println(t.data+" ");
t=t.link;
}
System.out.println("Done! ");
}
boolean contains(String n, double v)
{
ListNode t=head;
GroceryItem i = new GroceryItem(n,v);
while(t!=null)
{
if(t.data.equals(i))
{
return true;
}
else t=t.link;
}
return false;
}
double totalCost()
{
ListNode t=head;
double cost=0;
while(t!=null)
{
cost+=t.data.getValue();
t=t.link;
}
return cost;
}
public static void main(String a[])
{
System.out.println("Grocery List Tester");
System.out.println("Creating Grocery List");
GroceryList l = new GroceryList();
System.out.println("Adding 5 grocery items");
l.addItem("Taco Shells", 2.0);
l.addItem("Salsa", 1.59);
l.addItem("Ground Beef", 4.59);
l.addItem("Taco Shells", 1.3);
l.addItem("Shredded Cheese", 2.99);
l.showList();
System.out.println("Do I remember the salsa? "+l.contains("Salsa", 1.59));
System.out.println("Total Cost "+l.totalCost());
}
}
Solution
Here is the code
import java.util.*;
class GroceryItem
{
String name;
double value;
GroceryItem(String n, double v)
{
name=n;
value=v;
}
void setName(String n)
{
name=n;
}
void setValue(double n)
{
if(n>0)
value=n;
else value=0;
}
String getName()
{
return name;
}
double getValue()
{
return value;
}
public String toString()
{
return "Name: "+name+", Price: "+value;
}
boolean equals(GroceryItem m)
{
if(this.name.equals(m.name) && this.value==m.value) return true;
else return false;
}
}
public class GroceryList
{
class ListNode
{
GroceryItem data;
ListNode link;
ListNode(GroceryItem i)
{
data=i;
link=null;
}
ListNode()
{
data=null;
link=null;
}
}
ListNode head, current, previous;
GroceryList()
{
head=null;
current=head;
previous=head;
}
void addItem(String s, double v)
{
ListNode n=new ListNode(new GroceryItem(s,v));
if(head==null)
{
head=n;
}
else if(head.link==null)
{
head.link=n;
}
else
{
ListNode t=head;
while(t.link!=null)
{
t=t.link;
}
t.link=n;
}
}
void goToNext()
{
if(current.link!=null)
{
previous=current;
current=current.link;
}
}
String getDataAtCurrent()
{
return current.data.toString();
}
void setDataAtCurrent(GroceryItem n)
{
if(current!=null)
{
current.data.setName(n.getName());
current.data.setValue(n.getValue());
}
}
void insertAfterCurrent(GroceryItem m)
{
if(current!=null)
{
ListNode n= new ListNode(m);
n.link=current.link;
current.link=n;
System.out.println("Node Added");
}
}
void deleteCurrentNode()
{
if(current!=null)
{
previous.link=current.link;
}
}
void showList()
{
ListNode t=head;
while(t!=null)
{
System.out.println(t.data+" ");
t=t.link;
}
System.out.println("Done! ");
}
boolean contains(String n, double v)
{
ListNode t=head;
GroceryItem i = new GroceryItem(n,v);
while(t!=null)
{
if(t.data.equals(i))
{
return true;
}
else t=t.link;
}
return false;
}
double totalCost()
{
ListNode t=head;
double cost=0;
while(t!=null)
{
cost+=t.data.getValue();
t=t.link;
}
return cost;
}
public static void main(String a[])
{
System.out.println("Grocery List Tester");
System.out.println("Creating Grocery List");
GroceryList l = new GroceryList();
System.out.println("Adding 5 grocery items");
l.addItem("Taco Shells", 2.0);
l.addItem("Salsa", 1.59);
l.addItem("Ground Beef", 4.59);
l.addItem("Taco Shells", 1.3);
l.addItem("Shredded Cheese", 2.99);
l.showList();
System.out.println("Do I remember the salsa? "+l.contains("Salsa", 1.59));
System.out.println("Total Cost "+l.totalCost());
}
}

Here is the codeimport java.util.; class GroceryItem { Stri.pdf

  • 1.
    Here is thecode import java.util.*; class GroceryItem { String name; double value; GroceryItem(String n, double v) { name=n; value=v; } void setName(String n) { name=n; } void setValue(double n) { if(n>0) value=n; else value=0; } String getName() { return name; } double getValue() { return value; } public String toString() {
  • 2.
    return "Name: "+name+",Price: "+value; } boolean equals(GroceryItem m) { if(this.name.equals(m.name) && this.value==m.value) return true; else return false; } } public class GroceryList { class ListNode { GroceryItem data; ListNode link; ListNode(GroceryItem i) { data=i; link=null; } ListNode() { data=null; link=null; } } ListNode head, current, previous; GroceryList() { head=null; current=head; previous=head;
  • 3.
    } void addItem(String s,double v) { ListNode n=new ListNode(new GroceryItem(s,v)); if(head==null) { head=n; } else if(head.link==null) { head.link=n; } else { ListNode t=head; while(t.link!=null) { t=t.link; } t.link=n; } } void goToNext() { if(current.link!=null) { previous=current; current=current.link; } }
  • 4.
    String getDataAtCurrent() { return current.data.toString(); } voidsetDataAtCurrent(GroceryItem n) { if(current!=null) { current.data.setName(n.getName()); current.data.setValue(n.getValue()); } } void insertAfterCurrent(GroceryItem m) { if(current!=null) { ListNode n= new ListNode(m); n.link=current.link; current.link=n; System.out.println("Node Added"); } } void deleteCurrentNode() { if(current!=null) { previous.link=current.link; } } void showList() { ListNode t=head; while(t!=null) { System.out.println(t.data+" ");
  • 5.
    t=t.link; } System.out.println("Done! "); } boolean contains(Stringn, double v) { ListNode t=head; GroceryItem i = new GroceryItem(n,v); while(t!=null) { if(t.data.equals(i)) { return true; } else t=t.link; } return false; } double totalCost() { ListNode t=head; double cost=0; while(t!=null) { cost+=t.data.getValue(); t=t.link; } return cost; } public static void main(String a[]) {
  • 6.
    System.out.println("Grocery List Tester"); System.out.println("CreatingGrocery List"); GroceryList l = new GroceryList(); System.out.println("Adding 5 grocery items"); l.addItem("Taco Shells", 2.0); l.addItem("Salsa", 1.59); l.addItem("Ground Beef", 4.59); l.addItem("Taco Shells", 1.3); l.addItem("Shredded Cheese", 2.99); l.showList(); System.out.println("Do I remember the salsa? "+l.contains("Salsa", 1.59)); System.out.println("Total Cost "+l.totalCost()); } } Solution Here is the code import java.util.*; class GroceryItem { String name; double value; GroceryItem(String n, double v) { name=n; value=v;
  • 7.
    } void setName(String n) { name=n; } voidsetValue(double n) { if(n>0) value=n; else value=0; } String getName() { return name; } double getValue() { return value; } public String toString() { return "Name: "+name+", Price: "+value; } boolean equals(GroceryItem m) { if(this.name.equals(m.name) && this.value==m.value) return true; else return false; } } public class GroceryList {
  • 8.
    class ListNode { GroceryItem data; ListNodelink; ListNode(GroceryItem i) { data=i; link=null; } ListNode() { data=null; link=null; } } ListNode head, current, previous; GroceryList() { head=null; current=head; previous=head; } void addItem(String s, double v) { ListNode n=new ListNode(new GroceryItem(s,v)); if(head==null) { head=n; } else if(head.link==null) {
  • 9.
    head.link=n; } else { ListNode t=head; while(t.link!=null) { t=t.link; } t.link=n; } } void goToNext() { if(current.link!=null) { previous=current; current=current.link; } } StringgetDataAtCurrent() { return current.data.toString(); } void setDataAtCurrent(GroceryItem n) { if(current!=null) { current.data.setName(n.getName()); current.data.setValue(n.getValue()); } }
  • 10.
    void insertAfterCurrent(GroceryItem m) { if(current!=null) { ListNoden= new ListNode(m); n.link=current.link; current.link=n; System.out.println("Node Added"); } } void deleteCurrentNode() { if(current!=null) { previous.link=current.link; } } void showList() { ListNode t=head; while(t!=null) { System.out.println(t.data+" "); t=t.link; } System.out.println("Done! "); } boolean contains(String n, double v) { ListNode t=head; GroceryItem i = new GroceryItem(n,v); while(t!=null) { if(t.data.equals(i))
  • 11.
    { return true; } else t=t.link; } returnfalse; } double totalCost() { ListNode t=head; double cost=0; while(t!=null) { cost+=t.data.getValue(); t=t.link; } return cost; } public static void main(String a[]) { System.out.println("Grocery List Tester"); System.out.println("Creating Grocery List"); GroceryList l = new GroceryList(); System.out.println("Adding 5 grocery items"); l.addItem("Taco Shells", 2.0); l.addItem("Salsa", 1.59); l.addItem("Ground Beef", 4.59); l.addItem("Taco Shells", 1.3); l.addItem("Shredded Cheese", 2.99);
  • 12.
    l.showList(); System.out.println("Do I rememberthe salsa? "+l.contains("Salsa", 1.59)); System.out.println("Total Cost "+l.totalCost()); } }