Java Coding Problme 2

Coding Problem

The Regency International Hotel has two categories of rooms

-       Economy rooms go for Rs. 3500/- a day
-       Ocean view rooms go for Rs 5000/- a day.

In addition, it has a bar in which it sells Water, Soda, Beer and Wine.

Tax is calculated as follows
-       Sales tax is levied at 8% for all rooms.
-       For rooms whose daily rent is greater than Rs 4000, a luxury tax of 5% is charged. This tax is in addition to normal sales tax. Tax is based on total room rent only.
-       No sales tax on water. Water sells for Rs 10 a bottle.
-       Soda (Rs 10), domestic beer (Rs 50) and wine (Rs 80) incur normal sales tax at 8%
-       Imported beers (Rs 180) and wine (Rs 500)  incur additional import tax  of 10%

Write an application that generates a bill for a guest.

For a input such as
2 days in Economy Room
1 Water, 3 Imported beers and 1 domestic wine

the bill should contain
The Regency International Hotel
We appreciate your business

2 days in Economy room @ Rs3500 p.d = 7000
1 Water = 10
3 Imported beers @ Rs 180 each = 540
1 domestic wine @ Rs 80 = 80
Total Base Charges = 7630

Sales Tax
Room 8% of 7000 = 560
Beer 8% of 540 = 43.20
Wine 8% of 80 = 6.40
Import Tax
Beer 10% of 540 = 54

Total Tax =  663.60

Amount Due = 8293.60

The bill may be displayed on the console (no print support is required)
Logic to compute should be decoupled from input data so that it should be easy to change inputs independent of computing logic.

Solution:

package com.techdive.beans;

public class Beer {

        private float quantity;

        private BeerType beerType;

        public Beer(float importedBeersCount, BeerType beerType) {
                super();
                this.quantity = importedBeersCount;
                this.beerType = beerType;
        }

        public float getAmount() {
                return beerType.getUnitPrice() * quantity;
        }

        public float getSalesTax() {
                float amount = getAmount();

                return (amount * TaxType.SALESTAX.getPercentage()) / 100;
        }

        public float getImportedTax() {
                float amount = getAmount();
                if (beerType == BeerType.IMPORTED)
                        return (amount * TaxType.IMPORT_DUTY_TAX.getPercentage()) / 100;
                else
                        return 0;
        }
}
_________________________________________________________________________

package com.techdive.beans;

public enum BeerType {

        DOMESTIC(50), IMPORTED(180);

        float unitPrice;

        private BeerType(float price) {
                this.unitPrice = price;
        }

        public float getUnitPrice() {
                return unitPrice;
        }

}
_________________________________________________________________________________

package com.techdive.beans;

public class GuestDetails {

        private HotelRoomType hotelRoomType;

        private int daysStayed;

        private int waterBottleCount;

        private int SodaCount;

        private int domesticBeersCount;

        private int domesticWineCount;

        private int importedBeersCount;

        private int importedWineCount;

        public GuestDetails() {
                super();
                // TODO Auto-generated constructor stub
        }

        public GuestDetails(HotelRoomType hotel, int daysStayed,
                        int waterBottleCount, int sodaCount, int domesticBeersCount,
                        int domesticWineCount, int importedBeersCount, int importedWineCount) {
                super();
                this.hotelRoomType = hotel;
                this.daysStayed = daysStayed;
                this.waterBottleCount = waterBottleCount;
                SodaCount = sodaCount;
                this.domesticBeersCount = domesticBeersCount;
                this.domesticWineCount = domesticWineCount;
                this.importedBeersCount = importedBeersCount;
                this.importedWineCount = importedWineCount;
        }

        /**
         * @return the hotelRoomType
         */

        public HotelRoomType getHotelRoomType() {
                return hotelRoomType;
        }

        /**
         * @param hotelRoomType
         *            the hotelRoomType to set
         */

        public void setHotelRoomType(HotelRoomType hotelRoomType) {
                this.hotelRoomType = hotelRoomType;
        }

        /**
         * @return the daysStayed
         */

        public int getDaysStayed() {
                return daysStayed;
        }

        /**
         * @param daysStayed
         *            the daysStayed to set
         */

        public void setDaysStayed(int daysStayed) {
                this.daysStayed = daysStayed;
        }

        /**
         * @return the waterBottleCount
         */

        public int getWaterBottleCount() {
                return waterBottleCount;
        }

        /**
         * @param waterBottleCount
         *            the waterBottleCount to set
         */

        public void setWaterBottleCount(int waterBottleCount) {
                this.waterBottleCount = waterBottleCount;
        }

        /**
         * @return the sodaCount
         */

        public int getSodaCount() {
                return SodaCount;
        }

        /**
         * @param sodaCount
         *            the sodaCount to set
         */

        public void setSodaCount(int sodaCount) {
                SodaCount = sodaCount;
        }

        /**
         * @return the domesticBeersCount
         */

        public int getDomesticBeersCount() {
                return domesticBeersCount;
        }

        /**
         * @param domesticBeersCount
         *            the domesticBeersCount to set
         */

        public void setDomesticBeersCount(int domesticBeersCount) {
                this.domesticBeersCount = domesticBeersCount;
        }

        /**
         * @return the domesticWineCount
         */

        public int getDomesticWineCount() {
                return domesticWineCount;
        }

        /**
         * @param domesticWineCount
         *            the domesticWineCount to set
         */

        public void setDomesticWineCount(int domesticWineCount) {
                this.domesticWineCount = domesticWineCount;
        }

        /**
         * @return the importedBeersCount
         */

        public int getImportedBeersCount() {
                return importedBeersCount;
        }

        /**
         * @param importedBeersCount
         *            the importedBeersCount to set
         */

        public void setImportedBeersCount(int importedBeersCount) {
                this.importedBeersCount = importedBeersCount;
        }

        /**
         * @return the importedWineCount
         */

        public int getImportedWineCount() {
                return importedWineCount;
        }

        /**
         * @param importedWineCount
         *            the importedWineCount to set
         */

        public void setImportedWineCount(int importedWineCount) {
                this.importedWineCount = importedWineCount;
        }

}
_________________________________________________________________________

package com.techdive.beans;

public class Hotel {

        private String name = "The Regency International Hotel";

        private final float LUXURY_TAX_MIN_LIMIT = 4000;

        private HotelRoomType hotelRoomType;

        private float daysStayed;

        private float rent;

        private double waterBottleCost = 10;

        private double sodaCost = 10;

        private double domesticBearCost = 50;

        private double domesticWineCost = 80;

        private double importedBearCost = 180;

        private double importedWineCost = 500;

        public Hotel(String name, HotelRoomType hotelType, float daysStayed) {
                super();
                this.name = name;
                this.hotelRoomType = hotelType;
                this.daysStayed = daysStayed;
        }

        public float getTotalRoomRent() {
                return hotelRoomType.getRoomRent() * daysStayed;
        }

        public float getSaleSTax() {
                return (getTotalRoomRent() * TaxType.SALESTAX.getPercentage()) / 100;

        }

        public float getLuxuryTax() {
                if (hotelRoomType.getRoomRent() > LUXURY_TAX_MIN_LIMIT)
                        return (getTotalRoomRent() * TaxType.LUXURYTAX.getPercentage()) / 100;
                else
                        return 0;

        }

        /**
         * @return the name
         */

        public String getName() {
                return name;
        }

        /**
         * @param name
         *            the name to set
         */

        public void setName(String name) {
                this.name = name;
        }

        /**
         * @return the hotelType
         */

        public HotelRoomType getHotelType() {
                return hotelRoomType;
        }

        /**
         * @param hotelType
         *            the hotelType to set
         */

        public void setHotelType(HotelRoomType hotelType) {
                this.hotelRoomType = hotelType;
        }

        /**
         * @return the rent
         */

        public float getRent() {
                return rent;
        }

        /**
         * @param rent
         *            the rent to set
         */

        public void setRent(float rent) {
                this.rent = rent;
        }

        /**
         * @return the waterBottleCost
         */

        public double getWaterBottleCost() {
                return waterBottleCost;
        }

        /**
         * @return the sodaCost
         */

        public double getSodaCost() {
                return sodaCost;
        }

        /**
         * @return the domesticBearCost
         */

        public double getDomesticBearCost() {
                return domesticBearCost;
        }

        /**
         * @return the domesticWineCost
         */

        public double getDomesticWineCost() {
                return domesticWineCost;
        }

        /**
         * @return the importedBearCost
         */

        public double getImportedBearCost() {
                return importedBearCost;
        }

        /**
         * @return the importedWineCost
         */

        public double getImportedWineCost() {
                return importedWineCost;
        }

}
_________________________________________________________________________

package com.techdive.beans;

public enum HotelRoomType {

        ECONOMY(3500), LUXURY(5000);

        float roomRent;

        private HotelRoomType(float roomRent) {
                this.roomRent = roomRent;
        }

        public float getRoomRent() {
                return roomRent;
        }

}
_________________________________________________________________________________

package com.techdive.beans;

public enum HotelRoomType {

        ECONOMY(3500), LUXURY(5000);

        float roomRent;

        private HotelRoomType(float roomRent) {
                this.roomRent = roomRent;
        }

