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...
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...
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.
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 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...
in java code In the class Hw2, write a method removeDuplicates that given a sorted array,...
in java code In the class Hw2, write a method removeDuplicates that given a sorted array, (1) removes the duplicates so that each distinct element appears exactly once in the sorted order at beginning of the original array, and (2) returns the number of distinct elements in the array. The following is the header of the method: public static int removeDuplicates(int[ ] A) For example, on input A=0, 0, 1, 1, 1, 2, 2, 3, 3, 4, your method should:...
IN JAVA ECLIPSE PLEASE The xxx_Student class A Student has a: – Name - the name...
IN JAVA ECLIPSE PLEASE The xxx_Student class A Student has a: – Name - the name consists of the First and Last name separated by a space. – Student Id – a whole number automatically assigned in the student class – Student id numbers start at 100. The numbers are assigned using a static variable in the Student class • Include all instance variables • Getters and setters for instance variables • A static variable used to assign the student...
Code in Java Write a Student class which has two instance variables, ID and name. This...
Code in Java Write a Student class which has two instance variables, ID and name. This class should have a two-parameter constructor that will set the value of ID and name variables. Write setters and getters for both instance variables. The setter for ID should check if the length of ID lies between 6 to 8 and setter for name should check that the length of name should lie between 0 to 20. If the value could not be set,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT