import java.util.Scanner;
class Pet{
private int number;
private String variety;
private String color;
private int age;
private int price;
public Pet(int number,String variety,String color,int age,int price){
this.setNumber(number);
this.setVariety(variety);
this.setColor(color);
this.setAge(age);
this.setPrice(price);
}
public int getNumber(){
return number;
}
public void setNumber(int number){
this.number=number;
}
public String getVariety(){
return variety;
}
public void setVariety(String variety){
this.variety=variety;
}
public String getColor(){
return color;
}
public void setColor(String color){
this.color=color;
}
public int getAge(){
return age;
}
public void setAge(int age){
this.age=age;
}
public int getPrice(){
return price;
}
public void setPrice(int price){
this.price=price;
}
public String toString() {
return this.getNumber()+"\t"+this.getVariety()+"\t"+this.getColor()+
"\t"+this.getAge()+"岁\t"+this.getPrice()+"元";
}
}
class Dog extends Pet{
private String SmallOrLarge;
private int CountDog;
private int DogPrice;
public Dog(int number,String variety,String color,int age,int price,String sol){
super(number,variety,color,age,price);
this.setSmallOrLarge(sol);
}
public void setSmallOrLarge(String sol){
this.SmallOrLarge=sol;
}
public String getSmallOrLarge(){
return SmallOrLarge;
}
public void setCountDog(int cd){
this.CountDog=cd;
}
public int getCountDog(){
return CountDog;
}
public void setDogPrice(int dp){
this.DogPrice=dp;
}
public int getDogPrice(){
return DogPrice;
}
public String toString1() {
return "狗:\t"+super.getNumber()+"\t"+super.getVariety()+"\t"+super.getColor()+
"\t"+super.getAge()+"岁\t"+super.getPrice()+"\t\t"+this.getSmallOrLarge();
}
public String toString() {
return "狗:\t"+super.getNumber()+"\t"+super.getVariety()+"\t"+super.getColor()+
"\t"+super.getAge()+"岁\t"+super.getPrice()+"\t\t"+this.getSmallOrLarge()+
"\t共"+this.getCountDog()+"只\t"+this.getDogPrice();
}
}
class Cat extends Pet{
private int CountCat;
private int CatPrice;
public Cat(int number,String variety,String color,int age,int price){
super(number,variety,color,age,price);
}
public void setCountCat(int ct){
this.CountCat=ct;
}
public int getCountCat(){
return CountCat;
}
public void setCatPrice(int cp){
this.CatPrice=cp;
}
public int getCatPrice(){
return CatPrice;
}
public String toString1() {
return "猫:\t"+super.getNumber()+"\t"+super.getVariety()+"\t"+super.getColor()+
"\t"+super.getAge()+"岁\t"+super.getPrice()+"元";
}
public String toString() {
return "猫:\t"+super.getNumber()+"\t"+super.getVariety()+"\t"+super.getColor()+
"\t"+super.getAge()+"岁\t"+super.getPrice()+"\t\t\t共"+this.getCountCat()+
"只\t"+this.getCatPrice();
}
}
class Buy {
private Pet buy[];
public Buy(){};
public Pet[] getPet() {
return buy;
}
public void setPet(Pet[] buy) {
this.buy = buy;
}
}
public class InterfaceForUsers {
public static void main(String args[]){
int j=0;
Pet[] temp=new Pet[10];
Buy buy=new Buy();
Scanner input=new Scanner(System.in);
int choose1,choose2,choose3;
String choose4="n";
int price=0;
Dog dogs[]={
new Dog(1,"哈士奇","黑白色",5,200,"大型犬"),
new Dog(2,"藏獒","棕色",6,500,"大型犬"),
new Dog(3,"吉娃娃","黑色",3,100,"小型犬")
};
Cat cats[]={
new Cat(1,"黑猫","黑色",5,200),
new Cat(2,"白猫","白色",6,300),
new Cat(3,"大脸猫","白色",6,300),
new Cat(4,"机器猫","蓝色",20,1000)
};
int [] countdog=new int[dogs.length];
int [] countcat=new int[cats.length];
System.out.println("*****欢迎使用宠物商店*****");
System.out.println("本店设有狗类和猫类供您选择:\n");
do{
if(j==9){
System.out.println("您一次只可以购买10只宠物哦!");
break;
}
System.out.println("狗类:\n");
System.out.println("宠物\t编号\t类型\t颜色\t年龄\t价格(单位:元)\t备注");
for(int i=0;i
System.out.println(dogs[i].toString1());
}
System.out.println("\n猫类:\n");
System.out.println("宠物\t编号\t类型\t颜色\t年龄\t价格(单位:元)\t备注");
for(int i=0;i
System.out.println(cats[i].toString1());
}
System.out.println("请选择购买:1.狗类 2.猫类 \n请输入编号");
choose1=input.nextInt();
switch(choose1){
case 1:
System.out.println("请输入宠物狗编号:");
choose2=input.nextInt();
countdog[choose2-1]++;
break;
case 2:
System.out.println("请输入宠物猫编号:");
choose2=input.nextInt();
countcat[choose2-1]++;
break;
default:
System.out.println("您侧输入有误,请重新输入:");
break;
}
System.out.println("下一步:1.继续购买 2.结束购买");
choose3=input.nextInt();
if(choose3==1){
choose4="n";
}else if(choose3==2){
System.out.println("是否退出?(y/n)");
choose4=input.next();
}
}while(choose4.equals("n")==true || choose4.equals("N")==true);
for(int i=0;i
if(countdog[i]==0){
continue;
}else{
temp[j]=dogs[i];
j++;
dogs[i].setCountDog(countdog[i]);
dogs[i].setDogPrice(countdog[i]*dogs[i].getPrice());
price=price+dogs[i].getDogPrice();
}
}
for(int i=0;i
if(countcat[i]==0){
continue;
}else{
temp[j]=cats[i];
j++;
cats[i].setCountCat(countcat[i]);
cats[i].setCatPrice(countcat[i]*cats[i].getPrice());
price=price+cats[i].getCatPrice();
}
}
buy.setPet(temp);
System.out.println("您所选择的宠物有:");
System.out.println("宠物\t编号\t类型\t颜色\t年龄\t价格(单位:元)\t备注\t数量\t共计(单位:元)");
for(int i=0;i
System.out.println(buy.getPet()[i].toString());
}
System.out.println("共计:"+price+"元");
}
}
一键复制
编辑
Web IDE
原始数据
按行查看
历史