Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Could you please convert the java code to swift really urgent import java.text.NumberFormat; import java.util.Scanner; // A Java program that illustrates the use of: //

Could you please convert the java code to swift really urgent

import java.text.NumberFormat; import java.util.Scanner;

// A Java program that illustrates the use of: // various data types, control structures, I/O (scanner), import, arrays, number formatting, // use of multiple classes, etc.

public class GoShopping2 {

public static void main(String[] args) { Scanner sc; String str; int qty; String name; double pr; ShoppingCart myCart = new ShoppingCart(); // get a new cart object NumberFormat fmt = NumberFormat.getCurrencyInstance(); // this shows how to format currency; see below

do { System.out.println("Enter the name, price, and quantity of the item"); sc = new Scanner(System.in); // get a scanner object to scan input from the standard input name = sc.next(); // get the name pr = Double.parseDouble(sc.next()); // get the price (remember to convert) qty = Integer.parseInt(sc.next()); // get the qty (remember to convert) myCart.addToCart(name, pr, qty); System.out.println(myCart); System.out.println("Do you wish to continue (y/n)?"); str = sc.next(); } while(str.equalsIgnoreCase("y")); sc.close(); System.out.println("Please pay " + fmt.format(myCart.getTotalPrice())); // display in the proper currency } }

import java.text.NumberFormat;

public class Item { private String name; private double price; private int quantity; // ------------------------------------------------------- // Create a new item with the given attributes. // ------------------------------------------------------- public Item (String itemName, double itemPrice, int numPurchased) { name = itemName; price = itemPrice; quantity = numPurchased; } // ------------------------------------------------------- // Return a string with the information about the item // ------------------------------------------------------- public String toString () { NumberFormat fmt = NumberFormat.getCurrencyInstance(); return (name + "\t" + fmt.format(price) + "\t" + quantity + "\t" + fmt.format(price * quantity)); } // ------------------------------------------------- // Returns the unit price of the item // ------------------------------------------------- public double getPrice() { return price; } // ------------------------------------------------- // Returns the name of the item // ------------------------------------------------- public String getName() { return name; } // ------------------------------------------------- // Returns the quantity of the item // ------------------------------------------------- public int getQuantity() { return quantity; } }

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;

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

Database And Expert Systems Applications 33rd International Conference Dexa 2022 Vienna Austria August 22 24 2022 Proceedings Part 1 Lncs 13426

Authors: Christine Strauss ,Alfredo Cuzzocrea ,Gabriele Kotsis ,A Min Tjoa ,Ismail Khalil

1st Edition

3031124227, 978-3031124228

More Books

Students also viewed these Databases questions

Question

LO3 Name the seven categories of HR functions.

Answered: 1 week ago