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

Cities database - Practice Exercises Java


Lesson 4:

Structures and Strings


Exercise 4.15:

Cities database


Objetive:

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, and allocate space for up to 500 cities.

The menu should include the following options:
1 .- Add a new city (at the end of the existing data)
2 .- View all cities (name and inhabitants)
3 .- Modify a record (rename and / or change number of inhabitants)
4 .- Insert a new record (in a specified position, moving the following ones to the right)
5 .- Delete a record (moving the following ones to the left so that no empty spaces are left)
6 .- Search in the records (display the ones which contain a certain text in their name, whether in upper or lower case, using partial search)
7 .- Correct the capitalization of the names (turn into uppercase the first letter and the ones after a space, and make the rest lowercase).
0 .- Exit


Source Code:


import java.util.*;
public class Main
{
	private final static class city
	{
		public String name;
		public int inhabitants;

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

			varCopy.name = this.name;
			varCopy.inhabitants = this.inhabitants;

			return varCopy;
		}
	}

	public static void main(String[] args)
	{
		int maxCities = 500;
		city[] cities = new city[maxCities];
		int amount = 0;
		int currentCityNumber;
		String option;
		String textToSearch;
		boolean found;
		String textToModify;
		boolean finished = false;

		do
		{
			System.out.println();
			System.out.println("Cities database");
			System.out.println();
			System.out.println("1.- Add a new city");
			System.out.println("2.- View all cities");
			System.out.println("3.- Modify a record");
			System.out.println("4.- Insert a new record");
			System.out.println("5.- Delete a record");
			System.out.println("6.- Search in the records");
			System.out.println("7.- Correct the capitalization of the names");
			System.out.println("0.- Exit");
			System.out.println();
			System.out.print("Choose an option: ");
			option = new Scanner(System.in).nextLine();

			switch (option)
			{
				case "0":
					finished = true;
					break;

				case "1":
					if (amount > maxCities - 1)
					{
						System.out.println("the database is full");
					}
					else
					{
						System.out.printf("Entering data for city number %1$s" + "\r\n", amount + 1);
						System.out.print("Enter the city name: ");
						cities[amount].name = new Scanner(System.in).nextLine();
						System.out.print("Enter the inhabitants numbers: ");
						cities[amount].inhabitants = Integer.parseInt(new Scanner(System.in).nextLine());
						System.out.println("The data was entered correctly");
						amount++;
					}
					break;

				case "2":
					for (int i = 0; i < amount; i++)
					{
						System.out.printf("%1$s: %2$s, %3$s inhabitants" + "\r\n", i + 1, cities[i].name, cities[i].inhabitants);
					}
					System.out.println();
					break;

				case "3":
					System.out.print("Enter the city number: ");
					currentCityNumber = Integer.parseInt(new Scanner(System.in).nextLine());
					System.out.printf("Enter a new data for a city number: %1$s" + "\r\n", currentCityNumber);
					System.out.printf("City name (was %1$s; hit ENTER to leave as is): ", cities[currentCityNumber - 1].name);
					textToModify = new Scanner(System.in).nextLine();
					if (!textToModify.equals(""))
					{
						cities[currentCityNumber - 1].name = textToModify;
					}
						System.out.printf("Inhabitants (was %1$s; hit ENTER to leave as is): ", cities[currentCityNumber - 1].inhabitants);
						textToModify = new Scanner(System.in).nextLine();
						if (!textToModify.equals(""))
						{
							cities[currentCityNumber - 1].inhabitants = Integer.parseInt(textToModify);
						}
							System.out.println();
							break;
				case "4":
					if (amount > maxCities - 1)
					{
						System.out.println("The database is full");
					}
					else
					{
						System.out.print("Enter the number of the city to modify: ");
						currentCityNumber = Integer.parseInt(new Scanner(System.in).nextLine());
						System.out.printf("Insert a new data at %1$s position: " + "\r\n",currentCityNumber);
						amount++;
						for (int i = (int)amount; i > currentCityNumber - 1; i--)
						{
							cities[i] = cities[i - 1].clone();
						}
						System.out.print("City name: ");
						cities[currentCityNumber - 1].name = new Scanner(System.in).nextLine();
						System.out.print("Inhabitants: ");
						cities[currentCityNumber - 1].inhabitants = Integer.parseInt(new Scanner(System.in).nextLine());
					}
					break;

				case "5":
					System.out.print("Enter the city number for delete: ");
					currentCityNumber = Integer.parseInt(new Scanner(System.in).nextLine());
					System.out.printf("Deleting the number %1$s" + "\r\n", currentCityNumber);
					for (int i = currentCityNumber - 1; i < amount; i++)
					{
						cities[i] = cities[i + 1].clone();
					}
					amount--;
					break;

				case "6":
					System.out.print("Enter the text to search: ");
					textToSearch = new Scanner(System.in).nextLine();
					found = false;
					for (int i = 0; i < amount; i++)
					{
						if (cities[i].name.toUpperCase().indexOf(textToSearch.toUpperCase()) >= 0)
						{
							System.out.printf("%1$s found in %2$s" + "\r\n", textToSearch, cities[i].name);
							found = true;
						}
					}
					if (!found)
					{
						System.out.println("Not found.");
					}
						break;

				case "7":
					for (int i = 0;i < amount;i++)
					{
						String lowerCaseName = cities[i].name.toLowerCase();
						String correctedName = lowerCaseName.substring(0, 1).toUpperCase() + lowerCaseName.substring(1);
						for (int j = 1; j < correctedName.length() - 2; j++)
						{
							if (correctedName.charAt(j) == ' ')
							{
							correctedName = correctedName.substring(0, j) + " " + correctedName.substring(j + 1, j + 1 + 1).toUpperCase() + correctedName.substring(j + 2);
							}
						}
						cities[i].name = correctedName;
					}
					break;

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

		} while (!finished);
	}
}
Exercisey 4.15

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.