java代码 宠 物 商 店 分享。 急需宠物商店代码或者说是类图,不知道怎么下手。太多的类。还有数据库建设纠结中、希望有谁做过的可以打包发送我参考一下。以前控制台小游戏的时候都没有这么多类这个类也有点多了。qq675927961,希望联系我理解了加分先谢谢了大侠们。纯控制台输入非jsp 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 interface Pet{ // 定义宠物接口 public String getName() ; public String getColor() ; public int getAge() ;}class Cat implements Pet{ // 猫是宠物,实现接口 private String name ; // 宠物名字 private String color ; // 宠物颜色 private int age ; // 宠物年龄 public Cat(String name,String color,int age){ this.setName(name) ; this.setColor(color) ; this.setAge(age) ; } public void setName(String name){ this.name = name ; } public void setColor(String color){ this.color = color; } public void setAge(int age){ this.age = age ; } public String getName(){ return this.name ; } public String getColor(){ return this.color ; } public int getAge(){ return this.age ; }};class Dog implements Pet{ // 狗是宠物,实现接口 private String name ; // 宠物名字 private String color ; // 宠物颜色 private int age ; // 宠物年龄 public Dog(String name,String color,int age){ this.setName(name) ; this.setColor(color) ; this.setAge(age) ; } public void setName(String name){ this.name = name ; } public void setColor(String color){ this.color = color; } public void setAge(int age){ this.age = age ; } public String getName(){ return this.name ; } public String getColor(){ return this.color ; } public int getAge(){ return this.age ; }};class PetShop{ // 宠物商店 private Pet[] pets ; // 保存一组宠物 private int foot ; public PetShop(int len){ if(len>0){ this.pets = new Pet[len] ; // 开辟数组大小 }else{ this.pets = new Pet[1] ; // 至少开辟一个空间 } } public boolean add(Pet pet){ // 增加的是一个宠物 if(this.foot<this.pets.length){ this.pets[this.foot] = pet ; // 增加宠物 this.foot ++ ; return true ; }else{ return false ; } } public Pet[] search(String keyWord){ // 应该确定有多少个宠物符合要求 Pet p[] = null ; int count = 0 ; // 记录下会有多少个宠物符合查询结果 for(int i=0;i<this.pets.length;i++){ if(this.pets[i]!=null){ // 表示此位置有宠物 if(this.pets[i].getName().indexOf(keyWord)!=-1 ||this.pets[i].getColor().indexOf(keyWord)!=-1){ count++ ; // 修改查找到的记录数 } } } p = new Pet[count] ; // 开辟指定的大小空间 int f = 0 ; // 增加元素的位置标记 for(int i=0;i<this.pets.length;i++){ if(this.pets[i]!=null){ // 表示此位置有宠物 if(this.pets[i].getName().indexOf(keyWord)!=-1 ||this.pets[i].getColor().indexOf(keyWord)!=-1){ p[f] = this.pets[i] ; f++ ; } } } return p ; }};public class PetShopDemo{ public static void main(String args[]){ PetShop ps = new PetShop(5) ; // 五个宠物 ps.add(new Cat("白猫","白色的",2)) ; // 增加宠物,成功 ps.add(new Cat("黑猫","黑色的",3)) ; // 增加宠物,成功 ps.add(new Cat("花猫","花色的",3)) ; // 增加宠物,成功 ps.add(new Dog("拉步拉多","黄色的",3)) ; // 增加宠物,成功 ps.add(new Dog("金毛","金色的",2)) ; // 增加宠物,成功 ps.add(new Dog("黄狗","黑色的",2)) ; // 增加宠物,失败 print(ps.search("黑")) ; } public static void print(Pet p[]){ for(int i=0;i<p.length;i++){ if(p[i]!=null){ System.out.println(p[i].getName() + "," + p[i].getColor() +"," + p[i].getAge()) ; } } }}; petstore 那么著名的开源项目 你不会不知道吧。 数据库出错 请问这个程序哪里出错了?java写文件和读文件 监听器的问题 关于用java命令加载类文件的探索,望高手指正。 Vector中有方法可以实现的功能为什么还要用Iterator? 有创业想法的朋友来这里QQ群:10496416(创业中国) 问个基础问题:ARRAYLIST.REMOVE(I) 如何将一串字符旋转,即逆时针旋转90度???急急急 超级弱智问题大比拼!!!!!!!!(强烈郁闷中************) 先谢了!恳请高人指点:如何迅速学习java,推荐好书或Web. 参数的传递 關於全域變數怎麼使用呀
interface Pet{ // 定义宠物接口
public String getName() ;
public String getColor() ;
public int getAge() ;
}
class Cat implements Pet{ // 猫是宠物,实现接口
private String name ; // 宠物名字
private String color ; // 宠物颜色
private int age ; // 宠物年龄
public Cat(String name,String color,int age){
this.setName(name) ;
this.setColor(color) ;
this.setAge(age) ;
}
public void setName(String name){
this.name = name ;
}
public void setColor(String color){
this.color = color;
}
public void setAge(int age){
this.age = age ;
}
public String getName(){
return this.name ;
}
public String getColor(){
return this.color ;
}
public int getAge(){
return this.age ;
}
};
class Dog implements Pet{ // 狗是宠物,实现接口
private String name ; // 宠物名字
private String color ; // 宠物颜色
private int age ; // 宠物年龄
public Dog(String name,String color,int age){
this.setName(name) ;
this.setColor(color) ;
this.setAge(age) ;
}
public void setName(String name){
this.name = name ;
}
public void setColor(String color){
this.color = color;
}
public void setAge(int age){
this.age = age ;
}
public String getName(){
return this.name ;
}
public String getColor(){
return this.color ;
}
public int getAge(){
return this.age ;
}
};
class PetShop{ // 宠物商店
private Pet[] pets ; // 保存一组宠物
private int foot ;
public PetShop(int len){
if(len>0){
this.pets = new Pet[len] ; // 开辟数组大小
}else{
this.pets = new Pet[1] ; // 至少开辟一个空间
}
}
public boolean add(Pet pet){ // 增加的是一个宠物
if(this.foot<this.pets.length){
this.pets[this.foot] = pet ; // 增加宠物
this.foot ++ ;
return true ;
}else{
return false ;
}
}
public Pet[] search(String keyWord){
// 应该确定有多少个宠物符合要求
Pet p[] = null ;
int count = 0 ; // 记录下会有多少个宠物符合查询结果
for(int i=0;i<this.pets.length;i++){
if(this.pets[i]!=null){ // 表示此位置有宠物
if(this.pets[i].getName().indexOf(keyWord)!=-1
||this.pets[i].getColor().indexOf(keyWord)!=-1){
count++ ; // 修改查找到的记录数
}
}
}
p = new Pet[count] ; // 开辟指定的大小空间
int f = 0 ; // 增加元素的位置标记
for(int i=0;i<this.pets.length;i++){
if(this.pets[i]!=null){ // 表示此位置有宠物
if(this.pets[i].getName().indexOf(keyWord)!=-1
||this.pets[i].getColor().indexOf(keyWord)!=-1){
p[f] = this.pets[i] ;
f++ ;
}
}
}
return p ; }
};
public class PetShopDemo{
public static void main(String args[]){
PetShop ps = new PetShop(5) ; // 五个宠物
ps.add(new Cat("白猫","白色的",2)) ; // 增加宠物,成功
ps.add(new Cat("黑猫","黑色的",3)) ; // 增加宠物,成功
ps.add(new Cat("花猫","花色的",3)) ; // 增加宠物,成功
ps.add(new Dog("拉步拉多","黄色的",3)) ; // 增加宠物,成功
ps.add(new Dog("金毛","金色的",2)) ; // 增加宠物,成功
ps.add(new Dog("黄狗","黑色的",2)) ; // 增加宠物,失败
print(ps.search("黑")) ;
}
public static void print(Pet p[]){
for(int i=0;i<p.length;i++){
if(p[i]!=null){
System.out.println(p[i].getName() + "," + p[i].getColor()
+"," + p[i].getAge()) ;
}
}
}
};