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, which is a string and quantity which is an 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};

Solutions

Expert Solution

Code:

CartItem Class:

public class CartItem
{
   String itemNumber; String name; int quantity; double totalPrice;
  
   String itemNumberArray[] = {"101a", "201b", "301c","401d","501e"};
   String nameArray[] = {"Flashlight", "scissors", "Book mark","Paper Weight","Blue Pen"};
   double priceArray[] = {12.99, 4.55, 1.99, 3.50, 1.50};
  
   CartItem(String itemNumber, int quantity)
   {
       this.itemNumber=itemNumber;
       this.quantity=quantity;
       this.name=getItemName(itemNumber);
       this.totalPrice=calcPrice(itemNumber);
   }
  
   public double calcPrice(String itemNumber)
   {
       int i=0;
       for(i=0;i<itemNumberArray.length;i++)
       {
           if(itemNumberArray[i].equals(itemNumber))
               {break;}
       }
       return priceArray[i]*this.quantity;
   }
   public String getItemName(String itemNumber)
   {
       int i=0;
       for(i=0;i<itemNumberArray.length;i++)
       {
           if(itemNumberArray[i].equals(itemNumber))
               {break;}
       }
       return nameArray[i];
   }
   public void setItemName(String name)
   {
       this.name = name;
   }
   public String toString()
   {
       return this.itemNumber+" "+this.name+" "+this.quantity+" "+this.totalPrice;
   }
}

MainProgram Class:

import java.util.Scanner;
public class MainProgram
{
   public static void main(String[] args)
   {
       CartItem cart[] = new CartItem[100];
       int c = 1;int i=0;
       String itemNumber;int quantity;
       Scanner sc = new Scanner(System.in);
       while(c==1)
       {
           System.out.print("Enter Item Number To Add To Cart: ");
           itemNumber=sc.next();
           System.out.print("Enter Quantity For Entered Item: ");
           quantity=sc.nextInt();
           cart[i]=new CartItem(itemNumber, quantity);
           i++;
           System.out.println("");
           System.out.println("Updated Cart Contents:");
           System.out.println("ItemNumber Name Quantity TotalPrice");
           for(int j=0;j<i;j++)
           {
               System.out.println(cart[j]);
           }
           System.out.println("");
           System.out.print("Enter 1 To Add Another Item Or Any Other Number To Exit: ");
           c=sc.nextInt();
           System.out.println("");
       }
   }
}

Sample Output:


Related Solutions

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)
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...
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...
Create method addUserInput Write a method called addUserInput(). The method should ask the user to input...
Create method addUserInput Write a method called addUserInput(). The method should ask the user to input two integers (one by one), add the two integers, and return the sum. By using java.util.Scanner to get user input; The method may not compile due to Scanner Class which need to add a "throws" statement onto the end of the method header because some lines may throw exceptions Refer to the Java API documentation on Scanner to figure out which specific Exception should...
Show: Create an application that allows the user to enter the number of calories and fat...
Show: Create an application that allows the user to enter the number of calories and fat grams in a food. The application should display the percentage of the calories that come from fat. If the calories from fat are less than 30% of the total calories of the food, it should also display a message indicating the food is low in fat. One gram of fat has 9 calories, so: Calories from fat = fat grams *9 The percentage of...
Write a program that will ask for the user to input a filename of a text...
Write a program that will ask for the user to input a filename of a text file that contains an unknown number of integers. And also an output filename to display results. You will read all of the integers from the input file, and store them in an array. (You may need to read all the values in the file once just to get the total count) Using this array you will find the max number, min number, average value,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT