Apartment Water Bill
Problem
Design ApartmentWaterBill for one apartment and one fixed 30-day month. The apartment receives ordinary water from corporation and borewell sources in a fixed ratio. Guests add demand that must be supplied by progressively priced tanker water.
Requirements
- Select the base allowance. A
2BHKapartment has 900 base litres for the month, and a3BHKapartment has 1,500 base litres. - Split base water by the exact ratio. If the ratio is
corporationPart:borewellPart, corporation litres arebaseLitres * corporationPart / (corporationPart + borewellPart), and borewell litres are the corresponding borewell share. Keep both shares exact and do not round them. - Accumulate guests. The guest count starts at zero. Each
addGuests(count)call increases the same month's count bycount. - Supply guest demand from tankers. Every guest adds exactly 300 litres, all supplied as tanker water. Guest additions do not change the corporation or borewell quantities.
- Price contracted sources. Corporation water costs 1 charge unit per litre. Borewell water costs 1.5 charge units per litre. Apply each rate to its exact share.
- Price tanker water progressively. Charge the first 500 tanker litres at 2 per litre, the next 1,000 at 3 per litre, the next 1,500 at 5 per litre, and every litre above 3,000 at 8 per litre. Each rate applies only to litres inside its band.
- Calculate total litres.
getBill()returnstotalLitresequal to the base litres plus 300 times the accumulated guest count. - Aggregate the raw charge exactly. Add the exact corporation, borewell, and tanker charges. Do not round any source quantity or line-item charge before this sum.
- Round only the final charge.
getBill()returnstotalChargeby rounding the raw aggregate charge to the nearest integer. An exact half rounds upward. - Accept positive guest additions.
addGuests(count)returnstruefor a positive count and adds it to the cumulative guest count. - Reject nonpositive guest additions.
addGuests(count)returnsfalsewhencountis zero or negative. - Keep rejected additions atomic. A rejected
addGuestscall does not change cumulative guests. A latergetBill()returns the same result it would have returned if the rejected call had not occurred.
API
| Signature | Returns | Behavior |
|---|---|---|
ApartmentWaterBill(apartmentType: string, corporationPart: integer, borewellPart: integer) | Not applicable | Creates the apartment-month with zero guests. |
addGuests(count: integer) | boolean | Returns true and adds a positive count. Returns false without changing state for zero or a negative count. |
getBill() | BillResult | Returns an object with exactly totalLitres: integer and totalCharge: integer. |
Examples
For ApartmentWaterBill("2BHK", 3, 7):
| Step | Operation | Result |
|---|---|---|
| 1 | addGuests(2) | true |
| 2 | addGuests(3) | true |
| 3 | getBill() | {"totalLitres":2400,"totalCharge":5215} |
The 900 base litres split into 270 corporation litres and 630 borewell litres, costing 270 and 945. Five guests add 1,500 tanker litres, costing 500 * 2 + 1,000 * 3 = 4,000. The total is 2,400 litres and 5,215 charge units.
Constraints
apartmentTypeis exactly2BHKor3BHK.1 <= corporationPart <= 1,000,0001 <= borewellPart <= 1,000,000- Each
addGuestscall satisfies-1,000,000 <= count <= 1,000,000. - At most 100,000 public method calls occur per testcase.
- The cumulative guest count is at most 1,000,000,000.
- All exact intermediate numerators fit in a signed 64-bit integer.
- Only inputs satisfying these constraints are supplied and judged.
Notes
Calls are sequential on one object. Meter readings, corrections, tariff changes, statements, payment, month reset, invalid constructor behavior, out-of-range guest counts, persistence, and concurrent calls are outside scope.