Books database Java Exercise - Java Programming Course

 Exercise

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.

 Example 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);
	}
}

More Java Exercises of Arrays, Structures and Strings

 Reverse array
Create a Java program to ask the user for 5 numbers, store them in an array and show them in reverse order....
 Search in array
Create a Java program that says if a data belongs in a list that was previously created. The steps to take are: - Ask the user how many data will ...
 Array of even numbers
Write a Java program to ask the user for 10 integer numbers and display the even ones....
 Array of positive and negative numbers
Create a Java program to ask the user for 10 real numbers and display the average of the positive ones and the average of the negative ones....
 Many numbers and sum
Create a program which asks the user for several numbers (until he enters "end" and displays their sum). When the execution is going to end, it must d...
 Two dimensional array
Write a Java program to ask the user for marks for 20 pupils (2 groups of 10, using a two-dimensional array), and display the average for each group....
 Statistics V2
reate a statistical program which will allow the user to: - Add new data - See all data entered - Find an item, to see whether it has been entere...
 Struct
Create a "struct" to store data of 2D points. The fields for each point will be: x coordinate (short) y coordinate (short) r (red colour, byte) ...
 Array of struct
Expand the previous exercise (struct point), so that up to 1.000 points can be stored, using an "array of struct". Ask the user for data for the first...
 Array of struct and menu
Expand the previous exercise (array of points), so that it displays a menu, in which the user can choose to: - Add data for one point - Display al...
 Triangle V2
Write a Java program to ask the user for his/her name and display a triangle with it, starting with 1 letter and growing until it has the full length:...
 Rectangle V3
Write a Java program to ask the user for his/her name and a size, and display a hollow rectangle with it: Enter your name: Yo Enter size: 4 YoYoY...
 Centered triangle
Display a centered triangle from a string entered by the user: __a__ _uan_ Juan...
 Cities database
Create a database to store information about cities. In a first approach, we will store only the name of each city and the number of inhabitants, a...
 Banner
Create a Java program to imitate the basic Unix SysV "banner" utility, able to display big texts....
 Triangle right side
Create a Java program that asks the user for a string and displays a right-aligned triangle: ____n ___an __uan Juan...
 Strings manipulation
Create a Java program that asks the user for a string and: - Replace all lowercase A by uppercase A, except if they are preceded with a space - Di...
 Nested structs
Create a struct to store two data for a person: name and date of birth. The date of birth must be another struct consisting on day, month and ...
 Sort data
Create a Java program to ask the user for 10 integer numbers (from -1000 to 1000), sort them and display them sorted....
 Two dimensional array as buffer for screen
Create a Java program that declares a 70x20 two-dimensional array of characters, "draws" 80 letters (X, for example) in random positions and displays ...
 Two dimensional array 2: circunference on screen
Create a Java program that declares creates a 70x20 two-dimensional array of characters, "draws" a circumference or radius 8 inside it, and displays i...
 Computer programs
Create a Java program that can store up to 1000 records of computer programs. For each program, you must keep the following data: * Name * Categor...
 Exercise tasks
Create a Java program that can store up to 2000 "to-do tasks". For each task, it must keep the following data: • Date (a set of 3 data: day, month ...
 Household accounts
Create a program in Java that can store up to 10000 costs and revenues, to create a small domestic accounting system. For each expense (or income), sh...


Juan A. Ripoll - Programming Tutorials and Courses © 2024 All rights reserved.  Legal Conditions.