宠物管理系统

宠物管理系统

简易宠物管理系统

  • 宠物类(父类)
package homeworke2.pet;

public class Animal {
    private String name;

    public Animal(String name) {
        this.name = name;
    }

    public Animal() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

  • 狗狗类(子类)
package homeworke2.pet;

public class Dog extends Animal {

}

  • 添加任意数量的子类同狗狗类
  • 测试类
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.junit.Test;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.List;
import java.util.Scanner;

public class Demo {
    @Test
    public void test2() throws Exception {
        Scanner sc=new Scanner(System.in);
        String annimals = getAnnimals();

        System.out.println("请问您是要领养宠物还是捐献宠物:1,领养  2,捐献");
        int a = sc.nextInt();
        if(a==1){
            System.out.println("请选择您要领养的宠物:"+annimals);
            String name = sc.next();
            System.out.println("请输入您要领养的宠物名字"+getname1(name));
            String name1 = sc.next();

            removeName(name,name1);
        }else {
            System.out.println("请输入您要捐献的宠物"+annimals+"或其他");
            String name = sc.next();
            if(annimals.contains(name)){
                System.out.println("请输入您要捐献的宠物的名字");
                String name1 = sc.next();
                addName1(name,name1);
            }else {
                System.out.println("请输入您要捐献的宠物的名字");
                String name2 = sc.next();
                addName2(name,name2);

            }
        }
}

    private String getname1(String typename) throws Exception {
        SAXReader reader = new SAXReader();
        // 解析xml文档,得到document对象
        Document document = reader.read("D:\\IdeaWork\\day22.java22\\src\\homeworke2\\pet\\pet.xml");
        // 根据document对象获取根节点
        Element root = document.getRootElement();
        Element element = root.element(typename);
        List list = element.elements();
        String names="";
        for (int i = 0; i < list.size(); i++) {

                names+=list.get(i).getText();
                if(ilist = element.elements();

        for (int i = 0; i  animals = root.elements();
        String names="";
        for (int i = 0; i < animals.size(); i++) {
            names+=animals.get(i).getName();
            if(i

该系统可以将路径中的xml文件进行增加删除查找等功能

你可能感兴趣的:(Java,se,基础,xml,java)