CymsSoul

Untitled

Jan 25th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.79 KB | None | 0 0
  1.  
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import java.util.Map;
  5. import java.util.Scanner;
  6.  
  7.  
  8. /*
  9. * To change this license header, choose License Headers in Project Properties.
  10. * To change this template file, choose Tools | Templates
  11. * and open the template in the editor.
  12. */
  13. /**
  14. *
  15. * @author Lisa Newport
  16. */
  17. public class BirdwatcherDatabase {
  18.  
  19. private Map<String, String> birdNameMap = new HashMap<String, String>();
  20. private ArrayList<String> commonNameCount = new ArrayList<String>();
  21. private Scanner scanner;
  22. private String birdCommonName;
  23.  
  24. public BirdwatcherDatabase(Scanner scanner) {
  25. this.scanner = scanner;
  26. }
  27.  
  28. public void runProgram(String command) {
  29. String addCommand = "Add";
  30. String observCommand = "Observation";
  31. String statsCommand = "Statistics";
  32. String showCommand = "Show";
  33.  
  34. if (command.equalsIgnoreCase(addCommand)) {
  35. addData();
  36. } else if (command.equalsIgnoreCase(observCommand)) {
  37. addObservation();
  38. } else if (command.equalsIgnoreCase(statsCommand)) {
  39. printStats();
  40. } else if (command.equalsIgnoreCase(showCommand)) {
  41. System.out.print("What? ");
  42. birdCommonName = scanner.nextLine();
  43. showStatForOne(birdCommonName);
  44. }
  45. }
  46.  
  47. private void addData() {
  48. System.out.print("Name: ");
  49. birdCommonName = scanner.nextLine();
  50.  
  51. System.out.print("Latin Name: ");
  52. String latinName = scanner.nextLine();
  53.  
  54. birdNameMap.put(birdCommonName, latinName);
  55. }
  56.  
  57. private void addObservation() {
  58. System.out.print("What was observed:? ");
  59. birdCommonName = scanner.nextLine();
  60.  
  61. if (birdNameMap.containsKey(birdCommonName)) {
  62. commonNameCount.add(birdCommonName);
  63. } else {
  64. System.out.println("Is not a bird!");
  65. }
  66. }
  67.  
  68. private void printStats() {
  69. for (String key : birdNameMap.keySet()) {
  70. giveStats(key);
  71. }
  72. }
  73.  
  74. private void showStatForOne(String birdNameKey) {
  75. giveStats(birdCommonName);
  76. }
  77.  
  78. private void giveStats(String birdNameKey) {
  79. int birdNameCounter = 0;
  80.  
  81. System.out.print(birdNameKey + " (" + birdNameMap.get(birdNameKey) + "): ");
  82.  
  83. if (!commonNameCount.isEmpty()) {
  84. for (String birdName : commonNameCount) {
  85.  
  86. if (birdName.equalsIgnoreCase(birdNameKey)) {
  87. birdNameCounter++;
  88. }
  89. }
  90. System.out.println(birdNameCounter + " observations");
  91. birdNameCounter = 0;
  92. }
  93. else {
  94. System.out.println(birdNameCounter + " observations");
  95. }
  96. }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment