You are on page 1of 21

Republic of the Philippines

Western Mindanao State University


College of Computing Studies
Department of Information Technology

IT 122 - Integrative Programming and Technologies

Requirement submitted to the College of Computing Studies


as partial fulfillment of the requirements for the degree of
Bachelor of Science in Information Technology

Submitted by:
[Alibasa, Sheik aishir S.]
[Danilo Abilul]
[Al-khabir Hajihil]
[Wiljem Laureano]

Submitted to:
Mr. Jason A. Catadman

February 14, 2024


S.Y. 2023-2024, Second Semester
(Replace this with your system)Library Management System:
 Develop a system to manage library resources (books, magazines, etc.).
 Implement features like borrowing books, returning books, adding new books, and searching
for books.
 Utilize classes like Book, Library, and LibraryManager.

Source Code (All Classes):


LibraryManager.java
import java.util.Scanner;

public class LibraryManager {


private Library library;

public LibraryManager(Library library) {


this.library = library;
}

public void displayMenu() {


System.out.println("\nLibrary Management System Menu:");
System.out.println("1. Add Book");
System.out.println("2. Borrow Book");
System.out.println("3. Return Book");
System.out.println("4. Search Book");
System.out.println("5. Display Library Books");
System.out.println("6. Exit");
System.out.print("Enter your choice: ");
}

public void start() {


Scanner scanner = new Scanner(System.in);
int choice;

do {
displayMenu();
choice = Integer.parseInt(scanner.nextLine());

switch (choice) {
case 1:
addBook(scanner);
break;
case 2:
borrowBook(scanner);
break;
case 3:
returnBook(scanner);
break;
case 4:

Page 2 of 21
searchBook(scanner);
break;
case 5:
displayLibraryBooks();
break;
case 6:
System.out.println("Exiting Library Management System.
Goodbye!");
break;
default:
System.out.println("Invalid choice. Please try again.");
}
} while (choice != 6);

scanner.close();
}

private void addBook(Scanner scanner) {


System.out.print("Enter title of the book: ");
String title = scanner.nextLine();
System.out.print("Enter author of the book: ");
String author = scanner.nextLine();
library.addBook(new Book(title, author));
System.out.println("Book added successfully.");
}

private void borrowBook(Scanner scanner) {


System.out.print("Enter title of the book you want to borrow: ");
String title = scanner.nextLine();
library.borrowBook(title);
}

private void returnBook(Scanner scanner) {


System.out.print("Enter title of the book you want to return: ");
String title = scanner.nextLine();
library.returnBook(title);
}

private void searchBook(Scanner scanner) {


System.out.print("Enter title of the book you want to search: ");
String title = scanner.nextLine();
Book book = library.searchBook(title);
if (book != null) {
System.out.println("Book found: " + book);
} else {
System.out.println("Book not found.");
}
}

Page 3 of 21
private void displayLibraryBooks() {
System.out.println(library);
}

public static void main(String[] args) {


Library library = new Library();
LibraryManager manager = new LibraryManager(library);
manager.start();
}
}

Library.java
import java.util.ArrayList;
import java.util.List;

public class Library {


private List<Book> books;

public Library() {
this.books = new ArrayList<>();
}

public void addBook(Book book) {


books.add(book);
}

public Book searchBook(String title) {


for (Book book : books) {
if (book.getTitle().equalsIgnoreCase(title)) {
return book;
}
}
return null;
}

public void borrowBook(String title) {


Book book = searchBook(title);
if (book != null && book.isAvailable()) {
book.setAvailable(false);
System.out.println("You have successfully borrowed: " +
book.getTitle());
} else {
System.out.println("Sorry, the book is either not available or
does not exist.");
}
}

Page 4 of 21
public void returnBook(String title) {
Book book = searchBook(title);
if (book != null && !book.isAvailable()) {
book.setAvailable(true);
System.out.println("You have successfully returned: " +
book.getTitle());
} else {
System.out.println("Invalid book or it is already available in the
library.");
}
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("Library Books:\n");
for (Book book : books) {
sb.append(book).append("\n");
}
return sb.toString();
}
}

Book.java
public class Book {
private String title;
private String author;
private boolean available;

public Book(String title, String author) {


this.title = title;
this.author = author;
this.available = true;
}

public String getTitle() {


return title;
}

public String getAuthor() {


return author;
}

public boolean isAvailable() {


return available;

Page 5 of 21
}

public void setAvailable(boolean available) {


this.available = available;
}

@Override
public String toString() {
return "Title: " + title + ", Author: " + author + ", Available: " +
available;
}
}

Screenshot (output):

Page 6 of 21
Page 7 of 21
Page 8 of 21
Inheritance: Shape Activity
 Suppose we have a base class called Shape with attributes name and color, and a
method displayInfo() to display the name and color of the shape. Create two
subclasses Circle and Rectangle that inherit from the Shape class. The Circle class
should have an additional attribute radius, and the Rectangle class should have
additional attributes length and width. Implement a method calculateArea() in both
subclasses to calculate the area of the circle and rectangle respectively. Finally, create
objects of both subclasses and call the displayInfo() and calculateArea() methods.

Source Code (All Classes):


Main.java
import java.util.Scanner;

public class Main {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.println("Enter circle details:");


System.out.print("Name: ");
String circleName = scanner.nextLine();
System.out.print("Color: ");
String circleColor = scanner.nextLine();
System.out.print("Radius: ");
double radius = Double.parseDouble(scanner.nextLine());
Circle circle = new Circle(circleName, circleColor, radius);

System.out.println("\nEnter rectangle details:");


System.out.print("Name: ");
String rectangleName = scanner.nextLine();
System.out.print("Color: ");
String rectangleColor = scanner.nextLine();
System.out.print("Length: ");
double length = Double.parseDouble(scanner.nextLine());
System.out.print("Width: ");
double width = Double.parseDouble(scanner.nextLine());
Rectangle rectangle = new Rectangle(rectangleName, rectangleColor,
length, width);

System.out.println("\nDisplaying Circle Info:");


circle.displayInfo();
circle.calculateArea();

System.out.println("\nDisplaying Rectangle Info:");

Page 9 of 21
rectangle.displayInfo();
rectangle.calculateArea();

scanner.close();
}
}

Shape.java
public class Shape {
protected String name;
protected String color;

public Shape(String name, String color) {


this.name = name;
this.color = color;
}

public void displayInfo() {


System.out.println("Name: " + name);
System.out.println("Color: " + color);
}
}

Rectangle.java

public class Rectangle extends Shape {


private double length;
private double width;

public Rectangle(String name, String color, double length, double width) {


super(name, color);
this.length = length;
this.width = width;
}

public void calculateArea() {


double area = length * width;
System.out.println("Area of the rectangle: " + area);
}
}

Page 10 of 21
Circle.java

public class Circle extends Shape {


private double radius;

public Circle(String name, String color, double radius) {


super(name, color);
this.radius = radius;
}

public void calculateArea() {


double area = Math.PI * radius * radius;
System.out.println("Area of the circle: " + area);
}
}

Screenshot (output):

Page 11 of 21
Encapsulation: BankAccount Activity
 Suppose we have a BankAccount class with private attributes accountNumber, balance, and
customerName. Implement methods to set and get the values of these attributes using
encapsulation. Additionally, create methods deposit() and withdraw() to deposit and withdraw
money from the account respectively. Implement validation to ensure that the balance doesn't
go negative during withdrawals. Finally, create objects of the BankAccount class, set initial
values, deposit and withdraw money, and display account information.

Source Code (All Classes):


Main.java
import java.util.Scanner;

public class Main {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.println("Enter account number:");


String accountNumber = scanner.nextLine();

System.out.println("Enter initial balance:");


double initialBalance = scanner.nextDouble();
scanner.nextLine(); // Consume newline character

System.out.println("Enter customer name:");


String customerName = scanner.nextLine();

// Creating an object of BankAccount class


BankAccount account = new BankAccount(accountNumber, initialBalance,
customerName);

System.out.println("Enter amount to deposit:");


double depositAmount = scanner.nextDouble();
account.deposit(depositAmount);

System.out.println("Enter amount to withdraw:");


double withdrawAmount = scanner.nextDouble();
account.withdraw(withdrawAmount);

// Displaying account information


account.displayAccountInfo();

scanner.close();
}
}

Page 12 of 21
BankAccount.java
public class BankAccount {
private String accountNumber;
private double balance;
private String customerName;

// Constructor
public BankAccount(String accountNumber, double balance, String
customerName) {
this.accountNumber = accountNumber;
this.balance = balance;
this.customerName = customerName;
}

// Getter methods
public String getAccountNumber() {
return accountNumber;
}

public double getBalance() {


return balance;
}

public String getCustomerName() {


return customerName;
}

// Setter methods
public void setAccountNumber(String accountNumber) {
this.accountNumber = accountNumber;
}

public void setBalance(double balance) {


this.balance = balance;
}

public void setCustomerName(String customerName) {


this.customerName = customerName;
}

// Method to deposit money


public void deposit(double amount) {
if (amount > 0) {
balance += amount;
System.out.println(amount + " deposited successfully.");
} else {
System.out.println("Invalid amount for deposit.");
}

Page 13 of 21
}

// Method to withdraw money


public void withdraw(double amount) {
if (amount > 0 && balance >= amount) {
balance -= amount;
System.out.println(amount + " withdrawn successfully.");
} else {
System.out.println("Invalid amount or insufficient balance.");
}
}

// Method to display account information


public void displayAccountInfo() {
System.out.println("Account Number: " + accountNumber);
System.out.println("Customer Name: " + customerName);
System.out.println("Balance: " + balance);
}
}

Screenshot (output):

Page 14 of 21
Polymorphism: Employee Payroll Activity
 Create a program to calculate the payroll for different types of employees: full-time and part-
time. Implement polymorphism by defining a base class Employee and its subclasses
FullTimeEmployee and PartTimeEmployee. Each employee has a method calculatePay() to
calculate their pay. Calculate the total payroll for a list of employees using polymorphism.

Source Code (All Classes):


Main.java
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {


public static double calculateTotalPayroll(List<Employee> employees) {
double totalPayroll = 0;
for (Employee emp : employees) {
totalPayroll += emp.calculatePay();
}
return totalPayroll;
}

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);
List<Employee> employees = new ArrayList<>();

System.out.println("Enter the number of employees:");


int numEmployees = Integer.parseInt(scanner.nextLine());

for (int i = 0; i < numEmployees; i++) {


System.out.println("Enter employee type (FullTime or PartTime):");
String type = scanner.nextLine();
System.out.println("Enter employee name:");
String name = scanner.nextLine();
System.out.println("Enter hours worked:");
double hoursWorked = Double.parseDouble(scanner.nextLine());
System.out.println("Enter hourly rate:");
double hourlyRate = Double.parseDouble(scanner.nextLine());

if (type.equalsIgnoreCase("FullTime")) {
employees.add(new FullTimeEmployee(name, hoursWorked,
hourlyRate));
} else if (type.equalsIgnoreCase("PartTime")) {
employees.add(new PartTimeEmployee(name, hoursWorked,
hourlyRate));
} else {

Page 15 of 21
System.out.println("Invalid employee type. Please enter
FullTime or PartTime.");
i--; // decrement i to re-prompt for the current employee
}
}

double totalPayroll = calculateTotalPayroll(employees);


System.out.println("Total Payroll: $" + totalPayroll);
scanner.close();
}
}

