Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java programming language I have been working on a project and I am having troub

ID: 3918626 • Letter: J

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 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<Cashier> cashiers = new ArrayList<>();
private final List<Item> 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();
}
}

public class Item t 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) t this.title title public void setDescription(String description) this.description description; public void setPrice(double price) this.price -price; @Override public String toString return InTiletitle "nDescription"description + nPrice-$" price;

Explanation / Answer

Hi,

I have implemented one method in ShoppingCart class named as getTotalPrice() which will print total price of the cart items. That method I called in the main method after the printItems() method. I have used DecimalFormat class for rounding up the values by two precision. Only ShoppingCart and Main Class modified, rest of the class remains as same as given.

If you have any doubt or you want to modify this code then please comment it and also please rate it. Thanks!! :)


Here is ShoppingCart Class:

import java.text.DecimalFormat;

import java.util.ArrayList;

import java.util.List;

public class ShoppingCart

{

      private final List<Cashier> cashiers = new ArrayList<>();

      private final List<Item> items = new ArrayList<>();

      private int MAX_ITEMS = 10;

      // for Double values with only two precision

      private static DecimalFormat dfForDouble = new DecimalFormat(".##");

     

      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 void getTotalPrice()

      {

            double totalPrice = 0.0;

            for(Item item : items)

            {

                  totalPrice += item.getPrice();

            }

            System.out.println(" Total price of items = " + dfForDouble.format(totalPrice));

      }

}

and Here is Main-Class:

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().getTotalPrice();

            customer.getShoppingCart().removeItem(1);

            customer.getShoppingCart().removeItem(3);

            System.out.println(" Final status of shopping cart");

            System.out.println(" Customer: " + customer);

            customer.getShoppingCart().printItems();

            customer.getShoppingCart().getTotalPrice();

      }

}

Here is the output:

Welcome to Target!

Your cashier for today is:
ID = 789
First Name = John
Last Name = Smith
Checkstand Number = 7

Initial status of shopping cart

Customer:
ID = 123
First Name = Jane
Last Name = Doe

*** Shopping Cart ***

Book:
Tile = The Man Who Mistook His Wife for a Hat
Description = Book
Price = $10.87
Page count = 256

Movie:
Tile = The Incredibles
Description = Movie
Price = $14.53
Length in minutes = 115

Vegetable:
Tile = Chayote
Description = Vegetable
Price = $18.98
Weight in libras = 3.0

Movie:
Tile = Finding Nemo
Description = Movie
Price = $19.99
Length in minutes = 100

Book:
Tile = Why Zebras Don't Get Ulcers
Description = Book
Price = $12.75
Page count = 560

Fruit:
Tile = Honeycrisp Apple
Description = Fruit
Price = $2.99
Weight in libras = 1.0

CD:
Tile = Hot Fuss
Description = CD
Price = $5.99
Track count = 11

Movie:
Tile = Beetlejuice
Description = Movie
Price = $4.99
Length in minutes = 92

Total amount of items = 8

Total price of items = 91.09

Final status of shopping cart

Customer:
ID = 123
First Name = Jane
Last Name = Doe

*** Shopping Cart ***

Movie:
Tile = The Incredibles
Description = Movie
Price = $14.53
Length in minutes = 115

Vegetable:
Tile = Chayote
Description = Vegetable
Price = $18.98
Weight in libras = 3.0

Book:
Tile = Why Zebras Don't Get Ulcers
Description = Book
Price = $12.75
Page count = 560

Fruit:
Tile = Honeycrisp Apple
Description = Fruit
Price = $2.99
Weight in libras = 1.0

CD:
Tile = Hot Fuss
Description = CD
Price = $5.99
Track count = 11

Movie:
Tile = Beetlejuice
Description = Movie
Price = $4.99
Length in minutes = 92

Total amount of items = 6

Total price of items = 60.23

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote