Exercises Java with Examples-
Practice Exercises Java 4,5 5 302940

Friends database, using files - Practice Exercises Java


Lesson 8:

File management


Exercise 8.14:

Friends database, using files


Objetive:

Expand the "friends database", so that it loads data from file at the beginning of each session (if the file exists) and saves the data to file when the session ends. Therefore, the data entered in a session must be available for the next session.


Source Code:


import java.util.*;
public class FriendsDatabase
{
	private final static class people
	{
		public String name;
		public String email;
		public String address;
		public short year;

		public people clone()
		{
			people varCopy = new people();

			varCopy.name = this.name;
			varCopy.email = this.email;
			varCopy.address = this.address;
			varCopy.year = this.year;

			return varCopy;
		}
	}

	public static void main(String[] args)
	{
		int total = 275;
		people[] p = new people[total];
		int amount = 0;

		char option;
		boolean found;
		String textSearch;
		String name = "friends.dat";

		if ((new java.io.File(name)).isFile())
		{
			java.io.FileReader file = new java.io.FileReader(name);
			java.io.BufferedReader fileBufferedReader = new java.io.BufferedReader(file);
			amount = (int)fileBufferedReader.readLine();

			fileBufferedReader.readLine();

			for (int i = 0; i < amount; i++)
			{
				if (i < total)
				{
					p[i].name = fileBufferedReader.readLine();
					p[i].email = fileBufferedReader.readLine();
					p[i].address = fileBufferedReader.readLine();
					p[i].year = (short)fileBufferedReader.readLine();
					fileBufferedReader.readLine();
				}
			}
			file.close();
		}

		do
		{
			System.out.println("1- Add data");
			System.out.println("2- Show");
			System.out.println("3- View all data");
			System.out.println("4- Show between dates");
			System.out.println("5- Show oldest");
			System.out.println("6- Show fields match");
			System.out.println("0- Exit");

			System.out.print("Enter a option: ");
			option = (char)new Scanner(System.in).nextLine();

			switch (option)
			{

				case '1':
					if (amount < total - 1)
					{
						do
						{
							System.out.print("Name: ");
							p[amount].name = new Scanner(System.in).nextLine();

							if (p[amount].name.length() > 40)
							{
								System.out.println("Max 40 letters");
							}
						} while (p[amount].name.length() > 40);

						do
						{
							System.out.print("Email: ");
							p[amount].email = new Scanner(System.in).nextLine();

							if (p[amount].email.length() > 30)
							{
								System.out.println("Max 30 letters");
							}
						} while (p[amount].email.length() > 30);

						do
						{
							System.out.print("Address: ");
							p[amount].address = new Scanner(System.in).nextLine();

							if (p[amount].address.length() > 150)
							{
								System.out.println("Max 150 letters");
							}
						} while (p[amount].address.length() > 150);

						do
						{
							System.out.print("Year: ");
							p[amount].year = Short.parseShort(new Scanner(System.in).nextLine());

							if (p[amount].year < 1850 || p[amount].year > 2100)
							{
								System.out.println("1850-2100");
							}
						} while (p[amount].year < 1850 || p[amount].year > 2100);

						amount++;
						System.out.println();
					}
					else
					{
						System.out.println("Full");
					}

					break;

				case '2':
					if (amount == 0)
					{
						System.out.println("No data");
					}
					else
					{
						for (int i = 0; i < amount; i++)
						{
							if (p[i].name.length() <= 30)
							{
								System.out.printf("%1$s: Name = %2$s" + "\r\n", i + 1, p[i].name);
							}
							else
							{
								System.out.printf("%1$s: Name = %2$s" + "\r\n", i + 1, p[i].name.substring(0, 30) + "...");
							}

							if (i % 20 == 19)
							{
								new Scanner(System.in).nextLine();
							}
						}
					}

					break;
				case '3':
					System.out.print("Enter the person: ");
					textSearch = new Scanner(System.in).nextLine();

					found = false;
					for (int i = 0; i < amount; i++)
					{
						if (textSearch.toLowerCase().equals(p[i].name.toLowerCase()))
						{
							found = true;
							System.out.printf("%1$s: Year = %2$s, Email = %3$s, Address = %4$s" + "\r\n", i + 1, p[i].year, p[i].email, p[i].address);
						}
					}

					if (!found)
					{
						System.out.println("Not exists");
					}

					break;

				case '4':
					System.out.print("Enter the first year: ");

					int year1 = Short.parseShort(new Scanner(System.in).nextLine());

					System.out.print("Enter the second year: ");

					int year2 = Short.parseShort(new Scanner(System.in).nextLine());


					if (year1 > year2)
					{
						int aux = year2;
						year2 = year1;
						year1 = aux;
					}

					found = false;
					for (int i = 0; i < amount; i++)
					{
						if (p[i].year >= year1 && p[i].year <= year2)
						{
							System.out.print(p[i].name + " - ");
							found = true;
						}
					}

					if (!found)
					{
						System.out.println("Not found");
					}
					break;


				case '5':
					if (amount == 0)
					{
						System.out.println("No data");
					}
					else
					{
						int firstYear = p[0].year;
						found = false;

						for (int i = 1; i < amount; i++)
						{
							if (p[i].year < firstYear)
							{
								firstYear = p[i].year;
							}
						}

						for (int i = 0; i < amount; i++)
						{
							System.out.printf("%1$s: Address = %3$s, Year = %4$s" + "\r\n", i + 1, p[i].name, p[i].address, p[i].year);

							if (new Scanner(System.in).nextLine().toLowerCase().equals("end"))
							{
								break;
							}
						}
					}
					break;

				case '0':
					break;

				default:
					System.out.println("Wrong option");
					break;
			}

		} while (option != '0');

		java.io.FileWriter dataFile = new java.io.FileWriter(name);
		dataFile.write(String.valueOf(amount) + System.lineSeparator());
		dataFile.WriteLine();

		for (int i = 0; i < amount; i++)
		{
			dataFile.write(p[i].name + System.lineSeparator());
			dataFile.write(p[i].email + System.lineSeparator());
			dataFile.write(p[i].address + System.lineSeparator());
			dataFile.write(String.valueOf(p[i].year) + System.lineSeparator());
			dataFile.WriteLine();
		}

		dataFile.close();
	}
}
Exercisey 8.14

Share Now:


Google Ads





Privacy Policy:



Google uses associated advertising companies to serve ads when it visits our website. These companies may use the information they obtain from your visits to this and other websites (not including your name, address, email address, or phone number) to provide you with announcements about products and services that interest you. If you would like to learn more about this practice and know your options to prevent these companies from using this information. Click in... Privacy and Terms of Google.

Cookies

This site uses Google cookies to provide its services, to personalize advertisements and to analyze traffic. Google receives information about your use of this website. More information in... Privacy and Terms of Google.