This site requires JavaScript, please enable it in your browser!
Greenfoot back
adore.ange
adore.ange wrote ...

2018/12/9

Shopping Cart mouseClicked()

adore.ange adore.ange

2018/12/9

#
I'm not familiar with mouseClicked(). I have a shopping cart assignment which involves: The ShoppingCart (actor) will be a class with no image. The class will simulate purchasing the clicked on objects and required quantity. Must have Greenfoot.mouseClicked() and quantity = Integer.parseInt(Greenfoot.ask(Enter quantity:"); I have to be able to click the item image which is on the world and have it to let the player put in the quantity. Then have the number of items they inserted and the picture show on the world.
public class ShoppingCart extends Actor

{
   
    
    int quantity = 0;
    double totalPrice = 0;
    Actor Apple = MyWorld.grocery[0];
    Actor Lemon = MyWorld.grocery[1];
    Actor Strawberry = MyWorld.grocery[2];
    
    /**
     * Act - do whatever the ShoppingCart wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
       
        if (Greenfoot.mouseClicked(Apple))
        {
            quantity = Integer.parseInt(Greenfoot.ask("Enter quantity:"));
getWorld().addObject(new Apple(),4,1);
        }
}
danpost danpost

2018/12/9

#
adore.ange wrote...
I'm not familiar with mouseClicked(). I have a shopping cart assignment which involves: The ShoppingCart (actor) will be a class with no image. The class will simulate purchasing the clicked on objects and required quantity. Must have Greenfoot.mouseClicked() and quantity = Integer.parseInt(Greenfoot.ask(Enter quantity:"); I have to be able to click the item image which is on the world and have it to let the player put in the quantity. Then have the number of items they inserted and the picture show on the world. << Code Omitted >>
The mouseClicked part looks alright to me. The few things I see are (1) the class extends the Actor class and all actors have an image field; (2) lines 8 through 10 are not necessary (in fact, it would be much easier to deal with those actors while in the array, as they already are); (3) I only see you adding one clicked on object into the world and doing nothing more (where's the quantity value that is supposed to show in the world?); (4) you have a totalPrice field that does not appear to be used at all; and (5) it is possible that the player errors and either does not enter a proper value (unwanted characters within the input) or does not enter anything (whether on purpose or not) -- the parseInt will fail and throw an exception which will halt the program -- this should be caught and dealt with so the program does not crash. Also, you may want also to limit the range of values that would be acceptable (negative values are not wanted).
You need to login to post a reply.