Question

In: Computer Science

How can I write a separate java class to change a given name or a given...

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();

    }
}

Solutions

Expert Solution

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());

}

}


Related Solutions

b) Name the class ArithmeticMethods, and the java file ArithmeticMethods.java. c) Write separate methods to perform...
b) Name the class ArithmeticMethods, and the java file ArithmeticMethods.java. c) Write separate methods to perform arithmetic operations of sum, average, product, max and min of three integer numbers. These 5 methods should have three integer parameters to take the three integer values. They should be valuereturn methods, and return the arithmetic result in proper data type when they are invoked (called). The average method should return a double value, and all the other methods should return integer values. d)...
A Java question. You are given a Student class. A Student has a name and an...
A Java question. You are given a Student class. A Student has a name and an ArrayList of grades (Doubles) as instance variables. Write a class named Classroom which manages Student objects. You will provide the following: 1. public Classroom() a no-argument constructor. 2. public void add(Student s) adds the student to this Classroom (to an ArrayList 3. public String hasAverageGreaterThan(double target) gets the name of the first student in the Classroom who has an average greater than the target...
in java, write code that defines a class named Cat The class should have breed, name...
in java, write code that defines a class named Cat The class should have breed, name and weight as attributes. include setters and getters for the methods for each attribute. include a toString method that prints out the object created from the class, and a welcome message. And use a constructor that takes in all the attributes to create an object.
write program in java Create a class named PersonalDetails with the fields name and address. The...
write program in java Create a class named PersonalDetails with the fields name and address. The class should have a parameterized constructor and get method for each field.  Create a class named Student with the fields ID, PersonalDetails object, major and GPA. The class should have a parameterized constructor and get method for each field. Create an application/class named StudentApp that declare Student object. Prompts (GUI input) the user for student details including ID, name, address, major and GPA....
Java Write a class called Triangle that can be used to represent a triangle. Write a...
Java Write a class called Triangle that can be used to represent a triangle. Write a class called Describe that will interface with the Triangle class The Server • A Triangle will have 3 sides. It will be able to keep track of the number of Triangle objects created. It will also hold the total of the perimeters of all the Triangle objects created. • It will allow a client to create a Triangle, passing in integer values for the...
In java, I have problem how to declare BankAccount Account = new BasicAccount(100.00); Given the class...
In java, I have problem how to declare BankAccount Account = new BasicAccount(100.00); Given the class BankAccount , implement a subclass of BankAccount called BasicAccount whose withdraw method will only withdraw money if there is enough money currently in the account (no overdrafts allowed). Assume all inputs to withdraw and the constructor will be valid non-negative doubles. public class BankAccount {     private double balance;     public BankAccount() {         balance = 0;     }     public BankAccount(double initialBalance) {         balance = initialBalance;     }     public...
How can I make a method to check out a customer given their name and booking...
How can I make a method to check out a customer given their name and booking number in Java. I have provided the Book class as well as the customer class. I have done the method to add a customer into the array however im failng to implement a method that checks out the customer. If you need more classes associated with the system please let me know ASAP and I will provide them, ----------------------------------------------------------------------------------------------------------------------------------- public class Book{    String...
In a Java program, how could I write a program that can assign values that would...
In a Java program, how could I write a program that can assign values that would make a rock paper scissors game work? I have a program that will generate a computer response of either rock, paper, or scissors but how can I compare a user input of "rock", "paper", or "scissors" so that we can declare either the user or the computer the winner.
Given a large range of numbers how can I determine the class intervals to use and...
Given a large range of numbers how can I determine the class intervals to use and if I need an equal or unequal class width histogram? Thanks! Please show some example. Really struggling with how can I know when do I need which of the two.
In Java, Here is a basic Name class. class Name { private String first; private String...
In Java, Here is a basic Name class. class Name { private String first; private String last; public Name(String first, String last) { this.first = first; this.last = last; } public boolean equals(Name other) { return this.first.equals(other.first) && this.last.equals(other.last); } } Assume we have a program (in another file) that uses this class. As part of the program, we need to write a method with the following header: public static boolean exists(Name[] names, int numNames, Name name) The goal of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT