-2

I'm trying to create a program where the user can create a new person which will be stored in a HashMap. Further you can also lend DVD's across from these persons that I want to be stored. The problem arrives when I'm trying to use the method NyPerson, when the program just refuses to store the new name in the HashMap.

Here's my code(Lot's of code so bear with me):

the class with the nyPerson method:

import java.util.*;
class DVDAdministrasjon {

private HashMap<String, Person> navneliste = new HashMap<String, Person>();

public void nyPerson(String navnPerson) {
    if(navneliste.containsKey(navnPerson)) {
        System.out.println(navnPerson + " er allerde i listen");
    } else {
        Person nyPerson = new Person(navnPerson);
        navneliste.put(navnPerson, nyPerson);

    }
 }

 public void kjop(String navnPerson, String navnDVD) {
    if (navneliste.containsKey(navnPerson)) {
        navneliste.get(navnPerson).kjop(navnDVD);
    } else {
        System.out.println("Det er ingen personer som heter " + navnPerson);
    }
}

public void laan (String navnLaaner, String navnUtlaaner, String navnDVD) {
    if (navnLaaner.equals(navnUtlaaner)) {
        System.out.println("Du kan ikke laane en DVD fra deg selv");
    } else if (navneliste.containsKey(navnLaaner) &&               

     navneliste.containsKey(navnUtlaaner)) {
        Person utlaaner = navneliste.get(navnUtlaaner);
        Person laaner = navneliste.get(navnLaaner);

        if (utlaaner.ledig(navnDVD)) {
            laaner.laan(navnDVD, utlaaner);
        } else {
            System.out.println("Den DVD-en er utlaant eller eies ikke av           denne personen");
        }

    } else {
        System.out.println("Sjekk at laaner og utlaaner eksisterer");
    }
}

public void retur (String navnPerson, String navnDVD) {
    if (navneliste.containsKey(navnPerson)) {
        navneliste.get(navnPerson).retur(navnDVD);
    } else {
        System.out.println("Det er ingen personer som heter " + navnPerson);
    }
}

public void visPerson(String navnPerson) {
    if (navnPerson.equals("*")) {
        for (Person person : navneliste.values()) {
            person.printDVDer();
        }

    } else if (navneliste.containsKey(navnPerson)){
        navneliste.get(navnPerson).printDVDer();
    } else {
        System.out.println("Det er ingen personer som heter " + navnPerson);
    }
   }

    public void visOversikt() {
    for (Person person : navneliste.values()) {
        person.oversikt();
    }
  }

   public void printPersoner(){
    for(Person person : navneliste.values())
    System.out.println(person);
}

public void printArkiv(String navnPerson) {
    navneliste.get(navnPerson).printArkiv();
}

public void avslutt(){
    System.out.println("Goodbye");
    System.exit(0);
}
}

my Person class:

import java.util.*;

class Person {

private String navn;
private HashMap<String, DVD> arkiv = new HashMap<String, DVD>();
private HashMap<String, DVD> laante = new HashMap<String, DVD>();
private HashMap<String, DVD> utlaante = new HashMap<String, DVD>();

public Person(String navn) {
    this.navn = navn;
}

public void kjop(String navn) {
    if (arkiv.containsKey(navn)) {
        System.out.println("Du eier allerede denne dvd'en");
    } else {
        DVD nyDVD = new DVD(navn, this);
        arkiv.put(navn, nyDVD);
    }
}

public void laan(String navnDVD, Person utlaaner) {
    laante.put(navnDVD, utlaaner.utlaan(navnDVD));
}

public DVD utlaan(String navnDVD) {
    DVD utlaanDVD = arkiv.get(navnDVD);
    utlaante.put(navnDVD, utlaanDVD);
    return utlaanDVD;
}

public void retur(String navnDVD) {
    if (laante.containsKey(navnDVD)) {
        laante.remove(navnDVD).retur();
    } else {
        System.out.println(navn + " laaner ikke denne dvd-en");
    }
}

public void faaTilbake(String tilbake) {
    utlaante.remove(tilbake);
}

public boolean ledig(String navnDVD) {
    return (arkiv.containsKey(navnDVD) && !utlaante.containsKey(navnDVD));
}

public void printArkiv() {
    for (DVD dvd : arkiv.values()) {
        System.out.println(dvd);
    }
}

public void printDVDer() {
    System.out.println("\n" + navn + ":");
    System.out.println("Eier:");
    for (String key : arkiv.keySet()) {
        System.out.println("+ " + key);
    }
    System.out.println("Laaner:");
    for (String key : laante.keySet()) {
        System.out.println("- " + key);
    }
}

public void oversikt() {
    System.out.println("\n" + navn + ":");
    System.out.println("Eier: " + arkiv.size());
    System.out.println("Laant: " + laante.size());
    System.out.println("Utlaant: " + utlaante.size());
}

public String toString() {
    return this.navn;
}

}

