Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Can you rewrite this Java code in the Swift programming language? 2 //ShoppingCart.java 4 //Represents a shopping cart as an array of items 6 7
Can you rewrite this Java code in the Swift programming language?
2 //ShoppingCart.java 4 //Represents a shopping cart as an array of items 6 7 import java.text.NumberFormat; 8 9 public class ShoppingCart 10 //total number of items in the cart private int itemCount; private double totalPrice / total price of items in the cart private int capacity private Item[] cart; // current cart capacity // the actual array of items to store things in the cart 13 // Creates an empty shopping cart with a capacity of 3 items. public ShoppingCart() capacity 3; tota!Price 0.0 cart new Item[capacity]; 20 26 II Adds an item to the shopping cart public void addToCart (String itemName, double price, int quantity) 28 29 cart[itemCount++]new Item(itemName, price, quantity); tota!Price += price quantity; if(itemCount-= capacity) // if full, increase the size of the cart increaseSize(); 36 II Returns the contents of the cart together with 37 II summary information 39 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 fmtNumberFormat.getCurrencyInstance); String contents"nShopping Cartln"; contents"nItem tPriceltQty tTotal "; for (int i : 0; 1 itemCount; 1++) 42 45 46 47 contents += cart [i].toString() + " " contents "nTotal Price: "+fmt.format (totalPrice); contents += " "; return contents: 49Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started