Collections 4
Collections 4
1. PROBLEM STATEMENT
XYZ Online Bookstore is revamping its user authentication and account management system
to improve security and user experience. As a software developer assigned to this project,
you are tasked with implementing a User class and an AccountManager class based on the
following requirements:
User Class
- Attributes:
- `username`: A string representing the username of the user.
- `password`: A string representing the password of the user.
- `email`: A string representing the email address of the user.
- Create getter and setter methods for all attributes.
- Implement a default constructor that initializes the `username`, `password`, and `email`
attributes to empty strings.
AccountManager Class
- Attributes:
- `userList`: A list to store instances of the User class.
- Create getter and setter methods for `userList`.
- Implement a default constructor that initializes `userList` as an empty list.
Requirements:
1. **registerUser(String username, String password, String email)**:
- Method to register a new user.
- Check if the `username` and `email` are unique. If either is already taken, return `false`
and do not register the user.
- If both `username` and `email` are unique, create a new User instance with the provided
details, add it to `userList`, and return `true`.
**Additional Functionality:**
- Implement a method `getUserByUsername(String username)` in AccountManager to
retrieve a User object by username from `userList`.
- Implement a method `deleteUser(String username)` in AccountManager to remove a user
from `userList` by username.
**Sample Input/Output:**
```
Enter action (register, login, reset password, update email, delete user): register
Enter username: alice
Enter password: pass123
Enter email: [email protected]
User registered successfully.
Enter action (register, login, reset password, update email, delete user): login
Enter username: alice
Enter password: pass123
Login successful. Welcome, Alice!
Enter action (register, login, reset password, update email, delete user): reset password
Enter username: alice
Enter new password: newpass456
Password reset successful.
Enter action (register, login, reset password, update email, delete user): update email
Enter username: alice
Enter new email: [email protected]
Email updated successfully.
Enter action (register, login, reset password, update email, delete user): delete user
Enter username: alice
Are you sure you want to delete user 'alice'? (yes/no): yes
User 'alice' deleted successfully.
Enter action (register, login, reset password, update email, delete user): login
Enter username: alice
Enter password: newpass456
Invalid credentials. Please try again.
```
---
===========================================================================
2. PROBLEM STATEMENT
ABC Electronics is a leading retailer specializing in electronic gadgets. The company wants to enhance
its inventory management system to efficiently track sales based on product categories. The IT
department has assigned you, a software developer, to implement a program based on the
requirements.
SalesTracker Class
salesSet =>A set to store sales records in the format of `customerName:product` pairs. And create
setter and getter and default constructor.
Requirement 1: addSalesRecord(String record) => Method to add a sales record to `salesSet`. Each
record is provided in the format `customerName:product`.
In the `MAIN` class, the program interacts with the user as follows:
Prompt the user to enter the number of sales records they want to add.
Collect the sales records from the user in the format `Customer Name:Product`.
Split the input string by the delimiter and invoke the `addSalesRecord` method to add the
sales records. After adding records:
Prompt the user to enter a product name to find the number of customers who purchased it.
Display the count of customers or print `"No customers found for <product>"` if no
customers are found.
Prompt the user to enter a product name to retrieve the list of customers who purchased it.
Display the list of customers or print `"No customers found for <product>"` if no customers
are found.
Prompt the user to enter a customer name to find the product purchased by that customer.
Display the product purchased by the customer or print `"No product found for <customer
name>"` if no product is found.
Sample Input/Output:
Input/Output 1:
Alice:Laptop
Bob:Monitor
Charlie:Keyboard
laptop
Monitor
Bob
Charlie
Input/Output 2:
Eve:Headphones
Frank:Tablet
smartphone
Headphones
Eve
adam
Input/Output 3:
Alice:Phone
Bob:TV
Charlie:Speaker
David:Phone
TV
Phone
Alice, David
carl
=================================================================================
3. PROBLEM STATEMENT
Queen Elizabeth II decided to go for the caffeine survey in England, she wants Sarah John to get all
the coffee brands present in the UK stores and the average percentage of caffeine present in it.
Sarah being a code enthusiast found it easy to calculate the average value through the codes.
Your task here is to complete the classes using the Specifications given below. Consider default
visibility of classes, data fields, and methods unless mentioned otherwise.
Specifications
Task
Class Caffeine
data member:
coffeeBrand: String
caffeinePercentage: float
visibility: private
Caffeine(String coffeeBrand, float caffeinePercentage): constructor with
public visibility
Define getter setters with public visibility
method definitions:
compare(Caffeine o1, Caffeine o2):
return type: int
visibility: public
class CaffeineSurvey
data member:
ArrayList<Caffeine> coffeeList
method definitions:
sortByBrandName():
return type: ArrayList<Caffeine>
visibility: public
getAvgPercentage():
return type: double
visibility: public
Class CaffeineSurvey
- define the ArrayList<Caffeine> variable coffeeList
Implement the below methods for this class:
-ArrayList<Caffeine> sortByBrandName():
• Write a code to get the coffeeList sorted in ascending order by coffeBrand
• Do not change the coffeeList to maintain the default order of the list
• Return the new sorted list
-double getAvgPercentage():
• Write a code to to find the average percentage from coffeeList.
• Return the average percentage.
Sample Input
Sample Output
==========================================================
4. PROBLEM STATEMENT
Can You Align:
Complete the classes using the Specifications given below. Consider default visibility of
classes, data fields, and methods unless mentioned otherwise.
Specifications:
TASK
Class Size
- define all the variables according to the above specifications.
class definitions:
class Size:
data members:
int area
visibility : private
Size(int area): constructor with public visibility
define getter and setter with public visibility
class Content:
data members:
String text
boolean isImage
visibility :private
class WebPage:
data members:
method definition:
checkArea(Size size):
return : boolean
visibility : public
canCompress(Content content):
return : boolean
visibility : public
compressSize(Size size):
return : int
visibility : public
- define a constructor with getter and setter according to the above specifications.
Class Content
- define all the variables according to the above specifications.
- define a constructor with getter and setter according to the above specifications.
Class WebPage
- define all the variables according to the above specifications.
- define a constructor according to the above specifications.
Implement the below methods for this class:
-String addPage(Size size, Content content):
• Write a code that adds the send parameter to the HashMap page on the basis of
defined
conditions.
• If the given size is greater than the totalArea then return "No space".
• If the given size is equal to or less than the totalArea then add it to the HashMap
(page)
according to the below conditions -
1. If the canCompress method returns true then add it to the HashMap(page) with
the size
returned by the compressSize method and return "Page added after compress".
2. If the canCompress method returns false then add it to the HashMap(page) and
returns
"Page added".
3. The initial value of the index is 0 and it got increments after every successful addition
to the
HashMap(page).
-String checkArea(Size size):
• Write a code that returns true if the totalArea object's area is greater than equal to the
size
object's area otherwise return false.
-String canCompress(Content content):
• Write a code that returns true if the content's text length is greater than 100 and
content's
isImage is true else return false.
-String compressSize(Size size):
• Write a code that returns area on the basis of the following condition -
1. If the area is greater than 100 then return 75% of the area.
2. Otherwise returns the are area without modifications.
Sample Input
Sample Output