10 Replies - 15392 Views - Last Post: 03 November 2012 - 06:41 AM

#1 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2256
  • View blog
  • Posts: 9,444
  • Joined: 29-May 08

Challenge: Change Machine

Post icon  Posted 02 October 2012 - 06:11 AM

Back from the Hiatus. Bring on the challenge.

Challenge: Change Machine

Challenge:

Write a class that simulates the functionality of a change machine.

Requirements:-
  • A Constructor that accepts a list of coins.
    So you can setup the initial state of the machine. What denominations it accepts and initial quantities. etc.
  • A way to set the Unit Price.
    Edit: Unit Price -> The amount you have to pay, before goods are dispensed and change issued.
  • A way of inserting coins
  • A way to return the coins the user has inserted for the current transaction.
  • A way to dispense the change (once the unit price is met), made from the available coins in the machine.
    Excluding those the user has insert for the current transaction. After the transaction is completer those coins are available to be used for change.
  • Raise Events to reflect the internal state changes.


Submissions (preferably vb.net) must follow the traditional challenge standing orders.

This post has been edited by AdamSpeight2008: 02 October 2012 - 08:58 AM


Is This A Good Question/Topic? 1
  • +

Replies To: Challenge: Change Machine

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9094
  • View blog
  • Posts: 34,146
  • Joined: 12-June 08

Re: Challenge: Change Machine

Posted 02 October 2012 - 08:43 AM

Quote

A way to set the Unit Price.

What do you mean by this?
Was This Post Helpful? 0
  • +
  • -

#3 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2256
  • View blog
  • Posts: 9,444
  • Joined: 29-May 08

Re: Challenge: Change Machine

Posted 02 October 2012 - 08:51 AM

Unit Price -> The amount you have to pay, before goods are dispensed and change issued.
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9094
  • View blog
  • Posts: 34,146
  • Joined: 12-June 08

Re: Challenge: Change Machine

Posted 02 October 2012 - 08:58 AM

Ah... okay I thought this was just a change machine as in it took larger denominations and dispensed said denomination broken down into smaller change... not like a vending machine.

Well poop. That alters things.
Was This Post Helpful? 0
  • +
  • -

#5 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2256
  • View blog
  • Posts: 9,444
  • Joined: 29-May 08

Re: Challenge: Change Machine

Posted 02 October 2012 - 09:14 AM

Could be either, depends on how you configure the change mechanism algorithm.
Hence the events requirements.

Coin Dispenser
Set Unit Price to equal value of coin inserted.
Dispense coins

Change Giver
Set Unit Price to equal value of purchases
Insert coins
Dispense change.

Separation of functionality. Change Mechanism != Vending Machine.

This post has been edited by AdamSpeight2008: 02 October 2012 - 09:16 AM

Was This Post Helpful? 0
  • +
  • -

#6 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9094
  • View blog
  • Posts: 34,146
  • Joined: 12-June 08

Re: Challenge: Change Machine

Posted 02 October 2012 - 09:19 AM

Hrm... okay.
Was This Post Helpful? 0
  • +
  • -

#7 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 231
  • View blog
  • Posts: 765
  • Joined: 19-October 11

Re: Challenge: Change Machine

Posted 03 October 2012 - 12:56 AM

I'm submitting 75% done solution. I need to work on possible logical errors, and implement PurchaseGoods() method. I need to modify FindCoinsToBreak() so it will look first if it has sufficient coins, then modify state in containers (minor fix). Here and there I repeat some code, so I'll need to encapsulate it later, etc...It's spaghetti code, but will polish it. I'll continue with this in a day or two, because there's much work waiting for me ;) See you later.

Spoiler

This post has been edited by lucky3: 03 October 2012 - 12:57 AM

Was This Post Helpful? 0
  • +
  • -

#8 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2256
  • View blog
  • Posts: 9,444
  • Joined: 29-May 08

Re: Challenge: Change Machine

Posted 03 October 2012 - 07:29 AM

The change machine shouldn't really know anything about the goods,that is the job of the Vending Machine. It only really needs to know the total price.

The change machine should only handle the job of coin insertion and the producing of any change.

[Keypad]-->[Vending Machine]---{Sale Price Changed}-->[ Change Machine ]
 {Goods}<--                 <--{Coin Inserted}--------                  ---{Change}-->
                            ---{Refund Coins}-------->
                            <--{Price Met}------------   


Note: You may have use the expanded view of the code.

Partial view of my submission.
Spoiler

This post has been edited by AdamSpeight2008: 03 October 2012 - 07:42 AM

Was This Post Helpful? 0
  • +
  • -

#9 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 231
  • View blog
  • Posts: 765
  • Joined: 19-October 11

Re: Challenge: Change Machine

Posted 04 October 2012 - 08:39 AM

I have a strange problem with Double type variable, where I'm calculating sum of inserted/returned money. Although I'm inserting money with values in range 0.01 to 1000, I get sum of total with 500.25000000000001 and similar. Where the hell does those 000000000001 come from? Does this has something to do with threading and concurrency?

Here I have just some basic classes:
Spoiler

Was This Post Helpful? 0
  • +
  • -

#10 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: Challenge: Change Machine

Posted 24 October 2012 - 12:33 PM

View Postlucky3, on 04 October 2012 - 08:39 AM, said:

I have a strange problem with Double type variable...
I get sum of total with 500.25000000000001 and similar. Where the hell does those 000000000001 come from? Does this has something to do with threading and concurrency?

Probably comes from the normal behaviour of a double. Basically just rounding errors. Comparing two doubles is almost always problematic for that reason, and you are better off rounding to the number of decimal points, or using the decimal data type.
Was This Post Helpful? 1
  • +
  • -

#11 LilGhost  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 98
  • Joined: 12-October 12

Re: Challenge: Change Machine

Posted 03 November 2012 - 06:41 AM

View Postlucky3, on 04 October 2012 - 08:39 AM, said:

I have a strange problem with Double type variable, where I'm calculating sum of inserted/returned money. Although I'm inserting money with values in range 0.01 to 1000, I get sum of total with 500.25000000000001 and similar. Where the hell does those 000000000001 come from? Does this has something to do with threading and concurrency?

Here I have just some basic classes:
Spoiler

Set your precision to 2.
That should fix any issues.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1