Skip to content

Commit d4f3217

Browse files
authored
code
1 parent 237f2dd commit d4f3217

13 files changed

Lines changed: 537 additions & 0 deletions

File tree

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
class LRUCache {
2+
constructor(capacity) {
3+
this.map = new Object();
4+
this.size = 0; // to track the total number of items which should not be more than capacity
5+
this.store = new DoublyLinkedList();
6+
this.capacity = capacity; // max size of the cache
7+
}
8+
get(key) {
9+
// if key is not present
10+
if (!this.map[key]) {
11+
return -1;
12+
}
13+
// if key is present , remove the node from its position and put it at the tail as most recently used
14+
const targetNode = this.map[key];
15+
targetNode.remove();
16+
this.map[targetNode.key] = this.store.append(targetNode.key, targetNode.val);
17+
return targetNode.val;
18+
}
19+
put(key, val) {
20+
// if the node is present in cache
21+
if (this.map[key]) {
22+
const targetNode = this.map[key];
23+
targetNode.remove();
24+
this.size -= 1;
25+
}
26+
// in either case, a new node is created and put as the most recently used at the tail
27+
const newNode = this.store.append(key, val);
28+
this.map[key] = newNode;
29+
this.size += 1;
30+
if (this.size > this.capacity) {
31+
const firstNode = this.store.removeFirst(); // remove the least recently used
32+
delete this.map[firstNode.key];
33+
this.size -= 1;
34+
}
35+
}
36+
}
37+
38+
39+
40+
// Creating a node class of doubly LL
41+
class ListNode {
42+
constructor(key = null, val = null) {
43+
this.key = key;
44+
this.val = val;
45+
this.prev = null;
46+
this.next = null;
47+
}
48+
remove() {
49+
this.prev.next = this.next;
50+
this.next.prev = this.prev;
51+
this.prev = null;
52+
this.next = null;
53+
}
54+
}
55+
56+
57+
// Creating a doubly linked list
58+
class DoublyLinkedList {
59+
constructor() {
60+
this.head = new ListNode();
61+
this.tail = new ListNode();
62+
this.head.next = this.tail;
63+
this.tail.prev = this.head;
64+
}
65+
append(key, val) {
66+
const newNode = new ListNode(key, val);
67+
const lastNode = this.tail.prev;
68+
69+
lastNode.next = newNode;
70+
newNode.prev = lastNode;
71+
72+
this.tail.prev = newNode;
73+
newNode.next = this.tail;
74+
75+
return newNode;
76+
}
77+
removeFirst() {
78+
const firstNode = this.head.next;
79+
firstNode.remove();
80+
return firstNode;
81+
}
82+
}
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
2+
const BookStatus = {
3+
AVAILABLE: 'AVAILABLE',
4+
LOANED: 'LOANED',
5+
LOST: 'LOST'
6+
};
7+
const AccountStatus = {
8+
ACTIVE: 'ACTIVE',
9+
INACTIVE: 'INACTIVE',
10+
CLOSED: 'CLOSED',
11+
BLACKLISTED: 'BLACKLISTED'
12+
};
13+
14+
class LibraryCard {
15+
constructor() {
16+
this.cardNumber = this.generateRandomCardNumber();
17+
this.barCode = this.generateBarcode();
18+
this.issuedAt = new Date();
19+
this.isActive = true;
20+
}
21+
22+
setActive(status) {
23+
this.isActive = status;
24+
}
25+
}
26+
27+
class Account {
28+
constructor(name, email, password, status) {
29+
this.userId = this.geenrateRandomeID();
30+
this.name = name;
31+
this.email = email;
32+
this.password = password;
33+
this.status = status;
34+
this.libraryCard = new LibraryCard();
35+
}
36+
37+
login(email, password) {
38+
if (email === this.email && this.password === password) {
39+
console.log('Logged IN');
40+
this.status = AccountStatus.ACTIVE;
41+
}
42+
else {
43+
console.log('Wrong Credentials')
44+
}
45+
}
46+
logout() {
47+
this.status = AccountStatus.INACTIVE;
48+
};
49+
resetPassword(oldPassword, newPassword) {
50+
if (oldPassword === this.password) {
51+
this.password = newPassword;
52+
}
53+
else {
54+
alert('Old Password does not match')
55+
}
56+
};
57+
closeAccount() {
58+
this.status = AccountStatus.CLOSED;
59+
}
60+
}
61+
62+
class Member extends Account {
63+
constructor() {
64+
this.booksBorrowed = 0;
65+
this.searchBook = new Search();
66+
this.dateOfMemeberShip = new Date();
67+
}
68+
69+
payFine(amount) {
70+
71+
}
72+
checkForFine() {
73+
74+
}
75+
}
76+
77+
class Librarian extends Account {
78+
constructor() {
79+
this.issueService = new BookIssueService();
80+
this.searchBook = new Search();
81+
}
82+
83+
addBook(book);
84+
deleteBook(book_id);
85+
// return the updated book
86+
updateBook(book);
87+
blockMember(member);
88+
unBlockMember(member);
89+
cancelMembership(member);
90+
}
91+
92+
class Search {
93+
getBookByTitle(title) {
94+
return this.callAPI(title)
95+
};
96+
getBookByAuthor(author) {
97+
return this.callAPI(author)
98+
};
99+
getBookByCateogry(bookType) {
100+
return this.callAPI(bookType)
101+
};
102+
}
103+
104+
class Book {
105+
constructor(title, author, category, shelf, floor, status) {
106+
this.bookId = this.geenrateRandomeID();
107+
this.title = title;
108+
this.author = author;
109+
this.category = category;
110+
this.location = new Location(shelf, floor);
111+
this.status = status;
112+
}
113+
setBookStatus(status) {
114+
this.status = status;
115+
}
116+
setBorrowDate(date) {
117+
this.borrowDate = date;
118+
this.setDueDate(this.borrowDate + 15)
119+
}
120+
setDueDate(dueDate) {
121+
this.dueDate = dueDate;
122+
}
123+
}
124+
125+
class Location {
126+
constructor(shelf, floor) {
127+
this.shelfNo = shelf;
128+
this.floorNo = floor;
129+
}
130+
131+
getLocation() {
132+
133+
}
134+
}
135+
136+
class BookIssueService {
137+
calculateFine(days, user, book) {
138+
139+
};
140+
collectFine(user, days) {
141+
142+
};
143+
issueBook(book, user) {
144+
return dueDate
145+
};
146+
renewBook(book, user) {
147+
return dueDate;
148+
};
149+
returnBook(book, user) {
150+
151+
};
152+
}
153+
154+
155+
class BookIssueDetail {
156+
constructor(book, startDate, user) {
157+
this.book = book;
158+
this.startDate = startDate;
159+
this.user = user;
160+
this.dueDate = startDate + 15;
161+
}
162+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// https://prakhargurawa.medium.com/design-a-music-player-with-shuffle-functionality-f9d6f53ff028
2+
3+
class MusicPlayer {
4+
constructor() {
5+
this.totalSongCount = 0;
6+
this.musicList = [];
7+
this.musicQ = [];
8+
this.random = 0;
9+
this.addSong = this.addSong.bind(this);
10+
this.playRandomSong = this.playRandomSong.bind(this);
11+
this.playSong = this.playSong.bind(this);
12+
this.closeMusicPlayer = this.closeMusicPlayer.bind(this);
13+
this.printList = this.printList.bind(this);
14+
}
15+
addSong(song) {
16+
console.log(song.songName);
17+
this.musicList.splice(this.totalSongCount, 0, song);
18+
this.totalSongCount++;
19+
}
20+
playSong(id, addToQ) {
21+
for (let i = 0; i < this.totalSongCount; i++) {
22+
let song = this.musicList[i];
23+
console.log("Playing song " + song.songName);
24+
if (song.id === id) {
25+
if (addToQ) this.musicQ.push(song);
26+
this.musicList.splice(i, 1);
27+
this.musicList.push(song);
28+
this.totalSongCount--;
29+
if (this.totalSongCount < 0) {
30+
this.totalSongCount = this.musicList.length;
31+
}
32+
}
33+
}
34+
for (let i = this.totalSongCount; i < this.musicList.length; i++) {
35+
let song = this.musicList[i];
36+
if (song.id === id) {
37+
if (addToQ) this.musicQ.push(song);
38+
}
39+
}
40+
}
41+
playRandomSong() {
42+
this.random = Math.floor(Math.random() * this.totalSongCount);
43+
let song = this.musicList[this.random];
44+
console.log("Playing song " + song.songName + " at index " + this.random);
45+
this.musicQ.push(song);
46+
this.musicList.splice(this.random, 1);
47+
this.musicList.push(song);
48+
this.totalSongCount--;
49+
if (this.totalSongCount < 0) {
50+
this.totalSongCount = this.musicList.length;
51+
}
52+
this.printList();
53+
}
54+
closeMusicPlayer() {
55+
console.log("Closing Music Player");
56+
this.musicQ = [];
57+
this.totalSongCount = this.musicList.length;
58+
}
59+
printList() {
60+
console.log("Total Songs Count", this.totalSongCount);
61+
console.log(
62+
"MusicList :",
63+
this.musicList.map((ele) => `${ele.id}-${ele.songName}`)
64+
);
65+
console.log(
66+
"Music Queue: ",
67+
this.musicQ.map((ele) => `${ele.id}-${ele.songName}`)
68+
);
69+
}
70+
}
71+
72+
export default MusicPlayer;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
class Song {
3+
constructor(id, songName, singerName, genre) {
4+
this.id = id;
5+
this.songName = songName;
6+
this.singerName = singerName;
7+
this.genre = genre;
8+
}
9+
}
10+
11+
export default Song;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import MusicPlayer from "./MusicPlayer";
2+
import Song from "./Song";
3+
4+
let songs = [
5+
new Song(1, "Levitating", "Dua Lipa", "Hollywood"),
6+
new Song(2, "Love you Zindagi", "Alia Bhatt", "Bollywood"),
7+
new Song(3, "Dil", "Villain", "Bollywood"),
8+
new Song(4, "I Like me better", "Selena", "Hollywood"),
9+
new Song(5, "LoveStory", "Taylor", "Hollywood")
10+
];
11+
12+
let musicPlayer = new MusicPlayer();
13+
14+
songs.forEach((song) => musicPlayer.addSong(song));
15+
musicPlayer.printList();
16+
17+
[1, 2, 3, 4, 5, 6, 7].forEach((_) => musicPlayer.playRandomSong());
18+
musicPlayer.playSong(2, false);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Board {
2+
constructor(size) {
3+
this.size = size;
4+
}
5+
}
6+
7+
export default Board;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Dice {
2+
constructor() {
3+
this.maxNumber = 6
4+
}
5+
rollDice() {
6+
return Math.floor(Math.random() * this.maxNumber);
7+
}
8+
}
9+
10+
export default Dice;

0 commit comments

Comments
 (0)