Question:
Java Programming Shop.java I NEED HELP ASAP!?
Mysitque
2013-11-18 13:15:00 UTC
I need help completing this program, I am stuck. I am mostly having problems with printing the cart. Here is the assignment:

In this exercise you will implement a shopping cart using the ArrayList class. The file Item.java contains the definition of a
class named Item that models an item one would purchase (this class was used in an earlier lab). An item has a name, price,
and quantity (the quantity purchased). The file Shop.java is an incomplete program that models shopping.
1. Complete Shop.java as follows:
a. Declare and instantiate a variable cart to be an empty ArrayList.
b. Fill in the statements in the loop to add an item to the cart and to print the cart contents (using the default toString in
the ArrayList class). Comments in the code indicate where these statements go.
c. Compile your program and run it.
2. You should have observed two problems with using the default printing for the cart object: the output doesn’t look very
good and the total price of the goods in the cart is not computed or printed. Modify the program to correct these
problems by replacing the print statement with a loop that does the following:
a. gets each item from the cart and prints the item
b. computes the total price of the items in the cart (you need to use the getPrice and getQuantity methods of the Item
class). The total price should be printed after the loop.
3. Compile and run your program.

Here is the driver:

import java.text.NumberFormat;

public class Item
{
private String name;
private double price;
private int quantity;

// ----------------------------------------...
// Create a new item with the given attributes.
// ----------------------------------------...
public Item (String itemName, double itemPrice, int numPurchased)
{
name = itemName;
price = itemPrice;
quantity = numPurchased;
}

// ----------------------------------------...
// Return a string with the information about the item
// ----------------------------------------...
public String toString ()
{
NumberFormat fmt = NumberFormat.getCurrencyInstance();

return (name + "\t" + fmt.format(price) + "\t" + quantity + "\t"
+ fmt.format(price*quantity));
}

// ----------------------------------------...
// Returns the unit price of the item
// ----------------------------------------...
public double getPrice()
{
return price;
}

// ----------------------------------------...
// Returns the name of the item
// ----------------------------------------...
public String getName()
{
return name;
}

// ----------------------------------------...
// Returns the quantity of the item
// ----------------------------------------...
public int getQuantity()
{
return quantity;
}
}

And here is Shop.java

package Mackenzie;
//***************************************************************
//Shop.java
//Mackenzie Pardoe
// 11/13/13
//Uses the Item class to create items and add them to a shopping
//cart stored in an ArrayList.
//***************************************************************
import java.util.ArrayList;
import java.util.Scanner;
public class Shop
{
public static void main (String[] args)
{
ArrayList cart = new ArrayList();
Item item;
String itemName;
double itemPrice;
int quantity;

Scanner scan = new Scanner(System.in);

String keepShopping = "y";

do
{
System.out.print ("Enter the name of the item: ");
itemName = scan.nextLine();

System.out.print ("Enter the unit price: ");
itemPrice = scan.nextDouble();

System.out.print ("Enter the quantity: ");
quantity = scan.nextInt();

//*** create a new item and add it to the cart
item = new Item(itemName, itemPrice, quantity);
cart.add(item);
//*** print the contents of the cart object using println
System.out.println("Current items in your cart: " + cart);
System.out.print ("Continue shopping (y/n)? ");
keepShopping = scan.nextLine();
}
while (keepShopping.equals("y"));
}
double finalPrice = 0;
{
for (int i = 0; i {
item = cart.get(i);
}
for (Item xitem:cart)
{

}
}
}

I need help ASAP as this is due tomorrow! Please help me I am really stuck. I know there is something wrong with my loop but I don't know how to fix it. Help please! Thanks!
Three answers:
Leo D
2013-11-18 13:49:04 UTC
Please send me an email :)



Most of your code seems to check out, but it gets really confusing at the end:

- When you close the Do-while loop, you seem to close the main(String...) method as well.

- Then you seem to set a final price field.

