Problem overview
You build a VendingMachine class for a machine that sells one kind of item at a fixed price of 150. It remembers the money inserted so far, called the credit. Three methods work on it:
Insert(double amount)adds to the credit. It prints and returns nothing.Select()tries to buy one item and returns astring. If the credit is less than the price, it returns"Need N more", where N is the missing amount, and the credit stays as it was. Otherwise it subtracts the price from the credit and returns"Dispensing!". Exact payment works, and extra money stays in the credit.Refund()returns the whole credit and sets it back to 0.
The driver inserts 50, so Select() says Need 100 more. It inserts 150 more, making 200, so the next Select() dispenses and 50 remains, which Refund() returns. Then 150 is an exact payment: it dispenses and the refund is 0. Finally 100 is short by 50 and the refund is 100.
Approach: state plus actions
A class bundles data with the actions that change it. Here the data is two private fields, credit starting at 0 and price set to 150. Every machine made with new VendingMachine() has its own credit, and no constructor is needed because the field initialisers give each new object its starting values.
The one subtle point is when the credit changes. A failed purchase only reports what is missing and must leave the credit alone. The price is subtracted only after the check passes. If you subtract it on every call, the leftover credit is wrong and the refund is too big.
Step by step
- Declare the class
VendingMachine. Each object made from it keeps its own credit. - Add
private double credit = 0;, the money inserted so far. - Add
private double price = 150;, the cost of every item. - Declare
Insertas a publicvoidmethod takingdouble amount. - Add the amount with
credit += amount;. This adds to what is already there instead of replacing it. - Declare
Selectas a public method with no parameters, returningstring. - Handle too little money. If
credit < price, return"Need " + (price - credit) + " more"on one line. Joining a number to a string with+turns the number into text. The parentheses make the subtraction happen first. - Subtract the price from the credit. This line is reached only when the credit covered the price.
- Return
"Dispensing!". - Declare
Refundas a public method returningdouble. - Save the credit in a local
double r. Once the credit is reset its old value is gone. - Set
creditback to 0. - Return
r, notcredit, which is already 0.
Following the credit
The credit changes only in Insert and in a successful Select. For the start of the driver, it goes like this:
Insert 50 credit 50
Select Need 100 more (credit still 50)
Insert 150 credit 200
Select Dispensing! (credit now 50)
Refund returns 50
The failed Select left the credit at 50, so the following insert made 200. That is the reason the subtraction sits below the guard.
The solution in C#
class VendingMachine {
private double credit = 0;
private double price = 150;
public void Insert(double amount) {
credit += amount;
}
public string Select() {
if (credit < price) return "Need " + (price - credit) + " more";
credit -= price;
return "Dispensing!";
}
public double Refund() {
double r = credit;
credit = 0;
return r;
}
}
class Solution {
public void MainFunction() {
var m = new VendingMachine();
m.Insert(50);
Console.WriteLine(m.Select());
m.Insert(150);
Console.WriteLine(m.Select());
Console.WriteLine("Refunded: " + m.Refund());
m.Insert(150);
Console.WriteLine(m.Select());
Console.WriteLine("Refunded: " + m.Refund());
m.Insert(100);
Console.WriteLine(m.Select());
Console.WriteLine("Refunded: " + m.Refund());
}
}
The driver prints:
Need 100 more
Dispensing!
Refunded: 50
Dispensing!
Refunded: 0
Need 50 more
Refunded: 100
C# details
- Field initialisers.
private double credit = 0;runs for each new object before any method can be called. Adoublefield would default to 0 anyway, but writing it makes the intent clear. - Numbers become text with
+. In"Need " + (price - credit) + " more", thedouble100 prints as100, with no decimal point. A fractional amount keeps its fraction: with 100.5 inserted,Select()returnsNeed 49.5 more. credit -= price;is shorthand forcredit = credit - price;.- The return type matters.
Selectreturns astring, so the driver can print the result directly withConsole.WriteLine.
Why a class and not loose variables
The credit has to survive between calls: Insert runs, then later Select runs and must see the same money. A local variable inside a method disappears when the method returns, but a field lives as long as the object. Two machines created with new VendingMachine() do not share credit, because each object has its own copy of every field.
Edge cases and common mistakes
- Exact payment. The test is
credit < price, so a credit of exactly 150 goes on to dispense. Using<=would refuse exact payment. - Subtracting before the check. The credit would go negative on a failed purchase, and the refund would be wrong.
- Returning
creditafter resetting it.Refundwould always return 0. Save first, reset, then return the saved value. - Refunding twice. The second
Refund()in a row returns 0, because the first one emptied the credit. - Several purchases from one credit. After inserting 300, the first two
Select()calls returnDispensing!, the third returnsNeed 150 more, andRefund()gives 0.
Testing
- Insert 100.5 and call
Select():Need 49.5 more. ThenRefund()returns100.5, since the failed purchase changed nothing. - Insert 300 and call
Select()three times:Dispensing!,Dispensing!,Need 150 more.Refund()then returns0. - A fresh machine's
Refund()returns0.
