In: Computer Science
Write a program that prints the insurance fee to pay for a pet according to the following rules:(NOTE:You must use a switch statement to determine pet fee.)A dog that has been neutered costs $50.A dog that has not been neutered costs $80.A cat that has been spayedcosts $40.A cat that has not been spayedcosts $60.A bird or reptile costs nothing.Any other animal generates an error message.The program should prompt the user for the appropriate information: a character code for the pet type, and a yes/no response for the neutered status. Use a code letter to determine the kind of animal (i.e. D or d represents a dog, C or c represents a cat, B or b represents a bird, R or r represents a reptile, and anything else represents some other kind of animal). Use a code letter to determine the neutered/spayedstatus(i.e. Y or y represents yes, N or n represents no). The user should be allowed to enter the input in either upper or lower case.It prints out the type of animal (full name of animal) and the insurance fee. Any error in input data should generate an error message “Invalid data –no fee calculated”.
Code:
=====
import java.util.Scanner;
public class PetFee {
static Scanner sc;
static String pet_code, neut_stat;
static int cost = 0;
public static void main(String[] args) {
sc = new Scanner(System.in);
System.out.print("Enter pet code: ");
pet_code = sc.nextLine();
System.out.println();
if(pet_code.equalsIgnoreCase("B") || pet_code.equalsIgnoreCase("R")){
System.out.println("Bird/Reptile doesn't cost anything");
System.exit(0);
}
System.out.print("Enter neutered/spayed status: ");
neut_stat = sc.nextLine();
System.out.println();
if(pet_code.equalsIgnoreCase("D") && neut_stat.equalsIgnoreCase("Y")){
System.out.println("Neutered dog costs: "+60);
}
else if(pet_code.equalsIgnoreCase("D") && neut_stat.equalsIgnoreCase("N")){
System.out.println("Un-neutered dog costs: "+80);
}
else if(pet_code.equalsIgnoreCase("C") && neut_stat.equalsIgnoreCase("Y")){
System.out.println("Neutered cat costs: "+40);
}
else if(pet_code.equalsIgnoreCase("C") && neut_stat.equalsIgnoreCase("N")){
System.out.println("Un-neutered cat costs: "+60);
}
else{
System.out.println("Invalid data – no fee calculated");
}
}
}
Output screen:
============