Everything works except my removeItem method. I'm out of an idea. I searched many times, still have no clue; I'm just a beginner into java, but honestly, I feel like I'm not moving at all. It has been two days I've been trying. What im looking for is remove a specific Item.
import java.util.Scanner;
public class Run {
public static void runMenu(shoppy cart) {
String ordering = "";
Scanner scan = new Scanner(System.in);
// Loop to run the menu
do {
// print menu
System.out.println("--- Option ----");
System.out.println("1. Adding Product! ");
System.out.println("2. Remove Head item");//first
System.out.println("3. Remove index item");
System.out.println("4. My Receipt");
System.out.println("5. Clear cart");
System.out.println("6. Bye");
// get user choice
System.out.print( "your choice: ");
ordering = scan.nextLine();
// Decide the operation based on choice
switch (ordering) {
//1. Add an item
case "1": {
// Get name and their price
System.out.print("Enter name of the item: ");
String name = scan.nextLine();
System.out.print("Enter price: ");
double price = scan.nextDouble();
scan.nextLine();
cart.addItem(new Item(name, price));
break;
}
//2. Remove first item
case "2": {
cart.removeItem();
break;
}
//3. Remove specific item
case "3": {
// Get name and price
System.out.print("Enter name of the item: ");
String name = scan.nextLine();
System.out.print("Enter price: ");
double price = scan.nextDouble();
scan.nextLine();
cart.removeItem(new Item(name,price));
break;
}
//4. Print Receipt
case "4": {
cart.printReceipt();
break;
}
//5. Clear cart
case "5": {
cart.clear();
break;
}
//6. Quit
case "6": {
System.out.println("Thank you for using this program");
System.exit(0);
break;
}
default:
System.out.println("Invalid choice, please retry!");
break;
}
System.out.println();
} while (ordering != "6");
}
public static void main(String[] args) {
// Test Linked objects
System.out.println("Bag Linked Objects:");
System.out.println();
shoppy cart = new shoppy(new PlastiqArray<Item>());
runMenu(cart);
System.out.println();
// Test Bag Array
System.out.println("Bag Array:");
System.out.println();
shoppy cart1 = new shoppy(new Plasti_Bag<Item>(5));
runMenu(cart1);
}
}
//shoppy (problem is here)
public class shoppy {
private plastiq<Item> bastket;
// cart
shoppy(plastiq<Item> bastket) {
this.bastket = bastket;
}
//Adding an item to the cart
public void addItem(Item item) {
if (this.bastket.add(item) == true) {
System.out.println("Item is added to Bastket");
} else {
System.out.println("Item is not added to Bastket");
}
}
//Remove specific item from cart ***(here is the problem)***
public void removeItem(Item item) {
try {
if (this.bastket.remove(item) == true) {
// cart.remove(item);
System.out.println("Item is removed from cart: " + item.toString());
} else {
System.out.println("Item is not found in cart: " + item.toString());
}
}
catch (Exception e) {
System.out.println(e.getMessage());
}
}
//Remove the first item from the cart
public void removeItem() {
// if cart is empty, catch the exception
try {
// store the item removed and print it
Item item = this.bastket.remove();
System.out.println("Item is removed from cart: " + item.toString());
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
// Total Cost of the cart
public double getTotalCost() {
double total = 0;
// Convert into array
Object[] items = bastket.toArray();
// Loop through and add cost of each item
for (Object item : items) {
total += ((Item) item).getCost();
}
return total;
}
//Print receipt
public void printReceipt() {
// Convert into array
Object[] items = bastket.toArray();
// Loop through and print each item
int num = 0;
for (Object item : items) {
num++;
System.out.println(num + ". " + ((Item) item).toString());
}
// print total cost
System.out.println("--------------------------------------");
System.out.println("Total: $" + getTotalCost());
System.out.println("--------------------------------------");
}
public void clear() {
bastket.clear();
}
}
// Plasti_Bag
public class Plasti_Bag<T> implements plastiq<T> {
public static final int DEFAULT_CAPACITY = 5;
private int numberOfItems;
private T[] items;
public Plasti_Bag(int size) {
items = (T[]) new Object[size];
numberOfItems = 0;
}
public int getCurrentSize() {
return numberOfItems;
}
public boolean isEmpty() {
return numberOfItems == 0;
}
public boolean add(T item) {
if (numberOfItems == items.length) {
return false;
}
items[numberOfItems] = item;
numberOfItems++;
return true;
}
public T remove() {
if (isEmpty()) {
throw new IllegalStateException("Bag is empty");
}
numberOfItems--;
T item = items[numberOfItems];
items[numberOfItems] = null;
return item;
}
public boolean remove(T item) {
if (isEmpty()) {
throw new IllegalStateException("Bag is empty");
}
for (int index = 0; index < numberOfItems; index++) {
if (item.equals(items[index])) {
numberOfItems--;
items[index] = items[numberOfItems];
items[numberOfItems] = null;
return true;
}
}
return false;
}
public void clear() {
while (!isEmpty()) {
remove();
}
}
public int getFrequencyOf(T item) {
int frequency = 0;
for (int index = 0; index < numberOfItems; index++) {
if (items[index].equals(item)) {
frequency++;
}
}
return frequency;
}
@Override
public boolean contain(T item) {
for (int index = 0; index < numberOfItems; index++) {
if (items[index].equals(items)) {
return true;
}
}
return false;
}
@Override
public T[] toArray() {
T[] result = (T[]) new Object[numberOfItems];
for (int index = 0; index < numberOfItems; index++) {
result[index] = items[index];
}
return result;
}
}
//PlastiqArray
public class PlastiqArray<T> implements plastiq<T> {
//internal storage
private class Node<T> {
public T item;
public Node<T> next;
public Node(T item) {
this(item, null);
}
public Node(T item, Node<T> next) {
this.item = item;
this.next = next;
}
}
private int numberOfItems;
private Node<T> first;
public PlastiqArray() {
first = null;
numberOfItems = 0;
}
@Override
public void clear() {
first = null;
numberOfItems = 0;
}
@Override
public int getFrequencyOf(T item) {
int frequency = 0;
Node<T> current = first;
while (current != null) {
if (current.equals(item)) {
frequency++;
}
current = current.next;
}
return frequency;
}
@Override
public boolean contain(T item) {
Node<T> current = first;
while (current != null) {
if (current.equals(item)) {
return true;
}
current = current.next;
}
return false;
}
@Override
public T[] toArray() {
T[] result = (T[]) new Object[numberOfItems];
Node<T> current = first;
for (int index = 0; index < numberOfItems; index++) {
result[index] = (T) current.item;
current = current.next;
}
return result;
}
@Override
public boolean remove(T item) {
if (isEmpty()) {
throw new IllegalStateException("Bag is empty");
}
// find the previous node to the item passed
Node<T> current = first;
Node<T> previous = null;
boolean found = false;
while (current != null) {
if (current.equals(item)) {
found = true;
break;
}
previous = current;
current = current.next;
}
// if item not found
if (!found) {
return false;
} else {
// if previous is null, remove the first element
if (previous == null) {
first = first.next;
} else {
// set the previous node next to current next
previous.next = current.next;
}
// reduce the numitems by 1
numberOfItems--;
return true;
}
}
@Override
public int getCurrentSize() {
return numberOfItems;
}
@Override
public boolean isEmpty() {
return numberOfItems == 0;
}
@Override
public boolean add(T item) {
Node<T> newNode = new Node(item, first);
first = newNode;
numberOfItems++;
return true;
}
@Override
public T remove() {
if (isEmpty()) {
throw new IllegalStateException("Bag is Empty");
}
Node<T> toRemove = first;
first = first.next;
numberOfItems--;
return toRemove.item;
}
}
//Item
public class Item {
private String name;
private double cost;
public Item(String name, double cost) {
this.name = name;
this.cost = cost;
}
public Item(){
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @return the cost
*/
public double getCost() {
return cost;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @param cost the cost to set
*/
public void setCost(double cost) {
this.cost = cost;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Item other = (Item) obj;
return other.cost == this.cost && other.name.equalsIgnoreCase(this.name);
}
@Override
public String toString() {
return this.name + ", $" + this.cost;
}
}
//List
class List {
}
interface plastiq<T> {
public int getCurrentSize();
public boolean isEmpty();
public boolean add(T item);
public T remove();
public void clear();
public int getFrequencyOf(T item);
public boolean contain(T item);
public T[] toArray();
public boolean remove(T item);
}
What Am I missing ?