Move to B.Sc. folder
This commit is contained in:
Binary file not shown.
@@ -0,0 +1,61 @@
|
||||
package Main;
|
||||
|
||||
public abstract class Animal {
|
||||
String name, spec;
|
||||
int age;
|
||||
static int count = 0;
|
||||
|
||||
public Animal(String name, String spec, int age) {
|
||||
this.name = name;
|
||||
this.spec = spec;
|
||||
this.age = age;
|
||||
count++;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
public String getSpec() {
|
||||
return this.spec;
|
||||
}
|
||||
|
||||
public int getAge() {
|
||||
return this.age;
|
||||
}
|
||||
|
||||
public int getHumanAge() {
|
||||
if (getSpec().equals("Hedgehog")) {
|
||||
return getAge() * 20;
|
||||
} else if (getSpec().equals("Cat")) {
|
||||
return getAge() * 6;
|
||||
} else if (getSpec().equals("Lion")) {
|
||||
return getAge() * 2;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
public static int getNumberOfCreatedAnimals() {
|
||||
return count;
|
||||
}
|
||||
|
||||
public void speak() {
|
||||
System.out.println("Hi, I am " + getSpec() + " and my name is " + getName() + ".");
|
||||
}
|
||||
|
||||
public void play() {
|
||||
if (getSpec().equals("Hedgehog")) {
|
||||
System.out.println(getName() + " is having a lot of fun.");
|
||||
|
||||
} else if (getSpec().equals("Lion")) {
|
||||
System.out.println(getName() + " is having a lot of fun.");
|
||||
((Lion) this).jump();
|
||||
((Lion) this).land();
|
||||
} else {
|
||||
System.out.println(((Cat) this).getColor() + " " + getName() + " is having fun.");
|
||||
Cat.doNastyThings();
|
||||
((Cat) this).jump();
|
||||
((Cat) this).land();
|
||||
}
|
||||
}
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,31 @@
|
||||
package Main;
|
||||
|
||||
public class Cat extends Animal implements Jumpable {
|
||||
String color;
|
||||
|
||||
public Cat(String name, String spec, int age, String color) {
|
||||
super(name, spec, age);
|
||||
this.color = color;
|
||||
}
|
||||
|
||||
static void doNastyThings() {
|
||||
System.out.println("...and doing nasty things...");
|
||||
}
|
||||
|
||||
public String getColor() {
|
||||
return this.color;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void jump() {
|
||||
System.out.println("Cat is jumping!");
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void land() {
|
||||
System.out.println("Cat is landing!");
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,15 @@
|
||||
package Main;
|
||||
|
||||
public class Hedgehog extends Animal {
|
||||
|
||||
int spikes;
|
||||
|
||||
public Hedgehog(String name, String spec, int age, int spikes) {
|
||||
super(name, spec, age);
|
||||
this.spikes = spikes;
|
||||
}
|
||||
|
||||
public int getSpikes() {
|
||||
return this.spikes;
|
||||
}
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,7 @@
|
||||
package Main;
|
||||
|
||||
public interface Jumpable {
|
||||
public void jump();
|
||||
|
||||
public void land();
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,22 @@
|
||||
package Main;
|
||||
|
||||
public class Lion extends Animal implements Jumpable {
|
||||
|
||||
public Lion(String name, String spec, int age) {
|
||||
super(name, spec, age);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void jump() {
|
||||
System.out.println("Lion is jumping!");
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void land() {
|
||||
System.out.println("Lion is landing!");
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,64 @@
|
||||
package Main;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author gessle
|
||||
*/
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
Scanner sc = new Scanner(System.in);
|
||||
boolean exit = false;
|
||||
|
||||
System.out.println("Give the zoo a name:");
|
||||
String name = sc.nextLine();
|
||||
System.out.println("Give the manager a name:");
|
||||
String manager = sc.nextLine();
|
||||
|
||||
Zoo zoo = new Zoo(name, manager);
|
||||
|
||||
while (!exit) {
|
||||
System.out.println(
|
||||
"1) Add an animal to the Zoo, 2) List all the animals, 3) Let animals play, 4) Go to the next year, 0) Exit");
|
||||
|
||||
if (sc.hasNext()) {
|
||||
int i = 0;
|
||||
String si = sc.nextLine();
|
||||
try {
|
||||
i = Integer.parseInt(si);
|
||||
} catch (NumberFormatException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
|
||||
switch (i) {
|
||||
case 1:
|
||||
zoo.addAnimalMenu(sc); // Let's use the same scanner in Zoo's second level addAnimalMenu
|
||||
break;
|
||||
|
||||
case 2:
|
||||
zoo.listAnimals();
|
||||
break;
|
||||
|
||||
case 3:
|
||||
zoo.letAnimalsPlay();
|
||||
break;
|
||||
case 4:
|
||||
zoo.nextYear();
|
||||
break;
|
||||
|
||||
case 0:
|
||||
System.out.println(
|
||||
"The zoo has closed. " + Animal.getNumberOfCreatedAnimals() + " animals were created.");
|
||||
exit = true;
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
sc.close();
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package Main;
|
||||
|
||||
public class ReservedNameException extends Exception {
|
||||
public ReservedNameException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,122 @@
|
||||
package Main;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class Zoo {
|
||||
int choice, age, spikes;
|
||||
String name, manager, animal, color;
|
||||
ArrayList<Animal> animals = new ArrayList<Animal>();
|
||||
|
||||
public Zoo(String name, String manager) {
|
||||
this.name = name;
|
||||
this.manager = manager;
|
||||
}
|
||||
|
||||
public void verifyName(String name) throws ReservedNameException {
|
||||
if (name.equalsIgnoreCase("Pehmo") || name.equalsIgnoreCase("Pikseli") || name.equalsIgnoreCase("Leo")) {
|
||||
throw new ReservedNameException("The name " + name + " is reserved.");
|
||||
}
|
||||
}
|
||||
|
||||
public void addAnimalMenu(Scanner sc) {
|
||||
System.out.println("1) Add a hedgehog, 2) Add a cat, 3) Add a lion");
|
||||
try {
|
||||
choice = Integer.valueOf(sc.nextLine());
|
||||
try {
|
||||
switch (choice) {
|
||||
case 1:
|
||||
System.out.println("Give it a name:");
|
||||
animal = sc.nextLine();
|
||||
verifyName(animal);
|
||||
System.out.println("How old it is:");
|
||||
age = Integer.valueOf(sc.nextLine());
|
||||
System.out.println("How many spikes it has:");
|
||||
spikes = Integer.valueOf(sc.nextLine());
|
||||
Hedgehog hedgehog = new Hedgehog(animal, "Hedgehog", age, spikes);
|
||||
animals.add(hedgehog);
|
||||
hedgehog.speak();
|
||||
break;
|
||||
case 2:
|
||||
System.out.println("Give it a name:");
|
||||
animal = sc.nextLine();
|
||||
verifyName(animal);
|
||||
System.out.println("How old it is:");
|
||||
age = Integer.valueOf(sc.nextLine());
|
||||
System.out.println("Give it a color:");
|
||||
color = sc.nextLine();
|
||||
Cat cat = new Cat(animal, "Cat", age, color);
|
||||
animals.add(cat);
|
||||
cat.speak();
|
||||
break;
|
||||
case 3:
|
||||
System.out.println("Give it a name:");
|
||||
animal = sc.nextLine();
|
||||
verifyName(animal);
|
||||
System.out.println("How old it is:");
|
||||
age = Integer.valueOf(sc.nextLine());
|
||||
Lion lion = new Lion(animal, "Lion", age);
|
||||
animals.add(lion);
|
||||
lion.speak();
|
||||
break;
|
||||
}
|
||||
} catch (ReservedNameException e) {
|
||||
System.out.println(e.getMessage());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void listAnimals() {
|
||||
System.out.println(name + " is lead by " + manager + " and it has the following animals:");
|
||||
for (Animal i : animals) {
|
||||
if (i.getSpec().equals("Hedgehog")) {
|
||||
System.out.println("The Hedgehog " + i.getName() + " is " + i.getAge() + " years old ("
|
||||
+ i.getHumanAge() + " in human years) and has " + ((Hedgehog) i).getSpikes() + " spikes.");
|
||||
} else if (i.getSpec().equals("Cat")) {
|
||||
System.out.println(
|
||||
"The " + ((Cat) i).getColor() + " Cat " + i.getName() + " is " + i.getAge() + " years old ("
|
||||
+ i.getHumanAge() + " in human years).");
|
||||
} else if (i.getSpec().equals("Lion")) {
|
||||
System.out.println(
|
||||
"The Lion " + i.getName() + " is " + i.getAge() + " years old ("
|
||||
+ i.getHumanAge() + " in human years).");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void letAnimalsPlay() {
|
||||
for (Animal i : animals) {
|
||||
i.play();
|
||||
}
|
||||
}
|
||||
|
||||
public void nextYear() {
|
||||
System.out.println("One year has passed.");
|
||||
|
||||
Iterator<Animal> i = animals.iterator();
|
||||
while (i.hasNext()) {
|
||||
Animal s = i.next();
|
||||
if (s.getSpec().equals("Hedgehog")) {
|
||||
if (s.getAge() >= 7) {
|
||||
System.out.println(
|
||||
"Hedgehog " + s.getName() + " passed away at the age of " + (s.getAge()) + ".");
|
||||
i.remove();
|
||||
}
|
||||
} else if (s.getSpec().equals("Cat")) {
|
||||
if (s.getAge() >= 20) {
|
||||
System.out.println("Cat " + s.getName() + " passed away at the age of " + (s.getAge()) + ".");
|
||||
i.remove();
|
||||
}
|
||||
} else if (s.getSpec().equals("Lion")) {
|
||||
if (s.getAge() >= 25) {
|
||||
System.out.println("Lion " + s.getName() + " passed away at the age of " + (s.getAge()) + ".");
|
||||
i.remove();
|
||||
}
|
||||
}
|
||||
}
|
||||
for (Animal each : animals) {
|
||||
each.age = each.getAge() + 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user