The business domain we are modeling is The Dating Site.
We will create the following classes:
Profile to define profiles with interests, generate them, store them in an array, and then pass the array to another class MatchFinder (demonstrating passing data fields around on a method call) to find matches based on shared interests.
Step-by-Step Implementation
Step 1: Define the `Profile` Class
We'll start by defining a `Profile` class with properties such as `name`, `age`, and `interests`.
#### Step 2: Generate Profiles and Store Them in an Array
Next, we'll create several `Profile` objects and store them in an array.
Step 3: Define the `MatchFinder` Class
We'll create a `MatchFinder` class with a method to unpack the array and find matches based on shared interests.
### Complete Code
```kotlin
// Define the Profile class
class Profile(val name: String, val age: Int, val interests: List<String>) {
fun display() {
println("Name: $name, Age: $age, Interests: ${interests.joinToString(", ")}")
}
}
// Define the MatchFinder class
class MatchFinder {
fun findMatches(profileArray: Array<Any>, targetInterests: List<String>) {
val matches = mutableListOf<Profile>()
for (item in profileArray) {
// Cast each item to a Profile and check for matching interests
if (item is Profile) {
val commonInterests = item.interests.intersect(targetInterests)
if (commonInterests.isNotEmpty()) {
matches.add(item)
}
} else {
println("Unknown type")
}
}
// Display matches
println("Matches based on interests: ${targetInterests.joinToString(", ")}")
for (match in matches) {
match.display()
}
}
}
// Step 4: Pass the array to the findMatches method
matchFinder.findMatches(profiles as Array<Any>, targetInterests)
}
```
### Explanation
1. **Profile Class**:
- The `Profile` class has properties: `name`, `age`, and `interests`.
- The `display` method prints the profile details.
2. **MatchFinder Class**:
- The `MatchFinder` class has a method `findMatches` that takes an array of `Any` and a list of target interests.
- It iterates over the array, casts each item to a `Profile`, and checks for matching interests.
- If common interests are found, the profile is added to the matches list.
- After processing, it displays the matches.
3. **Main Function**:
- Creates an array of `Profile` objects with various interests.
- Creates an instance of `MatchFinder`.
- Defines target interests for matching.
- Passes the profiles array and target interests to the `findMatches` method.
Running the Program
When you run this program, it will create several `Profile` objects, store them in an array, pass this array to the `MatchFinder` class, and find matches based on shared interests. The output will look something like this:
This demonstrates the creation of profile objects, storing them in an array, passing the array to another class, and unpacking the array using casting to find matches based on shared interests.
Complete workflow: The steps to create these classes in IntelliJ IDEA.
### Step-by-Step Guide to Create Classes in IntelliJ IDEA
1. **Create a New Project (If not already created)**:
- Open IntelliJ IDEA.
- Click on `New Project`.
- Select `Kotlin` from the left menu.
- Choose `Kotlin/JVM` from the options.
- Click `Next`.
- Give your project a name (e.g., `TheDatingSite`) and specify the project location.
- Click `Finish`.
2. Create a New Kotlin File/Class for Main Function**:
- In the `Project` view, right-click on the `src` folder.
- Select `New` -> `Kotlin File/Class`.
- In the dialog that appears, enter `Main` as the name and select `File` from the options.
- Click `OK`.
- This will create `Main.kt` in the `src` folder.
3. Create the `Profile` Class
- In the `Project` view, right-click on the `src` folder.
- Select `New` -> `Kotlin File/Class`.
- In the dialog that appears, enter `Profile` as the name and select `Class` from the options.
- Click `OK`.
- This will create `Profile.kt` in the `src` folder.
- Add the `Profile` class code to `Profile.kt`:
```kotlin
class Profile(val name: String, val age: Int, val interests: List<String>) {
fun display() {
println("Name: $name, Age: $age, Interests: ${interests.joinToString(", ")}")
}
}
```
4. **Create the `MatchFinder` Class**:
- In the `Project` view, right-click on the `src` folder.
- Select `New` -> `Kotlin File/Class`.
- In the dialog that appears, enter `MatchFinder` as the name and select `Class` from the options.
- Click `OK`.
- This will create `MatchFinder.kt` in the `src` folder.
- Add the `MatchFinder` class code to `MatchFinder.kt`:
```kotlin
class MatchFinder {
fun findMatches(profileArray: Array<Any>, targetInterests: List<String>) {
val matches = mutableListOf<Profile>()
for (item in profileArray) {
// Cast each item to a Profile and check for matching interests
if (item is Profile) {
val commonInterests = item.interests.intersect(targetInterests)
if (commonInterests.isNotEmpty()) {
matches.add(item)
}
} else {
println("Unknown type")
}
}
// Display matches
println("Matches based on interests: ${targetInterests.joinToString(", ")}")
for (match in matches) {
match.display()
}
}
}
```
5. **Add the Main Function in `Main.kt`**:
- Open `Main.kt` and add the following code:
```kotlin
fun main() {
// Step 1: Create an array of Profile objects
val profiles = arrayOf(
Profile("Alice", 25, listOf("Reading", "Traveling", "Cooking")),
Profile("Bob", 30, listOf("Sports", "Cooking", "Gaming")),
Profile("Charlie", 28, listOf("Music", "Traveling", "Reading")),
Profile("David", 35, listOf("Gaming", "Technology", "Traveling")),
Profile("Eve", 22, listOf("Art", "Reading", "Traveling"))
)
// Step 2: Create an instance of MatchFinder
val matchFinder = MatchFinder()
// Step 3: Define target interests
val targetInterests = listOf("Reading", "Traveling")
// Step 4: Pass the array to the findMatches method
matchFinder.findMatches(profiles as Array<Any>, targetInterests)
}
```
1. **Run the Program**:
- Right-click on `Main.kt` in the `Project` view.
- Select `Run 'MainKt'`.
This will compile and run your Kotlin program, and you should see the output in the IntelliJ IDEA console, showing the matches based on interests.
### Summary
By following these steps, you can create the necessary classes in IntelliJ IDEA, generate profiles, store them in an array, pass the array to another class, and find matches based on interests using casting.
What the final output run looks like:
Want to print your doc? This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (