Question

In: Computer Science

Create a simple shopping cart application. Ask the user to input an item number and quantity....

Create a simple shopping cart application. Ask the user to input an item number and quantity. Update the shopping cart. Display the updated cart. Include the following when displaying cart: item number, name, quantity and total price. Ask for another update or end the program. Design Requires 2 classes: the main class and the cartItem class In the main class: Need one array named cartArray of type cartItem. Size = ? Need a loop that asks the user for input. Ask for item number, a string and quantity, and integer. Need to create a new cartItem from user input of item number and quantity. Load the cartItem object into the cartArray. (CartItem is a class that takes two values in constructor: item number and item quantity.) Print out all items in cartArray (item number, name, quantity, and total price) and ask user for more input. CartItem class CartItem has four instance variables. String itemNumber; String name; int quantity; double totalPrice; It also has a “database” made up of 3 arrays. Described below. The CartItem class has a constructor that takes a String(itemNumber) and an integer(quantity) as arguments. The constructor does the following: It passes the item number up to the corresponding instance variable. It also passes the quantity up to the quantity instance variable. It then calls the getItemName method to first, get the item name and then set the item name instance variable. It calls the calcPrice method to set the totalPrice instance variable by first getting the total price and then setting the totalPrice instance variable. The CartItem class Has at least the following methods: public double calcPrice(String itemNumber); Returns total price: calculated by multiplying price by quantity. Need to look up the price.Use the item number array to get the index of the item number. Use the item number index to find the price from the price array. Once you have the price you multiply the price by the quantity and return the total. public String getItemName(String itemNumber); returns item name. Use the item number array to get the index of the item number. Use this index to find the name of the corresponding item. Return the name. public void setItemName(String name); sets name instance variable. public String toString() returns the string values of all the variables. Array description The first is of type String that holds the item numbers. Example: {“101a”, “ 201b”, “301c”} The second array is of type String and it holds the item name. Example: {“Flashlight”, ”scissors”, “Book mark”}; Third array is of type double that has the corresponding prices: {12.99, 4.55, 1.99};

ITP120 (java) is my subject.

Solutions

Expert Solution

We are following the below approach:

1. User will give the all item details and CartItem object will be created and store in one itemArray.

2. Now user will be asked to give input (Press 1 to insert a new CartItem, Press 2 for displaying Cart Item, Press 0 to quit program)

3. Above menu will be displayed infinitely till 0 is pressed.

4. If user press 1 then, user will be asked other required details like : ItemNumber and Quantity.

It will check itemNumber in itemArray, if not found then will give a message that please enter a correct item Number. otherwise it will calculate total Price and store that item into Cart List.

5. If user is pressing 2 then it will iterate over Cart List and print the Cart object.

6. System execution will be stopped once 0 will be pressed.

There are two classes : 1) CartItem.java 2) Driver.java

CartItem.java

package com.cart;

public class CartItem {
        
        
        private String itemNumber; 
        private String name; 
        private int quantity; 
        private double price;
        private double totalPrice;
        public CartItem(String itemNumber, String itemName, double price) {
                super();
                this.itemNumber = itemNumber;
                this.name = itemName;
                this.price = price;
        }
        public String getItemNumber() {
                return itemNumber;
        }
        public void setItemNumber(String itemNumber) {
                this.itemNumber = itemNumber;
        }
        public String getName() {
                return name;
        }
        public void setName(String name) {
                this.name = name;
        }
        public int getQuantity() {
                return quantity;
        }
        public void setQuantity(int quantity) {
                this.quantity = quantity;
        }
        public double getTotalPrice() {
                return totalPrice;
        }
        public void setTotalPrice(double totalPrice) {
                this.totalPrice = totalPrice;
        }
        
        
        public double getPrice() {
                return price;
        }
        public void setPrice(double price) {
                this.price = price;
        }
        
        public double calcPrice(int quantity) {
                return this.getPrice()*quantity;
                
        }
        @Override
        public String toString() {
                return "CartItem [itemNumber=" + itemNumber + ", itemName=" + name + ", quantity=" + quantity + ", price=" + price
                                + ", totalPrice=" + totalPrice + "]";
        }
        
        
        
        
        

}

Driver.java

package com.cart;

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

public class Driver {

        static CartItem[] itemArray = null;

        public static void main(String[] args) {

                Scanner sc = new Scanner(System.in);
                // Collect all the items and take user input and put into itemArray
                System.out.println("Please enter item data size : ");
                int cart_size = sc.nextInt();
                itemArray = new CartItem[cart_size];

                for (int i = 0; i < cart_size; i++) {
                        System.out.println("Please enter item number : ");
                        String itemNumber = sc.next();
                        System.out.println("Please enter item name : ");
                        String itemName = sc.next();
                        System.out.println("Please enter price :");
                        double price = sc.nextDouble();
                        CartItem cart = new CartItem(itemNumber, itemName, price);
                        itemArray[i] = cart;
                }
                System.out.println("Please choose below option :");
                List<CartItem> cartList = new ArrayList<CartItem>();
                // now ask user to select item and follow the instruction
                while (true) {
                        System.out.println(
                                        "Press 1 to insert a new CartItem\nPress 2 for displaying Cart Item\nPress 0 to quit program");
                        int choice = sc.nextInt();
                        switch (choice) {
                        case 1:
                                // new cart item will be added into list
                                System.out.println("Please enter an item number: ");
                                String item_number = sc.next();
                                System.out.println("Enter quantity : ");
                                int quantity = sc.nextInt();
                                cartList.add(insertIntoCart(item_number, quantity));
                                break;
                        case 2:
                                // displaying all the cart item details
                                displayCartItem(cartList);
                                break;
                        case 0:
                                System.exit(0);
                                break;
                        default:
                                System.out.println("Please enter a correct number from the menu");
                                break;
                        }

                }

        }

