I am writing a java code by where I have to calculate a summation by using 2 given input value. I create a method in a class Sum. The name of the method is totalVal. Now in the main class I want call it in a switch case statement.I am describing my code in the following way-
At first I have created a enum class with two constant value,
public enum Product {
FIRST(400), SECOND(500);
private int value;
private Product(int value){
this.value=value;
}
public int getValue() {
return value;
}
}
Now in the model class I call this enam class and create another value named num and set getter and setter for this.
public class Model {
private Product prod;
private int num;
public Product getProd() {
return prod;
}
public void setProd(Product prod) {
this.produkte = produkte;
}
public int getNum() {
return num;
}
public void setNume(int num) {
this.prod = prod;
}
}
Now I create another class for the calculation method
public class Sum{
public int totalVal(Model mod){
int sum = mod.getNum()*mod.getProd().getValue();
return sum;
}
}
Now in the main method I want to set value of num and implement a switch case statement, so that when user choose First it will give a result with multiply by the num value. on the other hand if user choose SECOND it will give a result with multiply by num value. but I am having problem in switch case method, it shows null poi exception error. I described it more clearly in the code-
public static void main(String[] args) {
Scanner sc1 =new Scanner(System.in);
Scanner sc2 =new Scanner(System.in);
System.out.println("1\tFirst: 400\n2\tSecond: 700 ");
System.out.println("Please choose your 1 for Compact and 2 for product");
int swichValue=sc1.nextInt();
Model md=new Model();
System.out.println("enter number");
md.setNum(sc2.nextInt());
switch(swichValue){
case 1:{
Sum tot=new Sum();
int res=Product.FIRST.getValue()*tot.Sum(md); //having problem here I am not sure how to call the method here.
System.out.println("total value is : "+res);
break;
}
case 2:{
int res=Product.SECOND.getValue()*tot.Sum(md);// problem
System.out.println("total value is : "+res);
break;
}
}
}