Final Assignment.
Points: 100
Due: 12/9/18
Instructor: Abeer Khaleq
In this assignment you can work individually or in pairs (only two).
Make sure to add your name(s) to the code. If you are working in pairs then both students need to submit the assignment for a grade.
- You will develop a greenfoot scenario to simulate shopping for grocery (or other items).
- Your initial scenario will look something like this:
2.1 The items images along with their prices will be displayed. You can add as many items as you like, but the minimum is three. I suggest starting with that first and getting all the requirements to work.
2.2 Initially there is nothing in the shopping cart.
2.3 A Pay object will be used to calculate the total cost of items purchased. This is an image that you can create to simulate a pay button.
- The World class:
3.1 Declare an array of items of type Actor to be public static. This array will hold the items objects such as banana, grapes, and others later.
3.2 In the World constructor MyWorld(), you need to do the following:
- Create the items array of size 10.
- Assign the objects to each array element. You will be creating a new object of each item.
1
- Create an array prices of type double and size 10. This array will hold the prices of the items as displayed above. The two arrays will be parallel arrays.
- Initialize the prices array with the corresponding prices for the items.
- Using a for loop (and only one), display the item and the corresponding price from the two arrays items and prices correspondingly on the World as illustrated in the image above.
- You can use the method addObject() to add the item to the world.
- Finally add the Pay object.
- Make sure the items, prices are laid out as shown above.
- For the item classes. You just need to add a variable price of type double and initialize it to the item price. This will be a public variable above the act method. This is just to maintain the price for each item object.
- The ShoppingCart class will be a class with no image that will be added to the scenario. The class will simulate purchasing the clicked on objects and the required quantity.
5.1 Declare an integer variable quantity to represent the quantity ordered for each item.
5.2 Declare a double variable totalPrice to calculate the total price of items purchased.
5.3 In the class constructor, initialize both variables to 0.
5.4 In the act method, you need to test if the user clicks on any of the displayed items to purchase. Use the method Greenfoot.mouseClicked(). This method checks if a given object is clicked and if so it returns true. You will pass the object to the method. The objects are in the items array of the MyWorld class. Please refer to Greenfoot documentation on how to use this method.
5.5 When a given item is clicked, ask the user to enter the quantity. Use the method
Greenfoot.ask(“enter quantity:”). This method will display a user-input dialog allowing the user to enter a quantity number. Since the input will be taken as a String, it needs to be converted into an integer, so you will be doing the following to take the quantity as an integer:
quantity = Integer.parseInt(Greenfoot.ask(“Enter quantity:”);
The quantity will be used when calculating the total price.
5.6 The clicked object along with the quantity will be displayed under the shopping cart on the screen as follows: