'I writting a program for this plan but i want to know did program have any answer and different my writting?

A hotel has a pricing policy as follows:

  • 2 people: 80000
  • 3 people: 90000
  • 4 people: 95000
  • additional people: 6000 per person
  • If a customer is staying on a company business, there is a 20% discount.

In addition:

  1. If a customer is over 60 years of age, there is a 15% discount.
  2. A customer does not receive both discounts.
  3. Given the above data, write a solution to find cost of a room.
import javax.swing.JOptionPane;

public class NewClass {

    public static void main(String[] args) {

        String num = JOptionPane.showInputDialog(null, "Enter a number of people ?");
        int number = Integer.parseInt(num);

        String Inputage = JOptionPane.showInputDialog(null, "Enter your age ?");
        int age = Integer.parseInt(Inputage);

        String set = JOptionPane.showInputDialog(null, "You staying on a company business ? answer , yes or no !");
        String no = "no";
        if (age < 60 && set.equals(no)) {
            switch (number) {

                case 2:
                    JOptionPane.showMessageDialog(null, "80000");
                    break;
                case 3:
                    JOptionPane.showMessageDialog(null, "90000");
                    break;
                case 4:
                    JOptionPane.showMessageDialog(null, "95000");
                    break;
                default:
                    System.out.println("Error");
            }
        }
        if (age >= 60 && set.equals(no)) {
            switch (number) {

                case 2:
                    JOptionPane.showMessageDialog(null, "68000");
                    break;
                case 3:
                    JOptionPane.showMessageDialog(null, "76500");
                    break;
                case 4:
                    JOptionPane.showMessageDialog(null, "80750");
                    break;
                default:
                    System.out.println("Error");
            }
        }
        String yes = "yes";

        if (age < 60 && set.equals(yes)) {
            switch (number) {

                case 2:
                    JOptionPane.showMessageDialog(null, "64000");
                    break;
                case 3:
                    JOptionPane.showMessageDialog(null, "72000");
                    break;
                case 4:
                    JOptionPane.showMessageDialog(null, "76000");
                    break;
                default:
                    System.out.println("Error");
            }
        }
        if (age >= 60 && set.equals(yes)) {
            switch (number) {

                case 2:
                    JOptionPane.showMessageDialog(null, "You can't have both of offer !");
                    break;

                default:
                    System.out.println("Error");
            }

        }

    }
}


Solution 1:[1]

Possibly, a separate method to calculate the price should be implemented:

public static int getPrice(int people, int age, boolean onBusiness) {
    int price;
    switch (people) {
        case 1: // assuming the same price as for 2 people
        case 2:
            price = 80_000; break;
        case 3:
            price = 90_000; break;
        case 4:
            price = 95_000; break;
        default:
            price = 95_000 + 6_000 * (people - 4);
            break;
    }
    // applying only one discount, checking if the larger is applicable first
    if (onBusiness) {
        price = price * 80 / 100; // discount 20% 
    } else if (age >= 60) {
        price = price * 85 / 100; // discount 15%
    }
    return price;
}

If Java 12+ is used, more concise switch syntax may be used:

public static int getPrice(int people, int age, boolean onBusiness) {
    int price = switch (people) {
        case 1, 2 -> 80_000;
        case 3 -> 90_000;
        case 4 -> 95_000;
        default -> 95_000 + 6_000 * (people - 4);
    };
    // applying only one discount, checking if the larger is applicable first
    if (onBusiness) {
        price = price * 80 / 100; // discount 20% 
    } else if (age >= 60) { // discount for people at age 60 or older
        price = price * 85 / 100; // discount 15%
    }
    return price;
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1