Hi,
I was having problems with the pet store java code.
The class pet has private attributes, but i cannot use the extender class.
Can help me?
import java.util.*;
public class Pet
{
// Private fields
private String species; // Species name
private int ageInWeeks; // Pet's age in weeks
// Constructor method
public Pet(String species, int ageInWeeks)
{
this.species = species;
this.ageInWeeks = ageInWeeks;
}
// Accessor (getter) methods
public String getSpecies()
{
return species;
}
public int getAgeInWeeks()
{
return ageInWeeks;
}
// Mutator (setter) methods
public void setSpecies (String species)
{
this.species = species;
}
public void setAgeInWeeks (int ageInWeeks)
{
this.ageInWeeks = ageInWeeks;
}
}
class Chihuahua extends Pet
{
Chihuahua()
{
species = "Chihuahua";
ageInWeeks = 4;
}
}
class Poodle extends Pet
{
Poodle()
{
species = "Poodle";
ageInWeeks = 5;
}
}
class Dashshund extends Pet
{
Dashshund()
{
species = "Dashshund";
ageInWeeks = 3;
}
}
public class PetStore
{
public static void main(String[] args)
{
Scanner scan = new Scanner (System.in);
int numSelect;
Chihuahua chihuahua = new Chihuahua();
Poodle poodle = new Poodle();
Dashshund dashshund = new Dashshund();
do
{
do
{
System.out.println("\nPick your pet:"
+ "\n1. Chihuahua"
+ "\n2. Poodle"
+ "\n3. Dashshund"
+ "\n4. Exit");
System.out.print("Select (1 - 4): ");
numSelect = scan.nextInt();
} while (numSelect < 1 || numSelect > 4);
switch (numSelect)
{
case 1:
pricing (numSelect, chihuahua, poodle, dashshund);
break;
case 2:
pricing (numSelect, chihuahua, poodle, dashshund);
break;
case 3:
pricing (numSelect, chihuahua, poodle, dashshund);
break;
}
} while (numSelect != 4);
}
public static void pricing (int numSelect, Chihuahua chihuahua, Poodle poodle, Dashshund dashshund)
{
String speciesName;
int SpeciesAgeInWeeks;
double price;
if (numSelect == 1) // Chihuahua
{
speciesName = chihuahua.getSpecies();
SpeciesAgeInWeeks = chihuahua.getAgeInWeeks();
price = 250 * SpeciesAgeInWeeks;
System.out.println("Species name: " + speciesName);
System.out.println("Age in weeks: " + SpeciesAgeInWeeks);
System.out.println("Price per every week old: 250");
System.out.println("Pet price: " + price);
}
else if (numSelect == 2) // Poodle
{
speciesName = poodle.getSpecies();
SpeciesAgeInWeeks = poodle.getAgeInWeeks();
price = 300 * SpeciesAgeInWeeks;
System.out.println("Species name: " + speciesName);
System.out.println("Age in weeks: " + SpeciesAgeInWeeks);
System.out.println("Price per every week old: 300");
System.out.println("Pet price: " + price);
}
else if (numSelect == 3) // Dashshund
{
speciesName = dashshund.getSpecies();
SpeciesAgeInWeeks = dashshund.getAgeInWeeks();
price = 200 * SpeciesAgeInWeeks;
System.out.println("Species name: " + speciesName);
System.out.println("Age in weeks: " + SpeciesAgeInWeeks);
System.out.println("Price per every week old: 200");
System.out.println("Pet price: " + price);
}
}
}