        private static CartItem insertIntoCart(String itemNumber, int quantity) {
                CartItem cart = null;
                boolean isItemAvailable = false;
                for (int i = 0; i < Driver.itemArray.length; i++) {
                        if (Driver.itemArray[i].getItemNumber().equalsIgnoreCase(itemNumber)) {
                                cart = Driver.itemArray[i];
                                isItemAvailable = true;
                                break;
                        }
                }
                if (!isItemAvailable) {
                        System.out.println("Please enter a correct itemNumber");
                        return null;
                }
                cart.setQuantity(quantity);
                cart.setTotalPrice(cart.calcPrice(quantity));
                return cart;
        }

        private static void displayCartItem(List<CartItem> cartList) {
                if (cartList.size() == 0 || cartList == null)
                        System.out.println("No item is present into cart");
                else {
                        for (int i = 0; i < cartList.size(); i++) {
                                System.out.println(cartList.get(i));
                        }
                }
        }

}

In case if you still have any query, feel free to post into comment box. I would be glad to assist you. If you like my answer, please hit on like button, it really motivates us to provide a good quality answer.


Related Solutions

Create a simple shopping cart application. Ask the user to input an item number and quantity....
Create a simple shopping cart application. Ask the user to input an item number and quantity. Update the shopping cart. Display the updated cart. Include the following when displaying cart: item number, name, quantity and total price. Ask for another update or end the program. Design Requires 2 classes: the main class and the cartItem class In the main class: Need one array named cartArray of type cartItem. Size = ? Need a loop that asks the user for input....
Create a small program that contains the following. ask the user to input their name ask...
Create a small program that contains the following. ask the user to input their name ask the user to input three numbers check if their first number is between their second and third numbers
Language: C# Create a new Console Application. Your Application should ask the user to enter their...
Language: C# Create a new Console Application. Your Application should ask the user to enter their name and their salary. Your application should calculate how much they have to pay in taxes each year and output each amount as well as their net salary (the amount they bring home after taxes are paid!). The only taxes that we will consider for this Application are Federal and FICA. Your Application needs to validate all numeric input that is entered to make...
Create a project that gets input from the user. ( Name the project main.cpp) Ask the...
Create a project that gets input from the user. ( Name the project main.cpp) Ask the user for a value for each side of the triangle. Comment your code throughout. Include your name in the comments. Submit the plan-Psuedo code (include Flowchart, IPO Chart) and the desk check with each submission of your code. Submit the plan as a pdf. Snips of your output is not a plan. Save the plan and desk check as a pdf Name the code...
Create an application that makes the user guess a number. The user must be allowed tries....
Create an application that makes the user guess a number. The user must be allowed tries. You must have a loop, user input (Scanner), and a constructor. (JAVA)
Flowchart + Python. Ask the user for a value. Then, ask the user for the number...
Flowchart + Python. Ask the user for a value. Then, ask the user for the number of expressions of that value. Use While Loops to make a program. So then, it should be so that 5 expressions for the value 9 would be: 9 * 1 = 9 9 * 2 = 18 9 * 3 = 27 9 * 4 = 36 9 * 5 = 45 Flowcharts and Python Code. Not just Python code. I cannot read handwriting....
Write an application and perform the following:(NO USER INPUT) -Create at least three classes such as:...
Write an application and perform the following:(NO USER INPUT) -Create at least three classes such as: 1. listnode- for data and link, constructors 2. Singlelinkedlist-for method definition 3. linkedlist-for objects -Insert a node at tail/end in a linked list. -and display all the nodes in the list. -Delete a node at a position in the list Note: Add these methods in the same application which we have done in the class. this is what we've done in the class: class...
Using Python, write a simple application that takes user input of plaintext and key, and encrypt...
Using Python, write a simple application that takes user input of plaintext and key, and encrypt the plaintext with Vigenere Cipher. The application should then print out the plaintext and the ciphertext.
Write application that enables a user to input the grade and number of credit hours for any number of courses.
Write application that enables a user to input the grade and number of credit hours for any number of courses. Calculate the GPA on a 4.0 scale using those values. Grade point average (GPA) is calculated by dividing the total amount of grade points earned, sometimes referred to as quality points, by the total number of credit hours attempted. For each hour, an A receives 4 grade or quality points, a B receives 3 points, a C receives 2 points,...
Create a C++ program that will prompt the user to input an integer number and output...
Create a C++ program that will prompt the user to input an integer number and output the corresponding number to its numerical words. (From 0-1000000 only) **Please only use #include <iostream>, switch and if-else statements only and do not use string storing for the conversion in words. Thank you.** **Our class is still discussing on the basics of programming. Please focus only on the basics. Thank you.** Example outputs: Enter a number: 68954 Sixty Eight Thousand Nine Hundred Fifty Four...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT