Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Can you please rewrite this Java code in the Swift programming language? 2 //ShoppingCart.java 4 //Represents a shopping cart as an array of items 6

Can you please rewrite this Java code in the Swift programming language?

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

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: 49

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored 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

Recommended Textbook for

Refactoring Databases Evolutionary Database Design

Authors: Scott Ambler, Pramod Sadalage

1st Edition

0321774515, 978-0321774514

More Books

Students also viewed these Databases questions

Question

=+1.5. 1 The Cantor set C can be defined as the closure of A3(1).

Answered: 1 week ago

Question

1-4 How will MIS help my career?

Answered: 1 week ago