0% found this document useful (0 votes)
3 views3 pages

Java Exception Handling and Inventory Management

The document outlines a tutorial for an Object Oriented Programming course, detailing four programming tasks. These tasks involve creating custom exception classes for user input validation, managing a to-do list with an ArrayList, and handling a library inventory system with book details. Each task includes specific requirements for input handling, error messages, and data management using Java programming concepts.

Uploaded by

bsuv07
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views3 pages

Java Exception Handling and Inventory Management

The document outlines a tutorial for an Object Oriented Programming course, detailing four programming tasks. These tasks involve creating custom exception classes for user input validation, managing a to-do list with an ArrayList, and handling a library inventory system with book details. Each task includes specific requirements for input handling, error messages, and data management using Java programming concepts.

Uploaded by

bsuv07
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Faculty of Computing

SE1020 – Object Oriented Programming


Year 1 Semester 2 (2025)
Tutorial 05

Question 01

Write a program that prompts the user to enter a username and password, then perform
the following validations using two different custom exception classes as follows.

• InvalidUserName, is an exception class that prints out the error message “The
username must be at least 6 characters long” if the number of characters in the
user input for the username is less than 6.

• InvalidPasswordLength, is an exception class that prints out the error message


“The password must be at least 8 characters long” if the number of characters in
the user input for the password is less than 8.

Write another class called DemoApp to get the user inputs for the username, and pass-
word and then validate those for the above two conditions. If the input has one or more
custom exceptions, you should have proper try-catch statements to handle the exceptions.

Sample Output:

1
Question 02

Implement a program that validates user input for item pricing in a Warehouse to ensure
the system maintains accurate records and catches any errors in data entry. Write a
program to allow the warehouse manager to enter the price of an item. The program
should validate the input using three custom exception classes as follows:

Create the following custom exceptions to handle different pricing errors:

• NegativePriceException: If the entered price is less than zero, throw this excep-
tion. Display the error message as ”Price cannot be negative. Please enter a valid
amount.”.
• PriceOutOfRangeException: If the price is not within the valid range of $1 to
$10,000, throw this exception. Display the error message as ”Price out of range.
Please enter a value between $1 and $10,000.
• PriceNotNumericException: If the entered price is not a numeric value, throw
this exception. Display the error message as ”Invalid input. Please enter a numeric
value for the price.”

b) Create another class, InventoryProcessor, to prompt the user for item pricing and
manage input validation. Use the exception classes to handle errors in the input. Ensure
that the program includes appropriate try-catch blocks to handle each exception and
provide clear error messages for the user.

Question 03

Develop a Java program for managing a simple to-do list using ArrayList. The program
should perform the following tasks:

(a) Create an empty ArrayList to store task descriptions. Prompt the user to
enter tasks one by one (each as a String); keep accepting inputs until the user
enters ”-99”. Add each entered task to the ArrayList.
(b) Once the user finishes entering tasks (”-99”), ask the user to enter the index
of the task that needs to be removed.
• If the index is valid (between 0 and [Link]() – 1), remove the correspond-
ing task and display “Task removed successfully.”
• If the index is out of bounds, display “Invalid index. No task re-
moved.”
[Hint: use [Link]() and [Link](int index)]
(c) Find and display the total number of tasks currently in the ArrayList.
(d) Print all the remaining tasks in the ArrayList, one per line.

2
Question 04

Develop a Java program to manage a Library Inventory using an ArrayList. The


program should perform the following tasks:

(a) Define a Book class with two attributes as isbn, and title. Include a con-
structor to initialize both fields and a method displayDetails() that prints the
book’s ISBN and title.
(b) Implement a class, create an empty ArrayList named inventory. Prompt the
user to enter book details one at a time: first the ISBN, then the title. Keep
accepting entries until the user enters ”-99” as the ISBN. For each valid entry,
instantiate a new Book and add it to inventory.
(c) After input is complete, prompt the user to enter the ISBN of the book they
wish to remove from the inventory
• If a Book with that ISBN exists in inventory, remove it and display Book
removed successfully.”.
• If not found, display “ISBN not found. No book removed.”
(d) Display the total number of books currently in inventory.
(e) Print all remaining books in the inventory by calling each object’s
displayDetails() method.

You might also like