How can I fix my code so that objects when using find or remove are actually fou
ID: 3916344 • Letter: H
Question
How can I fix my code so that objects when using find or remove are actually found/removed in the driver?
Driver:
public static void main(String[] args) {
LinkedBag sodaBag = new LinkedBag();
Scanner input = new Scanner(System.in);
String userChoice;
//userName will store user's input for soda's name
String userName;
//userPrice will store user's input for soda's price
double userPrice;
//userSodaCalories will store user's input for soda's calories
int userSodaCalories;
do {
// display menu
System.out.println(" Bag of Sodas");
System.out.println(" A - Add 'Soda' to bag");
System.out.println("R - Remove 'Soda' from Bag");
System.out.println("F - Find 'Soda' from Bag");
System.out.println("D - Display 'Soda' from Bag");
System.out.println("X - Exit ");
System.out.println(" ");
System.out.print("Enter selection: ");
userChoice = input.nextLine();
if (userChoice.equalsIgnoreCase("X")
|| userChoice.equalsIgnoreCase("x")
|| userChoice.equalsIgnoreCase("Exit")) {
System.out.println(" Bye! ");
} else if (!(userChoice.equalsIgnoreCase("A")
|| userChoice.equalsIgnoreCase("R")
|| userChoice.equalsIgnoreCase("F")
|| userChoice.equalsIgnoreCase("D")
|| userChoice.equalsIgnoreCase("X"))) {
System.out.print(" Invalid Selection");
} else if (userChoice.equalsIgnoreCase("A")) {
System.out.print("Enter name of Soda to add to bag: ");
userName = input.nextLine();
System.out.print("Enter price of Soda to add to bag: ");
userPrice = input.nextDouble();
System.out.print("Enter calories of Soda to add to bag: ");
userSodaCalories = input.nextInt();
sodaBag.add(new Soda(userName, userPrice, userSodaCalories));
System.out.println("");
displayLinkedBagSoda(sodaBag);
}//Add
else if (userChoice.equalsIgnoreCase("R")) {
System.out.print("Enter name of Soda to remove to bag: ");
userName = input.nextLine();
System.out.print("Enter price of Soda to remove to bag: ");
userPrice = input.nextDouble();
System.out.print("Enter calories of Soda to remove to bag: ");
userSodaCalories = input.nextInt();
if (sodaBag.remove(new Soda(userName, userPrice, userSodaCalories))) {
System.out.print("Was able to remove " + " from Bag of Soda.");
} else {
System.out.print("Sorry! unable to remove " + " from Bag of Soda!");
}
System.out.println("");
displayLinkedBagSoda(sodaBag);
}//Remove
else if (userChoice.equalsIgnoreCase("F")) {
System.out.print("Enter name of Soda to find to bag: ");
userName = input.nextLine();
System.out.print("Enter price of Soda to find to bag: ");
userPrice = input.nextDouble();
System.out.print("Enter calories of Soda to find to bag: ");
userSodaCalories = input.nextInt();
if (sodaBag.exists(new Soda(userName, userPrice, userSodaCalories))) {
System.out.print(userName + " with " + userSodaCalories + " calories " + " @ $" + userPrice + " is in Bag of Soda.");
} else {
System.out.print(userName + " with " + userSodaCalories + " calories " + " @ $" + userPrice + " is not in Bag of Soda.");
}
}//Find
else if (userChoice.equalsIgnoreCase("D")) {
System.out.print("Soda Bag: ");
displayLinkedBagSoda(sodaBag);
System.out.println("");
}//Display
} while (!(userChoice.equalsIgnoreCase("X")
|| userChoice.equalsIgnoreCase("x")
|| userChoice.equalsIgnoreCase("Exit")));
}
public static void displaySize(LinkedBag bagToDisplay, String label) {
System.out.println(label + " Size: " + bagToDisplay.getSize());
}
public static void displayLinkedBagSoda(LinkedBag bagToDisplay) {
if (bagToDisplay.getSize() == 0) {
System.out.println("--Empty--");
}
Lister sodaList = bagToDisplay.iterator();
while (sodaList.hasNext()) {
Soda displaySoda = sodaList.next();
System.out.print("{" + displaySoda.getSodaName() + " " + displaySoda.getSodaPrice() + " " + displaySoda.getSodaCalories() + "}");
if (sodaList.hasNext()) {
System.out.print(", ");
}
}
}
public static String makeStringOfSoda(Soda sodaToConvert) {
return "[Soda " + sodaToConvert.getSodaName() + " " + sodaToConvert.getSodaPrice() + " " + sodaToConvert.getSodaCalories() + "]";
}
}
Soda/Object Class:
public class Soda implements Comparable{
private String sodaName;
private double sodaPrice;
private int sodaCalories;
/**
* Constructor
* @param initalPartName value for sodaName field
* @param initalPrice value for sodaPrice field
* @peram initalCalories value for sodaCalolires field
*/
public Soda(String initalSodaName, double initalPrice, int initalCalories )
{
sodaName = initalSodaName;
sodaPrice = initalPrice;
sodaCalories = initalCalories;
}
/**
* getSodaName method returns value from sodaName field
* @return value from sodaName field
*/
public String getSodaName()
{
return sodaName;
}
/**
* getSodaPrice method returns value in sodaPrize field
* @return value in sodaPrice field
*/
public double getSodaPrice()
{
return sodaPrice;
}
/**
* getSodaCalories method returns value in sodaCalories field
* @return value in sodaCalories field
*/
public int getSodaCalories()
{
return sodaCalories;
}
/**
* setSodaName sets a new value for sodaName field
* @param newSodaName new value for sodaName field
*/
public void setSodaName(String newSodaName)
{
sodaName = newSodaName;
}
/**
* setSodaPrice method sets a new value in sodaPrice field
* @param newSodaPrice new value for sodaPrice field
*/
public void setSodaPrice(double newSodaPrice)
{
sodaPrice = newSodaPrice;
}
/**
* setSodaCalories method sets a new value in sodaCalories field
* @param newSodaPrice new value for sodaCalories field
*/
public void setSodaCalories(int newSodaCalories)
{
sodaCalories = newSodaCalories;
}
public int compareTo(Soda anotherSoda)
throws ClassCastException
{
if (!(anotherSoda instanceof Soda))
throw new ClassCastException("A Soda object expected.");
return 0;
}
}
LinkedList/LinkedBag Object class :
public class LinkedBag> {
private Node head;
private Node tail;
private int numElements;
/**
* No-arg Constructor
*/
public LinkedBag() {
head = null;
tail = null;
numElements = 0;
}
/**
* The getSize method returns the linked bag's current size.
*
* @return The value in the numElements field.
*/
public int getSize() {
return numElements;
}
/**
* The add method adds a String to the linked bag This version just sticks
* the value at the end of the list
*
* @param newElement The String to be added to the linked bag.
*/
public void add(E newElement) {
Node holder = new Node (newElement, null);
if (tail == null) // is list empty?
{
head = holder;
tail = head;
} else {
Node current = head;
Node previous = null;
while (current != null) {
if (newElement.compareTo(current.getData()) < 0) {
break;
}
previous = current;
current = current.getLink();
}
//make the new node(holder) point to current node
holder.setLink(current);
//updates head to holder to front of list
if (current == head)
{
head = holder;
} else {
previous.setLink(holder);
}
//updates tail to holder to end of list
if (current == null)
{
tail = holder;
}
}
numElements++;
}
/**
* The exists method looks for a String in the linked bag
*
* @param target The String to be found in the linked bag.
* @return a boolean to indicate if target is found in the linked bag.
*/
public boolean exists(E target) {
boolean found = false;
Node cursor = head;
while (cursor != null && !found) {
if (cursor.getData().equals(target)) {
found = true;
} else {
cursor = cursor.getLink();
}
}
return found;
// although not "efficient", this method could contain just 1 line:
// return (countOccurences(target) > 0);
}
/**
* The countOccurrences method looks for a String in the linked bag
*
* @param target The String to be found in the linked bag.
* @return an int with the number of times target is found in the linked
* bag.
*/
public int countOccurrences(E target) {
int numOccurrences = 0;
Node cursor;
for (cursor = head; cursor != null; cursor = cursor.getLink()) {
if (cursor.getData().equals(target)) {
numOccurrences++;
}
}
return numOccurrences;
}
/**
* The remove method looks for a String in the linked bag and removes it
* this version of remove maintains order
*
* @param target The String to be removed from the linked bag.
* @return a boolean to indicate if the target was removed from the bag.
*/
public boolean remove(E target) {
boolean found = false;
Node cursor = head, previous = null;
while (cursor != null && !found) {
if (cursor.getData().equals(target)) {
found = true;
} else {
previous = cursor;
cursor = cursor.getLink();
}
}
if (found && cursor != null) {
if (previous == null) {
head = head.getLink();
} else {
previous.setLink(cursor.getLink());
}
if (tail == cursor) {
tail = previous;
}
numElements--;
}
return found;
}
/**
* the iteratorPrototype method "copies" the linked list and passes the
* copied linked list to a new ListerPrototype2
*
* @return a ListerPrototype2 using a copy of the linked list
*/
public Lister iterator() {
// declare variables
Node headOfListToReturn; // beginning of new "copied" list
Node cursorOfListToCopy; // active node of list to copy
Node lastNodeOfListToReturn; // end of new "copied" list
// establish the copied list
headOfListToReturn = null;
if (head != null) {
// create the head of the new list
headOfListToReturn = new Node(head.getData(), null);
// use lastNodeOfListToReturn as a pointer to the last node in the copied list
lastNodeOfListToReturn = headOfListToReturn;
/* for debugging:
if (head == tail)
System.out.println("T: "+head.getData());
*/
// use currentCursor as the pointer to the existing list
cursorOfListToCopy = head.getLink();
// if we have a node...
while (cursorOfListToCopy != null) {
// create a new node from the end of the new list
lastNodeOfListToReturn.setLink(new Node(cursorOfListToCopy.getData(), null));
// move lastNodeOfListToReturn to the new last node
lastNodeOfListToReturn = lastNodeOfListToReturn.getLink();
/* for debugging:
if (cursorOfListToCopy == tail)
System.out.println("T: "+cursorOfListToCopy.getData());
*/
// move the cursorOfListToCopy to the next node
cursorOfListToCopy = cursorOfListToCopy.getLink();
}
}
return new Lister(headOfListToReturn);
}
}
Node Class:
public class Node {
private E data;
private Node link;
/**
Constructor
@param initialData The data for the new node.
@param initialLink The link for the new node.
*/
public Node( E initialData, Node initialLink)
{
data = initialData;
link = initialLink;
}
/**
The getData method returns the node's data
@return String with value from data field.
*/
public E getData()
{
return data;
}
/**
The getLink method returns the node's pointer to the next node
@return a StringNode from the link field (pointer to next node).
*/
public Node getLink()
{
return link;
}
/**
The setData method stores a value in the data field for the node.
@param initialData The value to store in data.
*/
public void setData(E initialData)
{
data = initialData;
}
/**
The setLink method stores a value in the link field for the node
which points to the next node in the linked list.
@param initialLink The value to store in link.
*/
public void setLink(Node initialLink)
{
link = initialLink;
}
}
Listener Class:
public class Lister {
private Node cursor;
/**
* Constructor establishes cursor as the beginning of the
* linked list
* @param head points to a linked list of StringNodes
*/
public Lister(Node head)
{
/* note: this does not copy the underlying data.
We are assuming that the iteratorPrototype() in StringLinkedBag
copies the "data" */
cursor = head;
}
/**
* hasNext method indicates if the cursor is null or not
* @return boolean that is true is cursor is not null, false otherwise
*/
public boolean hasNext()
{
return cursor != null;
}
/**
* next method gets the String from the current node and advances cursor
* @return String from the "current" node
*/
public E next()
{
E valueToReturn;
if (!hasNext())
throw new NoSuchElementException("The lister is empty");
// get the string from the node
valueToReturn = cursor.getData();
// advance the cursor to the next node
cursor = cursor.getLink();
return valueToReturn;
}
}
Updated!
Explanation / Answer
your code is correct but need to implement the equals method in Soda class : --------->>>>>>>>
public class Soda implements Comparable{
private String sodaName;
private double sodaPrice;
private int sodaCalories;
/**
* Constructor
* @param initalPartName value for sodaName field
* @param initalPrice value for sodaPrice field
* @peram initalCalories value for sodaCalolires field
*/
public Soda(String initalSodaName, double initalPrice, int initalCalories )
{
sodaName = initalSodaName;
sodaPrice = initalPrice;
sodaCalories = initalCalories;
}
/**
* getSodaName method returns value from sodaName field
* @return value from sodaName field
*/
public String getSodaName()
{
return sodaName;
}
/**
* getSodaPrice method returns value in sodaPrize field
* @return value in sodaPrice field
*/
public double getSodaPrice()
{
return sodaPrice;
}
/**
* getSodaCalories method returns value in sodaCalories field
* @return value in sodaCalories field
*/
public int getSodaCalories()
{
return sodaCalories;
}
/**
* setSodaName sets a new value for sodaName field
* @param newSodaName new value for sodaName field
*/
public void setSodaName(String newSodaName)
{
sodaName = newSodaName;
}
/**
* setSodaPrice method sets a new value in sodaPrice field
* @param newSodaPrice new value for sodaPrice field
*/
public void setSodaPrice(double newSodaPrice)
{
sodaPrice = newSodaPrice;
}
/**
* setSodaCalories method sets a new value in sodaCalories field
* @param newSodaPrice new value for sodaCalories field
*/
public void setSodaCalories(int newSodaCalories)
{
sodaCalories = newSodaCalories;
}
public int compareTo(Soda anotherSoda)
throws ClassCastException
{
if (!(anotherSoda instanceof Soda))
throw new ClassCastException("A Soda object expected.");
return 0;
}
public boolean equals(Object obj){
if (!(obj instanceof Soda))
return false;
Soda in = (Soda)obj;
if(in.getSodaName().equals(sodaName) && in.getSodaCalories() == sodaCalories && in.getSodaPrice() == sodaPrice){
return true;
}
return false;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.