Skip to content

Latest commit

 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Social Network Simulation

A terminal application that models a social network as an undirected graph. Users are nodes, friendships are edges. Supports adding users, managing friendships, finding mutual friends, and generating friend suggestions.


What it demonstrates

  • Graph theory — undirected graph where every edge (friendship) goes both ways
  • Adjacency list — each user maps to a HashSet of their friend IDs
  • Set intersection — mutual friends are found by intersecting two friend sets
  • Friends-of-friends traversal — friend suggestions by walking two levels deep in the graph
  • OOP designUser as a node, SocialGraph as the graph engine, Main as the interface
  • Lombok@Getter and @Setter annotations on User.java replace boilerplate accessor methods

File Structure

social-network/
├── Main.java        — entry point, menu, all user interaction (comments here)
├── User.java        — node with Lombok @Getter / @Setter
├── SocialGraph.java — adjacency list graph + all operations
├── Makefile
└── lombok.jar       — auto-downloaded on first make run

Data structures inside SocialGraph

Structure Role
HashMap<Integer, User> Look up any user by ID in O(1)
HashMap<Integer, Set<Integer>> Adjacency list — maps each user to their set of friend IDs
HashSet<Integer> Each individual friend list — O(1) add, remove, lookup

Each user (node) has:

Field Type Notes
ID int Auto-assigned, unique
Name String Entered by user
Age int Entered by user
Bio String Short description

Requirements

  • Java 14 or higher
  • Internet connection on first run (to download Lombok)

Check your Java version:

java -version

Dependencies

This project uses Lombok to eliminate boilerplate getters and setters in User.java.

lombok.jar is automatically downloaded the first time you run make run — no manual setup needed.


How to Run

cd social-network
make run

On first run, make downloads lombok.jar then compiles and launches the program.

Other Makefile commands:

Command Action
make Download Lombok (if needed) and compile
make run Compile then launch the program
make clean Delete all .class files

Menu Options

  ┌─────────────────────────────────┐
  │  1  Add user                    │
  │  2  Add friendship              │
  │  3  Remove friendship           │
  │  4  View profile & friends      │
  │  5  Mutual friends              │
  │  6  Friend suggestions          │
  │  7  List all users              │
  │  8  Network map                 │
  │  0  Exit                        │
  └─────────────────────────────────┘

Interacting with the Program

1 — Add a user

Enter name, age, and a short bio. An ID is assigned automatically.

── Add User ────────────────────────────────
Name : Alice
Age  : 24
Bio  : Loves hiking

User added:
  [1] Alice              age: 24    Loves hiking
[Network]  users: 1  |  friendships: 0

2 — Add a friendship

Pick two user IDs. The friendship is added in both directions (undirected edge).

── Add Friendship ──────────────────────────
User ID :  1
Friend ID: 2

Alice ──── Bob  (friendship added)
[Network]  users: 4  |  friendships: 1

3 — Remove a friendship

Enter the two user IDs. The edge is removed from both sides.

── Remove Friendship ───────────────────────
User ID :  1
Friend ID: 2

Alice and Bob are no longer friends.

4 — View profile & friends

Shows a user's full profile and their current friend list.

── View Profile ────────────────────────────
  ──────────────────────────────────────────────────
  Name:      Alice
  Age:       24
  Bio:       Loves hiking
  Friends:   2
  ──────────────────────────────────────────────────
  Friend list:
    ↳ Bob (ID 2)
    ↳ Carla (ID 3)

5 — Mutual friends

Enter two user IDs. Returns every user who is a friend of both.

── Mutual Friends ──────────────────────────
First user ID :  1
Second user ID: 2

Mutual friends between Alice and Bob:
────────────────────────────────────────
  ↳ Carla (ID 3)
────────────────────────────────────────
Total: 1

How it works: the program intersects Alice's friend set and Bob's friend set. Anyone appearing in both is a mutual friend.

6 — Friend suggestions

Enter a user ID. Returns people who are friends-of-friends but not yet friends with that user.

── Friend Suggestions ──────────────────────
User ID: 1

Suggestions for Alice (friends of friends):
────────────────────────────────────────
  ↳ Dan (ID 4)
────────────────────────────────────────

How it works: for each of Alice's friends, the program looks at their friends. Anyone not already connected to Alice (and not Alice herself) becomes a suggestion.

7 — List all users

Shows every user in a table with their friend count.

── All Users ───────────────────────────────
────────────────────────────────────────────────────────────
ID     Name               Age    Friends Bio
────────────────────────────────────────────────────────────
1      Alice              24     2      Loves hiking
2      Bob                26     2      Gamer
3      Carla              23     3      Artist
4      Dan                28     1      Chef
────────────────────────────────────────────────────────────
[Network]  users: 4  |  friendships: 4

8 — Network map

Prints the full adjacency list — every user on one line with arrows to all their connections.

── Network Map (Adjacency List) ────────────

Alice (1)       ──── Bob (2), Carla (3)
Bob (2)         ──── Alice (1), Carla (3)
Carla (3)       ──── Alice (1), Bob (2), Dan (4)
Dan (4)         ──── Carla (3)
[Network]  users: 4  |  friendships: 4

Example graph visualised

After adding the friendships above, the graph looks like this:

Alice ── Bob
  \      /
   Carla
     |
    Dan
  • Alice and Bob share Carla as a mutual friend
  • Alice gets Dan as a friend suggestion (Carla's friend, not yet Alice's)

Error Handling

Situation Response
Empty name on add Name cannot be empty.
Non-numeric age input Invalid input — please enter a whole number.
ID not found One or both users not found.
Adding duplicate friendship Alice and Bob are already friends.
Removing non-existent friendship Alice and Bob are not friends.
User tries to friend themselves A user cannot befriend themselves.
Suggestions with no connections No suggestions yet — add more friendships to generate some.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages