In: Computer Science
JAVA
(1) Create two files to submit:
Build the ItemToPurchase class with the following specifications:
(2) In main(), prompt the user for two items and create two
objects of the ItemToPurchase class. Before prompting for the
second item, call scnr.nextLine(); to allow the
user to input a new string. (2 pts)
Ex:
Item 1 Enter the item name: You entered: Chocolate_Chips Enter the item price: You entered: 3 Enter the item quantity: You entered: 1 Item 2 Enter the item name: You entered: Bottled_Water Enter the item price: You entered: 1 Enter the item quantity: You entered:10
(3) Add the costs of the two items together and output the total
cost. (2 pts)
Ex:
TOTAL COST Chocolate_Chips 1 @ $3 = $3 Bottled_Water 10 @ $1 = $10 Total: $13
We will use the Scanner class for taking the input from the user.
We will first write code for the first part, that is, for the ItemToPurchase.java file.
Please find all the explanation for the code within the code itself as comments.
import java.util.*;
public class ItemsToPurchase {
// as specified in the problem statement, these are the private fields of the class
private String itemName;
private int itemPrice;
private int itemQuantity;
// default constructor
public ItemsToPurchase () {
this.itemName = "none";
this.itemPrice = 0;
this.itemQuantity = 0;
}
// as specified in the problem statement, these are the
// public member methods (mutators and accessors)
// this function sets the name of the item
public void setName (String itemName) {
this.itemName = itemName;
}
// this function returns the name of the item
public String getName () {
return this.itemName;
}
// this function sets the price of the item
public void setPrice (int itemPrice) {
this.itemPrice = itemPrice;
}
// this function returns the price of the item
public int getPrice () {
return this.itemPrice;
}
// this function sets the quantity of the item
public void setQuantity (int itemQuantity) {
this.itemQuantity = itemQuantity;
}
// this function returns the quantity of the item
public int getQuantity () {
return this.itemQuantity;
}
}
Now, for the second part, lets look at the ShoppingCartPrinter.java file.
In this file, we will prompt the user for the data of the ItemsToPurchase object and print what has been entered by the user like shown in the problem statement.
The code snippets are added for clarity. Please find the full code of ShoppingCartPrinter.java file at the end.
import java.util.*;
public class ShoppingCartPrinter {
public static void main (String [] args) {
// FOR ITEM 1
System.out.println ("Item 1");
ItemsToPurchase i1 = new ItemsToPurchase ();
// create an object of class Scanner to take the input from the user
// the argument of the Scanner's constructor would be "System.in"
// which specifies that from where the input has to be read.
// in our case, we are telling java that system input will be read from Standard input stream,
// which is associated with keyboard
Scanner scnr = new Scanner (System.in);
// prompt the user for name
System.out.print ("\nEnter the item name: ");
// we will take the input using next() function of Scanner class
String name = scnr.next();
i1.setName (name);
System.out.println ("You entered: " + i1.getName());
// prompt the user for price
System.out.print ("\nEnter the item price: ");
// we will take the input using next() function of Scanner class
int price = scnr.nextInt ();
i1.setPrice (price);
System.out.println ("You entered: " + i1.getPrice());
// prompt the user for quantity
System.out.print ("\nEnter the item quantity: ");
// we will take the input using next() function of Scanner class
int quantity = scnr.nextInt ();
i1.setQuantity (quantity);
System.out.println ("You entered: " + i1.getQuantity());
// according to the problem statement, we need to call nextLine () once after taking the
// first item's input to allow the user to input a new string
scnr.nextLine ();
// FOR ITEM 2
ItemsToPurchase i2 = new ItemsToPurchase ();
// prompt the user for name
System.out.println ("\n\nItem 2");
System.out.print ("Enter the item name: ");
// we will take the input using next() function of Scanner class
name = scnr.next();
i1.setName (name);
System.out.println ("You entered: " + i1.getName());
// prompt the user for price
System.out.print ("\nEnter the item price: ");
// we will take the input using next() function of Scanner class
price = scnr.nextInt ();
i1.setPrice (price);
System.out.println ("You entered: " + i1.getPrice());
// prompt the user for quantity
System.out.print ("\nEnter the item quantity: ");
// we will take the input using next() function of Scanner class
quantity = scnr.nextInt ();
i1.setQuantity (quantity);
System.out.println ("You entered: " + i1.getQuantity());
}
}
Now, the third part, we need to add the total cost so formed.
We can add the below code for that in our main function below the above code.
// printing the statements about the details of the shopping as mentioned in the problem statement
System.out.println ("\n\nTOTAL COST");
System.out.println (i1.getName () + " " + i1.getQuantity () + " @ $" + i1.getPrice () + " = " + "$" + (i1.getPrice () * i1.getQuantity ()));
System.out.println (i2.getName () + " " + i2.getQuantity () + " @ $" + i2.getPrice () + " = " + "$" + (i2.getPrice () * i2.getQuantity ()));
int item1Cost = (i1.getPrice () * i1.getQuantity ());
int item2Cost = (i2.getPrice () * i2.getQuantity ());
int totalCost = item1Cost + item2Cost;
System.out.println ("Total: $" + totalCost);
The full code is:
import java.util.*;
public class ShoppingCartPrinter {
public static void main (String [] args) {
// FOR ITEM 1
System.out.println ("Item 1");
ItemsToPurchase i1 = new ItemsToPurchase ();
// create an object of class Scanner to take the input from the user
// the argument of the Scanner's constructor would be "System.in"
// which specifies that from where the input has to be read.
// in our case, we are telling java that system input will be read from Standard input stream,
// which is associated with keyboard
Scanner scnr = new Scanner (System.in);
// prompt the user for name
System.out.print ("\nEnter the item name: ");
// we will take the input using next() function of Scanner class
String name = scnr.next();
i1.setName (name);
System.out.println ("You entered: " + i1.getName());
// prompt the user for price
System.out.print ("\nEnter the item price: ");
// we will take the input using next() function of Scanner class
int price = scnr.nextInt ();
i1.setPrice (price);
System.out.println ("You entered: " + i1.getPrice());
// prompt the user for quantity
System.out.print ("\nEnter the item quantity: ");
// we will take the input using next() function of Scanner class
int quantity = scnr.nextInt ();
i1.setQuantity (quantity);
System.out.println ("You entered: " + i1.getQuantity());
// according to the problem statement, we need to call nextLine () once after taking the
// first item's input to allow the user to input a new string
scnr.nextLine ();
ItemsToPurchase i2 = new ItemsToPurchase ();
// prompt the user for name
System.out.println ("\n\nItem 2");
System.out.print ("Enter the item name: ");
// we will take the input using next() function of Scanner class
name = scnr.next();
i2.setName (name);
System.out.println ("You entered: " + i2.getName());
// prompt the user for price
System.out.print ("\nEnter the item price: ");
// we will take the input using next() function of Scanner class
price = scnr.nextInt ();
i2.setPrice (price);
System.out.println ("You entered: " + i2.getPrice());
// prompt the user for quantity
System.out.print ("\nEnter the item quantity: ");
// we will take the input using next() function of Scanner class
quantity = scnr.nextInt ();
i2.setQuantity (quantity);
System.out.println ("You entered: " + i2.getQuantity());
// CALCULATING THE TOTAL COST
// printing the statements about the details of the shopping as mentioned in the problem statement
System.out.println ("\n\nTOTAL COST");
System.out.println (i1.getName () + " " + i1.getQuantity () + " @ $" + i1.getPrice () + " = " + "$" + (i1.getPrice () * i1.getQuantity ()));
System.out.println (i2.getName () + " " + i2.getQuantity () + " @ $" + i2.getPrice () + " = " + "$" + (i2.getPrice () * i2.getQuantity ()));
int item1Cost = (i1.getPrice () * i1.getQuantity ());
int item2Cost = (i2.getPrice () * i2.getQuantity ());
int totalCost = item1Cost + item2Cost;
System.out.println ("Total: $" + totalCost);
}
}
Please refer to the screenshots of the code for understanding the indentation of the code.
ItemsToPurchase.java file
ShoppingCartPrinter.java file
For the given input, the output of the code will be: