in java
Just need help with delete by the item that is all
my delete by item somehow not working the rest of the code is fine
plz help
in java
Just need help with delete by the item that is all
my delete by item somehow not working the rest of the code is fine
plz help
//shoppin class
public class ShoppingCart {
private BagInterface<Item> cart;
// cart
ShoppingCart(BagInterface<Item> cart)
{
this.cart = cart;
}
//Adding an item to the cart
public void addItem(Item item) {
if
(this.cart.add(item) == true) {
System.out.println("Item is added to cart");
} else {
System.out.println("Item is not added to cart");
}
}
//Remove specific item from cart
public void removeItem(Item item) {
try {
if (this.cart.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.cart.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 =
cart.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 =
cart.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() {
cart.clear();
}
}
//////////////////////////////////////Bag array class
public class BagArray<T> implements BagInterface<T>
{
public static final int DEFAULT_CAPACITY =
5;
private int numberOfItems;
private T[] items;
public BagArray(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;
}
}
///////////////////////////////////////////////////Item
class
public class Item {
private String name;
private double cost;
/**
* @param name
* @param 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;
}
}
////////////////Main, interface
import java.util.Scanner;
class bagg {
}
interface BagInterface<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);
}
in java Just need help with delete by the item that is all my delete by item somehow not working the rest of the code is
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am