Java實(shí)現(xiàn)寵物商店管理
本文實(shí)例為大家分享了Java實(shí)現(xiàn)寵物商店管理的具體代碼,供大家參考,具體內(nèi)容如下
第一種實(shí)現(xiàn)方式:抽象類(lèi)和對(duì)象數(shù)組
public abstract class AbstractPet //定義寵物模板 { private String name; //名稱(chēng) private String color; //顏色 private int age; //年齡 public AbstractPet(){} public AbstractPet(String name, String color, int age){ this.setName(name); this.setColor(color); this.setAge(age); } public String getName(){ return this.name; } public String getColor(){ return this.color; } public int getAge(){ return this.age; } public void setName(String name){ this.name = name; } public void setColor(String color){ this.color = color; } public void setAge(int age){ if (age > 0) { this.age = age; }else{ this.age = 1; } } //定義抽象方法 public abstract void printInfo(); //自我介紹 }
public class Dog extends AbstractPet { public Dog(String name, String color, int age){ super(name, color, age); } //實(shí)現(xiàn)抽象方法 public void printInfo(){ //自我介紹 System.out.println("狗: " + super.getName() + ",年齡 " + super.getAge() + "歲,顏色:" + super.getColor()); } }
public class Cat extends AbstractPet { public Cat(String name, String color, int age){ super(name, color, age); } //實(shí)現(xiàn)抽象方法 public void printInfo(){ //自我介紹 System.out.println("狗: " + super.getName() + ",年齡 " + super.getAge() + "歲,顏色:" + super.getColor()); } }
public class PetShop { private AbstractPet[] pets; private int foot; //定義下標(biāo) public PetShop(int len){ //寵物數(shù)量由用戶(hù)確定 if (len > 0) { this.pets = new AbstractPet[len]; }else{ this.pets = new AbstractPet[1]; } } //添加寵物的方法 public boolean add(AbstractPet pet){ if (this.foot < this.pets.length) { this.pets[foot] = pet; this.foot ++; return true; }else{ return false; } } //定義查詢(xún)寵物的方法[提供按照種類(lèi)查詢(xún)和按照姓名查詢(xún)兩種方法] public AbstractPet[] search(String keyword){ int n = 0; //定義查詢(xún)到的寵物數(shù)量 AbstractPet[] searchPets = null; 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) { n++; } } } searchPets = new AbstractPet[n]; n = 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) { searchPets[n] = this.pets[i]; n ++; } } } return searchPets; } }
public class testPetShop { public static void main(String[] args){ PetShop p = new PetShop(5); p.add(new Dog("狗1", "黑色的", 3)); p.add(new Dog("狗2", "紅色的", 2)); p.add(new Cat("貓1", "褐色的", 3)); p.add(new Cat("貓2", "黃色的", 3)); p.add(new Cat("貓3", "黑色的", 5)); p.add(new Dog("狗3", "棕色的", 4)); print(p.search("黑")); } public static void print(AbstractPet pets[]){ for (int i = 0; i < pets.length; i++) { if (pets[i] != null) { pets[i].printInfo(); } } } }
第二種實(shí)現(xiàn)方式:接口和對(duì)象數(shù)組
interface IPet { String getName(); //取得寵物姓名 String getColor(); //取得寵物顏色 int getAge(); //取得寵物年齡 void show(); //顯示寵物信息 }
public class Dog implements IPet { 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 String getName(){ return this.name; } public String getColor(){ return this.color; } public int getAge(){ return this.age; } public void setName(String name){ this.name = name; } public void setColor(String color){ this.color = color; } public void setAge(int age){ if (age < 0 || age > 50) { this.age = 1; //默認(rèn)值 }else{ this.age = age; } } public void show(){ System.out.println(this.toString()); } public String toString(){ return "狗:" + this.getName() + " " + this.getColor() + " " + this.getAge(); } }
public class Cat implements IPet { 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 String getName(){ return this.name; } public String getColor(){ return this.color; } public int getAge(){ return this.age; } public void setName(String name){ this.name = name; } public void setColor(String color){ this.color = color; } public void setAge(int age){ if (age < 0 || age > 50) { this.age = 1; //默認(rèn)值 }else{ this.age = age; } } public void show(){ System.out.println(this.toString()); } public String toString(){ return "貓:" + this.getName() + " " + this.getColor() + " " + this.getAge(); } }
public class PetShop { private IPet[] pets; private int foot; public PetShop(int len){ //寵物店的寵物數(shù)量由用戶(hù)決定 if (len > 0) { pets = new IPet[len]; }else{ pets = new IPet[1]; //默認(rèn)最小數(shù)量為1 } } public boolean add(IPet pet){ if (this.foot < this.pets.length) { this.pets[this.foot] = pet; this.foot ++; return true; }else{ return false; } } public IPet[] search(String keyword){ //定義一個(gè)新的寵物對(duì)象數(shù)組,用來(lái)存儲(chǔ)符合查詢(xún)條件的寵物 IPet[] resultPet = null; //不確定數(shù)量,要通過(guò)循環(huán)得到 int count = 0; //用來(lái)存儲(chǔ)符合查詢(xún)條件的寵物數(shù)量 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 ++; } } } resultPet = new IPet[count]; int n = 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) { resultPet[n] = this.pets[i]; n ++; } } } return resultPet; } }
public class TestPetShop { public static void main(String[] args){ //創(chuàng)建一個(gè)寵物商店 PetShop ps = new PetShop(7); //假設(shè)可以放置5只寵物 ps.add(new Dog("旺旺", "黑色的",4)); ps.add(new Dog("旺財(cái)", "白色的",6)); ps.add(new Dog("小黑", "黃色的",3)); ps.add(new Cat("波波", "褐色的",7)); ps.add(new Cat("咪咪", "黑色的",8)); ps.add(new Cat("小云", "灰色的",2)); ps.add(new Dog("仔仔", "黃色的",5)); print(ps.search("色")); } public static void print(IPet[] pet){ for (int i = 0; i < pet.length; i++) { if (pet[i] != null) { pet[i].show(); } } } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Java實(shí)現(xiàn)寵物商店管理系統(tǒng)
- Java畢業(yè)設(shè)計(jì)實(shí)戰(zhàn)之醫(yī)院心理咨詢(xún)問(wèn)診系統(tǒng)的實(shí)現(xiàn)
- Java畢業(yè)設(shè)計(jì)實(shí)戰(zhàn)項(xiàng)目之在線(xiàn)服裝銷(xiāo)售商城系統(tǒng)的實(shí)現(xiàn)流程
- Java畢業(yè)設(shè)計(jì)實(shí)戰(zhàn)項(xiàng)目之倉(cāng)庫(kù)管理系統(tǒng)的實(shí)現(xiàn)流程
- Java實(shí)戰(zhàn)花店商城系統(tǒng)的實(shí)現(xiàn)流程
- Java實(shí)戰(zhàn)玩具商城的前臺(tái)與后臺(tái)實(shí)現(xiàn)流程
- Java實(shí)現(xiàn)茶葉售賣(mài)商城系統(tǒng)(java+SSM+JSP+EasyUi+mysql)
- Java 仿天貓服裝商城系統(tǒng)的實(shí)現(xiàn)流程
- Java畢業(yè)設(shè)計(jì)實(shí)戰(zhàn)項(xiàng)目之寵物商城系統(tǒng)的實(shí)現(xiàn)流程
相關(guān)文章
MyBatisPlus 自定義sql語(yǔ)句的實(shí)現(xiàn)
這篇文章主要介紹了MyBatisPlus 自定義sql語(yǔ)句的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08Springboot項(xiàng)目使用Slf4j將日志保存到本地目錄的實(shí)現(xiàn)代碼
這篇文章主要介紹了Springboot項(xiàng)目使用Slf4j將日志保存到本地目錄的實(shí)現(xiàn)方法,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-05-05線(xiàn)程局部變量的實(shí)現(xiàn)?ThreadLocal使用及場(chǎng)景介紹
這篇文章主要為大家介紹了線(xiàn)程局部變量的實(shí)現(xiàn)?ThreadLocal使用及場(chǎng)景詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01java中File類(lèi)的構(gòu)造函數(shù)及其方法
這篇文章主要介紹了java中File類(lèi)的構(gòu)造函數(shù)及其方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-06-06SpringBoot整合jasypt進(jìn)行重要數(shù)據(jù)加密的操作代碼
Jasypt(Java?Simplified?Encryption)是一個(gè)專(zhuān)注于簡(jiǎn)化Java加密操作的開(kāi)源工具,它提供了一種簡(jiǎn)單而強(qiáng)大的方式來(lái)處理數(shù)據(jù)的加密和解密,使開(kāi)發(fā)者能夠輕松地保護(hù)應(yīng)用程序中的敏感信息,本文給大家介紹了SpringBoot整合jasypt進(jìn)行重要數(shù)據(jù)加密,需要的朋友可以參考下2024-05-05Intellij IDEA集成JProfiler性能分析工具
作為Java程序員,性能分析是我們必須掌握的技能之一,在性能分析中,JProfiler是一款非常強(qiáng)大的工具,本文就來(lái)介紹一下Intellij IDEA集成JProfiler性能分析工具,就有一定的參考價(jià)值,感興趣的可以了解一下2023-12-12java分頁(yè)攔截類(lèi)實(shí)現(xiàn)sql自動(dòng)分頁(yè)
這篇文章主要為大家詳細(xì)介紹了java分頁(yè)攔截類(lèi)可以實(shí)現(xiàn)sql自動(dòng)分頁(yè),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11SpringBoot WebSocket實(shí)時(shí)監(jiān)控異常的詳細(xì)流程
最近做了一個(gè)需求,消防的設(shè)備巡檢,如果巡檢發(fā)現(xiàn)異常,通過(guò)手機(jī)端提交,后臺(tái)的實(shí)時(shí)監(jiān)控頁(yè)面實(shí)時(shí)獲取到該設(shè)備的信息及位置,然后安排員工去處理。這篇文章主要介紹了SpringBoot WebSocket實(shí)時(shí)監(jiān)控異常的全過(guò)程,感興趣的朋友一起看看吧2021-10-10