DVD class:

import java.util.*;
class DVD {

private Person eier;
private Person laaner;
private String navn;

public DVD(String navn, Person eier) {
    this.navn = navn;
    this.eier = eier;
    this.laaner = null;
}

public Person getEier() {
    return this.eier;
}

public String toString() {
    return this.navn;
}

public void retur() {
    eier.faaTilbake(navn);
}

}

And here's my main(so far):

import java.util.*;

public class DVDMain{
public static void main(String [] args){

Scanner tastatur = new Scanner(System.in);
String alternativ = "";


while(!alternativ.equals("7")){
System.out.println("MENY FOR DVD-ADMINISTRASJON");
System.out.println("1. Ny person");
System.out.println("2. Kjop");
System.out.println("3. Laan");
System.out.println("4. Vis");
System.out.println("5. Oversikt");
System.out.println("6. Retur");
System.out.println("7. Avslutt");
alternativ = tastatur.nextLine();



DVDAdministrasjon dvdadmin = new DVDAdministrasjon();
if (alternativ.equals("1")){
  System.out.println("Hva heter den nye personen?");
  String nyperson = tastatur.nextLine();
  dvdadmin.nyPerson(nyperson);
  System.out.println(nyperson + " er registrert!");
}

else if (alternativ.equals("2")){
  System.out.println("Hvem har kjopt DVD-en?");
  String navnPerson = tastatur.nextLine();
  System.out.println("Hva er tittelen paa DVD-en?");
  String navnDVD = tastatur.nextLine();
  dvdadmin.kjop(navnPerson, navnDVD);
}
else if(alternativ.equals("3")){
  System.out.println("Hvem vil laane DVD-en?");
  String navnLaaner = tastatur.nextLine();
  System.out.println("Hvem skal DVD-en laanes fra?");
  String navnUtlaaner = tastatur.nextLine();
  System.out.println("Hva er tittelen paa DVD-en?");
  String navnDVD = tastatur.nextLine();
  dvdadmin.laan(navnLaaner, navnUtlaaner, navnDVD);
}
}
}
}
Knut Eriksen
  • 27
  • 1
  • 8
  • 1
    What does "program just refuses" mean? – Andreas Nov 05 '16 at 18:06
  • Not necessarily refuses, but it won't add the specific String as a value in the HashMap navneliste. – Knut Eriksen Nov 05 '16 at 18:09
  • Every times you choose a new menu option, you create a new `DVDAdministrasjon`. The new person *is* added to the `HashMap`, you just throw the entire map away every time. Perhaps you might want to move that *outside* the menu loop? – Andreas Nov 05 '16 at 18:11
  • Didn't notice that, thank you! – Knut Eriksen Nov 05 '16 at 18:16
  • Since this question is of no use to anyone else, you should delete it. That will also clear the negative rep you got from the down-votes. – Andreas Nov 05 '16 at 18:24

1 Answers1

0

As far as I understand, you start your application and type in 1 to add a new person. If you do it again, the person is not found in the map. The problem is, that you instantiate a new DVDAdministrasjon in every run of the loop, so everytime a person or new name is added, the map along with the whole object dvdadmin is overwritten.

To fix this, simply create your dvdadmin instance outside of the loop. As a further remark, always close I/O resources to avoid resource leaks, in your case the Scanner.

thatguy
  • 21,059
  • 6
  • 30
  • 40
  • `Scanner` on `System.in` should/need not be closed. – Andreas Nov 05 '16 at 18:25
  • @Andreas Could you explain why one should not close it? – thatguy Nov 05 '16 at 20:07
  • 1
    In general, the code that open/creates a resource is responsible for closing it. You're not opening the `System.in` resource, just wrapping it with a `Scanner`, so you're not the one responsible for the resource, and hence you should not close it. Said another way, if you didn't wrap `System.in` but just used it directly, would you have called `System.in.close()` before exiting the program? No. Similarly, nobody closes `System.out`. You're not supposed to. They are both "owned" by the JVM. – Andreas Nov 05 '16 at 22:17
  • @Andreas Thanks for the explanation. This is an important point, since a Java 7 compiler will give you a [warning](http://stackoverflow.com/questions/15613626/scanner-is-never-closed), if you do not close a scanner. Since you have created the scanner, but not ´System.in`, it would be good practice to close it, wouldn't it? – thatguy Nov 06 '16 at 02:12
  • No, because closing the `Scanner` will close `System.in` *(unless you first wrap `System.in` with a [close-shield](http://commons.apache.org/proper/commons-io/javadocs/api-release/org/apache/commons/io/input/CloseShieldInputStream.html))*. The Java compiler is too dumb to recognize the scenario, so just add annotation to ignore warning (`@SuppressWarnings("resource")`). – Andreas Nov 06 '16 at 09:00