Question: Java programming language I have been working on a project and I am having troubles with creating a method that adds up all prices for

Java programming language

I have been working on a project and I am having troubles with creating a method that adds up all prices for every item in the shopping cart in its initial and final status. I also would like to display the total price at the end of the initial and final status.

This is my code, each class has its own file. What do you suggest I do to get the total price and display it?

public class Item { private String title; private String description; private double price; public Item(String title, String description, double price) { this.title = title; this.description = description; this.price = price; } public String getTitle() { return title; } public String getDescription() { return description; } public double getPrice() { return price; } public void setTitle(String title) { this.title = title; } public void setDescription(String description) { this.description = description; } public void setPrice(double price) { this.price = price; }

@Override public String toString() { return " Tile = " + title + " Description = " + description + " Price = $" + price; } }

public class Book extends Item { private int pageCount; public Book(String title, String description, double price, int pageCount) { super(title, description, price); this.pageCount = pageCount; } public int getPageCount() { return pageCount; } public void setPageCount(int pageCount) { this.pageCount = pageCount; }

@Override public String toString() { return " Book: " + super.toString() + " Page count = " + pageCount; } }

public class CD extends Item{ private int trackCount; public CD(String title, String description, double price, int trackCount) { super(title, description, price); this.trackCount = trackCount; } public int getTrackCount() { return trackCount; }

public void setTrackCount(int trackCount) { this.trackCount = trackCount; } @Override public String toString() { return " CD: " + super.toString() + " Track count = " + trackCount; } }

public class Movie extends Item{ private int length; public Movie(String title, String description, double price, int length) { super(title, description, price); this.length = length; } public int getLength() { return length; } public void setLength(int length) { this.length = length; } @Override public String toString() { return " Movie: " + super.toString() + " Length in minutes = " + length; } }

public class Fruit extends Item{ private double weight; public Fruit(String title, String description, double price, double weight) { super(title, description, price); this.weight = weight; } public double getWeight() { return weight; } public void setWeight(int weight) { this.weight = weight; } @Override public String toString() { return " Fruit: " + super.toString() + " Weight in libras = " + weight; } }

public class Vegetable extends Item{ private double weight; public Vegetable(String title, String description, double price, double weight) { super(title, description, price); this.weight = weight; } public double getWeight() { return weight; } public void setWeight(int weight) { this.weight = weight; } @Override public String toString() { return " Vegetable: " + super.toString() + " Weight in libras = " + weight; } }

public class Customer { private int ID; private String firstName; private String lastName; private ShoppingCart shoppingCart; public Customer(int ID, String firstName, String lastName) { this.ID = ID; this.firstName = firstName; this.lastName = lastName; this.shoppingCart = new ShoppingCart(); } public int getID() { return ID; }

public String getFirstName() { return firstName; } public String getLastName() { return lastName; } public ShoppingCart getShoppingCart() { return shoppingCart; } public void setID(int ID) { this.ID = ID; }

public void setFirstName(String firstName) { this.firstName = firstName; } public void setLastName(String lastName) { this.lastName = lastName; } public void setShoppingCart(ShoppingCart shoppingCart) { this.shoppingCart = shoppingCart; } @Override public String toString() { return " ID = " + ID + " First Name = " + firstName + " Last Name = " + lastName; } }

public class Cashier { private int ID; private String firstName; private String lastName; private int checkstandNumber; public Cashier(int ID, String firstName, String lastName, int checkstandNumber) { this.ID = ID; this.firstName = firstName; this.lastName = lastName; this.checkstandNumber = checkstandNumber; } public int getID() { return ID; }

public String getFirstName() { return firstName; } public String getLastName() { return lastName; } public int getCheckstandNumber() { return checkstandNumber; } public void setID(int ID) { this.ID = ID; } public void setFirstName(String firstName) { this.firstName = firstName; } public void setLastName(String lastName) { this.lastName = lastName; } public void setCheckstandNumber(int checkstandNumber) { this.checkstandNumber = checkstandNumber; } @Override public String toString() { return " ID = " + ID + " First Name = " + firstName + " Last Name = " + lastName + " Checkstand Number = " + checkstandNumber; } }

import java.util.ArrayList; import java.util.List;

public class ShoppingCart { private final List cashiers = new ArrayList<>(); private final List items = new ArrayList<>(); private int MAX_ITEMS = 10; public void addItem(Item i) { if(items.size() >= MAX_ITEMS) { System.out.println("Cart is full!"); } items.add(i); } public void removeItem(int index) { items.remove(index - 1); } public void printItems() { System.out.println(" *** Shopping Cart ***"); items.forEach((item) -> { System.out.println(item); }); System.out.println(" Total amount of items = " + items.size()); } }

public class Main { public static void main(String[] args) { Customer customer = new Customer(123, "Jane", "Doe"); Cashier cashier = new Cashier(789, "John", "Smith", 7); Item item1 = new Book("The Man Who Mistook His Wife for a Hat", "Book", 10.87, 256); Item item2 = new Movie("The Incredibles", "Movie", 14.53, 115); Item item3 = new Vegetable("Chayote", "Vegetable", 18.98, 3.0); Item item4 = new Movie("Finding Nemo", "Movie", 19.99, 100); Item item5 = new Book("Why Zebras Don't Get Ulcers", "Book", 12.75, 560); Item item6 = new Fruit("Honeycrisp Apple", "Fruit", 2.99, 1.0); Item item7 = new CD("Hot Fuss", "CD", 5.99, 11); Item item8 = new Movie("Beetlejuice", "Movie", 4.99, 92);

customer.getShoppingCart().addItem(item1); customer.getShoppingCart().addItem(item2); customer.getShoppingCart().addItem(item3); customer.getShoppingCart().addItem(item4); customer.getShoppingCart().addItem(item5); customer.getShoppingCart().addItem(item6); customer.getShoppingCart().addItem(item7); customer.getShoppingCart().addItem(item8);

System.out.println("Welcome to Target!"); System.out.println(" Your cashier for today is: " + cashier); System.out.println(" Initial status of shopping cart"); System.out.println(" Customer: " + customer); customer.getShoppingCart().printItems();

customer.getShoppingCart().removeItem(1); customer.getShoppingCart().removeItem(3); System.out.println(" Final status of shopping cart"); System.out.println(" Customer: " + customer); customer.getShoppingCart().printItems(); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!