Java Programming: \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ShoppingCartDriver.java\\\\\\\
ID: 3691023 • Letter: J
Question
Java Programming:
\\\\\\\\ShoppingCartDriver.java\\\\\\\\\\\\
public class ShoppingCartDriver {
public static void main(String[] args) {
//Create a shopping cart
ShoppingCart cart = new ShoppingCart();
//create different items
Item item1 = new Item("butter", 4, 2);
Item item2 = new Item("milk", 5, 3);
Item item3 = new Item("eggs", 10, 3);
Item item4 = new Item("cheese", 20, 2);
//add items to the cart
cart.addItem(item1);
cart.addItem(item2);
cart.addItem(item3);
cart.addItem(item4);
//Display content of the cart
System.out.println(cart);
//Delete item4 from the cart
cart.deleteItem(item4);
//Display content of the cart
System.out.println(cart);
//Modify item1's name, when you this method
//item1's name will be set to UNKNOW
cart.modifyItem(item1);
//Display content of the cart
System.out.println(cart);
}
}
\\\\\\\\\\Output\\\\\\\\\\\
Explanation / Answer
// Item.java
import java.text.NumberFormat ;
public class Item {
private String name ;
private double price ;
private int quantity ;
// default constructor
public Item( String productName ,double productPrice ,int numPurchased ) {
name= productName ;
price= productPrice ;
quantity= numPurchased ;
}
// Quantity of the product
public int getQuantity ( ) {
return quantity ;
}
// Rate of the product
public double getPrice ( ) {
return price ;
}
// Name of the product
public String getName ( ) {
return name ;
}
// Data about the product
public String toString () {
NumberFormat format =NumberFormat.getCurrencyInstance ( ) ;
return ( name +" " +format.format ( price ) +" " +quantity +" " +format.format ( price * quantity ) ) ;
}
}
// ShoppingCart.java
import java.util.Scanner ;
import java.text.NumberFormat ;
import java.util.ArrayList ;
public class ShoppingCart
{
public static void main( String args[] ) {
// declare an empty ArrayList variable
ArrayList< Item > Shop =new ArrayList< Item > ( ) ;
Item product ;
String productName ;
double productPrice ;
int quantity ;
double total =0.0 ;
double totalSum =0.0 ;
Scanner input =new Scanner ( System.in ) ;
String continue =" yes " ;
do {
System.out.print( " Type d name of d product : " ) ;
productName =input.nextLine ( ) ;
System.out.print( " Type d unit price : " ) ;
productPrice = input.nextDouble ( ) ;
System.out.print( " type d quantity : " ) ;
quantity= input.nextInt ( ) ;
// develop a new product and add it to the Shop
product =new Item( productName ,productPrice ,quantity ) ;
Shop.add ( product ) ;
// display the result of the Shop object using println
for( int i =0 ;i< Shop.size ( ) ;i++ ) {
Item prdct = Shop.get ( i ) ;
System.out.println ( prdct ) ;
}
// display out the results
System.out.print( " Keep shopping ( yes / no ) ? " ) ;
input.nextLine ( ) ;
continue = input.nextLine ( ) ;
}
while( continue.equals ( " yes " ) ) ;
for( int i =0 ;i <Shop.size ( ) ;i++ ) {
Item prdct = Shop.get ( i ) ;
System.out.println ( prdct ) ;
total = prdct.getQuantity ( ) *prdct.getPrice ( ) ;
totalSum +=total ;
}
NumberFormat format =NumberFormat.getCurrencyInstance ( ) ;
System.out.println ( " The total rate is : " +format.format ( totalSum ) ) ;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.