Created
January 23, 2017 17:14
-
-
Save VictorVSa/fba8892d0495c023500d0b60ecf79cde to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Created by vrt on 23/01/2017. | |
*/ | |
public class Bird { | |
private String name; | |
private String latinName; | |
private int observations; | |
public Bird(String name, String latinName) { | |
this.name = name; | |
this.latinName = latinName; | |
} | |
public String toString(){ | |
return String.format("%s (%s): %d observations", name, latinName, observations); | |
} | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public String getLatinName() { | |
return latinName; | |
} | |
public void setLatinName(String latinName) { | |
this.latinName = latinName; | |
} | |
public int getObservations() { | |
return observations; | |
} | |
public void setObservations() { | |
this.observations++; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
/** | |
* Created by vrt on 23/01/2017. | |
*/ | |
public class Database { | |
private ArrayList<Bird> lista; | |
public Database() { | |
lista = new ArrayList<>(); | |
} | |
public void addBird(Bird pajaros) { | |
lista.add(pajaros); | |
} | |
public void observed(String name) { | |
if (checkName(name)) { | |
for (Bird pajaro : lista) { | |
if (pajaro.getName().trim().toLowerCase().contains(name.trim().toLowerCase())) { | |
pajaro.setObservations(); | |
} | |
} | |
}else System.out.println("Is not a bird!"); | |
} | |
public boolean checkName(String name) { | |
for (Bird pajaro : lista) { | |
if (pajaro.getName().trim().toLowerCase().contains(name.trim().toLowerCase())) { | |
return true; | |
} | |
} | |
return false; | |
} | |
public void statistics() { | |
for (Bird pajaro : lista) { | |
System.out.println(pajaro.toString()); | |
} | |
} | |
public void show(String name){ | |
if(checkName(name)){ | |
for (Bird pajaro : lista) { | |
if (pajaro.getName().trim().toLowerCase().contains(name.trim().toLowerCase())) { | |
System.out.println(pajaro.toString()); | |
} | |
}}else { | |
System.out.println("Is not a bird!");} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
public class Main { | |
public static void main(String[] args) { | |
String command; | |
Scanner scanner = new Scanner(System.in); | |
Database db = new Database(); | |
// 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 | |
do { | |
System.out.print("?: "); | |
command = scanner.nextLine(); | |
if (command.trim().toLowerCase().equals("quit")) { | |
break; | |
} | |
switch (command.trim().toLowerCase()) { | |
case "add": | |
addBird(scanner,db); | |
break; | |
case "observation": | |
observation(scanner, db); | |
break; | |
case "statistics": | |
statistics(db); | |
break; | |
case "show": | |
show(scanner, db); | |
break; | |
} | |
} while (true); | |
} | |
private static void addBird(Scanner scanner, Database db ) { | |
String name; | |
String latinName; | |
System.out.print("Name: "); | |
name = scanner.nextLine(); | |
System.out.print("Latin Name: "); | |
latinName = scanner.nextLine(); | |
if (!name.equals("") && !latinName.equals("")){ | |
Bird pajaro = new Bird(name, latinName); | |
db.addBird(pajaro); | |
} | |
} | |
private static void observation(Scanner scanner, Database db) { | |
System.out.println("What was observed:? "); | |
db.observed(scanner.nextLine()); | |
} | |
private static void statistics(Database db) { | |
db.statistics(); | |
} | |
private static void show(Scanner scanner, Database db) { | |
System.out.println("What? "); | |
db.show(scanner.nextLine()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment