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. 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
|
SideSectionTitleSideSection goes here. | |
---|---|---|
SolutionThere are some mental pre-steps you need to take before you start writing code. After, you need to set up the variables you know you need (part zero) and initialize them, then code the problem in two parts (part one and part two).
Break the problem downWhile this problem is a bit lengthier than others, the first rule of Computer Science is to break the program into pieces. Ultimately we want to get the whole program done, but we need to break it into parts we can work on and do it bit by bit. This means reading the problem and figuring out what you need to do logically step by step. Generally, in the problem description above, you will have to make up the "some details" section yourself as in future courses, it will not be provided. Declare your variablesBased on the problem description, we know we will need to use constants and use JOptionPane. JOptionPane means storing text in strings. We're also dealing with integers and decimal value numbers which are (doubles). Keep your code organizedThe COMP 1010 Coding Standards give the foundation required to complete the code organization task 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 get done. 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, starting outYou will need to have two string variables. You also need three integer variables, six double variables and four double constants. The String variables are to: The integer variables are to store the number of ______ bought: The double variables are to store: The double constant variables are to store: Part one, continue with some codeNow 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 ""), the next task you want to do is print out "Welcome to ArtSupply". You can do this using System.out.println(). Then you want to prompt the user to enter their name. To do this, you need to use JOptionPane. Then you want to store the text entered in a string. This can be simply done by: StringVariable = JOptionPane.showInputDialog After, 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. This can be done using 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 development: 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 these inputs are entered, 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 in order per line the subtotal, the tax, the total amount. You are now about half way done this program! Part twoNow 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. Being the programmer that you are, you know you need to code your program to allow for this transaction to occur. Again you do not need to error check to make sure the customer give the cashier a proper amount (meaning that the customer did not shortchange the cashier). Note for extended development: 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. Again use JOptionPane to prompt for an amount given, and then convert it to a double (no error checking again is needed, however once again you can use a try catch block for further development). To convert to a double, you can use: DoubleVariable = Double.parseDouble(StringVariable); Print out the money received, then subtract the total owed from the amount of change given. Then calculate any change the cashier needs to give back to the customer and print it out (even if it is $0.00). Finally round the amount of change given back to the nearest nickel and print that value. Your code is now complete! Final note for extended knowledge: If you get a value in your output that is longer than expected, ie: for calculating change, instead of $0.07 being printed, you get a number like: 0.069999999... there is no bug in your code causing this. You can fix it by multiplying your number by 100, rounding it, and then dividing it by 100. If you want more information on why extra numbers appear, please take COMP 2190 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. The other half is coders need to make sure their program works, and is done properly. 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 extra than what the above solution suggests, see if you can get rid of any extras you don't need. Try and 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 |