Employee.java
import java.util.Scanner;

public class Employee {


private String name;
private double hoursWorked;
private double hourlyRate;

public Employee(String name, double hoursWorked, double hourlyRate) {


this.name = name;
this.hoursWorked = hoursWorked;
this.hourlyRate = hourlyRate;
}

public double calculatePay() {


return 0; // Default implementation, overridden in subclasses
}

// Getters and setters


public String getName() {
return name;
}

public void setName(String name) {


this.name = name;
}

public double getHoursWorked() {


return hoursWorked;
}

public void setHoursWorked(double hoursWorked) {


this.hoursWorked = hoursWorked;
}

Page 16 of 21
public double getHourlyRate() {
return hourlyRate;
}

public void setHourlyRate(double hourlyRate) {


this.hourlyRate = hourlyRate;
}

public static Employee createEmployeeFromInput(Scanner scanner) {


System.out.println("Enter employee name:");
String name = scanner.nextLine();
System.out.println("Enter hours worked:");
double hoursWorked = Double.parseDouble(scanner.nextLine());
System.out.println("Enter hourly rate:");
double hourlyRate = Double.parseDouble(scanner.nextLine());
return new Employee(name, hoursWorked, hourlyRate);
}
}

FullTimeEmployee.java
public class FullTimeEmployee extends Employee {
public FullTimeEmployee(String name, double hoursWorked, double
hourlyRate) {
super(name, hoursWorked, hourlyRate);
}

@Override
public double calculatePay() {
return getHoursWorked() * getHourlyRate();
}
}

PartTimeEmployee.java
public class PartTimeEmployee extends Employee {
public PartTimeEmployee(String name, double hoursWorked, double
hourlyRate) {
super(name, hoursWorked, hourlyRate);
}

@Override
public double calculatePay() {
return getHoursWorked() * getHourlyRate();
}

Page 17 of 21
}

Screenshot (output):

Page 18 of 21
Abstraction: Bank Account Management Activity
 Create a program to manage different types of bank accounts: savings and checking.
Implement abstraction by defining a base class BankAccount and its subclasses
SavingsAccount and CheckingAccount. Each account has methods for depositing,
withdrawing, and checking the balance. Create instances of both account types and
perform transactions.

Source Code (All Classes):


Main.java
import java.util.Scanner;

// Main class to test the bank account management system


public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

// Create a savings account


SavingsAccount savingsAccount = new SavingsAccount("SA123", 5.0);

// Create a checking account


CheckingAccount checkingAccount = new CheckingAccount("CA456",
1000.0);

// Perform transactions
System.out.print("Enter deposit amount for savings account: ");
double savingsDeposit = scanner.nextDouble();
savingsAccount.deposit(savingsDeposit);

System.out.print("Enter withdrawal amount for savings account: ");


double savingsWithdrawal = scanner.nextDouble();
savingsAccount.withdraw(savingsWithdrawal);

System.out.print("Enter deposit amount for checking account: ");


double checkingDeposit = scanner.nextDouble();
checkingAccount.deposit(checkingDeposit);

System.out.print("Enter withdrawal amount for checking account: ");


double checkingWithdrawal = scanner.nextDouble();
checkingAccount.withdraw(checkingWithdrawal);

// Close the scanner


scanner.close();
}
}

Page 19 of 21
BankAccount.java
// Base class for BankAccount
public class BankAccount {
protected String accountNumber;
protected double balance;

public BankAccount(String accountNumber) {


this.accountNumber = accountNumber;
this.balance = 0.0;
}

public void deposit(double amount) {


balance += amount;
System.out.println("Deposited: $" + amount);
displayBalance();
}

public void withdraw(double amount) {


if (amount <= balance) {
balance -= amount;
System.out.println("Withdrawn: $" + amount);
displayBalance();
} else {
System.out.println("Insufficient funds!");
}
}

public void displayBalance() {


System.out.println("Account Balance: $" + balance);
}
}

CheckingAccount.java
// Subclass for CheckingAccount
public class CheckingAccount extends BankAccount {
private double overdraftLimit;

public CheckingAccount(String accountNumber, double overdraftLimit) {


super(accountNumber);
this.overdraftLimit = overdraftLimit;
}

@Override
public void withdraw(double amount) {
if (amount <= balance + overdraftLimit) {
balance -= amount;

Page 20 of 21
System.out.println("Withdrawn: $" + amount);
displayBalance();
} else {
System.out.println("Exceeded overdraft limit!");
}
}
}

SavingAccount.java
// Subclass for SavingsAccount
public class SavingsAccount extends BankAccount {
private double interestRate;

public SavingsAccount(String accountNumber, double interestRate) {


super(accountNumber);
this.interestRate = interestRate;
}

public void addInterest() {


double interest = balance * interestRate / 100;
balance += interest;
System.out.println("Interest added: $" + interest);
displayBalance();
}
}

Screenshot (output):

Page 21 of 21

You might also like