Object-Oriented Programming with Java, part I – Exercise 103

This exercise gets us to create a Bird Watchers database. Basically the same as the Library exercise from before.

My Code

Main


import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        // implement your program here
        // do not put all to one method/class but rather design a proper structure to your program

        // Your program should use only one Scanner object, i.e., it is allowed to call 
        // new Scanner only once. If you need scanner in multiple places, you can pass it as parameter
        Scanner reader = new Scanner(System.in);
        Database Database = new Database();

        while (true) {
            System.out.print("? ");
            String userInput = reader.nextLine();

            if (userInput.contentEquals("Add")) {
                System.out.print("Name: ");
                String name = reader.nextLine();
                System.out.print("Latin Name: ");
                String nameLatin = reader.nextLine();
                Bird bird = new Bird(name, nameLatin);
                Database.addBird(bird);
            } else if (userInput.contentEquals("Observation")) {
                System.out.print("What was observed:? ");
                String observation = reader.nextLine();
                Database.addObservation(observation);
            } else if (userInput.contentEquals("Statistics")) {
                Database.statistics();
            } else if (userInput.contentEquals("Show")) {
                System.out.print("What? ");
                String show = reader.nextLine();
                Database.show(show);
            } else if (userInput.contentEquals("Quit")) {
                System.exit(0);
            }
        }
    }

}

Bird

public class Bird {
    private String name;
    private String nameLatin;
    private int observations;
    
    public Bird(String name, String nameLatin, int observations){
        this.name = name;
        this.nameLatin = nameLatin;
        this.observations = observations;

    }
    
    public Bird(String name, String nameLatin){
        this.name = name;
        this.nameLatin = nameLatin;
        this.observations = 0;

    }
    
    public String name(){
        return this.name;
    }
    
    public String nameLatin(){
        return this.nameLatin;
    }
    
    public int observations(){
        return this.observations;
    }
    
    public void incObservation(){
        this.observations++;
    }

    
}

Database

import java.util.ArrayList;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author masonbee
 */
public class Database {

    private ArrayList<Bird> database;

    public Database() {
        this.database = new ArrayList<Bird>();
    }

    public void addBird(Bird bird) {
        this.database.add(bird);
    }

    public void addObservation(String observation) {
        boolean test = false;
        for (Bird bird : database) {
            if (bird.name().equals(observation)) {
                test = true;
                bird.incObservation();
                break;
            }
        }
        if (test == false) {
            System.out.println("Is not a bird!");
        }
    }

    public void statistics() {
        for (Bird bird : database) {
            System.out.println(bird.name() + " (" + bird.nameLatin() + "): " + bird.observations() + " observations");
        }

    }

    public void show(String show) {
        for (Bird bird : database) {
            if (bird.name().contentEquals(show)) {
                System.out.println(bird.name() + " (" + bird.nameLatin() + "): " + bird.observations() + " observations");
            }
        }

    }
}

Comments

As far as I can tell it works correctly but as I was unable to test it (there was an error from TMC) I might have missed something. I suppose I should clean it up and so on but since I can’t test it with them I will continue onto the next exercise instead.

Leave a comment