In: Computer Science
How can I write a separate java class to change a given name or a given email of a user that was already saved (keep in mind that there may be numerous names and emails).
Note: It should change the name or email that is saved in the txt file.
Here is my code so far:
User class
public class User { private String fName; private String lName; private String UserEmail; public User(String firstName, String lastName, String email) { this.fName = firstName; this.lName = lastName; this.UserEmail = email; } //constructor used to load saved profile information. public User(String firstName, String lastName, String email, double income, double expenses) { this.fName = firstName; this.lName = lastName; this.UserEmail = email; } public String getFirstName() { return fName; } public String getLastName() { return lName; } public String getEmail() { return UserEmail; } public void setFirstName(String firstName) { this.fName = firstName; } public void setLastName(String lastName) { this.lName = lastName; } public void setEmail(String email) { this.UserEmail = email; } @Override public String toString() { return fName + ", " + lName + ", " + UserEmail ; } }
Profile Create class
import java.io.IOException; import java.util.Scanner; public class ProfileCreate { public User createUser() throws IOException { String fName; String lName; String email; Scanner read = new Scanner(System.in); System.out.println("First name?"); fName = read.nextLine(); System.out.println("Last name?"); lName = read.nextLine(); System.out.println("Email?"); email = read.nextLine(); System.out.println("Successful Profile Creation"); User x = new User(fName, lName, email); return x; } }
Save User Info class
import java.util.Scanner; import java.io.*; import java.util.*; import java.lang.*; public class SaveUserInfo { public void writeDetails(String saveDetails) throws IOException{ FileWriter out = new FileWriter("Details.txt"); for (int x = 0; x < saveDetails.length(); x++){ out.write(saveDetails.charAt(x)); } out.close(); } }
import java.io.IOException;
import java.util.Scanner;
public class UserProfile
{
static int menu()
{
Scanner sc = new Scanner(System.in);
System.out.print("\n\n ************* MENU ************* ");
System.out.print("\n 1 - Change Name.");
System.out.print("\n 2 - Change Email.");
System.out.print("\n What is your choice? ");
return sc.nextInt();
}
static int searchName(User users[], String name)
{
for(int c = 0; c < users.length; c++)
if(users[c].getFirstName().equalsIgnoreCase(name))
return c;
return -1;
}
static int searchEmail(User users[], String email)
{
for(int c = 0; c < users.length; c++)
if(users[c].getFirstName().equalsIgnoreCase(email))
return c;
return -1;
}
static void change(User users[], int ch)
{
int found = -1;
Scanner sc = new Scanner(System.in);
switch(ch)
{
case 1:
System.out.print("\n Enter old first name to change: ");
String name = sc.nextLine();
found = searchName(users, name);
if(found == -1)
System.out.print("\n Name not available in the list.");
else
{
System.out.println("First name?");
String fName = sc.nextLine();
System.out.println("Last name?");
String lName = sc.nextLine();
users[found].setFirstName(fName);
users[found].setFirstName(lName);
}
break;
case 2:
System.out.print("\n Enter old first name to change: ");
String email = sc.nextLine();
found = searchName(users, email);
if(found == -1)
System.out.print("\n Email not available in the list.");
else
{
System.out.println("\n Enter new Email: ");
String newEmail = sc.nextLine();
users[found].setEmail(newEmail);
}
break;
default:
System.out.println("\n Invalid choice!!");
}
}
public static void main(String ss[])throws IOException
{
User users[] = new User[3];
ProfileCreate pc = new ProfileCreate();
SaveUserInfo sui = new SaveUserInfo();
String saveDetails = "";
for(int c = 0; c < users.length; c++)
{
users[c] = pc.createUser();
if(c == users.length - 1)
saveDetails += users[c].toString();
else
saveDetails += users[c].toString() + "\n";
}
System.out.println(saveDetails);
sui.writeDetails(saveDetails);
change(users, menu());
}
}