Great Deal! Get Instant $10 FREE in Account on First Order + 10% Cashback on Every Order Order Now

Assignment In this lab, we are going to create our own data type and then use that data type in an ArrayList. Specifically, we will be creating Items to be purchased and then printing a receipt like...

1 answer below »
Assignment

In this lab, we are going to create our own data type and then use that data type in an ArrayList. Specifically, we will be creating Items to be purchased and then printing a receipt like at a store.

STEP 1: Write the Item class

When we create our own data type, we do this by creating a classwithout a main method. There are several different parts which we will look at individually.

public class Item
{
// TODO: Instance variables to store information we want to remember about an Item //TODO: Constructor that takes any set up information and assigns
// the values to the instance variables.
//TODO: Accessors for any information we want to access
//TODO: Mutators for any information we want to be able to change
//TODO: toString() for printing the Object
}

Let’s start by looking at this code provided. Add in the following:

1. Constructor that takes two parameters so that this works:

Item first = new Item("Shoes", 48.99); // Name is "Shoes" and price is $48.99

2. name, price //Privateinstance variables
3.getName(),getPrice()// Accessor methods
4.setName(),setPrice()// Mutator methods
5. toString method (use @Override) that prints out a String version of the object like this with properly formatted currency and a tab in between the name and price:

Item 1    $5.00


STEP 2: Write a Tester class

Next we are going to write a tester class to see that everything appears to be working in our Item class. To do this, let’s make a new file with a main method. Inside the main method, we will:

//Instantiate (create) a new Item Object
Item first = new Item("Shoes", 48.99);
//Test if the toString and constructor worked
System.out.println(first);
//Create another Item Object
Item second = new Item("Computer", 798.99);
System.out.println(second);
//Test if the accessors work
//Test if the mutators work

Here we can see that the Item.java class is kind of like a cookie cutter; it tells us what all items will remember (variables) and be able to do (methods). But when we use the keyword new + the constructor, that’s like actually making cookies, which can all be a little different (different “states” -- variable values), but they all store and do the same things.

STEP 3: Take a look at ItemReceiptDriver.java

Here I have done a little work for you on the flow of this Receipt creator, but most of it you will have to complete yourself.

* Note: you will not need to change anything in this file. All of your “work” will be in the Receipt Class, mostly in the printReceipt method -- though this method will call the other methods, so you will write it last.

Notice that I created two ArrayLists:

ArrayList saleList;
ArrayList purchasedItems;

The first looks like ArrayLists you already know. saleList stores the names (String) of all the items that are on sale.

The second looks a little different. purchasedItems stores Items, which means that each value in the ArrayList is actually a collection, specifially the collection of data and methods that we defined in step 1.

In the driver class, I wrote the methods setSaleItems() and purchaseItems() to get some data to set up your ArrayLists. The ArrayLists that are returned by each of those methods is then passed to the Receipt constructor so that the receipt can “remember” that information. You may modify these methods if you like, but you don’t need to.

You can run the driver to see what currently happens in the program.

TAKE A LOOK AT RECEIPT.JAVA

STEP 4: Write the findSaleItems method

Go through the ArrayList of items to buy and check if the sale items list contains the name of each item. If it does, add that Item (the whole Item, not just the name) to a new ArrayList to return at the end.

Test that this method works by calling it in the printReceipt method and printing out the result.

STEP 5: Write the subtotal method

Complete the method to go through all the items to buy and calculate the subtotal. Ignore sale items in this method. Test this method in the printReceipt method.

STEP 6: Write the discount method

Complete the method to calculate the amount of discount by looping through the ArrayList returned by findSaleItems. The discount on each sale item is 50% off. Test your method in the printReceipt method.

For Added Fun,think about how you can eliminate the method entirely and instead just utilize the subtotal method.

STEP 7: Write the printReceipt method

Last we are going to write the printReceipt method. This method should call the methods you wrote above in order to print the receipt. You should not make any calculations that you have already made again.

The receipt should print something like this:

 
Items Cost
--------------------
Item 1 $5.00
Item 2 $10.00
Toothpaste $30.00
--------------------
SUBTOTAL $45.00
*Items on Sale = 1
Discount $15.00
--------------------
FINAL PRICE $30.00
*Items on sale include: [Toothpaste]

Answered 4 days After Mar 29, 2022

Solution

Aditya answered on Apr 02 2022
102 Votes
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here