- After that you do an anonymous constructor with two For loops inside.



That's what's confusing me. However, your question seems to be about printing the cart. You seemed to do that fine though. The cart is just a list* of Items, right? Did you need to instead loop through the list and print each item in a different format?



*Speaking of this:

a) I would make the type of the cart reference just List. This allows for the flexibility of changing the list type down the road. There is no advantage in using array list as the type since you aren't using any exclusively Array List methods.

b) I would use a linked list for this. (In which case, the cart reference type could instead be Queue or just Collection or even Iterable.) Array lists are specifically for when you can make a good guess of an overestimate of how many elements the list will have (e.g: the cart will never have more than X items). If you can't make such a guess, you should use a linked list instead. Array lists have to create a new array, copy the contents from the old array to the new, and free up the old array to garbage collection every time they grow beyond capacity. Linked lists just use nodes.



If the output must be an array list under such condition, you should probably rethink using whatever it is that you're outputting too. However, if that's not an option or even the desired behaviour (e.g: you really need that random accessibility), creating an array list from a linked list is a little more expensive than resizing an array list, but a lot less expensive than doing it multiple times.



***Re: "I am confused on what you are suggesting I do. I have to keep the program as an array list. In the while loop, I keep getting an error saying that "cart" isn't a variable or something. My main problem is that I need the loop to continually print the cart contents and then I need to get each item from the cart and print the item. Then I need to compute the total of all the items. I have no idea how to do all that with the while loop. Do you understand how to do it? Thank you, I appreciate your help."



I actually mentioned a possible answer for why it's saying that. Sorry if I wasn't clear enough. You have an extra, unnecessary closing curly brace after the while loop:

        while (keepShopping.equals("y"));

    }



The program thinks that you ended your main method.



After that, the finalPrince variable is set as a field for the Mackenzie.Shop class (rather than a variable for the main method). And the for loops are in anonymous initializers.



Just remove that extra curly brace and you should be fine :)



And about the first suggestion, it's programming by interface (or programming by contract) and lets you keep it as an Array List, but refer to it as a List (any kind of list). Here's what I mean. Instead of:

    ArrayList cart = new ArrayList();

It would be:

    List cart = new ArrayList(); // with the necessary import java.util.List; of course.



This suggestion is because you aren't using any of the array list-specific methods (methods like clone, ensureCapacity, trimToSize, honestly for micromanaging), so you don't lose any thing from it :) Programming to an interface instead of a specific class's methods is also a good programming habit since it removes dependency and allows you to write more abstract, reusable code. It's recommended to do it wherever you can so you can more easily spot such opportunities.



I see what you mean about not being able to the second suggestion =/ Instructors make odd requirements like that =Þ I'd suggest asking the "customer" user for an estimate of many items they think they'll buy and adding space for two more, or 50% or so more to that, (the array list would resize if they underestimated, but it is still better than resizing from 10 items every now and again). However that probably isn't allowed seeing as an invisible change like using a linked list isn't allowed.
Bonna
2016-03-08 12:41:54 UTC
import java.util.Random; public class DiceRoller { private int rollOne; private int rollTwo; private int numRolls; public DiceRoller() { rollOne = 0; rollTwo = 0; numRolls = 0; } public static void main(String[] args) { DiceRoller roller = new DiceRoller(); roller.roll(10); } public void roll(int check) { if(check > 12 || check < 2) return; Random r = new Random(); while(rollOne + rollTwo != check) { rollOne = r.nextInt(6) + 1; rollTwo = r.nextInt(6) + 1; numRolls++; System.out.println("Roll " + numRolls + ": " + rollOne + " and " + rollTwo); } System.out.println("Roll " + numRolls + ": " + rollOne + " + " + rollTwo + " = " + check); } }
HMJava
2013-11-18 13:33:19 UTC
Hi, no one has responded because there is not an actual "question" if you wish for someone to do it for you visit -> http://www.homeworkjava.com


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...