欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

宠物管理系统

程序员文章站 2022-03-27 09:22:57
宠物管理系统简易宠物管理系统宠物类(父类)package homeworke2.pet;public class Animal { private String name; public Animal(String name) { this.name = name; } public Animal() { } public String getName() { return name; }...

宠物管理系统

简易宠物管理系统

  • 宠物类(父类)
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 <Element>list = element.elements();
        String names="";
        for (int i = 0; i < list.size(); i++) {

                names+=list.get(i).getText();
                if(i<list.size()-1)
                    names+=",";
        }
        return names;
    }

    //捐献的宠物不在animals中
    private void addName2(String name,String name1) 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();
        root.addElement(name).addElement("name").addText(name1);

        XMLWriter xmlWriter = new XMLWriter(new FileOutputStream("D:\\IdeaWork\\day22.java22\\src\\homeworke2\\pet\\pet.xml"), OutputFormat.createPrettyPrint());
        xmlWriter.write(document);
        xmlWriter.flush();
        XMLWriter xmlWriter2 = new XMLWriter(System.out, OutputFormat.createPrettyPrint());
        xmlWriter2.write(document);
        xmlWriter2.flush();
    }
    //捐献的宠物在animals中
    private void addName1(String name,String name1) 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(name);
        element.addElement("name").addText(name1);

        XMLWriter xmlWriter = new XMLWriter(new FileOutputStream("D:\\IdeaWork\\day22.java22\\src\\homeworke2\\pet\\pet.xml"), OutputFormat.createPrettyPrint());
        xmlWriter.write(document);
        xmlWriter.flush();
        XMLWriter xmlWriter2 = new XMLWriter(System.out, OutputFormat.createPrettyPrint());
        xmlWriter2.write(document);
        xmlWriter2.flush();

    }

    //领养宠物
    private void removeName(String name,String name1) 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(name);
        List <Element>list = element.elements();

        for (int i = 0; i <list.size() ; i++) {
            if(list.get(i).getText().contains(name1))
                element.remove(list.get(i));
        }

        if(element.elements().size()==0)
            root.remove(element);



        XMLWriter xmlWriter = new XMLWriter(new FileOutputStream("D:\\IdeaWork\\day22.java22\\src\\homeworke2\\pet\\pet.xml"), OutputFormat.createPrettyPrint());
        xmlWriter.write(document);
        xmlWriter.flush();
        XMLWriter xmlWriter2 = new XMLWriter(System.out, OutputFormat.createPrettyPrint());
        xmlWriter2.write(document);
        xmlWriter2.flush();
    }
//获取宠物信息
    private String getAnnimals() 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();
        List<Element> animals = root.elements();
        String names="";
        for (int i = 0; i < animals.size(); i++) {
            names+=animals.get(i).getName();
            if(i<animals.size()-1)
                names+=",";
        }
        return names;
    }

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

本文地址:https://blog.csdn.net/weixin_53103836/article/details/111997666