Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.Map;
- import java.util.Scanner;
- /*
- * 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 Lisa Newport
- */
- public class BirdwatcherDatabase {
- private Map<String, String> birdNameMap = new HashMap<String, String>();
- private ArrayList<String> commonNameCount = new ArrayList<String>();
- private Scanner scanner;
- private String birdCommonName;
- public BirdwatcherDatabase(Scanner scanner) {
- this.scanner = scanner;
- }
- public void runProgram(String command) {
- String addCommand = "Add";
- String observCommand = "Observation";
- String statsCommand = "Statistics";
- String showCommand = "Show";
- if (command.equalsIgnoreCase(addCommand)) {
- addData();
- } else if (command.equalsIgnoreCase(observCommand)) {
- addObservation();
- } else if (command.equalsIgnoreCase(statsCommand)) {
- printStats();
- } else if (command.equalsIgnoreCase(showCommand)) {
- System.out.print("What? ");
- birdCommonName = scanner.nextLine();
- showStatForOne(birdCommonName);
- }
- }
- private void addData() {
- System.out.print("Name: ");
- birdCommonName = scanner.nextLine();
- System.out.print("Latin Name: ");
- String latinName = scanner.nextLine();
- birdNameMap.put(birdCommonName, latinName);
- }
- private void addObservation() {
- System.out.print("What was observed:? ");
- birdCommonName = scanner.nextLine();
- if (birdNameMap.containsKey(birdCommonName)) {
- commonNameCount.add(birdCommonName);
- } else {
- System.out.println("Is not a bird!");
- }
- }
- private void printStats() {
- for (String key : birdNameMap.keySet()) {
- giveStats(key);
- }
- }
- private void showStatForOne(String birdNameKey) {
- giveStats(birdCommonName);
- }
- private void giveStats(String birdNameKey) {
- int birdNameCounter = 0;
- System.out.print(birdNameKey + " (" + birdNameMap.get(birdNameKey) + "): ");
- if (!commonNameCount.isEmpty()) {
- for (String birdName : commonNameCount) {
- if (birdName.equalsIgnoreCase(birdNameKey)) {
- birdNameCounter++;
- }
- }
- System.out.println(birdNameCounter + " observations");
- birdNameCounter = 0;
- }
- else {
- System.out.println(birdNameCounter + " observations");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment