Question

In: Computer Science

Write a shoppingcartmanager.java that contains a main method for this code in java Itemtopurchase.java public class...

Write a shoppingcartmanager.java that contains a main method for this code in java

Itemtopurchase.java

public class ItemToPurchase {
   // instance variables
   private String itemName;
   private String itemDescription;
   private int itemPrice;
   private int itemQuantity;
   // default constructor
   public ItemToPurchase() {
       this.itemName = "none";
       this.itemDescription = "none";
       this.itemPrice = 0;
       this.itemQuantity = 0;
   }
   public ItemToPurchase(String itemName, int itemPrice, int itemQuantity,String itemDescription) {
       this.itemName = itemName;
       this.itemDescription = itemDescription;
       this.itemPrice = itemPrice;
       this.itemQuantity = itemQuantity;
   }
   // method to set name of the item
   public void setName(String name) {
       itemName = name;
   }
   // method to set price of the item
   public void setPrice(int price) {
       itemPrice = price;
   }
   // method to set quantity of the item
   public void setQuantity(int quantity) {
       itemQuantity = quantity;
   }
   public void setDescription(String description) {
       itemDescription = description;
   }
   // method to get name of the item
   public String getName() {
       return itemName;
   }
   // method to get price of the item
   public int getPrice() {
       return itemPrice;
   }
   // method to get quantity of the item
   public int getQuantity() {
       return itemQuantity;
   }
   public String getDescription() {
       return itemDescription;
   }
   public void printItemPurchase() {
       System.out.println(itemName + " " + itemQuantity + " @ $" + itemPrice + " = $" + (itemPrice * itemQuantity));
   }
   public void printItemDescription() {

       System.out.println(itemName+": "+itemDescription);
   }
}

*******************

shoppingcart.java

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

public class ShoppingCart {
   private String customerName;
   private String currentDate;
   private List<ItemToPurchase> cartItems=new ArrayList<>();
   ShoppingCart() {
       customerName = "none";
       currentDate = "January 1, 2016";
   }
   ShoppingCart(String customerName, String currentDate) {
       super();
       this.customerName = customerName;
       this.currentDate = currentDate;
   }
   public String getCustomerName() {
       return customerName;
   }
   public String getDate() {
       return currentDate;
   }
   public void addItem(ItemToPurchase item) {
       cartItems.add(item);
   }
   public void removeItem(String itemName) {
       boolean check = false;
       for (ItemToPurchase item : cartItems) {
           if(item.getName().equalsIgnoreCase(itemName)) {
               cartItems.remove(item);
               check=true;
           }
       }
       if(!check)
           System.out.println("Item not found in cart. Nothing removed.");
   }
   public void modifyItem(ItemToPurchase item) {
       boolean check = false;
       for (ItemToPurchase itemToPurchase : cartItems) {
           if(itemToPurchase.getName().equalsIgnoreCase(item.getName())) {
               cartItems.remove(itemToPurchase);
               cartItems.add(item);
               check=true;
           }
       }
       if(!check)
           System.out.println("Item not found in cart. Nothing modified.");
   }
   public int getNumItemsInCart() {
       int total=0;
       for (ItemToPurchase item : cartItems) {
           total+=item.getQuantity();
       }
       return total;
   }
   public int getCostOfCart() {
       int cost=0;
       for (ItemToPurchase item : cartItems) {
           cost+=item.getPrice()*item.getQuantity();
       }
       return cost;
   }
   public void printTotal() {
       System.out.println(customerName+"'s Shopping Cart - "+currentDate);
       System.out.println("Number of Items: "+getNumItemsInCart());
       System.out.println();
       for (ItemToPurchase item : cartItems) {
           System.out.println(item.getName()+" "+item.getQuantity()+" @ $"+item.getPrice()+" = $"+(item.getQuantity()*item.getPrice()));
       }
       System.out.println();
       System.out.println("Total: $"+getCostOfCart());
   }
   public void printDesciptions() {
       System.out.println(customerName+"'s Shopping Cart - "+currentDate);
       System.out.println();
       System.out.println("Item Descriptions");
       for (ItemToPurchase item : cartItems)
           System.out.println(item.getName()+": "+item.getDescription());
   }
   public static void main(String[] args) {
       Scanner sc=new Scanner(System.in);
       System.out.println("Enter Customer's Name:");
       String name=sc.nextLine();
       System.out.println("Enter Today's Date:");
       String date=sc.nextLine();
       ShoppingCart cart=new ShoppingCart(name,date);
       System.out.println();
       System.out.println();
   }
}

Solutions

Expert Solution

Here I am providing the answer for the above question:

Code:

import java.util.Scanner;

public class ShoppingCartManager
{
   public static void main(String[] args)
   {
   Scanner sc=new Scanner(System.in);
   System.out.println("Enter Customer's Name:");
   String name=sc.nextLine();
   System.out.println("Enter Today's Date:");
   String date=sc.nextLine();
     
   ShoppingCart cart=new ShoppingCart(name,date);
   System.out.println("Enter total number of items:");
   int n=sc.nextInt();
   sc.nextLine();
   for(int i=1;i<=n;i++)
   {
       System.out.println("Enter item"+i+"name:");
       String item=sc.nextLine();
       System.out.println("Enter item"+i+"description:");
       String description=sc.nextLine();
       System.out.println("Enter item"+i+"price:");
       int price=sc.nextInt();
       System.out.println("Enter item"+i+"quantity:");
       int quantity=sc.nextInt();
       sc.nextLine();
       cart.addItem(new ItemToPurchase(item,price,quantity,description));
   }
  
   cart.printTotal();
   cart.printDesciptions();
   }
}

