Java homework part 5.

Java homework part 5.

kuppi

Been a while since my last post. Some of my homework has been really simple so I didn't find them post worthy but the previous week we dove into exceptions and maps/hashmaps.

I found exception handling to be extremely interesting and probably really valuable information when it comes to my future with coding. the program I will be going through below is the the last exercise of 4 utilizing both hashmaps and exception handling.

I booked an interview for a tech support summer job. So I'll be easing my foot off the gas on coding for a bit due to me needing time practicing my Windows AD skills and some other projects for school.

 

4/4

So this exercise had us create a Java program with which the user can add friend/contact details to the program, search through them unique key, delete contacts by the same unique key and list all contacts. I'll post the program here bit by bit. For the full code go to my github

The first thing I did was I created the Ystavat.java class to create the "friend" object for the main program.

public class Ystava{
        
    public String fullName;
    public String nickName;
    public String phoneNr;
    public String birthDay;

    public Ystava() {
        fullName = "";
        nickName = "";
        phoneNr = "";
        birthDay = null;
    }
    public Ystava(String fullName, String nickName, String phoneNr, String birthDay){
        this.fullName = fullName;
        this.nickName = nickName;
        this.phoneNr = phoneNr;
        this.birthDay = birthDay;
    }
    
    public void setFullName(String fullName) {
        this.fullName = fullName;
    }
    public void setNickName(String nickName) {
        this.nickName = nickName;
    }
    public void setPhone(String phoneNr) {
        this.phoneNr = phoneNr;
    }
    public void setBirthDay(String birthDay) {
        this.birthDay = birthDay;
    }
    
    public String getFullName() {
        return fullName;
    }
    public String getNickName() {
        return nickName;
    }
    public String getPhone() {
        return phoneNr;
    }
    public String getBirthDay() {
        return birthDay;
    }
    
    public String toString() {
        return "lempinimi= " + nickName + ", nimi= " + fullName + ", puhelin= " + phoneNr
                + ", syntpvm= " + birthDay;
    }

}

Pretty simple.

Next up I created the main for the main class YstavatSovellus.java

    public static void main(String[] args) {
        // Takes in useraction and functions accordingly.
        
        Scanner input = new Scanner(System.in);
        YstavatSovellus newFriend = new YstavatSovellus(); // create olio of same class to call functions from same class
        
        int userInput = 0;
        // runs program while userinput != zero.
        do {
            System.out.println("1) Lisää ystävä\n2) Etsi ystävä\n3) Poista ystävä\n4) Tulosta ystävät\n0) Lopeta");
            System.out.print("Valitse: ");
            userInput = input.nextInt();
            switch(userInput){
                case 1:{
                    newFriend.addFriend();
                    break;
                        }
                case 2:{
                    newFriend.searchFriend();
                    break;
                        }
                case 3:{
                    newFriend.deleteFriend();
                    break;
                        }
                case 4:{
                    newFriend.printAll();
                    break;
                        }
                case 0:{
                    break;
                        }        
        }
            
        }while(userInput != 0);
        
    }

I've started commenting on my code more actively but I'll still throw some info here. The "interface" of the program loops around until the user types in a 0 after which the program closes. Now that I think of it I could've made the program function better by handling input that's out of the 0-4 range. But while the input is in the 0-4 range the program calls the created functions.

addFriend

The addFriend method just adds a new friend to the hashmap only if a friend with the same nickname doesn't exist.

run1

    public void addFriend() {
        // Adds a friend to hashmap
        String friendNick, friendName, friendPhone, friendBirth;
        boolean exist = false;
        
        Set<String> nicks = friends.keySet();
        Iterator<String> it = nicks.iterator();
        
        String nextNick = null;
        Ystava nextYstava = null;
        
        
        // Ask for friend details
        // check if friend exists already
        System.out.print("Anna ystävän lempinimi: ");
        friendNick = input.nextLine();
        
        nextYstava = friends.get(friendNick);
        
        if(nextYstava == null) {
            System.out.print("Anna ystävän nimi: ");
            friendName = input.nextLine();
            System.out.print("Anna ystävän puhelinnumero: ");
            friendPhone = input.nextLine();
            System.out.print("Anna ystävän syntymäpäivämäärä muodossa pp.kk.vvvv: ");
            friendBirth = input.nextLine();
            friendBirth = parseTheDate(friendBirth); // Makes sure the userinput is in dd.MM.yyyy format
                
            //create new friend
            Ystava newYstava = new Ystava(friendName, friendNick, friendPhone, friendBirth);
            // put friend in hashmap with key attribute nickname
            friends.put(friendNick, newYstava);
            }
            
        else {
            System.out.println("Olet jo lisännyt tämän ystävän");
            System.out.println("Ystävän tiedot: " + nextYstava.toString());
        }

    }

The addFriend method runs the parseTheDate method to make sure that the birthday the user inputs gets parsed to the format I want. dd.MM.yyyy. Could've used more exception handling here, but it was not necessary for the exercise.

    public String parseTheDate(String friendBirth) {
        Date parseDate =null;
        try {
            parseDate = simpledate.parse(friendBirth);
        }
        catch(ParseException e){
            System.out.println("Juupajuu");
        }
        friendBirth = simpledate.format(parseDate);
        return friendBirth;
    }

searchFriend

The second part of the program searches for the user desired friend from the hashmap using map functions.

richard

    public void searchFriend() {
        // this could have been done better... I feel like just searching with friends.get(keyvalue) would've worked.
        Set<String> nicks = friends.keySet();
        Iterator<String> it = nicks.iterator();
        
        String searchFor;
        boolean found = false;
        
        System.out.print("Anna ystävän lempinimi: ");
        searchFor = input.nextLine();
        
        String nextNick = null;
        Ystava nextYstava = null;
        
        while(it.hasNext() || found == false) {
            try {
            nextNick = it.next();
            nextYstava = friends.get(searchFor);        
            found = true;
            if (found == true) {
                System.out.println("Ystävän tiedot: " + nextYstava.toString());
                break;
                }
            }

        catch(NoSuchElementException e){
            System.out.println("Ystävää, jonka lempinimi on " + searchFor + " ei ole");
            break;
        }
        catch(NullPointerException e) {
            System.out.print("Ystävää, jonka lempinimi on " + searchFor + " ei ole");
            break;
        }
        }
    }

I feel like this could've been done a little simpler but this ended up working.

deleteFriend

notrich

This one deletes friends based off unique key. Also it doesn't do anything if the given friend does not exist using exceptions.

    public void deleteFriend() {
        String delFriend;
        System.out.print("Anna ystävän lempinimi: ");
        delFriend = input.nextLine();
        try {
        System.out.print("Poistetaan ystävän tiedot: " + friends.get(delFriend).toString());
        }
        catch(NoSuchElementException e) {
            System.out.println("Ystävää ei ole");
        }
        catch(NullPointerException e) {
            System.out.println("Ystävää ei ole");
        }
        friends.remove(delFriend);
    }

printAll

Simply prints all the given friends in the hashmap. I added a few for flavor.

listall

    public void printAll() {
        //runs through hashmap and prints all values toString
        Set<String> nicks = friends.keySet();
        Iterator<String> it = nicks.iterator();
        
        String nextNick = null;
        Ystava nextYstava = null;
        
        while(it.hasNext()) {
            nextNick = it.next();
            nextYstava = friends.get(nextNick);
            
            System.out.println(nextYstava.toString());
        }
    }

The other exercises this week were just simpler versions of this so I won't be posting about them I might write something about the previous week and using lists though!