Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: CashRegister class- adding a method getItemCount

  1. #1
    Junior Member
    Join Date
    Jan 2010
    Location
    norcross, ga
    Posts
    24
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Question CashRegister class- adding a method getItemCount

    hello everyone,

    new person here. i'm a college student working on my programming homework. i have a class CashRegister that reads the payment in and calculates change due. i have to add a new method to it called getItemCount() that keeps track of the total number of items in a sale. it returns the number of items in the current purchase. remember to reset the count at the end of the purchase. how do i do this?

    public class CashRegister
    {
    /**
    * Constructs a cash register with no money in it
    * */
    public CashRegister()
    {
    purchase = 0;
    payment = 0;
    }

    public int getItemCount()
    {

    /**
    * Records the purchase price of an item.
    * @param amount the price of a purchased item
    * */
    public void recordPurchase(double amount)
    {
    purchase = purchase + amount;
    }

    /**
    * Enters the amount in bills received from the customer
    * */
    public void enterDollars(int dollars)
    {
    payment = dollars;
    }

    /*
    * the following methods take the number of each coins given, and multiply them by
    * their constant value and add that to the total payment received
    * */
    public void enterQuarters(int quarters)
    {
    payment = payment + quarters * QUARTER_VALUE;
    }

    public void enterDimes(int dimes)
    {
    payment = payment + dimes * DIME_VALUE;
    }

    public void enterNickels(int nickels)
    {
    payment = payment + nickels * NICKEL_VALUE;
    }

    public void enterPennies(int pennies)
    {
    payment = payment + pennies * PENNY_VALUE;
    }

    /*
    * this method supplies the total payment received and subtracts the purchase price.
    * change is then computed.
    * */
    public double giveChange()
    {
    double change = payment - purchase;
    purchase = 0;
    payment = 0;
    return change;
    }

    public static final double QUARTER_VALUE = .25;
    public static final double DIME_VALUE = .1;
    public static final double NICKEL_VALUE = .05;
    public static final double PENNY_VALUE = .01;

    private double purchase;
    private double payment;
    }


  2. #2
    Member
    Join Date
    Aug 2009
    Posts
    53
    Thanks
    2
    Thanked 3 Times in 2 Posts

    Default Re: CashRegister class- adding a method getItemCount

    You could introduce a new field called count. Adding an item to the items purchased increments the variable, while bying sets it to 0.

  3. #3
    Junior Member
    Join Date
    Jan 2010
    Posts
    7
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: CashRegister class- adding a method getItemCount

    Well, for me I would look at the process as a transaction. I would have a start() method that sets up everything... store each purchase amount in an array so you can easily get a count and also return a list of items like a receipt. I would also create getters for the amount paid in and the amount due (total). the giveChange() would probably be the last method you call - but I would let the start method put everything back to zero in case you want to handle the fact the amount paid in is less than the amount due and giveChange() would return less than 0. If you set everything to zero you can't add another dollar and make it "right" like a real cash register.

    I suspect your professor will eventually ask you to advance this class to do such things....

    Hope this helps.
    ---------------------------------------------
    Mike

Similar Threads

  1. Method Adding elements to an array with certain restrictions
    By Newoor in forum Collections and Generics
    Replies: 1
    Last Post: December 13th, 2009, 10:13 AM
  2. Calling a void method into a static void main within same class
    By sketch_flygirl in forum Object Oriented Programming
    Replies: 3
    Last Post: November 15th, 2009, 04:24 PM
  3. (.readLine() method) of BufferedReader class
    By chronoz13 in forum File I/O & Other I/O Streams
    Replies: 7
    Last Post: October 13th, 2009, 06:59 PM
  4. adding main method to a code
    By IDK12 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: August 31st, 2009, 08:52 PM
  5. adding get mothods to a class extending thread
    By aliaa2a in forum Object Oriented Programming
    Replies: 6
    Last Post: August 3rd, 2009, 06:41 AM