Answered step by step
Verified Expert Solution
Link Copied!

Question

...
1 Approved Answer

Can you fix my driver class and checkbudget method. The checkbudget method should check to see if the given budget is large enough to pay

Can you fix my driver class and checkbudget method. The checkbudget method should check to see if the given budget is large enough to pay
for everything in the cart. If not, remove an Item from the shopping cart, one at a time Here is the code import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class ShoppingCart{
private List cart; // array based implementation
public ShoppingCart(){
cart = new ArrayList<>(); // initializing an array list for cart
}
public void addItem(Item item){
cart.add(item); // add an item to the cart
}
public void addMultipleItems(Item item, int quantity){
for (int i =0; i < quantity; i++){
cart.add(item); // add nultiple items to the cart
}
}
public boolean removeItem(Item item){
return cart.remove(item); // remove a specified item
}
//removes random unspecified item
public void removeUnspecifiedItem(){
if (!cart.isEmpty())
{
Random random = new Random();
int randomIndex = random.nextInt(cart.size());
cart.remove(randomIndex);
}
}
public void checkout(){
double totalCost =0.0;
System.out.println("Items in the cart:");
for (Item item : cart){
System.out.println(item);
totalCost += item.getPrice()/100.0; // Convert price to dollars and add to total
}
System.out.println("Total cost: $"+ totalCost);
}
public boolean checkBudget(double budget){
double totalCost = calculateTotalCost();
if (totalCost <= budget){
System.out.println("Budget is sufficient.");
return true;
}
else {
while (totalCost > budget){
removeUnspecifiedItem();
}
return true;
}
}
private double calculateTotalCost(){
double totalCost =0.0;
for (Item item : cart){
totalCost += item.getPrice()/100.0; // Convert price to dollars and add to total
}
return totalCost;
}
}/**
* Item.java - implementation of an Item to be placed in ShoppingCart
*/
public class Item
{
private String name;
private int price; //in cents
//Constructor
public Item(String n, int p)
{
name = n;
price = p;
}
public boolean equals(Object other)
{
if ((this.getClass()!= other.getClass())|| other == null)
return false;
Item otherItem =(Item) other;
return this.name.equals(otherItem.name) && this.price == otherItem.price;
}
//displays name of item and price in properly formatted manner
public String toString()
{
return name +", price: $"+ price/100+"."+ price%100;
}
//Getter methods
public int getPrice()
{
return price;
}
public String getName()
{
return name;
}
}public class ShoppingCartDriver {
public static void main(String[] args){
// Create some Item instances
Item item1= new Item("Item 1",500); // $5.00
Item item2= new Item("Item 2",300); // $3.00
Item item3= new Item("Item 3",200); // $2.00
Item item4= new Item("Item 4",400); // $4.00
// Create a shopping cart
ShoppingCart cart = new ShoppingCart();
// Add items to the shopping cart
cart.addItem(item1);
cart.addMultipleItems(item2,2);
cart.addItem(item3);
cart.addItem(item4);
//remove items
cart.removeItem(item4);
cart.removeUnspecifiedItem();
// Test the checkBudget method
double budget =600; // $2.00 budget
boolean isBudgetSufficient = cart.checkBudget(budget);
if (isBudgetSufficient){
System.out.println("Proceed with the purchase.");
} else {
System.out.println("Adjust your cart items to fit the budget.");
}
// Simulate the checkout process
System.out.println("Items in the cart:");
cart.checkout();
}
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started