In: Computer Science
4. Create a program that takes in the following CMD arguments: Item name, quantity, cost per item, and total cost. Your task then is to take this given information to print a simple receipt as indicated below (Don’t worry about the calculations for now.)
1
Sample run 1:
Java lab01_task04 Banana 6 3.25 19.50
Output:
Welcome to FCI Fresh Goods
Sales
======================
You have purchased:
6 X Banana @ N$ 3.25
including 15% VAT
======================
Total Cost: N$ 19.50
======================
Following is the java code for the above problem.
It takes four command line arguments as stated in the question.
Note that in java, by default a String is created named args[] which contains all the command line arguments specified by the user.
So,
args[0] ---> contains ItemName
args[1] ----> Quantity
args[2] ----> Cost per item
args[3] ----> Total cost
public class Main
{
public static void main(String[] args) {
String item_name = args[0];
String quantity = args[1];
String cost_per_item = args[2];
String total_cost = args[3];
System.out.println("Welcome to FCI Fresh Goods");
System.out.println("Sales");
System.out.println("======================");
System.out.println("You have purchased:");
System.out.println(quantity+" X "+ item_name + " @ " + " N$ "+cost_per_item);
System.out.println("including 15% VAT");
System.out.println("======================");
System.out.println("Total Cost: N$ "+ total_cost);
System.out.println("======================");
}
}
Note: You should name the file (in which you paste the above code) to be Main.java because the public class name is also Main. Not doing it may give you errors. Please let me know if you have any questions/doubts/need more explanation. Thanks
Output:
Code Screenshot: