Difference between revisions of "Cash Receipt"

From CompSciWiki
Jump to: navigation, search
Line 115: Line 115:
  
 
Your code is now complete!
 
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 [https://aurora.umanitoba.ca/banprod/bwckctlg.p_disp_course_detail?cat_term_in=201090&subj_code_in=COMP&crse_numb_in=2190 Comp 2190]
  
 
==Look it over==
 
==Look it over==
Line 124: Line 126:
 
==Optimize the Code by Removing Unnecessary Variables==
 
==Optimize the Code by Removing Unnecessary Variables==
 
At the top of [[Your First Java Program#The Main Method|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.
 
At the top of [[Your First Java Program#The Main Method|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 program==
 +
In this question, breaking your code would be easy to do as 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 a 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 can not 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.
  
 
==Parting statements==
 
==Parting statements==
  
Remember 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 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.
  
 
|SolutionCode=
 
|SolutionCode=

Revision as of 12:01, 31 March 2011

Back to the Case Studies homepage

Problem

Today your manager wants you to write a complete Java program that can be used by a cashier to generate customer receipts for her convenience store which sells only three items.

First: The three items are brush sets, paint sets and primer. Use constants to store the cost of each of the three items and the tax rate.

Then, since the program is going to be used by multiple cashiers, use JOptionPane to prompt the user to enter:
1) the cashier’s name
2) the quantity of each item.

The program will calculate and output:
1) the subtotal
2) tax
3) total.

After the program calculates and prints out the receipt up to the total amount owed.
1) prompt the cashier to enter a payment amount,
2) calculate any change needed to be given back to the customer and
3) round the change given to the nearest nickel.

Calculate and output:
1) The change due (to the nearest penny)
2) The change due (to the nearest nickel)
3) the name of the cashier.

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:
1) the subtotal (the cost of the items without tax)
2) tax
3) total (of product plus taxes).
Also output the name of your store at the top of the receipt. When finding the change, use Math.ceil to round up so the customer gets the full change, and possibly more. For example, if the change due is 4.637, round to 4.64 using pennies and 4.65 using nickels.
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), your output should look like:

Welcome to ArtSupply.

Cashier: Sarah

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


Your employer wants you to use proper programming practices which happen to be exactly the same as the standards in Comp 1010.

 

SideSectionTitle

SideSection goes here.

Solution

There are some mental pre-steps you need to take before you start writing code. After, you need to set up your variables you know you need (part zero), then code the problem in two more parts (part one and part two).


Break the problem down

While this problem is a bit more lengthy 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.

Declare your variables

Based 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 (doubles).

Keep your code organized

The 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 and initialized to zero or empty string constants which are "", and comments are included in your code.

Get it done

Some programs will take longer than one sitting to get done. You must realize and accept that some programs can be done quickly, and some take a very long time, and that no matter what you code, you will always have to deal with bugs. Start on the programs early enough and don't procrastinate.

Part zero, starting out

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 to:
Handle all input strings
Store the cashier's name

The integer variables are to store the number of ______ bought:
Brushes
Paint sets
Primer

The double variables are to store:
The tax
The sub total of the bill
The total of the bill (with tax)
The total payment given
The total change returned
The total change given to the nearest nickel

The double constant variables are to store: The tax rate
The cost of the brushes
The cost of the paint
The cost of the primer

Part one, continue with some code

Now 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 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 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. This can be done using

IntegerVariable = Integer.parseInt(StringVariable);

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.

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 Cashier's name, the number of brushes bought, the number of paint cans bought, the number of primer bought, the subtotal, the tax, the total amount.

Part two

Now the cashier is waiting for you to pay. Assuming you're not a thief, you will hand the cashier an amount greater than or equal to the total amount owing. Again you do not need to error check to make sure you give the cashier a proper amount.

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 recieved, 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

Look it over

After your program is working, check for efficiency. This will come into play later on in the degree, but you should practice doing it now. Below are some methods to help you become more efficient.

Fix the Errors in the Code

If you have any bugs in your program, check them over and fix them!

Optimize the Code by Removing Unnecessary Variables

At 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 program

In this question, breaking your code would be easy to do as 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 a 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 can not 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.

Parting statements

Remember 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

Solution Code

Back to the Case Studies homepage