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

Books database - Practice Exercises Java


Lesson 4:

Structures and Strings


Exercise 4.11:

Books database


Objetive:

Create a small database, which will be used to store data about books. For a certain book, we want to keep the following information:

Title
Author
The program must be able to store 1000 books, and the user will be allowed to:

Add data for one book
Display all the entered books (just title and author, in the same line)
Search for the book(s) with a certain title
Delete a book at a known position (for example, book number 6)
Exit the program

Hint: to delete an item in an array, you must move backwards every item which was placed after it, and the decrease the counter.


Source Code:


import java.util.*;
public class Main
{
	private final static class book
	{
		public String title;
		public String author;

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

			varCopy.title = this.title;
			varCopy.author = this.author;

			return varCopy;
		}
	}

	public static void main(String[] args)
	{
		int capacity = 1000;
		book [] books = new book [capacity];
		boolean repeat = true;
		String option;
		int amount = 0;
		String search;
		boolean found;

		do
		{
			System.out.println();
			System.out.println("Books database");
			System.out.println();
			System.out.println("1- Add a new book");
			System.out.println("2- Display all books");
			System.out.println("3- Exact search for any book");
			System.out.println("4- Partial search");
			System.out.println("5- Delete a book");
			System.out.println("0- Exit");
			System.out.print("Enter an option: ");
			option = new Scanner(System.in).nextLine();

			switch (option)
			{
				case "1": // Add a new book
					if (amount < capacity)
					{
						System.out.printf("Enter data for book %1$s" + "\r\n",amount + 1);
						System.out.print("Enter the name of the book: ");
						books[amount].title = new Scanner(System.in).nextLine();
						System.out.print("Enter the author: ");
						books[amount].author = new Scanner(System.in).nextLine();
						amount++;
						System.out.println();
					}
					else
					{
						System.out.println("Database full");
					}
						break;


				case "2": // Display all books
					if (amount == 0)
					{

					else
					{
						for (int i = 0;i < amount;i++)
						{
						System.out.printf("%1$s: Title = %2$s,  Author = %3$s" + "\r\n", i + 1, books[i].title, books[i].author);
						}
						System.out.println();
					}
					}
					break;
				case "3": // Exact search
					System.out.println("Enter the name of the book");
					search = new Scanner(System.in).nextLine();
					found = false;

					for (int i = 0;i < amount;i++)
					{
						if (books[i].title.equals(search))
						{
						System.out.printf("Book %1$s found" + "\r\n",books[i].title);
						found = true;
						}
					}

					if (!found)
					{
					System.out.println("Not found!");
					}
					System.out.println();
					break;
				case "4": // Partial search
					System.out.println("Enter the search string");
					search = new Scanner(System.in).nextLine();
					found = false;

					for (int i = 0;i < amount;i++)
					{
					if (books[i].title.toUpperCase().contains(search.toUpperCase()) books[i].author.toUpperCase().contains(search.toUpperCase()))
					{
						System.out.printf("%1$s found in %2$s" + "\r\n", search, books[i].title);
						found = true;
					}
					}

					System.out.println();

					if (!found)
					{
						System.out.println("Not found!");
					}
						break;
				case "5": // Delete
					if (amount == 0)
					{
						System.out.println("No data to delete");
					}
					else
					{
						System.out.printf("Enter the number of book to delete (1 to %1$s)" + "\r\n", amount);
						int posToDelete = Integer.parseInt(new Scanner(System.in).nextLine()) - 1;
						for (int i = posToDelete; i < amount - 1; i++)
						{
						books[i] = books[i + 1].clone();
						}
						amount--;
					}
					break;
				case "0": // End
					repeat = false;
					break;
					default:
					System.out.println();
					System.out.println("Wrong option. Please re-enter\n");
					break;
			}
		} while (repeat);
	}
}
Exercisey 4.11

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.