        public float getRoomRent() {
                return roomRent;
        }

}
_________________________________________________________________________________

package com.techdive.beans;

public enum TaxType {

        SALESTAX(8), LUXURYTAX(5), IMPORT_DUTY_TAX(10);

        float percentage;

        private TaxType(float percentage) {
                this.percentage = percentage;
        }

        public float getPercentage() {
                return percentage;
        }

}

_________________________________________________________________________________

package com.techdive.beans;

public class WaterBottle {

        private final float price = 10;

        private float quantity;

        public WaterBottle(float waterBottleCount) {
                super();
                this.quantity = waterBottleCount;
        }

        /**
         * @return the quantity
         */

        public float getQuantity() {
                return quantity;
        }

        /**
         * @param quantity
         *            the quantity to set
         */

        public void setQuantity(int quantity) {
                this.quantity = quantity;
        }

        /**
         * @return the price
         */

        public float getPrice() {
                return price;
        }

        public float getAmount() {
                return price * quantity;
        }

        public float getTax() {
                return 0;
        }

        public void printDetails() {

        }
}

_________________________________________________________________________

package com.techdive.beans;

public class Wine {

        private float quantity;

        private WineType wineType;

        public Wine(float importedWinecount, WineType wineType) {
                super();
                this.quantity = importedWinecount;
                this.wineType = wineType;
        }

        public float getAmount() {
                return wineType.getUnitPrice() * quantity;
        }

        public float getSalesTax() {
                float amount = getAmount();

                return (amount * TaxType.SALESTAX.getPercentage()) / 100;
        }

        public float getImportedTax() {
                float amount = getAmount();
                if (wineType == WineType.IMPORTED)
                        return (amount * TaxType.IMPORT_DUTY_TAX.getPercentage()) / 100;
                else
                        return 0;
        }
}
_________________________________________________________________________________

package com.techdive.beans;

public enum WineType {

        DOMESTIC(80), IMPORTED(500);

        float unitPrice;

        private WineType(float price) {
                this.unitPrice = price;
        }

        public float getUnitPrice() {
                return unitPrice;
        }

}

package com.techdive.serivce;

import com.techdive.beans.GuestDetails;

public interface BillGenerator {

        void generateBill(GuestDetails guestDetails);
}
_________________________________________________________________________

package com.techdive.serivce;

import com.techdive.beans.Beer;
import com.techdive.beans.BeerType;
import com.techdive.beans.GuestDetails;
import com.techdive.beans.Hotel;
import com.techdive.beans.HotelRoomType;
import com.techdive.beans.Soda;
import com.techdive.beans.TaxType;
import com.techdive.beans.WaterBottle;
import com.techdive.beans.Wine;
import com.techdive.beans.WineType;

public class HotelBillGeneratorImpl implements BillGenerator {

