0% found this document useful (0 votes)
14 views18 pages

Ooad 4

Uploaded by

ikshalingthep
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views18 pages

Ooad 4

Uploaded by

ikshalingthep
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Lab Exercise-4

Title: To study about the mapping design to code(in Java).


Aim: To be able to map design to code in Java and give an example of
Library Management System and ATM.
Description:
Mapping a design to code in Java for given systems involves translating the
conceptual design into actual classes, methods, and attributes. Here’s a step-
by-step guide on how to do this:.
Task:1

1. Design Overview

Requirements:
- Entities: Library, Book, Member, Loan.
- Actions: Add books, register members, issue books, return books.

Use Case Example:


- A library adds books to its collection.
- A member registers with the library.
- The member borrows a book.
- The member returns the book.

2. Mapping Design to Code


//Library Class
import java.util.ArrayList;
import java.util.List;
public class Library {
private List<Book> books;
private List<Member> members;

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

public void addBook(Book book) {


books.add(book);
System.out.println("Book added: " + book.getTitle());
}

public void registerMember(Member member) {


members.add(member);
System.out.println("Member registered: " + member.getName());
}

public Book findBook(String title) {


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

public class Book {


private String title;
private String author;
private boolean isAvailable;

public Book(String title, String author) {


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

public String getTitle() {


return title;
}

public boolean isAvailable() {


return isAvailable;
}

public void setAvailable(boolean available) {


isAvailable = available;
}
}
//Member Class

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

public class Member {


private String name;
private List<Loan> loans;

public Member(String name) {


this.name = name;
this.loans = new ArrayList<>();
}

public String getName() {


return name;
}

public void borrowBook(Book book) {


if (book.isAvailable()) {
loans.add(new Loan(book));
book.setAvailable(false);
System.out.println(name + " borrowed " + book.getTitle());
} else {
System.out.println("Book is not available.");
}
}

public void returnBook(Book book) {


for (Loan loan : loans) {
if (loan.getBook().equals(book)) {
loans.remove(loan);
book.setAvailable(true);
System.out.println(name + " returned " + book.getTitle());
break;
}
}
}
}

//Loan Class
public class Loan {
private Book book;

public Loan(Book book) {


this.book = book;
}

public Book getBook() {


return book;
}
}
//Main Class (LibraryManagementSystem)**

public class LibraryManagementSystem {


public static void main(String[] args) {
Library library = new Library();

// Adding books to the library


Book book1 = new Book("The Great Gatsby", "F. Scott Fitzgerald");
Book book2 = new Book("1984", "George Orwell");
library.addBook(book1);
library.addBook(book2);

// Registering a member
Member member1 = new Member("John Doe");
library.registerMember(member1);

// Borrowing a book
member1.borrowBook(book1);

// Trying to borrow the same book again


member1.borrowBook(book1);

// Returning the book


member1.returnBook(book1);
// Borrowing the book again after returning
member1.borrowBook(book1);
}
}
# Expected Output

Task 2
Mapping a design to code in Java for an ATM system involves translating the
requirements and design specifications into actual code. Let's break down this
process:
1. Requirements Analysis
-Actors: Customer, ATM machine, Bank server
- Use Cases: Withdraw Cash, Deposit Cash, Check Balance, Transfer
Funds, Change PIN, etc.

2. Class Diagram Design


Identify the main classes and their relationships. For an ATM system, the
following classes might be relevant:
- ATM
- Handles user interactions, verifies PIN, and initiates transactions.
- Account
- Represents a bank account with operations like debit, credit, and balance
check.
- Transaction
- Abstract class for different types of transactions (Withdraw, Deposit,
Transfer).
- Bank
- Manages accounts and validates transactions.
- Card
- Represents the ATM card with information like card number and
associated account.
- Screen
- Displays messages to the user.
- Keypad
- Handles user input.
- CashDispenser
- Dispenses cash.
- ReceiptPrinter
- Prints transaction receipts.
- BankDatabase
- Simulates a database for storing account information.

3. Mapping Design to Code


Here’s a basic outline of how you can map the above design to Java code:
ATM Class
This class interacts with users, checks the PIN, and processes transactions.
Java Code
package atm;
import java.util.Scanner;
public class ATM {
private boolean userAuthenticated;
private int currentAccountNumber;
private Screen screen;
private Keypad keypad;
private CashDispenser cashDispenser;
private ReceiptPrinter receiptPrinter;
private BankDatabase bankDatabase;

public ATM() {
userAuthenticated = false;
currentAccountNumber = 0;
screen = new Screen();
keypad = new Keypad();
cashDispenser = new CashDispenser();
receiptPrinter = new ReceiptPrinter();
bankDatabase = new BankDatabase();
}

public void run() {


while (true) {
while (!userAuthenticated) {
screen.displayMessage("Please enter your account number: ");
int accountNumber = keypad.getInput();
screen.displayMessage("Enter your PIN: ");
int pin = keypad.getInput();

userAuthenticated =
bankDatabase.authenticateUser(accountNumber, pin);

if (userAuthenticated) {
currentAccountNumber = accountNumber;
} else {
screen.displayMessage("Invalid account number or PIN. Please
try again.");
}
}
performTransactions();
userAuthenticated = false;
currentAccountNumber = 0;
screen.displayMessage("Thank you! Goodbye!");
}
}

private void performTransactions() {


boolean userExited = false;
while (!userExited) {
screen.displayMessage("Main menu:");
screen.displayMessage("1 - View my balance");
screen.displayMessage("2 - Withdraw cash");
screen.displayMessage("3 - Deposit funds");
screen.displayMessage("4 - Exit\nEnter a choice: ");
int choice = keypad.getInput();
switch (choice) {
case 1:
viewBalance();
break;
case 2:
withdrawCash();
break;
case 3:
depositFunds();
break;
case 4:
userExited = true;
break;
default:
screen.displayMessage("Invalid selection. Try again.");
break;
}
}
}
public void start() {
// Example of calling the getInput() method from Keypad
System.out.print("Enter your PIN: ");
int pin = keypad.getInput(); // Call the method from Keypad class
System.out.println("You entered PIN: " + pin);

// Example of calling the getInputString() method from Keypad


System.out.print("Enter your transaction type: ");
String transactionType = keypad.getInputString(); // Call the method
from Keypad class
System.out.println("You selected: " + transactionType);
}
private void viewBalance() {
Account account = bankDatabase.getAccount(currentAccountNumber);
screen.displayMessage("Balance Information:");
screen.displayMessage("Available balance: " +
account.getAvailableBalance());
screen.displayMessage("Total balance: " + account.getTotalBalance());
}

private void withdrawCash() {


// Implementation for withdrawing cash
}
private void depositFunds() {
// Implementation for depositing funds
}
}

/*BankDatabase Class
*This class simulates a bank database.
*java*/
import java.util.HashMap;
import java.util.Map;

public class BankDatabase {


private Map<Integer, Account> accounts;

public BankDatabase() {
accounts = new HashMap<>();
accounts.put(12345, new Account(12345, 1234, 1000.00, 1200.00));
accounts.put(67890, new Account(67890, 5678, 200.00, 200.00));
}

public boolean authenticateUser(int accountNumber, int pin) {


Account account = accounts.get(accountNumber);
return account != null && account.validatePIN(pin);
}
public Account getAccount(int accountNumber) {
return accounts.get(accountNumber);
}
}
```
/*
*Account Class
*This class represents a user account.
*java
public class Account {
private int accountNumber;
private int pin;
private double availableBalance;
private double totalBalance;

public Account(int accountNumber, int pin, double availableBalance,


double totalBalance) {
this.accountNumber = accountNumber;
this.pin = pin;
this.availableBalance = availableBalance;
this.totalBalance = totalBalance;
}

public boolean validatePIN(int pin) {


return this.pin == pin;
}
public double getAvailableBalance() {
return availableBalance;
}

public double getTotalBalance() {


return totalBalance;
}

public void credit(double amount) {


totalBalance += amount;
}

public void debit(double amount) {


availableBalance -= amount;
totalBalance -= amount;
}
}
/*Keypad class*/
public class Keypad {
private Scanner scanner;

public Keypad() {
scanner = new Scanner(System.in);
}

// Method to get user input as an integer


public int getInput() {
return scanner.nextInt();
}

// Method to get user input as a string


public String getInputString() {
return scanner.nextLine();
}
}
/*Screen class*/
class Screen {

void displayMessage1() {
System.out.println("please_enter_your_account_number_");
}
void displayMessage2() {
System.out.println("enter your pin");
}
void displayMessage3() {
System.out.println("Invalid account number or PIN. Please try again.");
}
void displayMessage4() {
System.out.println("Thank you! Goodbye!");
}

void displayMessage5() {
System.out.println("Main menu:");
}
void displayMessage6() {
System.out.println(" View my balance");
}
void displayMessage7() {
System.out.println("Withdraw cash");
}
void displayMessage8() {
System.out.println(" Deposit funds");}
void displayMessage9() {
System.out.println(" Exit\nEnter a choice: ");}
void displayMessage10() {
System.out.println("Invalid selection. Try again.");
}
void displayMessage11() {
System.out.println("Balance Information:");
}void displayMessage12() {
System.out.println( "Available balance:12345 ");}

void displayMessage13() {
System.out.println("Total balance:9875 " ); }

}
This example provides a basic structure and can be expanded based on
additional requirements, such as handling multiple accounts, connecting to a
real database, or implementing a GUI.
Conclusion: We learned about Mapping Design to Code and and successfully
executed programs.in Netbeans .

You might also like