Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions README.md

This file was deleted.

29 changes: 29 additions & 0 deletions Week 4/Bonus Task/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
### IntelliJ IDEA ###
out/
!**/src/main/**/out/
!**/src/test/**/out/

### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
bin/
!**/src/main/**/bin/
!**/src/test/**/bin/

### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/

### VS Code ###
.vscode/

### Mac OS ###
.DS_Store
3 changes: 3 additions & 0 deletions Week 4/Bonus Task/.idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions Week 4/Bonus Task/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Week 4/Bonus Task/.idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions Week 4/Bonus Task/.idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions Week 4/Bonus Task/Bonus Task.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
25 changes: 25 additions & 0 deletions Week 4/Bonus Task/src/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import java.util.*;
public class Main {
public static void main(String[] args) {
StringManipulation sm = new StringManipulation();
StringTokenizerManipulation stm = new StringTokenizerManipulation();

Scanner scanner = new Scanner(System.in);
String sentence = "Hello / World, Hello /word: Hello -World ?";
String word = "World";

System.out.println("number of occurrences of " + word);
System.out.println("Using StringTokenizer Class: " + stm.wordCounts(sentence,word));
System.out.println("Using String Class(Method 1): " + sm.wordCounts1(sentence,word));
System.out.println("Using String Class(Method 1): " + sm.wordCounts2(sentence,word));



word = "Hello";
System.out.println("\nnumber of occurrences of " + word);
System.out.println("Using StringTokenizer Class: " + stm.wordCounts(sentence,word));
System.out.println("Using String Class(Method 1): " + sm.wordCounts1(sentence,word));
System.out.println("Using String Class(Method 1): " + sm.wordCounts2(sentence,word));

}
}
37 changes: 37 additions & 0 deletions Week 4/Bonus Task/src/StringManipulation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import java.util.HashMap;
import java.util.StringTokenizer;
public class StringManipulation {

String removeSpecialLetters(String sentence){
StringBuilder sb = new StringBuilder(sentence);
for(int i=0 ; i<sb.length();i++){
if(!Character.isLetter(sb.charAt(i)))
sb.replace(i,i+1," ");
}
return sb.toString();
}


public int wordCounts1 (String sentence, String word){
sentence = removeSpecialLetters(sentence);
String[] words = sentence.split(" ");
int count = 0;
for (String w : words) {
if (w.equals(word)) count++;
}
return count;
}

public int wordCounts2 (String sentence, String word){
sentence = removeSpecialLetters(sentence);
int count = 0;
int begin = 0;
for(int i=0;i<sentence.length();i++){
if(sentence.charAt(i) == ' '){
if(sentence.substring(begin,i).equals(word)) count++;
begin = i+1;
}
}
return count;
}
}
13 changes: 13 additions & 0 deletions Week 4/Bonus Task/src/StringTokenizerManipulation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import java.util.StringTokenizer;

public class StringTokenizerManipulation {
public int wordCounts(String sentence, String word){
sentence = new StringManipulation().removeSpecialLetters(sentence);
StringTokenizer st = new StringTokenizer(sentence);
int count = 0;
while (st.hasMoreElements()) {
if (word.equals(st.nextToken())) count++;
}
return count;
}
}
29 changes: 29 additions & 0 deletions Week 4/Library System/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
### IntelliJ IDEA ###
out/
!**/src/main/**/out/
!**/src/test/**/out/

### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
bin/
!**/src/main/**/bin/
!**/src/test/**/bin/

### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/

### VS Code ###
.vscode/

### Mac OS ###
.DS_Store
8 changes: 8 additions & 0 deletions Week 4/Library System/.idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions Week 4/Library System/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Week 4/Library System/.idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Empty file.
Empty file.
11 changes: 11 additions & 0 deletions Week 4/Library System/Library System.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
37 changes: 37 additions & 0 deletions Week 4/Library System/src/Book.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
public class Book {
private String book_title , author_name;
private int book_id;
private double book_version;
private boolean isAvailable;

public Book(String book_title,String author_name,int book_id,double book_version) {
this.book_title = book_title;
this.book_id = book_id;
this.author_name = author_name;
this.book_version = book_version;
this.isAvailable = true;
}

public String getBook_title() {
return book_title;
}

public String getAuthor_name() {
return author_name;
}

public int getBook_id() {
return book_id;
}

public double getBook_version() {
return book_version;
}

public boolean isAvailable() {
return isAvailable;
}
public void setAvailable(boolean available){
this.isAvailable = available;
}
}
129 changes: 129 additions & 0 deletions Week 4/Library System/src/Library.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
import java.util.ArrayList;

public class Library {
private String library_name;
private int library_code;
private ArrayList<Book> books = new ArrayList<>() ;
private ArrayList<User> users = new ArrayList<>();

public Library(String library_name,int library_code){
this.library_name = library_name;
this.library_code = library_code;
}
public void addUser(User user){
users.add(user);
System.out.println("User added successfully!");
}
public void addBook(Book book){
books.add(book);
System.out.println("Book added successfully!");
}

// This method removed a book from a collection of books by taking an integer parameter book_id.
// The method iterates through the collection of books to find the book with the matching book_id,
// removes it from the collection, and prints a success message. If the book_id is not found,
// the method prints an error message and exits the program.
public void removeBook(int book_id){
for(Book book:books){
if(book_id == book.getBook_id()){
books.remove(book);
System.out.println("Book removed successfully!");
return;
}
}
System.out.println("invalid book id!");
System.exit(0);
}

// This method called checkoutBook, which takes two integer arguments: user_id and book_id.
// The method iterates through the list of users to find the user with the specified user_id. It then checks
// if the book with the specified book_id is available to be borrowed. If the book is available, it is borrowed by the user,
// and a success message is printed to the console. If the book is not available, an error message is printed and the program exits
public void checkoutBook(int user_id,int book_id){
for(User u : users){
if(user_id == u.getUser_id()) {
for (Book book : books)
if (book_id == book.getBook_id() && book.isAvailable()) {
u.borrow_book(book);
System.out.println("Book checked out successfully!");
return;
}
}
}

System.out.println("Book unavailable for check out!");
System.exit(0);
}

// This method used to return a book borrowed by a user. It takes two parameters, user_id and book_id,
// and searches through the collection of users to find the user with the matching user_id. It then searches through
// the borrowed_books list of that user to find the book with the matching book_id. If the book is found,
// the user's return_book() method is called to return the book and a success message is printed. If the book is not found,
// a "Book not found!" message is printed and the program exits.
public void returnBook(int user_id,int book_id){
for(User u : users){
if(user_id == u.getUser_id()){
for(Book book: u.getBorrowed_books()){
if(book_id == book.getBook_id()){
u.return_book(book);
System.out.println("Book returned successfully!");
return;
}
}
}
}
System.out.println("Book not found!");
System.exit(0);
}
// This method used to display a list of available books.
// It iterates through a collection of Book objects, checks if each book is available by calling the isAvailable() method,
// and prints the titles of available books to the console.
public void showAvailableBooks(){
System.out.println("Available Books:");
for(Book book : books){
if(book.isAvailable())
System.out.print( " " + book.getBook_title());
}
System.out.println();
}

// This method takes a userId as an argument and displays the list of books borrowed by the user with the given userId,
// and then iterates over a list of users to find the user with the given userId. If the user is found and has borrowed books,
// the method prints the list of borrowed books for that user. Otherwise, the method prints a message indicating that the user has no borrowed books.
// If the user is not found in the list of users, the method prints a message indicating that the user was not found and exits the program.
public void showBorrowedBooks(int userId){
for(User u: users){
if(userId == u.getUser_id()){
if(u.getBorrowed_books().size() > 0) {
System.out.println("Borrowed Books of user with id = " + userId);
for (Book book : u.getBorrowed_books()) {
System.out.println(book.getBook_id() + " " + book.getBook_title() + '\n');
}
} else {
System.out.println("No Borrowed Books!");
}
return;
}
}
System.out.println("User not found!");
System.exit(0);
}

// This method takes an integer book_id as an argument and displays the details of the corresponding book if it is found in the collection of books.
// and then iterates through the list of books and checks if the book_id matches the id of any book in the list.
// If a match is found, the details of the book (such as title, author, version, and availability) are printed to the console.
//If the book is not found in the list, the message "Book not found!" is printed to the console, and the program exits.
public void showBookDetails(int book_id){
for(Book book : books){
if(book_id == book.getBook_id()){
System.out.println("Book Details:");
System.out.println("Book Id: " + book.getBook_id() + "\nBook Title:" + book.getBook_title() +
"\nBook Author: " + book.getAuthor_name()
+ "\nBook Version: " + book.getBook_version() + "\nBook Availability:" + book.isAvailable());
return;
}
}
System.out.println("Book not found!");
System.exit(0);
}
}
Loading