Code:
import java.io.*;
public class PetTest {
public static void main(String[] args) {
try {
BufferedReader br = new BufferedReader(
new InputStreamReader(System.in));
System.out.println("------------------------------------------------");
System.out.println("Please Choose From the Following:\n 1. Dogs " +
"2. Cats 3. Fish 4. Birds 5. VIEW ALL ");
System.out.println("------------------------------------------------");
String line = br.readLine().trim();
int selection = Integer.parseInt(line);
String s = "";
switch(selection) {
case 1:
s = "dog";
break;
case 2:
s = "cat";
break;
case 3:
s = "bird";
break;
case 4:
s = "fish";
break;
case 5:
s = "View All";
break;
default:
s = "unexected selection";
}
System.out.println("selection = " + s);
Dog dog = null ;
Cat cat = null;
Fish fish = null;
Bird bird = null;
if(s.equalsIgnoreCase("dog") || s.equalsIgnoreCase("View All"))
{
dog = new Dog(4, 12, 50.00, "Terrier", Dog.DRY);
System.out.println("\ndog = " + dog);
}
if(s.equalsIgnoreCase("cat") || s.equalsIgnoreCase("View All")){
cat = new Cat(5, 5, 25.00, "Persian", Cat.CLAWED);
System.out.println("\ncat = " + cat);}
if(s.equalsIgnoreCase("bird")|| s.equalsIgnoreCase("View All")){
bird = new Bird(3, 2, 5.00, "Canary", "USA");
System.out.println("\nbird = " + bird);}
if(s.equalsIgnoreCase("fish")|| s.equalsIgnoreCase("View All")){
fish = new Fish(1, 1, 10.00, "Angel", Fish.SALT);
System.out.println("\nfish = " + fish);
}
br.close();
} catch(IOException e) {
System.out.println("Read error: " + e.getMessage());
}
}
}
class Pet {
int age;
double weight;
double price;
protected Pet(int age, double weight, double price) {
this.age = age;
this.weight = weight;
this.price = price;
}
public String toString() {
//String name = getClass().getName();
return ", \nage:" + age +
", \nweight:" + weight +
", \nprice:" + price;
}
}
class Dog extends Pet {
String breed;
String foodType;
final static String CANNED = "canned food";
final static String DRY = "dry food";
public Dog(int age, double weight, double price,
String breed, String foodType) {
super(age, weight, price);
this.breed = breed;
this.foodType = foodType;
}
public String toString() {
return "\n\nDog \nbreed:" + breed +
", \nfoodType:" + foodType + super.toString() + "";
}
}
class Cat extends Pet {
String breed;
String clawStatus;
final static String CLAWED = "has claws";
final static String DECLAWED = "de-clawed";
public Cat(int age, double weight, double price,
String breed, String clawStatus) {
super(age, weight, price);
this.breed = breed;
this.clawStatus = clawStatus;
}
public String toString() {
return "\n\nCat \nbreed:" + breed +
", \nclawStatus:" + clawStatus +
super.toString() + "";
}
}
class Bird extends Pet {
String type;
String nationalOrigin;
public Bird(int age, double weight, double price,
String type, String origin) {
super(age, weight, price);
this.type = type;
nationalOrigin = origin;
}
public String toString() {
return "\n\nBird \ntype:" + type +
", \nnationalOrigin:" + nationalOrigin +
super.toString() + "";
}
}
class Fish extends Pet {
String type;
String waterType;
final static String FRESH = "freah water";
final static String SALT = "salt water";
public Fish(int age, double weight, double price,
String type, String habitat) {
super(age, weight, price);
this.type = type;
waterType = habitat;
}
public String toString() {
return "\n\nFish \ntype:" + type +
", \nwaterType:" + waterType +
super.toString() + "";
}
}
Bookmarks