I want to update the employee name giving the choice from the user and update the corresponding player details.I am using getter and setter method to update the value. But in the output the new updated value is not showing. Review the below code where i am wrong.
import java.io.*;
import java.util.*;
class product
{
String name;
public product(String name)
{
this.name = name;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public void display()
{
System.out.println("Name is " + getName());
}
}
class Main {
public static void main (String[] args)
{
Scanner sc = new Scanner(System.in);
String n;
System.out.println("Update player name");
n = sc.nextLine();
product p1 = new product(n);
p1.display();
while(true)
{
int num=sc.nextInt();
switch(num)
{
case 1:
System.out.println("Update name");
String n1 = sc.nextLine();
product p2 = new product(n1);
p2.display();
case 2:
System.out.println("Display");
p1.display();
}
}
}
}
Here in output i am not getting the updated name and while displaying the details its showing the previous name not the updated name. Plaese tell me how can i get the updated value using getter and setter. Thank You