Your Tasks Complete the classes (1-4) shown in the UML diagram and described bel
ID: 3701549 • Letter: Y
Question
Your Tasks Complete the classes (1-4) shown in the UML diagram and described below. Also complete the testing described below (5) 1. Item Attributes Main i. name-stores the name of the item ii. price-stores the price for a single item ili. quantity-stores the quantity of this ShoppingSimulator item purchased Methods 912 i. Constructor-initializes the Item object ii getPrice-returns the price of a single iii. getName-returns the name of this item given a name, price and quantity item iv getQuantity-returns the quantity of thisITTA CAP: SEC rinal in . addQuantity (int amt)-increase the ioString-return a string with the items inameistring noabie) : vosa item purchased -Ccnstructor) getTotalPrice: double quantity of this item by amt name, quantity and the total price. The total price is the item's price times its quantity. Item 2. Shopping Cart Attributes -name: String -price: double i. itemList-array of the item objects in the shopping cart getPrice): double ii numItems-the total number of unique items in the cart. INITIAL CAP 5- static constant representing the size of the array initially GROWBY-3-static constant that defines how much the size of the array s increased if it becomes full. iv. - Methods i. Constructor initializes array StemsList to be of size INITIAL CAP and initializes numItems as O ii. getTotalPrice-returns the total price of the items in the cart. This should be the sum of il. get I tent ndex-H itenList has an item with the name passed in, return the index of that iv. display-print out the name, total price and quantity of each item in the itemList the price quantity of each item in the cart. item in the array. Otherwise return-1 (Hint: use tostring 0 of item).Explanation / Answer
here is your program : -------------->>>>>>>>>
Item.java : ---------->>>>>>>>
public class Item{
private int quantity;
private String name;
private double price;
public Item(String n,double p,int q){
quantity = q;
name = n;
price = p;
}
public int getQuantity(){
return quantity;
}
public String getName(){
return name;
}
public double getPrice(){
return price;
}
public void addQuantity(int quan){
quantity = quantity + quan;
}
public String toString(){
return "Name : "+name+" Price : "+price+" Quantity : "+quantity;
}
}
ShoppingCart.java : ---------->>>>>>>>>
public class ShoppingCart{
private Item[] itemList;
private int numItems;
public static final int INITIAL_CAP = 5;
public static final int GROW_BY = 3;
public ShoppingCart(){
itemList = new Item[INITIAL_CAP];
numItems = 0;
}
public double getTotalPrice(){
double pr = 0.0;
for(int i = 0;i<numItems;i++){
pr = pr + (itemList[i].getPrice()*itemList[i].getQuantity());
}
return pr;
}
public int getItemIndex(String name){
for(int i = 0;i<numItems;i++){
if(itemList[i].getName().equals(name)){
return i;
}
}
return -1;
}
public void display(){
for(int i = 0;i<numItems;i++){
System.out.println(itemList[i]);
}
}
public void increaseSize(){
int t = itemList.length;
t = t + ShoppingCart.GROW_BY;
Item[] temp = new Item[t];
for(int i = 0;i<numItems;i++){
temp[i] = itemList[i];
}
itemList = temp;
}
public void addToCart(Item new_item){
int n = getItemIndex(new_item.getName());
if(n == -1){
if(numItems >= itemList.length){
increaseSize();
}
itemList[numItems++] = new_item;
}else{
itemList[n].addQuantity(new_item.getQuantity());
}
}
}
ShoppingSimulator.java : ------------>>>>>>>>>
import java.util.Scanner;
public class ShoppingSimulator{
private Scanner sc;
private ShoppingCart cart;
public ShoppingSimulator(){
sc = new Scanner(System.in);
cart = new ShoppingCart();
}
public void go(){
boolean toadd = true;
String name;
int quan;
double price;
while(toadd){
System.out.println("Enter The Item Name : ");
name = sc.next();
while(true){
System.out.println("Enter the Item Price : ");
price = sc.nextDouble();
if(price >= 1 && price <= 100){
break;
}
System.out.println("Enter Price Between 1-100");
}
while(true){
System.out.println("Enter The quantity : ");
quan = sc.nextInt();
if(quan >= 1 && quan <= 10){
break;
}
System.out.println("Enter quantity Between 1-10");
}
cart.addToCart(new Item(name,price,quan));
System.out.println("Want to add More Item (y/n) ? ");
if(sc.next().equals("n")){
break;
}
}
cart.display();
System.out.println("Please Pay ...... "+cart.getTotalPrice()+" rs.");
}
}
Main.java : --------->>>>>>>
public class Main{
public static void main(String[] args) {
ShoppingSimulator sm = new ShoppingSimulator();
sm.go();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.