        @Override
        public void generateBill(GuestDetails guestDetails) {
                // TODO Auto-generated method stub

                float daysStayed = guestDetails.getDaysStayed();
                HotelRoomType hotelRoomType = guestDetails.getHotelRoomType();

                Hotel hotel = new Hotel("", hotelRoomType, daysStayed);

                int waterBottleCount = guestDetails.getWaterBottleCount();

                WaterBottle waterBottel = new WaterBottle(waterBottleCount);

                int sodaCount = guestDetails.getSodaCount();

                Soda soda = new Soda(sodaCount);

                int importedBeersCount = guestDetails.getImportedBeersCount();

                Beer importedBeer = new Beer(importedBeersCount, BeerType.IMPORTED);

                int importedWinecount = guestDetails.getImportedWineCount();

                Wine importedWine = new Wine(importedWinecount, WineType.IMPORTED);

                int domesticBeerCount = guestDetails.getDomesticBeersCount();

                Beer domesticBeer = new Beer(domesticBeerCount, BeerType.DOMESTIC);

                int domesticWineCount = guestDetails.getDomesticWineCount();

                Wine domesticWine = new Wine(domesticWineCount, WineType.DOMESTIC);

                System.out.format("%.0f day(s) in %s room @ Rs%.2f p.d = %.2f%n",
                                daysStayed, hotelRoomType, hotelRoomType.getRoomRent(),
                                hotel.getTotalRoomRent());

                System.out.format("%d water = %.2f%n", waterBottleCount,
                                waterBottel.getAmount());

                System.out.format("%d soda = %.2f%n", sodaCount, soda.getAmount());

                if (importedBeersCount > 0)
                        System.out.format("%d imported beers @ Rs %.2f each = %.2f%n",
                                        importedBeersCount, BeerType.IMPORTED.getUnitPrice(),
                                        importedBeer.getAmount());

                if (importedWinecount > 0)
                        System.out.format("%d imported wine @ Rs %.2f each = %.2f%n",
                                        importedWinecount, WineType.IMPORTED.getUnitPrice(),
                                        importedWine.getAmount());

                if (domesticBeerCount > 0)
                        System.out.format("%d domestic beers @ Rs %.2f each = %.2f%n",
                                        domesticBeerCount, BeerType.DOMESTIC.getUnitPrice(),
                                        domesticBeer.getAmount());

                if (domesticWineCount > 0)
                        System.out.format("%d domestic wine @ Rs %.2f each = %.2f%n",
                                        domesticWineCount, WineType.DOMESTIC.getUnitPrice(),
                                        domesticWine.getAmount());

                float totalBaseCharge = hotel.getTotalRoomRent()
                                + waterBottel.getAmount() + soda.getAmount()
                                + importedBeer.getAmount() + importedWine.getAmount()
                                + domesticBeer.getAmount() + domesticWine.getAmount();

                System.out.format("total base charges = %.2f%n", totalBaseCharge);

                System.out.println("Sales Tax");
                float saleTax = TaxType.SALESTAX.getPercentage();
                float importTax = TaxType.IMPORT_DUTY_TAX.getPercentage();
                System.out.format("Room %.2f %% of %.2f = %.2f%n", saleTax,
                                hotel.getTotalRoomRent(), hotel.getSaleSTax());

                if (sodaCount > 0)
                        System.out.format("Soda %.2f%% of %.2f = %.2f%n", saleTax,
                                        soda.getAmount(), soda.getTax());

                if (domesticBeerCount > 0)
                        System.out.format("Beer %.2f%% of %.2f = %.2f%n", saleTax,
                                        domesticBeer.getAmount(), domesticBeer.getSalesTax());

                if (importedBeersCount > 0)
                        System.out.format("Beer %.2f%% of %.2f = %.2f%n", saleTax,
                                        importedBeer.getAmount(), importedBeer.getSalesTax());

                if (domesticWineCount > 0)
                        System.out.format("Wine %.2f%% of %.2f = %.2f%n", saleTax,
                                        domesticWine.getAmount(), domesticWine.getSalesTax());

                if (importedWinecount > 0)
                        System.out.format("Wine %.2f%% of %.2f=%.2f%n", saleTax,
                                        importedWine.getAmount(), importedWine.getSalesTax());

                float totalSalesTax = hotel.getSaleSTax() + domesticBeer.getSalesTax()
                                + importedBeer.getSalesTax() + soda.getTax()
                                + domesticWine.getSalesTax() + importedWine.getSalesTax();

                System.out.println("Import Tax");

                System.out.format("Beer %.2f%% of %.2f = %.2f%n", importTax,
                                importedBeer.getAmount(), importedBeer.getImportedTax());

                System.out.format("Wine %.2f%% of %.2f = %.2f%n", importTax,
                                importedWine.getAmount(), importedWine.getImportedTax());

                float totalImportTax = importedBeer.getImportedTax()
                                + importedWine.getImportedTax();

                float totalLuxuryTax = 0;
                if (hotel.getLuxuryTax() > 0) {
                        System.out.println("Luxury Tax");
                        System.out.format("Room %.2f %% of %.2f = %.2f%n", saleTax,
                                        hotel.getTotalRoomRent(), hotel.getLuxuryTax());
                        totalLuxuryTax = hotel.getLuxuryTax();
                }
                System.out.format("Total Tax = %.2f%n", totalSalesTax + totalImportTax
                                + totalLuxuryTax);

                System.out.format("Amount Due = %.2f%n", totalBaseCharge
                                + totalSalesTax + totalImportTax);
        }

}

BillGenTest.java

package com.techdive.main;

import com.techdive.beans.GuestDetails;
import com.techdive.beans.HotelRoomType;
import com.techdive.serivce.BillGenerator;
import com.techdive.serivce.HotelBillGeneratorImpl;

/**
 * @author Arun
 *
 * To Test the Bill Generator Service
 */

public class BillGenTest {

        public static void main(String[] args) {
                GuestDetails guestDetails = new GuestDetails();
                guestDetails.setDaysStayed(2);
                guestDetails.setHotelRoomType(HotelRoomType.LUXURY);
                guestDetails.setDomesticBeersCount(1);
                guestDetails.setDomesticWineCount(0);
                guestDetails.setImportedBeersCount(2);
                guestDetails.setImportedWineCount(0);
                guestDetails.setWaterBottleCount(1);
                guestDetails.setSodaCount(2);

                BillGenerator billGenerator = new HotelBillGeneratorImpl();
                billGenerator.generateBill(guestDetails);

        }

}

Interview Questions: 

Search