Could someone edit my code? The readFromFile() method should check is a file nam
ID: 3574215 • Letter: C
Question
Could someone edit my code?
The readFromFile() method should check is a file named "data" exists, and if it does, new information is added on to the existing file. If it does not exist, it creates a new file named "data" and writes new information into the file.
Also, in the getPerson method in the Main class, instead of returning a new constructor Person(String x, etc...), call the setFirstName(), setLastName(), set...() methods from the class Person.
import java.util.ArrayList;
import java.util.Scanner;
/**
* Main class tests the class ContactList by getting the input from the user and saves the program, adds a new contact
* list, reads the contact from file, show contact by inputting the last name, or shows all contract based on the input
* from the user.
*
* @author Jae
* @produced 11/30/16
* @version 2.0
*/
public class Main {
public static void main(String[] args) {
int option = 5;
//creates an object of each of the other classes,
ContactList list = new ContactList();
Scanner scanner = new Scanner(System.in);
String lastName;
String fileName = "data";
list.readFromFile(fileName);
System.out.println("Welcome to contact list manager!");
//calls each method on each of the objects it creates, and
while (option != 0) {
System.out.println("0. Quit the program");
System.out.println("1. Add new person to contact list");
System.out.println("2. Show contact by last name");
System.out.println("3. Show all contact");
System.out.print("Enter your option: ");
option = Integer.parseInt(scanner.nextLine());
switch (option) {
case 0:
list.saveToFile(fileName);
System.out.println("Contact list has been saved! Exiting program now.");
break;
case 1:
list.addPerson(getPerson(scanner));
break;
case 2:
System.out.print("Enter a last name to search for: ");
lastName = scanner.nextLine();
list.showPersonByLastName(lastName);
break;
case 3:
System.out.println("Contact List:");
list.printContactList();
break;
}
}
}
/**
* get person's information from user
*
* @param scanner input stream
* @return a person
*/
private static Person getPerson(Scanner scanner) {
String firstName;
String lastName;
String streetAddress;
String emailAddress;
String phoneNumber;
String note;
System.out.print("Enter first name: ");
firstName = scanner.nextLine();
System.out.print("Enter last name: ");
lastName = scanner.nextLine();
System.out.print("Enter street address: ");
streetAddress = scanner.nextLine();
System.out.print("Enter email address: ");
emailAddress = scanner.nextLine();
System.out.print("Enter phone number: ");
phoneNumber = scanner.nextLine();
System.out.print("Enter note: ");
note = scanner.nextLine();
return new Person(firstName, lastName, streetAddress, emailAddress, phoneNumber, note);
}
}
import java.util.ArrayList;
import java.util.Scanner;
import java.io.*;
/**
* creates a resizable(grows as it exceeds the size) array of objects, Person. This class is able to add the person's
* information to Person, print all the contact list in it, retrieve the person's information by last name, save to the
* file if methods are called.
*
* @author Jae
* @produced 11/25/16
* @version 1.0
*/
public class ContactList {
private ArrayList<Person> list;
/**
* constructor
*/
public ContactList() {
list = new ArrayList<>();
}
/**
* check the Person p it cannot be null its lastName field cannot be blank
* it will be added to the contact list in order of last name when all
* condition are met
*
*
* @param p
*/
public void addPerson(Person p) {
if (p != null && p.getLastName() != null) {
boolean added = false;
for (int i = 0; i < list.size(); i++) {
if (p.compareTo(list.get(i)) < 0 ) {
list.add(i, p);
added = true;
break;
}
}
if (added == false) {
list.add(p);
}
}
}
/**
* Prints all information about all contacts to the console, sorted by last
* name first then their first name
*/
public void printContactList() {
for (Person person : list) {
System.out.println(person);
}
}
/**
* Retrieve and display all information of people whose last name match with
* lastName
*
* @param lastName
*/
public void showPersonByLastName(String lastName) {
if (lastName != null) {
for (Person person : list) {
if (lastName.equals(person.getLastName())) {
System.out.println(person);
}
}
}
}
/**
* Save the current contact list to disk at fileName
*
* @param fileName
*/
public void saveToFile(String fileName) {
try {
FileWriter fw = new FileWriter(fileName);
String line;
for (Person p : list) {
line = " " + p.getFirstName()
+ ", " + p.getLastName()
+ ", " + p.getStreetAddress()
+ ", " + p.getEmailAddress()
+ ", " + p.getPhoneNumber()
+ ", " + p.getNote()
+ System.lineSeparator();
fw.write(line);
}
fw.close();
} catch (IOException ex) {
System.err.println(ex.getMessage());
}
}
/**
* Read the current contact list from disk at fileName
*
* @param fileName
*/
public void readFromFile(String fileName) {
}
}
/**
* The class Person sets the information of the individual to each of the String with its methods, and orders the list by
* last name alphabetically.
*
* @author Jae
* @produced 11/25/16
* @version 1.0
*/
public class Person implements Comparable<Person>{
private String firstName; //private member variable
private String lastName; //private member variable
private String streetAddress; //private member variable
private String emailAddress; //private member variable
private String phoneNumber; //private member variable
private String note; //private member variable
/**
* constructor
*/
public Person() {
this.firstName = null;
this.lastName = null;
this.streetAddress = null;
this.emailAddress = null;
this.phoneNumber = null;
this.note = null;
}
/**
* constructor
* @param firstName
* @param lastName
* @param streetAddress
* @param emailAddress
* @param phoneNumber
* @param note
*/
public Person(String firstName, String lastName, String streetAddress, String emailAddress, String phoneNumber, String note) {
this.firstName = firstName;
this.lastName = lastName;
this.streetAddress = streetAddress;
this.emailAddress = emailAddress;
this.phoneNumber = phoneNumber;
this.note = note;
}
/**
* get first name
* @return first name
*/
public String getFirstName() {
return firstName;
}
/**
* get last name
* @return last name
*/
public String getLastName() {
return lastName;
}
/**
* get street address
* @return street address
*/
public String getStreetAddress() {
return streetAddress;
}
/**
* get email address
* @return email address
*/
public String getEmailAddress() {
return emailAddress;
}
/**
* get phone number
* @return phone number
*/
public String getPhoneNumber() {
return phoneNumber;
}
/**
* get note
* @return note
*/
public String getNote() {
return note;
}
/**
* set value for first name to firstName
* @param firstName
*/
public void setFirstName(String firstName) {
this.firstName = firstName;
}
/**
* set value for last name to lastName
* @param lastName
*/
public void setLastName(String lastName) {
this.lastName = lastName;
}
/**
* set value for street address to streetAddress
* @param streetAddress
*/
public void setStreetAddress(String streetAddress) {
this.streetAddress = streetAddress;
}
/**
* set value for email address to emailAddress
* @param emailAddress
*/
public void setEmailAddress(String emailAddress) {
this.emailAddress = emailAddress;
}
/**
* set value for phone number to phoneNumber
* @param phoneNumber
*/
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
/**
* set value for note to note entered
* @param note
*/
public void setNote(String note) {
this.note = note;
}
/**
* Returns the String representation of values for all attributes of the Person that calls it.
* @return
*/
public String toString() {
return "Name: " + firstName + " " + lastName +
" Address: " + streetAddress +
" Email: " + emailAddress +
" Phone#: " + phoneNumber +
" Note: " + note;
}
/**
* compares 2 people
* @param o
* @return the difference between their last name and their first name
*/
public int compareTo(Person x) {
if (x == null) {
return 1;
}
int comp = lastName.compareTo(x.lastName);
if (comp == 0) {
if (firstName == null) {
return -1;
}
return firstName.compareTo(x.firstName);
}
return comp;
}
}
Explanation / Answer
import java.util.ArrayList;
import java.util.Scanner;
/**
* Main class tests the class ContactList by getting the input from the user and saves the program, adds a new contact
* list, reads the contact from file, show contact by inputting the last name, or shows all contract based on the input
* from the user.
*
* @author Jae
* @produced 11/30/16
* @version 2.0
*/
public class Main {
public static void main(String[] args) {
int option = 5;
//creates an object of each of the other classes,
ContactList list = new ContactList();
Scanner scanner = new Scanner(System.in);
String lastName;
String fileName = "data";
list.readFromFile(fileName);
System.out.println("Welcome to contact list manager!");
//calls each method on each of the objects it creates, and
while (option != 0) {
System.out.println("0. Quit the program");
System.out.println("1. Add new person to contact list");
System.out.println("2. Show contact by last name");
System.out.println("3. Show all contact");
System.out.print("Enter your option: ");
option = Integer.parseInt(scanner.nextLine());
switch (option) {
case 0:
list.saveToFile(fileName);
System.out.println("Contact list has been saved! Exiting program now.");
break;
case 1:
list.addPerson(getPerson(scanner));
break;
case 2:
System.out.print("Enter a last name to search for: ");
lastName = scanner.nextLine();
list.showPersonByLastName(lastName);
break;
case 3:
System.out.println("Contact List:");
list.printContactList();
break;
}
}
}
/**
* get person's information from user
*
* @param scanner input stream
* @return a person
*/
private static Person getPerson(Scanner scanner) {
String firstName;
String lastName;
String streetAddress;
String emailAddress;
String phoneNumber;
String note;
Person p1=new Person();
System.out.print("Enter first name: ");
firstName = scanner.nextLine();
System.out.print("Enter last name: ");
lastName = scanner.nextLine();
System.out.print("Enter street address: ");
streetAddress = scanner.nextLine();
System.out.print("Enter email address: ");
emailAddress = scanner.nextLine();
System.out.print("Enter phone number: ");
phoneNumber = scanner.nextLine();
System.out.print("Enter note: ");
note = scanner.nextLine();
p1.setFirstName(first Name);
p1.setLastName(lastName);
p1.setStreetAddress(streetAddress);
p1.setEmailAddress(emailAddress);
p1.setPhoneNumber(phoneNumber);
p1.setNote(note);
return p1;
}
}
import java.util.ArrayList;
import java.util.Scanner;
import java.io.*;
/**
* creates a resizable(grows as it exceeds the size) array of objects, Person. This class is able to add the person's
* information to Person, print all the contact list in it, retrieve the person's information by last name, save to the
* file if methods are called.
*
* @author Jae
* @produced 11/25/16
* @version 1.0
*/
public class ContactList {
private ArrayList<Person> list;
Boolean bool=false;
/**
* constructor
*/
public ContactList() {
list = new ArrayList<>();
}
/**
* check the Person p it cannot be null its lastName field cannot be blank
* it will be added to the contact list in order of last name when all
* condition are met
*
*
* @param p
*/
public void addPerson(Person p) {
if (p != null && p.getLastName() != null) {
boolean added = false;
for (int i = 0; i < list.size(); i++) {
if (p.compareTo(list.get(i)) < 0 ) {
list.add(i, p);
added = true;
break;
}
}
if (added == false) {
list.add(p);
}
}
}
/**
* Prints all information about all contacts to the console, sorted by last
* name first then their first name
*/
public void printContactList() {
for (Person person : list) {
System.out.println(person);
}
}
/**
* Retrieve and display all information of people whose last name match with
* lastName
*
* @param lastName
*/
public void showPersonByLastName(String lastName) {
if (lastName != null) {
for (Person person : list) {
if (lastName.equals(person.getLastName())) {
System.out.println(person);
}
}
}
}
/**
* Save the current contact list to disk at fileName
*
* @param fileName
*/
public void saveToFile(String fileName) {
try {
FileWriter fw = new FileWriter(fileName);
String line;
for (Person p : list) {
line = " " + p.getFirstName()
+ ", " + p.getLastName()
+ ", " + p.getStreetAddress()
+ ", " + p.getEmailAddress()
+ ", " + p.getPhoneNumber()
+ ", " + p.getNote()
+ System.lineSeparator();
fw.write(line);
}
fw.close();
} catch (IOException ex) {
System.err.println(ex.getMessage());
}
}
/**
* Read the current contact list from disk at fileName
*
* @param fileName
*/
public void readFromFile(String fileName) {
File f=new File(filename);
Bool=f.exists();
if(bool==false) {
System.out.println(“file”+fileName+”not exists”);
f.createNewFile();
System.out.println(“new file named”+fileName+”created”);
}
BufferedReader br = null;
FileReader fr = null;
Person p2=new Person();
fr = new FileReader(FILENAME);
br = new BufferedReader(fr);
String sCurrentLine;
br = new BufferedReader(new FileReader(FILENAME));
while ((sCurrentLine = br.readLine()) != null) {
int sum=0;
Scanner obj=new Scanner(System.in);
StringTokenizer st=new StringTokenizer(sCurrentLine," ");
while(st.hasMoreTokens())
{
p2.setFirstName(st.nextToken());
p2.setLastName(st.nextToken());
p2.setStreetAddress(st.nextToken());
p2.setEmailAddress(st.nextToken());
p2.setPhoneNumber(st.nextToken());
p2.setNote(st.nextToken());
}
addPerson(p2);
}
}
/**
* The class Person sets the information of the individual to each of the String with its methods, and orders the list by
* last name alphabetically.
*
* @author Jae
* @produced 11/25/16
* @version 1.0
*/
public class Person implements Comparable<Person>{
private String firstName; //private member variable
private String lastName; //private member variable
private String streetAddress; //private member variable
private String emailAddress; //private member variable
private String phoneNumber; //private member variable
private String note; //private member variable
/**
* constructor
*/
public Person() {
this.firstName = null;
this.lastName = null;
this.streetAddress = null;
this.emailAddress = null;
this.phoneNumber = null;
this.note = null;
}
/**
* constructor
* @param firstName
* @param lastName
* @param streetAddress
* @param emailAddress
* @param phoneNumber
* @param note
*/
public Person(String firstName, String lastName, String streetAddress, String emailAddress, String phoneNumber, String note) {
this.firstName = firstName;
this.lastName = lastName;
this.streetAddress = streetAddress;
this.emailAddress = emailAddress;
this.phoneNumber = phoneNumber;
this.note = note;
}
/**
* get first name
* @return first name
*/
public String getFirstName() {
return firstName;
}
/**
* get last name
* @return last name
*/
public String getLastName() {
return lastName;
}
/**
* get street address
* @return street address
*/
public String getStreetAddress() {
return streetAddress;
}
/**
* get email address
* @return email address
*/
public String getEmailAddress() {
return emailAddress;
}
/**
* get phone number
* @return phone number
*/
public String getPhoneNumber() {
return phoneNumber;
}
/**
* get note
* @return note
*/
public String getNote() {
return note;
}
/**
* set value for first name to firstName
* @param firstName
*/
public void setFirstName(String firstName) {
this.firstName = firstName;
}
/**
* set value for last name to lastName
* @param lastName
*/
public void setLastName(String lastName) {
this.lastName = lastName;
}
/**
* set value for street address to streetAddress
* @param streetAddress
*/
public void setStreetAddress(String streetAddress) {
this.streetAddress = streetAddress;
}
/**
* set value for email address to emailAddress
* @param emailAddress
*/
public void setEmailAddress(String emailAddress) {
this.emailAddress = emailAddress;
}
/**
* set value for phone number to phoneNumber
* @param phoneNumber
*/
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
/**
* set value for note to note entered
* @param note
*/
public void setNote(String note) {
this.note = note;
}
/**
* Returns the String representation of values for all attributes of the Person that calls it.
* @return
*/
public String toString() {
return "Name: " + firstName + " " + lastName +
" Address: " + streetAddress +
" Email: " + emailAddress +
" Phone#: " + phoneNumber +
" Note: " + note;
}
/**
* compares 2 people
* @param o
* @return the difference between their last name and their first name
*/
public int compareTo(Person x) {
if (x == null) {
return 1;
}
int comp = lastName.compareTo(x.lastName);
if (comp == 0) {
if (firstName == null) {
return -1;
}
return firstName.compareTo(x.firstName);
}
return comp;
}
}