Answer to Question #249509 in Java | JSP | JSF for sandyjay

Question #249509

write a simple application which takes data from the console and saves it to a file. The program should take the following data:

name, address and phone number. The program should have a menu which allows users to load data from previous sessions.


1
Expert's answer
2021-10-11T02:56:02-0400
import java.io.File;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;


class Person {
	private String name;
	private String address;
	private String phoneNumber;


	public Person() {
	}


	public Person(String name, String address, String phoneNumber) {
		this.name = name;
		this.address = address;
		this.phoneNumber = phoneNumber;
	}


	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}


	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}


	/**
	 * @return the address
	 */
	public String getAddress() {
		return address;
	}


	/**
	 * @param address the address to set
	 */
	public void setAddress(String address) {
		this.address = address;
	}


	/**
	 * @return the phoneNumber
	 */
	public String getPhoneNumber() {
		return phoneNumber;
	}


	/**
	 * @param phoneNumber the phoneNumber to set
	 */
	public void setPhoneNumber(String phoneNumber) {
		this.phoneNumber = phoneNumber;
	}


	@Override
	public String toString() {
		return "Name: " + name + "\nAddress: " + address + "\nPhone number: " + phoneNumber + "\n";


	}


	public String toFile() {
		return name + "," + address + "," + phoneNumber;
	}
}


public class App {


	public static void main(String[] args) {


		ArrayList<Person> persons = new ArrayList<Person>();
		Scanner input = new Scanner(System.in);
		readPersons(persons);
		int ch = -1;


		while (ch != 3) {
			System.out.println("1. Add a new person");
			System.out.println("2. Display all person");
			System.out.println("3. Exit");
			System.out.print("Your choice: ");
			ch = input.nextInt();
			input.nextLine();
			switch (ch) {
			case 1: {
				System.out.print("Enter name: ");
				String name = input.nextLine();
				System.out.print("Enter address: ");
				String address = input.nextLine();
				System.out.print("Enter phone number: ");
				String phoneNumber = input.nextLine();
				persons.add(new Person(name, address, phoneNumber));
				savePersons(persons);
			}
				break;
			case 2: {
				for (int i = 0; i < persons.size(); i++) {
					System.out.println(persons.get(i).toString() + "\n");
				}
			}
				break;


			case 3: {
				// exit
			}
				break;
			default: {


			}
				break;
			}


		}


		input.close();


	}


	private static void savePersons(ArrayList<Person> persons) {
		try {
			final String FILE_NAME = "Persons.txt";
			try (PrintWriter out = new PrintWriter(FILE_NAME)) {
				for (int i = 0; i < persons.size(); i++) {
					out.println(persons.get(i).toFile());
				}


			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}


	}


	private static void readPersons(ArrayList<Person> persons) {
		try {
			final String FILE_NAME = "Persons.txt";


			Scanner input = new Scanner(FILE_NAME);
			File file = new File(input.nextLine());
			if (file.exists()) {
				input = new Scanner(file);
				while (input.hasNextLine()) {
					String[] values = input.nextLine().split(",");
					persons.add(new Person(values[0], values[1], values[2]));
				}
				input.close();
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}


	}


}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS