I'm learning Java in a MOOC right now and this is my solution for exercise Big year.
This is the exercise:
In this exercise you will design and implement a database for bird-watchers. The database contains birds, each of which has a name (string) and a name in Latin (string). The database also counts the observations of each bird.
The program must implement the following commands:
- Add - adds a bird.
- Observation - adds an observation.
- All - prints all birds.
- One - prints one bird.
- Quit - ends the program.
Incorrect input must also be handled.
This is my solution:
Main.java:
import java.util.Scanner;
public class mainProgram {
public static void main(String[] args) {
// NB! Do not create other scanner objects than the one below
// if and when you create other classes, pass the scanner to them
// as a parameter
Scanner scan = new Scanner(System.in);
BirdDataBase birds = new BirdDataBase();
UserInterface program = new UserInterface(birds, scan);
program.start();
}
}
Bird.java:
public class Bird {
private final String name;
private final String latinName;
private int observation;
public Bird(String name, String latinName) {
this.name = name;
this.latinName = latinName;
this.observation = 0;
}
public String getName() {
return name;
}
public String getLatinName() {
return latinName;
}
public void observation(){
observation++;
}
public int getObservation() {
return observation;
}
}
BirdDataBase.java:
import java.util.ArrayList;
public class BirdDataBase {
private final ArrayList<Bird> birds;
public BirdDataBase() {
this.birds = new ArrayList<>();
}
public void add(Bird bird) {
if (!(this.birds.contains(bird))) {
birds.add(bird);
} else {
System.out.println("Bird is already in Birds Database");
}
}
public ArrayList birds() {
return birds;
}
}
UserInterface.java:
import java.util.ArrayList;
import java.util.Scanner;
public class UserInterface {
private final BirdDataBase birdsDataBase;
private final Scanner scanner;
public UserInterface(BirdDataBase birds, Scanner scanner) {
this.birdsDataBase = birds;
this.scanner = scanner;
}
public void start() {
ArrayList<Bird> birds = this.birdsDataBase.birds();
while (true) {
System.out.print("? ");
String command = scanner.nextLine().toLowerCase();
if (command.equals("add")) {
System.out.print("Name: ");
String name = scanner.nextLine().toUpperCase();
System.out.print("Name in Latin: ");
String latinName = scanner.nextLine().toUpperCase();
birds.add(new Bird(name, latinName));
} else if (command.equals("observation")) {
System.out.print("Bird: ");
String name = scanner.nextLine().toUpperCase();
for (int i = 0; i < birds.size(); i++) {
if (birds.get(i).getName().equals(name)) {
birds.get(i).observation();
} else {
System.out.println("Not a Bird!");
}
}
} else if (command.equals("all")) {
for (Bird bird : birds) {
System.out.println(bird.getName() + " ("
+ bird.getLatinName() + "): " + bird.getObservation()
+ " observations.");
}
} else if (command.equals("one")) {
System.out.println("Bird?");
String name = scanner.nextLine();
for (Bird bird : birds) {
if (bird.getName().equals(name)) {
System.out.println(bird.getName() + " ("
+ bird.getLatinName() + "): " + bird.getObservation()
+ " observations.");
}
}
} else if (command.equals("quit")) {
break;
}
}
}
}
In your opinion, is this a good solution?
UserInterface
start
method into subordinate methods, one for each menu option. \$\endgroup\$