Exercise tasks Java Exercise - Java Programming Course

 Exercise

Exercise tasks

 Objetive

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 and year)
• Description of task
• Level of importance (1 to 10)
• Category

The program should allow the user the following operations:

1 - Add a new task (the date must "seem correct": day 1 to 31, month 1 to 12, year between 1000 and 3000).

2 - Show the tasks between two certain dates (day, month and year). If the user presses Enter without specifying date, it will be taken as "today". It must display the number of each record, the date (DD/ MM/YYYY), description, category and importance, all in the same line, separated with hyphens.

3 - Find tasks that contain a certain text (in description or category, not case sensitive). It will display number, date and description (only 50 letters, in case it was be longer). The user should be notified if none is found.

4 - Update a record (it will ask for the number, will display the previous value of each field and the user can press Enter not to modify any of the data). The user should be warned (but not asked again) if he enters an incorrect record number. It is not necessary to validate any of the fields.

5 - Delete some data, between two positions indicated by the user. The user should be warned (but not asked again) if he enters any incorrect record number. Each record to be deleted must be displayed, and the user must be asked for confirmation.

6 - Sort the data alphabetically by date and (if two dates are the same) by description.

7 - Find Duplicates: If two records have the same description, both will be displayed on-screen.

Q - Quit (end the application; as we do not store the information, it will be lost).

(Hint: you can know the current date using DateTime.Now.Day, DateTime.Now.Month and DateTime.Now.Year).

 Example Code

import java.util.*;
public class Main
{
	private final static class DateType
	{
		public short year;
		public byte month;
		public byte day;

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

			varCopy.year = this.year;
			varCopy.month = this.month;
			varCopy.day = this.day;

			return varCopy;
		}
	}

	private final static class TaskType
	{
		public DateType date = new DateType();
		public String description;
		public byte level;
		public String category;

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

			varCopy.date = this.date.clone();
			varCopy.description = this.description;
			varCopy.level = this.level;
			varCopy.category = this.category;

			return varCopy;
		}
	}

	public static void main(String[] args)
	{
		int capacity = 2000;
		TaskType[] tasks = new TaskType[capacity];
		char option;
		int counter = 0;
		String search, newValue;
		boolean found;

		do
		{
			System.out.println();
			System.out.println("Tasks database");
			System.out.println();
			System.out.println("1- Add a new task.");
			System.out.println("2- Show the tasks between two certain dates.");
			System.out.println("3- Find tasks that contain a certain text.");
			System.out.println("4- Update a record.");
			System.out.println("5- Delete some data, between two positions indicated.");
			System.out.println("6- Sort the data alphabetically by date.");
			System.out.println("7- Find Duplicates.");
			System.out.println("Q- Quit.");
			System.out.println("Enter an option:");
			option = (char)(new Scanner(System.in).nextLine().toUpperCase());

			switch (option)
			{
				case '1': //Add a new task
					if (counter < capacity)
					{
						System.out.print("Enter the Description of the task: ");
						tasks[counter].description = new Scanner(System.in).nextLine();

						System.out.print("Enter the Level of the task (1-10): ");
						tasks[counter].level = Byte.parseByte(new Scanner(System.in).nextLine());

						System.out.print("Enter the Category of the task: ");
						tasks[counter].category = new Scanner(System.in).nextLine();

						do
						{
							System.out.print("Enter the Day of the task (1 to 31): ");
							tasks[counter].date.day = Byte.parseByte(new Scanner(System.in).nextLine());
							if (tasks[counter].date.day < 1 || tasks[counter].date.day > 31)
							{
								System.out.println("Not a valid day!");
							}
						} while (tasks[counter].date.day < 1 || tasks[counter].date.day > 31);

						do
						{
							System.out.print("Enter the Month of the task (1 to 12): ");
							tasks[counter].date.month = Byte.parseByte(new Scanner(System.in).nextLine());
							if (tasks[counter].date.month < 1 || tasks[counter].date.month > 12)
							{
								System.out.println("Not a valid month!");
							}
						} while (tasks[counter].date.month < 1 || tasks[counter].date.month > 12);

						do
						{
							System.out.print("Enter the Year of the task: ");
							tasks[counter].date.year = Short.parseShort(new Scanner(System.in).nextLine());
							if (tasks[counter].date.year < 1000 || tasks[counter].date.year > 3000)
							{
								System.out.println("Not a valid year!");
							}
						} while (tasks[counter].date.year < 1000 || tasks[counter].date.year > 3000);
						counter++;
					}
					else
					{
						System.out.println("Database full.");
					}
					break;

				case '2': //Show the tasks between two certain dates
					if (counter >= 1)
					{
						byte startDay, startMonth;
						short startYear;
						byte endDay, endMonth;
						short endYear;

						System.out.println("Starting day: ");
						String number = new Scanner(System.in).nextLine();

						if (number.equals(""))
						{
							startDay = (byte)java.time.LocalDateTime.now().getDayOfMonth();
						}
						else
						{
							startDay = Byte.parseByte(number);
						}

						System.out.println("Starting month: ");
						number = new Scanner(System.in).nextLine();
						if (number.equals(""))
						{
							startMonth = (byte)java.time.LocalDateTime.now().getMonthValue();
						}
						else
						{
							startMonth = Byte.parseByte(number);
						}

						System.out.println("Starting year: ");
						number = new Scanner(System.in).nextLine();
						if (number.equals(""))
						{
							startYear = (short)java.time.LocalDateTime.now().getYear();
						}
						else
						{
							startYear = Short.parseShort(number);
						}

						System.out.println("Final day: ");
						number = new Scanner(System.in).nextLine();
						if (number.equals(""))
						{
							endDay = (byte)java.time.LocalDateTime.now().getDayOfMonth();
						}
						else
						{
							endDay = Byte.parseByte(number);
						}

						System.out.println("Final month: ");
						number = new Scanner(System.in).nextLine();
						if (number.equals(""))
						{
							endMonth = (byte)java.time.LocalDateTime.now().getMonthValue();
						}
						else
						{
							endMonth = Byte.parseByte(number);
						}

						System.out.println("Final year: ");
						number = new Scanner(System.in).nextLine();
						if (number.equals(""))
						{
							endYear = (short)java.time.LocalDateTime.now().getYear();
						}
						else
						{
							endYear = Short.parseShort(number);
						}

						String startDate = "" + startYear + (new Byte(startMonth)).toString("00") + (new Byte(startDay)).toString("00");

						String endDate = "" + endYear + (new Byte(endMonth)).toString("00") + (new Byte(endDay)).toString("00");

						for (int i = 0; i < counter; i++)
						{
							String currentDate = "" + tasks[i].date.year + (new Byte(tasks[i].date.month)).toString("00") + (new Byte(tasks[i].date.day)).toString("00");
							if (currentDate.compareTo(startDate) >= 0 && currentDate.compareTo(endDate) <= 0)
							{
								System.out.printf("The number is %1$s: %2$s/%3$s/" + "%4$s - %5$s - %6$s - %7$s." + "\r\n", i + 1, tasks[i].date.day, tasks[i].date.month, tasks[i].date.year, tasks[i].description, tasks[i].category, tasks[i].level);
							}
						}
					}
					else
					{
						System.out.println("Database empty.");
					}
					break;

				case '3': //Find tasks that contain a certain text
					if (counter >= 1)
					{
						System.out.print("Enter the text to search: ");
						search = new Scanner(System.in).nextLine();
						found = false;
						String newValue5;

						for (int i = 0; i < counter; i++)
						{
							if (tasks[i].description.indexOf(search) >= 0 || tasks[i].category.indexOf(search) >= 0)
							{
								if (tasks[i].description.length() > 50)
								{
									newValue5 = tasks[i].description.substring(0, 50);
								}
								else
								{
									newValue5 = tasks[i].description;
								}

								found = true;
								System.out.printf("%1$s: %2$s/%3$s/%4$s - %5$s" + "\r\n", i + 1, tasks[i].date.day, tasks[i].date.month, tasks[i].date.year, newValue5);
							}
						}
						if (!found)
						{
							System.out.println("Not found.");
						}
					}
					else
					{
						System.out.println("Database empty.");
					}
					break;

				case '4': //Update a record
					if (counter >= 1)
					{
						System.out.print("Enter the number of the task to update: ");
						int update = Integer.parseInt(new Scanner(System.in).nextLine()) - 1;
						if ((update >= 0) && (update < counter))
						{
							System.out.printf("Description (%1$s): ", tasks[update].description);
							newValue = new Scanner(System.in).nextLine();
							if (!newValue.equals(""))
							{
								tasks[update].description = newValue;
							}

							System.out.printf("Level (%1$s): " + "\r\n", tasks[update].level);
							newValue = new Scanner(System.in).nextLine();
							if (!newValue.equals(""))
							{
								tasks[update].level = Byte.parseByte(newValue);
							}

							System.out.printf("Category (%1$s): " + "\r\n", tasks[update].category);
							newValue = new Scanner(System.in).nextLine();
							if (!newValue.equals(""))
							{
								tasks[update].category = newValue;
							}

							System.out.printf("Year (%1$s): " + "\r\n", tasks[update].date.year);
							newValue = new Scanner(System.in).nextLine();
							if (!newValue.equals(""))
							{
								tasks[update].date.year = Short.parseShort(newValue);
							}

							System.out.printf("Month (%1$s): " + "\r\n", tasks[update].date.month);
							newValue = new Scanner(System.in).nextLine();
							if (!newValue.equals(""))
							{
								tasks[update].date.month = Byte.parseByte(newValue);
							}

							System.out.printf("Day (%1$s): " + "\r\n", tasks[update].date.day);
							newValue = new Scanner(System.in).nextLine();
							if (!newValue.equals(""))
							{
								tasks[update].date.day = Byte.parseByte(newValue);
							}
						}
						else
						{
							System.out.println("Wrong number entered.");
						}
					}
					else
					{
						System.out.println("Database empty.");
					}
					break;

				case '5': //Delete some data, between two positions indicated by the user
					if (counter >= 1)
					{
						System.out.print("Enter the first number of data to delete: ");
						int delete = Integer.parseInt(new Scanner(System.in).nextLine()) - 1;
						System.out.print("Enter the second number of data to delete: ");
						int delete2 = Integer.parseInt(new Scanner(System.in).nextLine()) - 1;

						for (int pos = delete; pos <= delete2; pos++)
						{
							for (int i = delete; i < counter; i++)
							{
								tasks[i] = tasks[i + 1].clone();
							}
							counter--;
						}
					}
					else
					{
						System.out.println("Database empty.");
					}
					break;

				case '6': //Sort the data alphabetically on date + description
					for (int i = 0; i < counter - 1; i++)
					{
						String firstDate = "" + tasks[i].date.year + (new Byte(tasks[i].date.month)).toString("00") + (new Byte(tasks[i].date.day)).toString("00") + tasks[i].description;
						for (int j = i + 1; j < counter; j++)
						{
							String secondDate = "" + tasks[j].date.year + (new Byte(tasks[j].date.month)).toString("00") + (new Byte(tasks[j].date.day)).toString("00") + tasks[j].description;

							if (firstDate.compareTo(secondDate) > 0)
							{
								TaskType aux = tasks[i].clone();
								tasks[i] = tasks[j].clone();
								tasks[j] = aux.clone();
							}
						}
					}
					break;

				case '7': //Find Duplicates
					for (int i = 0; i < counter - 1; i++)
					{
						for (int j = i + 1; j < counter; j++)
						{
							if (tasks[i].description.equals(tasks[j].description))
							{
								System.out.printf("%1$s - %2$s/%3$s/%4$s" + "\r\n", tasks[i].description, tasks[i].date.day, tasks[i].date.month, tasks[i].date.year);
								System.out.printf("%1$s - %2$s/%3$s/%4$s" + "\r\n", tasks[j].description, tasks[j].date.day, tasks[j].date.month, tasks[j].date.year);
							}
						}
					}
					break;

				case 'Q': //Quit
					System.out.println("Quitting...");
					break;

				default:
					System.out.println("You entered a wrong option. Please re-enter it.");
					break;
			}
		} while (option != 'Q');
	}
}

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...
 Books database
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...
 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...
 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.