Java Menu Driven Program to Determine Cost of New Membership


When we go anywhere for any type of membership they have different prices for different plans. For example silver membership, gold membership, diamond membership, platinum membership etc. where the silver membership costs less than others and platinum membership costs more than other plans.

In this article we will see how to check the cost of a membership by using the Java programming language. We will be implementing the application using a switch case.

To show you some instances 

Instance-1

Suppose we have Rs 1840 as base membership price and you want to have a silver membership. The cost of silver membership is 1932. Cost of silver membership is calculated as Base amount + 10% tax – 5% discount.

Instance-2

Suppose we have Rs 1840 as base membership price and you want to have a gold membership. The cost of gold membership is 2116. Cost of gold membership is calculated as Base amount + 20% tax – 5% discount.

Instance-3

Suppose we have Rs 1840 as base membership price and you want to have a diamond membership. The cost of diamond membership is 2300. Cost of diamond membership is calculated as Base amount + 30% tax – 5% discount.

Instance-4

Suppose we have Rs 1840 as base membership price and you want to have a platinum membership. The cost of platinum membership is 2484. Cost of platinum membership is calculated as Base amount + 40% tax – 5% discount.

Algorithm

Step-1 − Ask user to input the current base membership price.

Step-2 − Display the menu.

Step-3 − Ask the user to enter their choice.

Step-4 − Use a switch case to go to the choice and perform the operation.

Step-5 − Print the result.

Let’s see the program to understand it clearly.

Example

import java.util.*; public class Main{ public static void main(String args[]){ int num; Scanner sc = new Scanner(System.in); System.out.print("Enter the base price to opt for a membership: "); num = sc.nextInt(); mainLoop: while (true) { System.out.println("\n***Menu***"); System.out.println("1. Check cost for Silver Membership"); System.out.println("2. Check cost for Gold Membership"); System.out.println("3. Check cost for Diamond Membership"); System.out.println("4. Check cost for Platinum Membership"); System.out.println("5. Terminate the program"); System.out.println("Enter action number (1-5)"); int command = sc.nextInt(); switch(command) { case 1: if(num>1000){ double a1 = num + (.1*num) - (.05*num); System.out.println("Cost for Silver Membership is " + a1); } else{ System.out.println("Base membership price should be greater than 1000"); } break; case 2: if(num>1000) { double a2 = num + (.2*num) - (.05*num); System.out.println("Cost for Gold Membership is "+a2); } else { System.out.println("Base membership price should be greater than 1000"); } break; case 3: if(num>1000) { double a3 = num + (.3*num) - (.05*num); System.out.println("Cost for Diamond Membership is "+a3); } else { System.out.println("Base membership price should be greater than 1000"); } break; case 4: if(num>1000) { double a4 = num + (.4*num) - (.05*num); System.out.println("Cost for Platinum Membership is "+a4); } else{ System.out.println("Base membership price should be greater than 1000"); } break; case 5: System.out.println("Program terminated"); break mainLoop; default: System.out.println("Wrong choice!!"); } } } }

Output

Enter the base price to opt for a membership: 2000

***Menu***
1. Check cost for Silver Membership
2. Check cost for Gold Membership
3. Check cost for Diamond Membership
4. Check cost for Platinum Membership
5. Terminate the program
Enter action number (1-5)
1
Cost for Silver Membership is 2100.0

***Menu***
1. Check cost for Silver Membership
2. Check cost for Gold Membership
3. Check cost for Diamond Membership
4. Check cost for Platinum Membership
5. Terminate the program
Enter action number (1-5)
2
Cost for Gold Membership is 2300.0

***Menu***
1. Check cost for Silver Membership
2. Check cost for Gold Membership
3. Check cost for Diamond Membership
4. Check cost for Platinum Membership
5. Terminate the program
Enter action number (1-5)
3
Cost for Diamond Membership is 2500.0

***Menu***
1. Check cost for Silver Membership
2. Check cost for Gold Membership
3. Check cost for Diamond Membership
4. Check cost for Platinum Membership
5. Terminate the program
Enter action number (1-5)
4
Cost for Platinum Membership is 2700.0

***Menu***
1. Check cost for Silver Membership
2. Check cost for Gold Membership
3. Check cost for Diamond Membership
4. Check cost for Platinum Membership
5. Terminate the program
Enter action number (1-5)
5
Program terminated

In this article, we explored how to check the cost of membership in Java by using a menu driven approach.

Updated on: 27-Dec-2022

228 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements