Question: Convert the following to swift code import java.text.NumberFormat; public class ShoppingCart { private int itemCount; // total number of items in the cart private double

Convert the following to swift code

import java.text.NumberFormat;

public class ShoppingCart

{

private int itemCount; // total number of items in the cart

private double totalPrice; // total price of items in the cart

private int capacity; // current cart capacity

private Item[] cart; // the actual array of items to store things in the cart

// -----------------------------------------------------------

// Creates an empty shopping cart with a capacity of 3 items.

// -----------------------------------------------------------

public ShoppingCart()

{

capacity = 3;

itemCount = 0;

totalPrice = 0.0;

cart = new Item[capacity];

}

// -------------------------------------------------------

// Adds an item to the shopping cart.

// -------------------------------------------------------

public void addToCart(String itemName, double price, int quantity)

{

cart[itemCount++] = new Item(itemName, price, quantity);

totalPrice += price * quantity;

if(itemCount == capacity) { // if full, increase the size of the cart

increaseSize();

}

}

// -------------------------------------------------------

// Returns the contents of the cart together with

// summary information.

// -------------------------------------------------------

public String toString() // this method is called when an object needs to be "printed"

// (when System.out.println() is called, this overriden method is called)

{

NumberFormat fmt = NumberFormat.getCurrencyInstance();

String contents = " Shopping Cart ";

contents += " Item\tPrice\tQty\tTotal ";

for (int i = 0; i < itemCount; i++) {

contents += cart[i].toString() + " ";

}

contents += " Total Price: " + fmt.format(totalPrice);

contents += " ";

return contents;

}

// ------------------------------------------------------------

// Increases the capacity of the shopping cart by doubling it.

// ------------------------------------------------------------

private void increaseSize()

{

Item[] tempItem = new Item[capacity];

capacity *= 2; // double the size

for (int i = 0; i< itemCount; i++)

{

tempItem[i] = cart[i];

}

cart = new Item[capacity];

for (int i = 0; i < itemCount; i++)

{

cart[i] = tempItem[i];

}

// The above can also be accomplished by using:

// System.arraycopy(...)

}

/**

* @return Returns the totalPrice.

*/

public double getTotalPrice() {

return totalPrice;

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!