Question: Could you code this java code in swift import java.text.NumberFormat; import java.util.Scanner; // A Java program that illustrates the use of: // various data types,
Could you code this java code in swift
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 } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
