Class Methods - Public and Private


Class Methods - Public and Private

Bunny.java
BunnyTestHarness.java


Classwork


Part A

☑ Create a class StoreItem which has a name and a price.  Create the usual methods, and include a parameterized constructor that takes both name and price (don't forget to chain constructors!).  The toString should look something like

can of spam ($1.89)

In this class, (except inside the accessors and mutators themselves) use the accessors and mutators instead of the bare instance variables every time you need to use an instance var.  EVERY TIME.

Add a method cheaper which takes another StoreItem.  It returns whichever StoreItem is cheaper, this item or the one passed in.

☑ In a separate class with a main, create three StoreItems: one keeping the default values,  one using the parameterized constructor, and one using the default constructor then setting values using  the mutators.  Check that your cheaper method works.

Part B

It turns out that a single number is too simple to keep track of all the information about the price of our items  It turns out that the price is determined by the base price (that we paid the manufacturer) plus some amount of markup.  So, for instance, if the base price is 10 and the markup amount is .10, then the final price with markup is 10 +.10 =10.10.

☑ Back in StoreItem, add instance variables basePrice and markup.   Add accessors and mutators and make sure that markup starts as a default of $.10, but leave the rest of the methods as originally written.

In the old accessor for price, return an expression that calculates a final price as described, based on the new variables, and returns this result instead of the old price variable.

In the old mutator for price, use the markup to work backward from the price passed in, and set the basePrice accordingly. 

We are assuming that if someone tries to set the price rather than the base price, they don't know about the new system and are doing the math for markup themselves (with the default markup) to get the price they pass in, and we need to undo it, removing the markup. 

So if they give us a price of 11, and we assume the markup is .10, to find the base price... (you figure out the algebra, and make Java do the right math.)

Now remove the old price instance variable, and everything including your main should still work by using your updated "accessor" and "mutator" for price (if not, figure out where you were not using accessors and mutators).

☑ Change the main so that for the StoreItem you previously left with default values, you now set the basePrice and markup through the mutators.

(hint: when you made these changes, you should have been using the accessors and mutators for the NEW instance variables everywhere too!)


[EC]

In your main, ask the user how many items there are.  In a loop that many times, ask the user for the name and then the price (remember the input incantations are linked from the class page), and set up a new StoreItem object each time based on that information, using the parameterized constructor.  At the end, print the cheapest StoreItem you saw.  Use the cheaper method you wrote earlier to do this. 

To do this, you only have to store two StoreItems at any time: the current one, and the cheapest one you've seen so far.  (hint: when you have only read in one item, it is automatically the cheapest (you might decide to read in one before the loop or find another way to handle this)). {note: when reading in different types, Scanner can behave oddly, try using next() instead of nextLine() if you are having problems (this will not allow names with spaces in them; that's fine)}