Screenshot of code:

Output:

Hoping that the above answer will help you...Thank you...


Related Solutions

Java Class Create a class with a main method. Write code including a loop that will...
Java Class Create a class with a main method. Write code including a loop that will display the first n positive odd integers and compute and display their sum. Read the value for n from the user and display the result to the screen.
write JAVA program have a public class named GeometricShapes that has the main() method. In the...
write JAVA program have a public class named GeometricShapes that has the main() method. In the main() method the user needs to select if he want 2D shapes or 3D shape -if user select 2D user needs to select the shape type (Square, Circle, or Triangle) after selected shape the user needs to specify whether to find the Area or the Perimeter or to find side-length (radius for the circles), accordingly the needed constructor is used. (using Polymorphism principle) -if...
In Java Create a class called "TestZoo" that holds your main method. Write the code in...
In Java Create a class called "TestZoo" that holds your main method. Write the code in main to create a number of instances of the objects. Create a number of animals and assign a cage and a diet to each. Use a string to specify the diet. Create a zoo object and populate it with your animals. Declare the Animal object in zoo as Animal[] animal = new Animal[3] and add the animals into this array. Note that this zoo...
WRITE THIS JAVA CODE IN PSEUDOCODE!! import java.util.Scanner; public class License { public static void main(String[]...
WRITE THIS JAVA CODE IN PSEUDOCODE!! import java.util.Scanner; public class License { public static void main(String[] args) { char correctAnswers[] = {'B', 'D', 'A', 'A', 'C', 'A', 'B', 'A', 'C', 'D', 'B', 'C', 'D', 'A', 'D', 'C', 'C', 'B', 'D', 'A'}; char userAnswers[] = new char[correctAnswers.length]; Scanner scanner = new Scanner(System.in); for (int i = 0; i < userAnswers.length; i++) { String answer = ""; System.out.printf("Question #%d. Enter your answer( A, B, C or D): ", i + 1); do...
Write the following Java code into Pseudocode import java.util.*; public class Main { // Searching module...
Write the following Java code into Pseudocode import java.util.*; public class Main { // Searching module public static void score_search(int s,int score[]) { // Initialise flag as 0 int flag=0; // Looping till the end of the array for(int j=0;j<10;j++) { // If the element is found in the array if(s==score[j]) { // Update flag to 1 flag=1; } } // In case flag is 1 element is found if(flag==1) { System.out.println("golf score found"); } // // In case flag...
In Java, please write a tester code. Here's my code: public class Bicycle {     public...
In Java, please write a tester code. Here's my code: public class Bicycle {     public int cadence; public int gear;   public int speed;     public Bicycle(int startCadence, int startSpeed, int startGear) {         gear = startGear;   cadence = startCadence; speed = startSpeed;     }     public void setCadence(int newValue) {         cadence = newValue;     }     public void setGear(int newValue) {         gear = newValue;     }     public void applyBrake(int decrement) {         speed -= decrement;    ...
You are to write a class StringPlay that has only a main method. This class contains...
You are to write a class StringPlay that has only a main method. This class contains all interaction with the user. The main method Uses a Scanner to accept user input Maintains a “current” String Uses a sentinel-controlled loop to accept multiple user commands, or exit The program displays a message asking the user to enter one of the following commands a – enter a new value for current b – padLeft asks the user for the number of characters...
Java programming. Write a public Java class called DecimalTimer with public method displayTimer, that prints a...
Java programming. Write a public Java class called DecimalTimer with public method displayTimer, that prints a counter and then increments the number of seconds. The counter will start at 00:00 and each time the number of seconds reaches 60, minutes will be incremented. You will not need to implement hours or a sleep function, but if minutes or seconds is less than 10, make sure a leading zero is put to the left of the number. For example, 60 seconds:...
DESCRIBE WHAT THE FOLLOWING JAVA CODE DOES: public class Main{ public static void main(String[] args) {...
DESCRIBE WHAT THE FOLLOWING JAVA CODE DOES: public class Main{ public static void main(String[] args) { new MyFrame(); } } import javax.swing.*; public class MyFrame extends JFrame{ MyPanel panel; MyFrame(){ panel = new MyPanel(); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.add(panel); this.pack(); this.setLocationRelativeTo(null); this.setVisible(true); } } import java.awt.*; import javax.swing.*; public class MyPanel extends JPanel{ //Image image; MyPanel(){ //image = new ImageIcon("sky.png").getImage(); this.setPreferredSize(new Dimension(500,500)); } public void paint(Graphics g) { Graphics2D g2D = (Graphics2D) g; //g2D.drawImage(image, 0, 0, null); g2D.setPaint(Color.blue); g2D.setStroke(new BasicStroke(5)); g2D.drawLine(0, 0, 500,...
write a java code to represent a sales class as follows: 1- The Sales class contains...
write a java code to represent a sales class as follows: 1- The Sales class contains the names of sellers (strings) and the sales/seller/day (matrix of integers). Assume the number of sellers is set dynamically by the constructor and that the sellers work 6 days/week. Example: names/days 0 1 2 3 4 5 Ali 30 5 89 71 90 9 Ahmad 15 81 51 69 78 25 Omar 85 96 7 87 41 54 The class should contain the following...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT