• Post Reply Bookmark Topic Watch Topic
  • New Topic

cash register program  RSS feed

 
Jesse Just
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello I have to write a program for class and I am having trouble with it the parameters are :

Create a program that simulates a standard Cash Register that meets the following requirements:

1) Stores separate sales tax rates for food and non-food items. The rate for food items will be 3% and the rate for non-food items will be 7%.
2) The program will prompt the user to select the tax type of the item (food or non-food) and then to enter the price of the items
3) The program will ask the user after each item whether the user wants to enter another item. If the user selects yes then the program should repeat prompting the user for item type and price.(See Requirement 2) If the user selects no then the program will stop prompting for item entry.
4) The program will then display the price for each item, the subtotal, calculate the two separate sales taxes and display each, and then display a total (all item prices and taxes).
5) The program will then prompt the user to enter the amount tendered by the customer and then display the amount of change owed the customer including a breaking down the change owed in dollars, quarters, dimes, nickels and pennies.

Hints

Create an Item class that is made up from an Item Type (food or non-food) and and Item Price. Then create an array of Item objects. Item Type could be represented by a Boolean variable called something like “isFoodItem” where true indicates it is a food item and false indicates it is a non-food item.


I am not to sure on how to call the items from the array individually and display them with the tax. I would like to create a method to do all the calculations and then print it out. Here is what I have so far in my program and the object class.





any help would be much appreciated!

 
K. Tsang
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

The hint provided by the assignment should point you to the right direction. I suspect your current "registerObjects" class corresponds to the Item class which is not close to what the requirements are looking. The tax thing should be in the Item class.

Step back a bit and setup the Item class then focus on the user input and calculation. Also you should consider using a List rather than an array to keep track of those items because you don't know how many items an user can buy. Setting an array size too small may run into program error and too big just waster memory.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesse Just wrote:I am not to sure on how to call the items from the array individually and display them with the tax.

Then why have you written any code at all? Programming isn't (or shouldn't be) an existential process, it's a refinement one; and in order to make it so, you need to understand the problem before you start writing code. There's nothing wrong with writing a program in small pieces (in fact, it's a very good idea), but you need to write a piece AND test it completely before you continue to the next one.

My advice: StopCoding (←click). And read the advice in that page thoroughly.

Tip #1: Rename your current class CashRegister. Class names should always start with capital letters.

Tip #2: Remove ALL your code from your main() method. Here's a simple way to do it:

1. Rename your main() method to run() and remove the static qualifier, viz:
public void run() { ...
(Note: no static, and no parameters).
and for now just leave the code that you've already written inside it.

2. Create a new public class in a new file called CashRegisterTest, and give it a main() method, which should look exactly as follows:Now you have a properly written Java class with a test driver; and this is how all your exercise classes should start out life.
It may look like we've just moved the code from main() to a method called run(), but we've actually done something much more important:
Each CashRegister is now an object.

Tip #3: Initially write your CashRegister class (or rather, its run() method) to handle ONE item - and ONLY one. Then add another...then another...and when you're on your 3rd or 4th, then start thinking about how you can turn that into a loop than handles items until you tell it to stop.

Tip #4: Break up your run() logic into discrete functions (or, as in the case of an Item, a class), and write those functions as methods in your CashRegister class, which you then call from your run() method.

HIH

Winston
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesse Just wrote:hello I have to write a program...

I hope you don't mind, but I've used your post as an illustration for a new page I've written called MainIsAPain that covers the methodology in my last post - specifically, the exercise of writing a CashRegister class - don't worry, no names are mentioned .

If you don't like it, I'll change it to something else; but it seemed like a good example of an exercise someone might be given.

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!