Da er koden endret litt:
Kode:
public class BibliotekTest {
public static void main(String[] args) {
Bibliotek etBibliotek = new Bibliotek();
etBibliotek.leggTilBok("Italienske sko", "Henning Mankell", 1999, 306, "Gyldendal");
System.out.println(etBibliotek.visAntallBoker());
Bok enBok = etBibliotek.finnBok("Henning Mankell", "Italienske sko");
etBibliotek.leggTilBok("Berliner Poplene", "Anne B. Ragde", 2001, 411, "Per");
Bok toBok = etBibliotek.finnBok("Anne B. Ragde", "Berliner Poplene");
etBibliotek.leggTilBok("Erasmus Montanus", "Hans Nilsen", 1908, 200, "Gyldendal");
Bok treBok = etBibliotek.finnBok("Hans Nilsen", "Erasmus Montanus");
//if (enBok != null) {
//System.out.print(enBok.visTittel());
//System.out.println(etBibliotek.finnBok("Test2", "Test1"));
//}
}
}
Kode:
public class Bok {
private String tittel;
private String forfatter;
private int utgivelsesår;
private String forlag;
private int antallSider;
public Bok(String tittel, String forfatter, int år, int antSider, String forlag){
this.tittel = tittel;
this.forfatter = forfatter;
endreUtgivelsesår(år);
endreAntSider(antSider);
this.forlag = forlag;
}
//--------------- metoder som returnerer verdier --------------
public String visTittel (){
return tittel;
}
public String visForfatter (){
return forfatter;
}
public int visUtgivelsesÅr (){
return utgivelsesår;
}
//------------------- metoder som endrer på attributter ------------------------
public void endreUtgivelsesår(int år) {
utgivelsesår = år;
}
public void endreAntSider(int antSider){
if(antSider > 0){
antallSider = antSider;
}
}
public void endreForfatter(String nyForfatter){
forfatter = nyForfatter;
}
public String toString(){
return String.format("%s (%s, %d) ",
tittel, forfatter, utgivelsesår);
}
}
Kode:
import java.util.*;
//import java.text.*;
import java.util.ArrayList;
import java.util.Collections; //for å sortere arraylistene..
public class Bibliotek {
private ArrayList<Bok> bokListe;
public Bibliotek(){
bokListe = new ArrayList<Bok>();
}
private int finnBokPosisjon(String forfatter, String tittel){
int BokPosIndex = -1;
for(int i = 0; i < bokListe.size(); i++){
//System.out.println("Test om forfatter:"+forfatter+" og "+bokListe.get(i) .visForfatter()+" er like");
//System.out.println("Test om tittel:"+tittel+" og "+bokListe.get(i) .visTittel()+" er like");
if(bokListe.get(i).visForfatter().equalsIgnoreCase(forfatter)&& bokListe.get(i).visTittel().equalsIgnoreCase(tittel)){
BokPosIndex = i;
break; //antar at søket skal stoppe ved første treff
}
}
return BokPosIndex;
}
public int visAntallBoker(){
return bokListe.size();
}
public boolean leggTilBok(String forfatter,String tittel,int år, int antSider, String forlag){
boolean BokBleLagtTil = false;
if(finnBokPosisjon(forfatter, tittel) == -1){
Bok enBok = new Bok(forfatter, tittel, år, antSider, forlag);
Bok toBok = new Bok(forfatter, tittel, år, antSider, forlag);
Bok treBok = new Bok(forfatter, tittel, år, antSider, forlag);
bokListe.add(enBok);
bokListe.add(toBok);
bokListe.add(treBok);
BokBleLagtTil = true;
}
return BokBleLagtTil;
}
public Bok finnBok(String forfatter, String tittel){
//System.out.print("Det gikk");
System.out.print(bokListe.get(0).visForfatter());
//System.out.print(bokListe.get(0).visTittel());
Bok funnetBok = null;
int BokIndex = finnBokPosisjon(forfatter, tittel);
if(BokIndex != -1){
funnetBok = bokListe.get(BokIndex);
}
return funnetBok;
}
public Bok slettBok(String tittel, String forfatter){
Bok funnetBok = null;
int BokIndex = finnBokPosisjon(forfatter, tittel);
if(BokIndex != -1){
funnetBok = bokListe.get(BokIndex);
bokListe.remove(BokIndex);
}
return funnetBok;
}
public ArrayList<Bok> finnAlleBoker(String forfatter){
ArrayList<Bok> bokListePaaForfatter = new ArrayList<Bok>();
for(int i = 0; i < bokListe.size(); i++){
if(bokListe.get(i).visForfatter().equalsIgnoreCase(forfatter)){
bokListePaaForfatter.add(bokListe.get(i));
}
}
//Collections.sort(bokListePaaForfatter);
return bokListePaaForfatter;
}
//public boolean Test (String enBok, String toBok){
//if(forfatter).equals (forfatter)) {
//System.out.println("Funker");
//}
//System.out
public int sammenlign(Bok enBok, Bok toBok){
if (enBok.getNumPages()<toBok.getNumPages()){
return -1;
} else if (enBok.getNumPages()>toBok.getNumPages()){
return 1;
}
// must be equal, return 0
return 0;
}
}