H6 Bibliotheek met hash Flashcards

1
Q

Card 1: Class: Lid (Member)

Front: Write the class definition for Lid. It should include:

Private String fields for id, naam (name), adres (address), and gemeente (municipality).
A 4-argument constructor to initialize these fields.
Getter for id and naam.
toString() method.
equals() and hashCode() methods (generated by IntelliJ, comparing only the id field).

A

package be.vives.ti;

import java.util.Objects;

public class Lid {

	private String id;
	private String naam;
	private String adres;
	private String gemeente;

	public Lid(String id, String naam, String adres, String gemeente) {
		this.id = id;
		this.naam = naam;
		this.adres = adres;
		this.gemeente = gemeente;
	}

	public String getId() {
		return id;
	}

	public String getNaam() {
		return naam;
	}

	public String toString() {
		return "Naam: " + naam + "\n"
				\+ "Adres: " + adres + "\n"
				\+ "Gemeente: " + gemeente + "\n";
	}

	@Override
	public boolean equals(Object o) {
		if (this == o) return true;
		if (o == null || getClass() != o.getClass()) return false;
		Lid lid = (Lid) o;
		return id.equals(lid.id);
	}

	@Override
	public int hashCode() {
		return Objects.hash(id);
	}
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Card 2: Class: Boek (Book)

Front: Write the class definition for Boek. It should include:

Private String fields for titel (title), auteur (author), and isbn.
Private int field for id (identification number).
Private boolean field aanwezig (present/available), initialized to true in the constructor.
A 4-argument constructor.
Getter for id.
isAanwezig() method to check book status.
setAanwezig() method to modify book status.
toString() method.

A

package be.vives.ti;

public class Boek {

	private String titel;
	private String auteur;
	private String isbn;
	private int id;
	private boolean aanwezig;

	public Boek(String titel, String auteur, String isbn, int id) {
		this.titel = titel;
		this.auteur = auteur;
		this.isbn = isbn;
		this.id = id;
		aanwezig = true;
	}

	public boolean isAanwezig() {
		return aanwezig;
	}

	public void setAanwezig(boolean aanwezig) {
		this.aanwezig = aanwezig;
	}
	public int getId() {
		return id;
	}

	public String toString() {
		return "Nr: " + id + "\n"
				\+ "Titel: " + titel + "\n"
				\+ "Auteur: " + auteur + "\n"
				\+ "ISBN-nummer: " + isbn + "\n"
				\+ "Status: " + (aanwezig ? "Aanwezig" : "Uitgeleend") + "\n";

	}
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Card 3: Class: Balie (Counter/Reception Desk) - Attributes

Front: Declare the attributes (member variables) for the Balie class. Consider the most suitable collection types for storing books, members, and loans, keeping in mind the requirements outlined in the prompt 1 2.

A

package be.vives.ti;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;

public class Balie {

    private HashMap<Integer, Boek> boeken;  // key = boekId -> value = boek
    private HashSet<Lid> leden;
    private HashMap<Integer, Lid> ontleningen; // key = boekId -> value = lid

    // zou ook correct zijn
    // private HashMap<Boek, Lid> ontleningen; // key = boek -> value = lid
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Card 4: Class: Balie - Constructor

Front: Write the 0-argument constructor for the Balie class. Initialize the collection attributes boeken, leden, ontleningen.

A

public Balie() {
boeken = new HashMap<>();
leden = new HashSet<>();
ontleningen = new HashMap<>();
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Card 5: Class: Balie - toevoegenBoek() (addBook)

Front: Write the toevoegenBoek() method for the Balie class. It takes a Boek object as a parameter and adds it to the appropriate collection.

A

public void toevoegenBoek(Boek boek) {
boeken.put(boek.getId(), boek);
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Card 6: Class: Balie - toevoegenLid() (addMember)

Front: Write the toevoegenLid() method for the Balie class. It takes a Lid object as a parameter and adds it to the appropriate collection.

A

public void toevoegenLid(Lid lid) {
leden.add(lid);
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Card 7: Class: Balie - ontleen() (borrow)

Front: Write the ontleen() method for the Balie class. It takes a boekId (int) and a Lid object as parameters. It should:

Check if the book exists.
Check if the book is currently available.
If both conditions are met, record the loan, and update the book’s status.
Print appropriate error messages if the book doesn’t exist or is already lent out 3

A

public void ontleen(int boekId, Lid lid) {
Boek boek = boeken.get(boekId);
if (boek != null) {
if (boek.isAanwezig()) {
boek.setAanwezig(false);
ontleningen.put(boek.getId(), lid);
} else {
System.out.println(“Het boek is reeds uitgeleend !”);
}
} else {
System.out.println(“Boek niet gevonden!”);
}
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Card 8: Class: Balie - brengTerug() (returnBook)

Front: Write the brengTerug() method for the Balie class. It takes a boekId (int) as a parameter. It should:

Check if the book exists.
If the book exists, remove the loan record and update the book’s status.
Print appropriate error messages if the book doesn’t exist. 4

A

public void brengTerug(int boekId) {
Boek boek = boeken.get(boekId);
if(boek != null) {
ontleningen.remove(boekId);
boek.setAanwezig(true);
} else {
System.out.println(“Boek niet gevonden!”);
}
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Card 9: Class: Balie - overzichtGeleendeBoeken() (printBorrowedBooks)

Front: Write the overzichtGeleendeBoeken() method. It prints a list of all currently borrowed books, using the toString() method of the Boek class.

A

public void overzichtGeleendeBoeken() {
System.out.println(“Overzicht geleende boeken:”);
System.out.println(“==========================”);
for (Integer boekid : ontleningen.keySet()) {
System.out.println(boeken.get(boekid));
}
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Card 10: Class: Balie - overzichtAanwezigeBoeken() (printAvailableBooks)

Front: Write the overzichtAanwezigeBoeken() method. It prints a list of all currently available books, using the toString() method of the Boek class.

A

public void overzichtAanwezigeBoeken() {
System.out.println(“Overzicht aanwezige boeken:”);
System.out.println(“==========================”);
for (Boek boek : boeken.values()) {
if (boek.isAanwezig()) {
System.out.println(boek);
}
}
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Card 11: Class: Balie - printOntleendeBoekenVanPersoon() (printBorrowedBooksByMemberName)

Front: Write the printOntleendeBoekenVanPersoon() method. It takes a member’s name (naam) as a parameter and prints a list of all books currently borrowed by that member, using the toString() method of the Boek class. Use entrySet() to iterate through the loans.

A

public void printOntleendeBoekenVanPersoon(String naam) {
System.out.println(“Overzicht geleende boeken door “ + naam);
System.out.println(“”);
for (Map.Entry<Integer, Lid> ontlening : ontleningen.entrySet()) {
if (ontlening.getValue().getNaam().equals(naam)) {
System.out.println(boeken.get(ontlening.getKey()));
}
}
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Card 12: Class: TryBibliotheek - Main Method - Setup

Front: Write the main method in the TryBibliotheek class. Instantiate a Balie object. Create several Boek and Lid objects. Add the books and members to the Balie.

A

package be.vives.ti;
public class TryBibliotheek {

    public static void main(String[] args) {
        // maak een aantal boeken
        Boek beginningJava2_1 = new Boek("Beginning Java 2","Ivor Horton","1-861003-66-8",1);
        Boek beginningJava2_2 = new Boek("Beginning Java 2","Ivor Horton","1-861003-66-8",2);
        Boek jsp = new Boek("Java Server Programming", "Mark Wilcox","2-788888-45-6",3);
        Boek justJava2_1 = new Boek("Just Java 2", "Peter van der linden", "2-33344545-56-7",4);
        Boek justJava2_2 = new Boek("Just Java 2", "Peter van der linden", "2-33344545-56-7",5);
        Boek justJava2_3 = new Boek("Just Java 2", "Peter van der linden", "2-33344545-56-7",6);
        Boek javaFoundations = new Boek("Java Foundations","Aaron Wilson","1-6745532-78-9",7);
        Boek xml = new Boek("Professional XML","Stephen Mohr","6-65564437-90-7",8);
        Boek javaExamples = new Boek("Java Examples","Steve Flanagan","5-33422318-43-5",9);

        // maak een aantal Leden

        Lid johan = new Lid("1", "Johan","Kezelberg 21","Moorsele");
        Lid katrien = new Lid("2", "Katrien","Zolderstraat 12D","Otegem");
        Lid frank = new Lid( "3", "Frank","Hoogleedsesteenweg 315","Roeselare");
        Lid marc = new Lid("4","Marc","Gaston Martensstraat 23","Waregem");
        Lid dirk = new Lid("5", "Dirk","Don Boscolaan 20","Kortrijk");
        Lid luc1 = new Lid("6", "Luc","Kwadenbulk 37","Wortegem-Petegem");
        Lid luc2 = new Lid("7", "Luc","Domien Craccostraat 51","Roeselare");

        // voeg de boeken en de leden toe tot het Balie-object
        Balie deBib = new Balie();
        deBib.toevoegenBoek(beginningJava2_1);
        deBib.toevoegenBoek(beginningJava2_2);
        deBib.toevoegenBoek(jsp);
        deBib.toevoegenBoek(justJava2_1);
        deBib.toevoegenBoek(justJava2_2);
        deBib.toevoegenBoek(justJava2_3);
        deBib.toevoegenBoek(javaFoundations);
        deBib.toevoegenBoek(xml);
        deBib.toevoegenBoek(javaExamples);
        deBib.toevoegenBoek(new Boek("Java Examples","Steve Flanagan","5-33422318-43-5",9));

        deBib.toevoegenLid(johan);
        deBib.toevoegenLid(katrien);
        deBib.toevoegenLid(frank);
        deBib.toevoegenLid(marc);
        deBib.toevoegenLid(dirk);
        deBib.toevoegenLid(luc1);
        deBib.toevoegenLid(luc2);
        deBib.toevoegenLid(new Lid("2", "Katrien","Zolderstraat 12D","Otegem"));
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Card 13: Class: TryBibliotheek - Main Method - Simulate Loans and Returns

Front: In the main method, simulate several book loans and returns using the ontleen() and brengTerug() methods. Include an attempt to borrow a book that is already out. After the loans and returns, print the lists of available and borrowed books. Finally, print the books borrowed by a specific member.

A

// doe een aantal ontleningen
deBib.ontleen(beginningJava2_1.getId(),johan);
deBib.ontleen(beginningJava2_2.getId(),johan);
deBib.ontleen(xml.getId(), johan);
deBib.ontleen(beginningJava2_1.getId(),johan); // beginningJava2_1 is reeds ontleend!!
deBib.ontleen(justJava2_2.getId(),luc1);
deBib.ontleen(jsp.getId(),dirk);

        deBib.overzichtAanwezigeBoeken();
        deBib.overzichtGeleendeBoeken();

        deBib.brengTerug(beginningJava2_2.getId());
        deBib.brengTerug(justJava2_2.getId());

        deBib.overzichtAanwezigeBoeken();
        deBib.overzichtGeleendeBoeken();
        deBib.printOntleendeBoekenVanPersoon(johan.getNaam());
        deBib.ontleendeBoekenVanPersoon(johan);

        //niet gevraagd, maar handig als controle
        deBib.printLeden();
    }

}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly