Cash Receipt
Back to the Case Studies homepage
ProblemYou are a self-employed junior programmer. You've just started your own business and today, you've gotten your first job from a manager at a small arts and crafts store. The manager wants you to write a complete Java program that can be used by any cashier within her company to generate customer receipts for her convenience store which currently sells only three items.
Then, since the program is going to be used by multiple cashiers, use JOptionPane to prompt the user to enter:
Now the program waits for the cashier to enter in a payment given amount. So now:
Some details:Constants: Store the cost of each item as a constant. Also, store the tax rate (7%) as a constant. Input: Prompt the user to enter his/her name. Then use JOptionPane to input the quantity of each item. Finally use JOptionPane to input the amount of change given. Calculate and Output:
Use Math.round() to round all output to two decimal spots. If your input values were 2 brush sets ($8 each), 3 paint sets ($40 each), and 4 cans of primer ($18.75 each), and your cashier's name is Joni, your output should look like:
Welcome to ArtSupply. Cashier: Joni 2 brush sets at $ 8.0 3 paint sets at $ 40.0 4 primer at $ 18.75 Product Total: $211.0 Tax: $ 14.77 Total = $ 225.77 Money received: $ 250.0 Change due: $ 24.23 Change rounded up to nearest nickel: $ 24.25 Change given by: Joni
|
| |
---|---|---|
SolutionBefore you start writing code, you need to think about the problem in greater detail. Then you need to declare and initialize the variables you know you need. Finally you can program the problem in two parts: handling input, and obtaining output. Break the Problem DownWhile this problem is lengthier than others, the first rule of Computer Science is to break the problem into pieces. Ultimately we want the whole program done, but we need to break it into parts we can work on bit by bit. Start by reading the problem and figuring out what to do step by step. Keep your Code OrganizedThe COMP 1010 Coding Standards give the foundation required to keep your code organized for this case study. You need to make sure your code is properly indented, variable names are properly named (as in: double change, as opposed to: double a) and all variables are initialized to zero or empty string constants which are "". Also, comments should always be included in your code. Get it DoneSome programs will take longer than one sitting to complete. You must realize and accept that some programs can be done quickly, and other programs can take a very long time. No matter what you code, you will always have to deal with bugs. Start on your programs early enough and don't procrastinate. Leaving a program to the last minute — especially further on in the Computer Science degree — will cost you mentally and hurt your marks. Part Zero: Declare Variables and ConstantsBased on the problem description, we know we will need to use constants and use JOptionPane. JOptionPane requires that we store text in strings, so we must use strings. We're also dealing with integers and decimal value numbers which we will store as (doubles). You will need to have two string variables. You also need three integer variables, six double variables and four double constants. The string variables are for:
The integer variables are to store the quantity of items bought. You will need one integer variable for each of the following :
The double variables are for storing:
The double constant variables are for storing:
By the way to declare a constant variable you need to add the keyword final before the type. For example, if you wanted to declare a constant double called cost, you would write: final double cost; Part One: Handling InputNow that you've completed the first task of setting up variables (and remembering to initialize them all to either 0 or empty string constants which are ""), you need to program a sequence of prompts, calculations and some printing to the terminal. The following pseudocode gives you a good overview of what needs to be done. Pseudocode is a great tool to start writing a programming without getting bogged down in technical details. It is so easy that you can even start writing pseudocode while you are reading the program description. //Pseudocode: Print out "Welcome to ArtSupply" Prompt for cashier's name. Prompt for number of brush sets. Prompt for number of primers. Calculate product total. Print product total. Calculate tax. Print tax. Calculate total. Print total. Prompt for money received. Print the money received. Calculate change due. Print the change due. Calculate change rounded up to the nearest nickel. Print change rounded up to the nearest nickel. Print cashier's name.
System.out.println("Hi there"); To prompt for input, you need to use JOptionPane. Then you want to store the text entered in a string. This can be simply done roughly like this (you will need to fill in the parameters to the showInputDialog method): StringVariable = JOptionPane.showInputDialog(); Once you have the text stored, you want to print out the string value and move the cursor to a new line. You can do this by using the System.out.println("<text>") function. Continue on by prompting the user to enter in 3 numbers using JOptionPane. Each time the user enters a number you want to convert the entered value to an integer and then print the numbers. The following code illustrates how to convert the entered value to an integer: IntegerVariable = Integer.parseInt(StringVariable); To print the numbers, you use System.out.println(<variable name>) We can also assume that the user is entering a number and therefore we do not need to do any error checking. Note for extended knowledge: to error check this code, you can take the input text and store it into a string. Then you can use a piece of code called a try catch block along with a NumberFormatException. After the inputs are entered and properly stored, you need to do some math. Proceed by calculating the subtotal, tax and the total amount using simple math equations. After the values are calculated, print out the subtotal, the tax, the total amount. Make sure the output is in the same order as the output shown in the problem description. You are now about half way done this program! Part Two: OutputNow the cashier is waiting for the customer to pay. Assuming the customer is not a thief, they would hand the cashier an amount greater than or equal to the total amount owing. This means you do not need to error-check to make sure the customer gives the cashier a proper amount (meaning that the customer did not shortchange the cashier). As you already know, you need to code your program to allow for this transaction to occur. Note for extended knowledge: You can check to make sure that the amount of change you give to the cashier is greater than the total owing by using an if statement. Use JOptionPane to prompt for an amount given, and then convert the string returned to a double (no error checking is needed). The following code shows how to convert a string to a double: DoubleVariable = Double.parseDouble(StringVariable);
Your code is now complete! Post-programming InstructionsNow you may want to close your program, submit it and be happy that you're done! Unfortunately, reality doesn't work that way. You are only about half done with your program. Now you need to make sure your program works, and is up to standards. Below are a few steps you need to take when you feel you have finished your program. Look it overAfter your program is working, check for efficiency. This will come into play later on in the Computer Science degree, but you should practice doing it now. The next two sections are some methods to help you become more efficient. Fix the Errors in the CodeIf you have any bugs in your program, check them over and fix them! Optimize the Code by Removing Unnecessary VariablesAt the top of the main method, you should have a number of variables. If you have any more than what the above solution suggests, see if you can get rid of any extras you don't need. Try to Break your ProgramIn this question, breaking your code would be easy to do unless you implement the error-checking procedures. You could easily break your code by entering in a letter instead of a number and trying to convert that string into an integer or double. However, if you do implement the error checking procedures in this program, and for your future programs, the best way to make sure you have complete and fantastic code is if you cannot make your program explode no matter what you do. The number one reason individuals are penalized for their code is because a random situation arises, even though it's highly unlikely to happen (but they do), and the programmer has not added the necessary code to handle the exception. The Feeling of HopelessnessThere will be times that arise when you've gone too far down one path and your code is too far gone to fix properly. Understand that at times completely restarting the whole program over is a quicker and saner option than attempting to fix it properly. In other cases, backing up part way is more efficient. Make sure you save working versions of your program along the way. Lastly within this section, back up your code! If your computer dies and you lose your file, that's your responsibility! Parting StatementsRemember that this is not the only "correct" way to do this program. There can be many different ways to accomplish this program and it can be further developed to handle error-checking from any aspect. Remember to code efficiently, accurately, and properly. This means: no more excess code than needed, know what you're trying to code and break it down as needed to accomplish parts of it at a time rather than the whole thing in one sitting, and comment and indent your code appropriately. Always remember to test your program as much as you possibly can, and practice, practice, practice! The more you code, the better you will become at it. | ||
Code |