I am a beginner who just picked up Java (2 months ago) and I am solving some questions. For this question, the overall problem wants me to choose the cheapest price plan, so I listed out the possibilities of every plan and 'hardcoded' the logic by inputting the number of services, as the services is more important than the plan. However, I would like to improve the algorithm of this method and as a beginner my foundation is very weak.
Is there any other ways where I can further improve this method or is there any other solutions? This is because if I need to implement a new service, my entire solution will be flawed. Hence, I am looking at constructors/interfaces and arrays. However, it gets very confusing when there is a plan and services being involved and then to get the cheapest price as well.
Therefore, I would appreciate any help or advise to improve on this method of solving. Please pardon me for my weak explanation and lack of concept in Java as this is my first post.
Problem statement
Create a java program
The seller from corporation Test provides the following plans and its price:
- PLAN1: (voice, email), $100 per year
- PLAN2: (email, database, admin), $150 per year
- PLAN3: (voice, admin), $125 per year
- PLAN4: (database, admin), $135 per year
The customer wants (email, voice, admin) service. He has the following choices to cover his need:
- (PLAN1, PLAN2): 100 + 150 = $250 (PLAN1, PLAN3): 100 + 125 = $225
- (PLAN2, PLAN3): 150 + 125 = $270 (PLAN1, PLAN4): 100 + 135 = $235
- (PLAN2, PLAN3, PLAN4) = 150 + 125 + 135 = $410
- …// other possible plan choices omitted
And the minimum price is $225, PLAN1 + PLAN3.
My solution
package practice;
import java.util.Scanner;
public class Test {
private static Scanner input;
public static void main(String[] args) {
input = new Scanner(System.in);
System.out.println("We have the following services:");
System.out.println("Voice, Database, Admin, Email");
System.out.println("How many services would you like?");
// input number of services
int choice = input.nextInt();
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Logic starts here
// when number of services required is 1
switch (choice) {
case 1:
Scanner input1 = new Scanner(System.in);
System.out.println("What type of services would you like?");
System.out.println("1. Voice");
System.out.println("2. Database");
System.out.println("3. Admin");
System.out.println("4. Email");
// input 1 option
int choice1 = input1.nextInt();
switch (choice1) {
case 1:
System.out.println("$100");
break;
case 2:
System.out.println("$135");
break;
case 3:
System.out.println("$125");
break;
case 4:
System.out.println("$100");
break;
default:
System.out.println("Invalid option. Please try again");
}
break;
// when number of services required is 2
case 2:
Scanner input2 = new Scanner(System.in);
System.out.println("What type of services would you like?");
System.out.println("1. Voice");
System.out.println("2. Database");
System.out.println("3. Admin");
System.out.println("4. Email");
// input 2 options
int choice2 = input2.nextInt();
int choice3 = input2.nextInt();
// plan(1,4) -> $100
if (choice2 == 1 && choice3 == 4) {
System.out.println("$100");
// plan(1,3) -> $125
} else if (choice2 == 1 && choice3 == 3) {
System.out.println("$125");
// plan(2,3) & (3,4) -> $125
} else if ((choice2 == 2 && choice3 == 3) || (choice2 == 3 && choice3 == 4)) {
System.out.println("$150");
} else
System.out.println("Invalid option. Please try again");
break;
// when number of services required is 3
case 3:
Scanner input3 = new Scanner(System.in);
System.out.println("What type of services would you like?");
System.out.println("1. Voice");
System.out.println("2. Database");
System.out.println("3. Admin");
System.out.println("4. Email");
// input 3 options
int choice4 = input3.nextInt();
int choice5 = input3.nextInt();
int choice6 = input3.nextInt();
// plan(1,3,4) -> $225
if (choice4 == 1 && choice5 == 3 && choice6 == 4) {
System.out.println("$225");
// plan(1,2,3) -> $235
} else if (choice4 == 1 && choice5 == 2 && choice6 == 3) {
System.out.println("$325");
// plan(2,3,4) -> $150
} else if (choice4 == 2 && choice5 == 3 && choice6 == 4) {
System.out.println("$150");
} else
System.out.println("Invalid option. Please try again");
break;
// when number of services required is all 4
case 4:
System.out.println("All 4 services will cost $250");
break;
default:
System.out.println("Invalid input.");
}
}
}
Output
We have the following services:
Voice, Database, Admin, Email
How many services would you like?
3
What type of services would you like?
1. Voice
2. Database
3. Admin
4. Email
1
3
4
$225