Must have two Files Data You will be creating a shopping cart program for Murray
ID: 3679928 • Letter: M
Question
Must have two Files
Data
You will be creating a shopping cart program for Murray’s Custom Computers (MCC).
MCC sells only laptop and tablet computers. It then further customizes the computers by only upgrading the RAM and hard drive options.
Pick two different actual manufacturers of tablet computers and get real life pricing for their base models of a particular screen size. You will need a total of 4 tablet options. The distribution is not important. For instance, you could have 3 of one manufacturer and 1 of a different manufacturer.
Each tablet will have 2 upgrade options for RAM
Each tablet will have 3 upgrade options for the hard drive size/seek speed
Pick three different actual manufacturers of laptop computers and get real life pricing for their base models of a particular screen size. You will need a total of 6 laptop options. The distribution is not important. For instance you could have 3 of one manufacturer, 2 of a different manufacturer and 1 of another.
Each laptop will have 4 upgrade options for RAM
Each laptop will have 5 upgrade options for the hard drive type/size/seek speed
You will create a class of your own. This class will hold information about the computer. You must keep track of at least 7 class variables. You can decide what to store, but make sure it would be data pertaining to a single computer. Also make sure you are storing information that I will be requiring you to retrieve later in the project.
Only create objects when needed (when creating/adding a new computer)
Do not create an object(s) for the computer information being shown to the user. Keep that information in variables that are defined in your main method. For example:
String tablet1_manufacturer=”Apple”;
String tablet1_model=”Ipad”;
String tablet2_manufacturer=”Microsoft”;
String tablet2_model=”Surface 4”;
Once the user has chosen, you will create a computer object based on their choices.
Your class must have at least 10 methods. Those methods can include the constructor and the toString method
Make one of these a constructor method
Make one the toString method
Make a method that sets all class variables at once (like the constructor)
Make a getter method for each class variable (at least 7 methods, one for each of the class variables.
Make a method that creates a total price for a computer object that has been upgraded. Store that information in the object as well. Do not do this in the constructor method.
You must include 3 separate and different conditional statements
You must have at least 2 while loop (This is the only loop I will allow. Do..while and For loops are in Chapter 6. So you will not be able to use those loops in this project.)
Important note: Only create an object once they have chosen a computer.
Program Flow
You will creating a shopping cart program. Assume only one person needs to submit data. The person will need to select at least 1 computer and will be allowed to select as many as 3 computers. Once the program collects the information, creates the objects, and outputs the necessary information, the program will terminate
Through the normal input/output window (no Jframes or applets), request the user to select between a Tablet or a Laptop
Provide them a list of the upgraded memory options for their computer. One of these options is to not do an upgrade. They should only get options for that particular type of computer. For example, if they chose a tablet they should only get 2 RAM memory upgrades as options plus a choice of not upgrading. If they chose a laptop, then they should get 4 RAM upgrade options plus a choice of not upgrading.
Provide them a list of the hard drive options for their computer. One of these options is to not do an upgrade. They should only get options for that particular type of computer. For example, if they chose a tablet they should only get 3 hard drive upgrades as options plus a choice of not upgrading. If they chose a laptop, then they should get 5 hard drive upgrades options plus a choice of not upgrading.
Once they had submitted data, create a computer object for each computer they have supplied data for.
All changes to the data once the object has been created must be done with one or more setter methods. That is, make all your class variables private.
Prompt the user if they want to purchase another computer. If they type Y or Yes , then you will loop back up to step 1. *Note If the user has already completed 3 computers, you will exit the loop without re-prompting them for another purchase.
Once out of the loop, get the final price from each computer object and add them to a running total. Do not store the running total in an object.
Calculate the sales tax for the purchase. Use 7%. Store this value in a variable, not in the object.
In a Jframe (not standard input/output window) Supply the user with the following pieces of data:For each computer:
The type of computer (Tablet or Laptop)
The manufacturer of the computer
The model of the computer
The amount of RAM they picked with the dollar amount for that (make sure it is in a currency format with exactly 2 decimal places showing for any type of number)
The size of the hard drive they picked with the dollar amount for that (make sure it is in a currency format with exactly 2 decimal places showing for any type of number)
A subtotal of all purchased computers (make sure it is in a currency format with exactly 2 decimal places showing for any type of number)
Sales tax amount (make sure it is in a currency format with exactly 2 decimal places showing for any type of number)
A final total (make sure it is in a currency format with exactly 2 decimal places showing for any type of number)
Your program must have the following:
Proper comments including the main description block at the beginning of each file and additional comments in the body of your code. You will also need to provide a short description of each of the classes
Your program must have meaningful variable and method names. A variable named a is not meaningful. A variable name computer_manufacturer is meaningful.
Explanation / Answer
Please use program code as shown below for a shopping cart program:
package cart;
import entity.Product;
import java.util.*;
public class ShoppingCart {
List<ShoppingCartItem> items;
int numberOfItems;
double total;
public ShoppingCart() {
items = new ArrayList<ShoppingCartItem>();
numberOfItems = 0;
total = 0;
}
/**
* Adds a <code>ShoppingCartItem</code> to the <code>ShoppingCart</code>'s
* <code>items</code> list. If item of the specified <code>product</code>
* already exists in shopping cart list, the quantity of that item is
* incremented.
*
* @param product the <code>Product</code> that defines the type of shopping cart item
* @see ShoppingCartItem
*/
public synchronized void addItem(Product product) {
boolean newItem = true;
for (ShoppingCartItem scItem : items) {
if (scItem.getProduct().getId() == product.getId()) {
newItem = false;
scItem.incrementQuantity();
}
}
if (newItem) {
ShoppingCartItem scItem = new ShoppingCartItem(product);
items.add(scItem);
}
}
/**
* Updates the <code>ShoppingCartItem</code> of the specified
* <code>product</code> to the specified quantity. If '<code>0</code>'
* is the given quantity, the <code>ShoppingCartItem</code> is removed
* from the <code>ShoppingCart</code>'s <code>items</code> list.
*
* @param product the <code>Product</code> that defines the type of shopping cart item
* @param quantity the number which the <code>ShoppingCartItem</code> is updated to
* @see ShoppingCartItem
*/
public synchronized void update(Product product, String quantity) {
short qty = -1;
// cast quantity as short
qty = Short.parseShort(quantity);
if (qty >= 0) {
ShoppingCartItem item = null;
for (ShoppingCartItem scItem : items) {
if (scItem.getProduct().getId() == product.getId()) {
if (qty != 0) {
// set item quantity to new value
scItem.setQuantity(qty);
} else {
// if quantity equals 0, save item and break
item = scItem;
break;
}
}
}
if (item != null) {
// remove from cart
items.remove(item);
}
}
}
/**
* Returns the list of <code>ShoppingCartItems</code>.
*
* @return the <code>items</code> list
* @see ShoppingCartItem
*/
public synchronized List<ShoppingCartItem> getItems() {
return items;
}
/**
* Returns the sum of quantities for all items maintained in shopping cart
* <code>items</code> list.
*
* @return the number of items in shopping cart
* @see ShoppingCartItem
*/
public synchronized int getNumberOfItems() {
numberOfItems = 0;
for (ShoppingCartItem scItem : items) {
numberOfItems += scItem.getQuantity();
}
return numberOfItems;
}
/**
* Returns the sum of the product price multiplied by the quantity for all
* items in shopping cart list. This is the total cost excluding the surcharge.
*
* @return the cost of all items times their quantities
* @see ShoppingCartItem
*/
public synchronized double getSubtotal() {
double amount = 0;
for (ShoppingCartItem scItem : items) {
Product product = (Product) scItem.getProduct();
amount += (scItem.getQuantity() * product.getPrice().doubleValue());
}
return amount;
}
/**
* Calculates the total cost of the order. This method adds the subtotal to
* the designated surcharge and sets the <code>total</code> instance variable
* with the result.
*
* @param surcharge the designated surcharge for all orders
* @see ShoppingCartItem
*/
public synchronized void calculateTotal(String surcharge) {
double amount = 0;
// cast surcharge as double
double s = Double.parseDouble(surcharge);
amount = this.getSubtotal();
amount += s;
total = amount;
}
/**
* Returns the total cost of the order for the given
* <code>ShoppingCart</code> instance.
*
* @return the cost of all items times their quantities plus surcharge
*/
public synchronized double getTotal() {
return total;
}
/**
* Empties the shopping cart. All items are removed from the shopping cart
* <code>items</code> list, <code>numberOfItems</code> and
* <code>total</code> are reset to '<code>0</code>'.
*
* @see ShoppingCartItem
*/
public synchronized void clear() {
items.clear();
numberOfItems = 0;
total = 0;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.