(1) Create two files to submit: ItemToPurchase.java - Class definitionShoppingCa
ID: 3801175 • Letter: #
Question
(1) Create two files to submit:
ItemToPurchase.java - Class definitionShoppingCartPrinter.java - Contains main() method
Build the ItemToPurchase class with the following specifications:
Private fieldsString itemName - Initialized in default constructor to "none"int itemPrice - Initialized in default constructor to 0int itemQuantity - Initialized in default constructor to Default constructorPublic member methods (mutators & accessors)setName() & getName() setPrice() & getPrice() setQuantity() & getQuantity()
(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.
Example
Item 1 Enter the item name: Chocolate Chips Enter the item price: 3 Enter the item quantity: 1 Item 2 Enter the item name: Bottled Water Enter the item price: 1 Enter the item quantity: 10
(3) Add the costs of the two items together and output the total cost.
Ex:
TOTAL COST Chocolate Chips 1 @ $3 = $3 Bottled Water 10 @ $1 = $10 Total: $13
Explanation / Answer
ItemToPurchase.java
public class ItemToPurchase
{
private String itemName;
private int itemQuantity;
private int itemPrice;
public ItemToPurchase() //default constructor
{
itemName = "none";
itemQuantity = 0;
itemPrice = 0;
}
//mutators and accessors
void setName(String itemName)
{
this.itemName = itemName;
}
String getName()
{
return itemName;
}
void setPrice(int itemPrice)
{
this.itemPrice = itemPrice;
}
int getPrice()
{
return itemPrice;
}
void setQuantity(int itemQuantity)
{
this.itemQuantity = itemQuantity;
}
int getQuantity()
{
return itemQuantity;
}
}
ShoppingCartPrinter.java
import java.util.*;
public class ShoppingCartPrinter
{
public static void main(String []args)
{
String name;
int quantity,price;
Scanner scnr = new Scanner(System.in);
System.out.println("Enter item name,quantity and price");
name = scnr.nextLine();
quantity = scnr.nextInt();
price = scnr.nextInt();
ItemToPurchase item1 = new ItemToPurchase(); //default constructor
item1.setName(name); //set values
item1.setQuantity(quantity);
item1.setPrice(price);
scnr.nextLine();
System.out.println("Enter item name,quantity and price");
name = scnr.nextLine();
quantity = scnr.nextInt();
price = scnr.nextInt();
ItemToPurchase item2 = new ItemToPurchase();
item2.setName(name);
item2.setQuantity(quantity);
item2.setPrice(price);
System.out.println("Total Cost");
//get values
System.out.println(item1.getName() +item1.getQuantity() +"@ $" +item1.getPrice()+" : "+ item1.getQuantity()*item1.getPrice());
System.out.println(item2.getName() +item2.getQuantity() +"@ $" +item2.getPrice()+" : "+ item2.getQuantity()*item2.getPrice());
System.out.println("Total Cost : $"+(item1.getQuantity()*item1.getPrice() + item2.getQuantity()*item2.getPrice()));
}
}
output:
Enter item name,quantity and price
Chocolate Chips
1
3
Bottled Water
10
1
Total Cost
Chocolate Chips 1 @ $3 = $3
Bottled Water 10 @ $1 = $10
Total: $13
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.