You are on page 1of 7

PROLANG: LAB ACTIVITY 2

FUNCTIONS AS EXPRESSIONS AND VALUES

WEEK 7-8 DATE MARCH 20-25, 2023


MODE ASYCHRONOUS TIME
STARTED
STUDENT CERAFIA JR. ALEJANDRO R STUDENT TAMARES, FORTUNE
1: 2: MATTHEW

LEARNING OUTCOME

Built a simple application using functions in Java as the programming


language.

MATERIALS

MS WORD
ANY JAVA IDE or ONLINE COMPILER

ACTIVITY

A library system is a transaction between a librarian and a borrower. Normally, the


transactions inside the library are Borrowing and Returning of books. Consider the
following sample test data. Your task is to create a JAVA program for a simple Library
System that will execute the borrowing and returning of books. All underline shall be user
input. Use functions/ methods in each transaction.

SAMPLE TEST DATA 1:

Enter borrower type [1] Student [2] Teacher: 1

Enter student’s name: Juan Dela Cruz

Choose transaction [B] borrow [R] return: B

Select Book to Borrow from the list:

[a] Intro to Java

[b] Computer Programming 2

[c] Object Oriented Programming in C#

[d] Web Development Tools

[e] Web Applications

You choose: b
Max of 2 books only. Add again? [Y] Yes [N] No: Y

Select Book to Borrow from the list:

[a] Intro to Java

[b] Computer Programming 2

[c] Object Oriented Programming in C#

[d] Web Development Tools

[e] Web Applications

You choose: e

Max of 2 books only. Add again? [Y] Yes [N] No: N

You have chosen:

Computer Programming 2

Web Applications

SAMPLE TEST DATA 2:

Enter borrower type [1] Student [2] Teacher: 2

Enter teacher’s name: Jose Rizal

Choose transaction [B] borrow [R] return: B

Select Book to Borrow from the list:

[a] Information Management

[b] Mobile Applications

[c] Data Structures

[d] Design and Algorithms

[e] Software Engineering

You choose: a

Max of 2 books only. Add again? [Y] Yes [N] No: Y

Select Book to Borrow from the list:

[a] Information Management

[b] Mobile Applications

[c] Data Structures

[d] Design and Algorithms

[e] Software Engineering

You choose: e

Max of 2 books only. Add again? [Y] Yes [N] No: N

Page 2 of 7
You have chosen:

Information Management

Software Engineering

SAMPLE TEST DATA 3:

Enter borrower type [1] Student [2] Teacher: 1

Enter student’s name: Juan Dela Cruz

Choose transaction [B] borrow [R] return: R

Penalty is 10.00 per day

Enter how many days late: 5

Penalty is 50.00!

SAMPLE TEST DATA 4:

Enter borrower type [1] Student [2] Teacher: 2

Enter teacher’s name: Jose Rizal

Choose transaction [B] borrow [R] return: R

Book/s returned!

No penalty for a teacher.

Note: The students (pair) shall submit their program codes and screenshot (during
runtime) to TBL hub until MARCH 27, 2023 Monday.

Page 3 of 7
Source Code:

import java.util.ArrayList;
import java.util.Scanner;

public class LibrarySystem {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter borrower type [1] Student [2] Teacher: ");
int borrowerType = scanner.nextInt();
scanner.nextLine(); // consume the remaining newline character

System.out.print("Enter borrower's name: ");


String borrowerName = scanner.nextLine();

System.out.print("Choose transaction [B] borrow [R] return: ");


String transactionType = scanner.nextLine();

if (transactionType.equalsIgnoreCase("B")) {
borrowBooks(scanner, borrowerType, borrowerName);
} else if (transactionType.equalsIgnoreCase("R")) {
returnBooks(scanner, borrowerType, borrowerName);
} else {
System.out.println("Invalid transaction type.");

}
}

public static void borrowBooks(Scanner scanner, int borrowerType,


String borrowerName) {

ArrayList<String> borrowedBooks = new ArrayList<>();


while (borrowedBooks.size() < 2) {
System.out.println("Select Book to Borrow from the list:");

System.out.println("[a] Information Management");


System.out.println("[b] Mobile Applications");
System.out.println("[c] Data Structures");
System.out.println("[d] Design and Algorithms");
System.out.println("[e] Software Engineering");

System.out.print("You choose: ");


String bookChoice = scanner.nextLine();

switch (bookChoice.toLowerCase()) {
case "a":
borrowedBooks.add("Information Management");
break;
case "b":
borrowedBooks.add("Mobile Applications");
break;
case "c":
borrowedBooks.add("Data Structures");
break;
case "d":

Page 4 of 7
borrowedBooks.add("Design and Algorithms");
break;
case "e":
borrowedBooks.add("Software Engineering");
break;
default:
System.out.println("Invalid book choice.");
break;
}

// Check if borrower wants to add more books


if (borrowedBooks.size() < 2) {
System.out.print("Max of 2 books only. Add again? [Y] Yes
[N] No: ");
String addMore = scanner.nextLine();
if (addMore.equalsIgnoreCase("N")) {
break;
}
}
}

// Print borrowed books


System.out.println("You have chosen:");
for (String book : borrowedBooks) {
System.out.println(book);
}
}

public static void returnBooks(Scanner scanner, int borrowerType,


String borrowerName) {
System.out.println("Penalty is " + (borrowerType == 1 ? "10.00" :
"0.00") + " per day");

// Get days late


System.out.print("Enter how many days late: ");
int daysLate = scanner.nextInt();

// Calculate penalty
double penalty = borrowerType == 1 ? daysLate * 10.00 : 0.00;
System.out.println("Penalty is " + String.format("%.2f", penalty)
+ "!");

// Confirm book return


System.out.println("Book/s returned!");
System.out.println("No penalty for a teacher.");

}
}

Page 5 of 7
Output 1 – Student Borrowing

Output – Student Return Late

Page 6 of 7
Output 3 – Borrowing Teacher

Page 7 of 7

You might also like