CS 0401 Intermediate Programming

Assignment 1

Topics: Review of expressions, conditions, loops and I/O


Online: Sunday, May 21, 2017

Due: All source (.java) files and a completed Assignment Information Sheet zipped into a single file and submitted via the submission site by 11:59PM on Monday, June 5, 2017.

Late Due Date: 11:59PM on Wednesday, June 7, 2017.


Submission note:  Your .zip file should contain only .java files and your Assignment Information Sheet.  There should be no project files or project subdirectories in the .zip file.  Also, your TA must be able to compile your program from the command line using the javac command.  Test your files to make sure this will work (especially if you use an IDE such as NetBeans for development).  If the TA cannot compile your program you will receive minimal credit!



After passing your OWLs and NEWTs but discovering that there are currently no openings for your dream job in the "Misuse of Muggle Artifacts Office", you wonder how to make a living while you wait for an opening (you may even accept a position with the "Accidental Magical Reversal Squad", but alas those are all filled as well).  Luckily a franchise for "Weasleys' Wizard Wheezes" is available, and you decide to open up a small joke shop in Hogsmeade while you wait.  Your shop is very small and you want to keep things as simple as possible, so you stock only three items: Extendable Ears, Skiving Snackboxes and Exploding Whizz Poppers.


Your prices are as follows:


Extendable Ears

2 Sickles (= 58 Knuts) each

Skiving Snackboxes

30 Knuts (small size) or

40 Knuts (deluxe size)

Exploding Whizz Poppers

5 Sickles (= 145 Knuts) each or

Bag of 4 for 1 Galleon (= 493 Knuts)


For your best customers you have a secret password that enables them to get discounts on your products.  Customers who know the password (they get two tries to guess it) get the following discounted prices:


Extendable Ears

50 Knuts each

Skiving Snackboxes

Deluxe size for the price of small (30 Knuts)

Exploding Whizz Poppers

No discount for single items

Bag of 4 is now 16 Sickles (= 464 Knuts)


10% off over and above any other discounts if the overall order (after other discounts) is 20 Sickles (= 580 Knuts) or more, rounded to the nearest Knut


To accommodate the occasional muggle who may drop by, or to remind the absent-minded 3rd years of the currency, you also post the following sign prominently within your shop:


Note 1: We appreciate exact change!


Note 2: Recall our currency options:

            29 Knuts == 1 Sickle

            17 Sickles == 1 Galleon == 493 Knuts


Your assignment is to write a Java program that will simulate transactions between you and some of your customers.  The program should:


1)    Ask if there is another customer to be waited on.  If so, continue; if not, quit the program.

2)    Ask the customer for the secret password (pick any password that you would like – don't worry about encryption).  If the customer knows the password, show him/her the discounted prices; otherwise show the customer the regular price list.

3)    Allow the customer to order items from the menu.  This process should be iterative, with the customer possibly changing his / her order until finalizing it prior to checking out.  For simplicity (to allow the line to move more quickly), purchases will be restricted in the following ways:

a)     Purchase of Exploding Whizz Poppers will always be made by number of individual items.  If a customer wishes to buy a bag, he / she should specify 4 and your program should automatically detect that as a bag of 4.  Furthermore, for any number of Whizz Poppers, K, you should automatically count as many whole bags as you can before charging by the individual item.  For example, if a customer asks for 14 Whizz Poppers, this should be 3 bags plus 2 single items.

b)     Discount passwords will be sent to customers either via owl or perhaps floo powder transmissions.  In any case, customers will not be given the password during store transactions – they must already know the password when they are waited on.

For an idea of how a customer transaction might proceed, see my sample output in a1out.txt.

4)    Tabulate the total (don't forget the 10% overall discount if it applies) and show the itemized bill (with unit and subtotal prices for each item) on the display.  Only show items on the bill that have actually been purchased.  See the example output in a1out.txt.

5)    Ask the customer for some money, input the amount, and give the customer his/her change (by showing it on the display).  The payment should be an iterative process – the customer enters an amount in Knuts, Sickles or Galleons (only one currency at a time) and the amount is added to the total.  The loop should continue until the total value entered exceeds the total bill.   If change is given, you will always give the minimum number of coins back (so you give larger currency before smaller currency).  For example, if a customer's total bill is 300 Knuts and he/she pays with 1 Galleon, then the change should be 6 Sickles and 19 Knuts.  See how this process works in the example output in a1out.txt. 

6)    Go back to 1) and do it all again


For some example runs, see file a1out.txt


Important Notes:

-      Clearly there are a few places in your program where loops are required.  We discussed (or will discuss) several different looping constructs in lecture – choose one that is appropriate for the task at hand (more than one correct answer is possible).

-      You will also need to do some math to calculate totals and discounts and to convert currency.  Think carefully about how you can do these things in relatively simple ways.

-      Be careful about handling special cases with regard to input.  If the value the user enters is invalid your program should handle it in some reasonable way.  However, you can assume that the type of the data the user enters will be valid (i.e. if you are expecting a number the user will not enter a non-numeric string).  Some special cases are demonstrated in the example runs that will be put online.

-      Use the Scanner class for your input.  We will see later how we can get input into our programs in other ways as well.

-      Format your code nicely (indenting, etc) and be sure to use comments to explain your code where appropriate.  Minimally you should have your name, course, section and a brief explanation at the beginning of your program. 

-      Your output should also be nicely formatted (make the user wants to run your program).  In particular, your itemized bill must be presented in a nice, readable way.  See file a1out.txt for some example output.  There are several ways to format output in Java.  See your text and online documentation for some help with this.

-      Be sure to electronically fill out and follow the directions on your Assignment Information Sheet, and to submit all materials following the correct submission guidelines.  Note that the TA should be able to compile and run your program directly from your submission directory after unzipping your files. If there are any problems with your submission you will lose some points.   

-      If you are interested in doing some extra credit, you can enhance your program in many ways.  Here are some examples:

-      Show after the total the user's itemized overall savings for being in Dumbledore's Army (i.e. for knowing the password).  In other words, for a customer who knows the password you should calculate the bill both with and without the discounts, and the difference is the amount the Dumbledore's Army member saved.  Show this total but also the individual savings for each different item.

-      Add some graphical output to your program using JOptionPane.  You should NOT use this for input, however, since a Scanner class is required.

-      If you have any other good ideas check with me to see if they qualify for extra credit.  Be sure to mention any extra credit that you do on your Assignment Information Sheet.

-      Be sure to look at the a1out.txt file for ideas about the look and feel of your program.  Your program DOES NOT have to look exactly like the sample run, but it should have the same functionality and should be well-formatted.