首页 > 代码库 > JAVA课程结课实验代码(河北省重大技术需求征集系统设计)

JAVA课程结课实验代码(河北省重大技术需求征集系统设计)

第一部分:账号密码类

package IOP;
/**
 * 数1401班
 * 20143253
 * 吕鹏博
 * 时间 2016年12月21日17:45:42
 */
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.StringTokenizer;

/*
 * 账号、密码类*/
public class Pass {
    private String accounts, password;// 账号密码

    Pass() {
        this.accounts = "0";
        this.password = "0";
    }

    Pass(String a, String b) {
        this.accounts = a;
        this.password = b;
    }

    public Pass[] readAccount() {
        Pass[] a = new Pass[10];
        try {
            BufferedReader in = new BufferedReader(new FileReader("user.txt"));
            String s1 = in.readLine();
            for (int i = 0; i <= 4; i++) {
                String s = in.readLine();
                StringTokenizer t = new StringTokenizer(s, "|");
                a[i] = new Pass(t.nextToken(), t.nextToken());
            }
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
        } 
            return a;
        }
    /*
     * 验证密码是否正确
     */
    public boolean isTrue(String a, String b) {
        boolean i = false;// 不正确输出0
        Pass[] p = new Pass[5];
        p = this.readAccount();
        for (int j = 0; j <= 4; j++) {
            if (a.equals(p[j].accounts) && b.equals(p[j].password)) {
                i = true;
            } // 登录成功
        }
        return i;
    }

    /*
     * 初始化账户文件
     */
    public void initializeAccount(Pass[] a) throws IOException {
        PrintWriter in = new PrintWriter(new FileWriter("user.txt"), true);
        in.println("账号" + "密码");
        for (int i = 0; i <= a.length - 1; i++) {
            in.println(a[i].accounts + "|" + a[i].password);
        }
        in.close();
    }
}

class LOginException extends Exception {
    LOginException(String s) {
        super(s);
    }
}

第二部分:操作类(包括增删改查等)

package IOP;
/**
 * 数1401班
 * 20143253
 * 吕鹏博
 * 时间 2016年12月21日17:45:42
 */
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
import java.util.StringTokenizer;

class Information

{
    private String infornumber;
    private String inforname;
    private String infocontent;
    private int inforyear;
    private double infosum;
    private int infostate;

    public Information()// 定义默认信息
    {
        infornumber = "000";
        inforname = "noname";
        infocontent = "noone";
        infosum = 0;
        inforyear = 2016;
        infostate = 0;
    }

    public Information(String a, String b, String c, double d, int e)// 实例化方法
    {
        infornumber = a;
        inforname = b;
        infocontent = c;
        infosum = d;
        inforyear = e;
        infostate = 0;
    }

    public void setinfornumber(String x) {

        infornumber = x;

    }

    public String getinfornumber() {
        return infornumber;
    }

    public void setinfornam(String Y) {
        inforname = Y;
    }

    public String getinforname() {
        return inforname;
    }

    public void setinfocontent(String z) {
        infocontent = z;
    }

    public String getinfocontent() {
        return infocontent;
    }

    public void setinfosum(double s) {
        infosum = s;
    }

    public double getinfosum() {
        return infosum;
    }

    public void setinfoyear(int s) {
        inforyear = s;
    }

    public int getinfoyear() {
        return inforyear;
    }
    
    public void setinfostate(int s) {
        infostate = s;
    }

    public double getinfostate() {
        return infostate;
    }

    public String tostring() {

        return "编号" + this.infornumber + "名称" + this.inforname + "内容" + this.infocontent + "投资金额" + this.infosum
                + "需求年份" + this.inforyear + "需求状态" + this.infostate;

    }

    public void readData(BufferedReader in) throws IOException {// 把一条记录从输出流中读出
        String s = in.readLine();
        StringTokenizer t = new StringTokenizer(s, "|");
        infornumber = t.nextToken();
        inforname = t.nextToken();
        infocontent = t.nextToken();
        infosum = Double.parseDouble(t.nextToken());
        inforyear = Integer.parseInt(t.nextToken());
        infostate = Integer.parseInt(t.nextToken());
    }

    public void writeData(PrintWriter out) throws IOException {// 把一条记录写入输入流中
        out.println(
                infornumber + "|" + inforname + "|" + infocontent + "|" + infosum + "|" + inforyear + "|" + infostate);
    }
}

@SuppressWarnings("serial")
class InputException extends Exception {
    InputException(String s){
    super(s);}
}
public class InformationManage 
{
    static void writeall(Information[] tm) {// 把数组从文件中读出,以后只对数组进行操作
        try {
            PrintWriter out = new PrintWriter(new FileWriter("TechInformation.txt"));
            writeData(tm, out);
            out.close();
        } catch (IOException e) {
            System.out.print(e);
        }
    }

    static void writeData(Information[] tm2, PrintWriter out) throws IOException {// 把数组存入写入流
        out.println(tm2.length);
        for (Information e : tm2) {
            e.writeData(out);
        }
    }

    static Information[] readData(BufferedReader in) throws IOException {// 从输出流中读取数组
        int n = Integer.parseInt(in.readLine());
        Information[] employees = new Information[n];
        for (int i = 0; i < n; i++) {
            employees[i] = new Information();
            employees[i].readData(in);
        }
        return employees;

    }

    @SuppressWarnings("finally")
    public static Information[] getArray() throws FileNotFoundException {// 从文件中直接取出数组,然后被每个函数调用,进行操作
        BufferedReader in = null;
        Information[] newstaff = null;
        try {
            in = new BufferedReader(new FileReader("TechInformation.txt"));
            newstaff = readData(in);
            in.close();
        } catch (IOException e) {
            System.out.println(e);
        } finally {

            return newstaff;
        }
    }

    public static void showInformation() {//显示所有的信息
        BufferedReader in;
        try {
            in = new BufferedReader(new FileReader("TechInformation.txt"));
            Information[] newstaff = null;
            try {
                newstaff = readData(in);
                in.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            for (int i = 0; i < newstaff.length; i++)
                if (!newstaff[i].getinfornumber().equals("000"))
                    System.out.println(newstaff[i].tostring());
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    static String addop(int i) {//累加编号,为增添信息的方法做铺垫
        String io = "";
        if (i / 10 == 0) {
            io = "00" + String.valueOf(i);
        } else if (i / 100 == 0 && i % 10 != 0) {
            io = "0" + String.valueOf(i);
        } else if (i / 1000 == 0 && i % 100 != 0) {
            io = String.valueOf(i);
        }
        return io;
    }
    
    @SuppressWarnings("resource")
    public static void addInformation() {//增加信息
        String str1 = null;
        String str2 = null;
        String str3 = null;
        String str4 = null;
        Information stu;
        double i1 = 0;
        int count1 = 0;
        boolean se = true;
        int i2 = 0;
        Scanner input0;
        System.out.println("请输入项目名称:(如:雄鹰计划)\n");
        input0 = new Scanner(System.in);
        str1 = input0.nextLine();
        System.out.println("请输入项目内容:(如:xiaoming)\n");
        input0 = new Scanner(System.in);
        str2 = input0.nextLine();
        System.out.println("请输入项目金额:(如:200.0)\n");
        input0 = new Scanner(System.in);
        str3 = input0.nextLine();
        i1 = Double.parseDouble(str3);
        System.out.println("请输入项目年限:(如:2016)\n");
        while (se) {
            Scanner input3 = new Scanner(System.in);
            str4 = input3.nextLine();
            i2 = Integer.parseInt(str4);
            if (i2 > 2016) {
                System.out.println("请重新输入项目年限:(如:2016)\n");
                se = true;
                try {
                    throw new InputException("超过实际年限\n");
                } catch (InputException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            } else {
                se = false;
            }
            System.out.println("请输入项目年限:(如:2016)\n");
        }
        Information[] newstaff1 = null;
        try {
            newstaff1 = getArray();
            for (int i = 0; i < newstaff1.length; i++) {
                if (newstaff1[i].getinforname().equals(str1)) {
                    System.out.println("该项目已存在,请重新添加。");
                    break;
                } else if (newstaff1[i].getinforname().equals("noname")) {
                    count1 = i;
                    stu = new Information(addop(i), str1, str2, i1, i2);
                    newstaff1[count1] = stu;
                    System.out.println("存储成功。");
                    break;
                }
            }
        } catch (FileNotFoundException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        writeall(newstaff1);
    }

    public static void deleteInformation() {//删除的方法(可通过项目编号和项目名称分别选择)
        String key = null;
        System.out.println("[1]=项目编号\n[2]=项目名称");
        @SuppressWarnings("resource")
        Scanner br = new Scanner(System.in);
        key = br.nextLine();
        int s = Integer.parseInt(key);
        if (s == 1) {
            br = new Scanner(System.in);
            System.out.print("请输入项目编号:");
            key = br.nextLine();
        } else {
            br = new Scanner(System.in);
            System.out.print("请输入项目名称:");
            key = br.nextLine();
        }
        int count = 0;
        Information[] newstaff = null;
        try {
            newstaff = getArray();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        for (int i = 0; i < newstaff.length; i++) {
            if (newstaff[i].getinfornumber().equals(key) || newstaff[i].getinforname().equals(key)) {
                newstaff[i] = new Information();
                System.out.println("删除成功。");
                break;
            }
            count++;
        }
        if (count == newstaff.length) {
            System.out.println("您所需要删除的信息不存在!");
        }
        writeall(newstaff);
    }

    @SuppressWarnings("resource")
    public static void examInformation()// 审核的方法
    {
        String key = null;
        Information[] newstaff = null;
        try {
            newstaff = getArray();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        System.out.println("目前所拥有的未审核项目的编号和名称如下所示:");
        for(int i=0;i<newstaff.length;i++){
            if(newstaff[i].getinfostate()==0&&(!newstaff[i].getinfornumber().equals("000"))&&(!newstaff[i].getinforname().equals("noname"))){
        System.out.println("项目编号:"+newstaff[i].getinfornumber()+"   项目名称:"+newstaff[i].getinforname());}}
        System.out.println("\n请输入你的选择(如(1或2))\n[1]=项目编号\n[2]=项目名称\n");
        Scanner br = new Scanner(System.in);
        key = br.nextLine();
        int s = Integer.parseInt(key);
        Scanner br1;
        if (s == 1) {
            br1 = new Scanner(System.in);
            System.out.print("请输入项目编号:\n");
            key = br1.nextLine();
        } else {
            br1 = new Scanner(System.in);
            System.out.print("请输入项目名称:\n");
            key = br1.nextLine();
        }
        int count = 0;
        int special = 0;
        for (int i = 0; i < newstaff.length; i++) {
            if (newstaff[i].getinfornumber().equals(key) || newstaff[i].getinforname().equals(key)) {
                special = i;
                break;
            }
            count++;
        }
        if (count == newstaff.length) {
            System.out.println("您所要审阅的信息不存在!");
        }
        if (newstaff[special].getinfostate() == 0) {
            String mes = "****************\n" + "审核技术需求信息" + "\n****************\n" + "1、返回主界面" + "\n2、通过审核"
                    + "\n3、退回审核\n";
            System.out.print(mes);
            Scanner sq = new Scanner(System.in);
            int cho = sq.nextInt();
            switch (cho) {
            case 1:
                break;
            case 2:
                newstaff[special].setinfostate(1);
                break;
            case 3:
                newstaff[special].setinfostate(2);
                break;
            }
        } else if (newstaff[special].getinfostate() == 1) {
            System.out.println("已通过审核");
        } else if (newstaff[special].getinfostate() == 2) {
            System.out.println("该信息已被退回");
        }
        System.out.println("审核完毕,返回上一层界面。");
        writeall(newstaff);
    }

    @SuppressWarnings("resource")
    public void selectInformation() {//查询方法:(只能用项目名称查询)
        Information[] a = null;
        try {
            a = getArray();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.print("请输入要查询的项目名称\n");
        Scanner s = new Scanner(System.in);
        String name = s.nextLine();
        int uo = 0;
        for (int value = http://www.mamicode.com/0; value < a.length; value++) {
            if (a[value].getinforname().equals(name)) {
                uo = value;
            }

        }
        System.out.print(a[uo].tostring() + "\n");
    }

    public void countStudent() {//统计方法
        Information[] a = null;
        try {
            a = getArray();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        int o = 0, b = 0, c = 0;
        for (int i = 0; i < a.length; i++) {
            if (!a[i].getinforname().equals("noname")) {
                if (a[i].getinfostate() == 0) {
                    o++;
                } else if (a[i].getinfostate() == 1) {
                    b++;
                } else if (a[i].getinfostate() == 2) {
                    c++;
                }
            }
        }
        System.out.print("未审核的需求信息" + o + "\n通过审核的需求信息" + b + "\n未通过审核的需求信息" + c + "\n");

    }

}

第三部分:主类(在主函数中实现,包括界面等)

package IOP;

/**
 * 数1401班
 * 20143253
 * 吕鹏博
 * 时间 2016年12月21日17:45:42
 */
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;

public class WINDOWSHOW {

    public static void main(String[] args) throws FileNotFoundException {

        Pass[] a = new Pass[5];
        a[0] = new Pass("0", "0");
        a[1] = new Pass("1", "1");
        a[2] = new Pass("2", "2");
        a[3] = new Pass("3", "3");
        a[4] = new Pass("4", "4");
        try {
            a[0].initializeAccount(a);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Information[] a1 = new Information[20];
        a1[0] = new Information("001", "a", "1231", 18, 2016);
        a1[1] = new Information("002", "b", "1231", 18, 2016);
        a1[2] = new Information("003", "c", "1231", 18, 2016);
        a1[3] = new Information("004", "d", "1231", 18, 2016);
        a1[4] = new Information("005", "e", "1231", 18, 2016);
        for (int i = 5; i < a1.length; i++) {
            a1[i] = new Information();
        }
        InformationManage sq = new InformationManage();
        sq.writeall(a1);
        try {
            if (LOgin(a[0])) {
                operation(sq);
            }
        } catch (LOginException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.exit(0);
    }

    static void operation(InformationManage sq) {// 把操作过程集成在此方法中.
        show();

        @SuppressWarnings("resource")
        Scanner in = new Scanner(System.in);
        int choice = in.nextInt();

        while (choice != 7) {
            switch (choice) {
            case 1:
                sq.addInformation();
                break;
            case 2:
                sq.deleteInformation();
                break;
            case 3:
                sq.examInformation();
                break;
            case 4:
                sq.showInformation();
                break;
            case 5:
                sq.selectInformation();
                ;
                break;
            case 6:
                sq.countStudent();
                break;
            }
            show();
            in = new Scanner(System.in);
            choice = in.nextInt();
        }
        if (choice == 7) {
            System.out.println("结束");
        }
    }

    /*
     * 登录界面,输入账号密码。传入一个Pass实例化的类,用来调用类中的isTrue方法 返回一个整型参数i,1表示高级权限,2表示低级权限
     */
    static boolean LOgin(Pass a) throws LOginException {// 登录系统时的方法
        int j = 1;
        boolean flag = false;
        while (j <= 3) {
            System.out.println("***********************************************");
            Scanner in = new Scanner(System.in);
            System.out.println("请输入账号");
            String account = in.nextLine();
            System.out.println("请输入密码");
            String password = in.nextLine();
            if (!a.isTrue(account, password)) {
                System.out.println("密码错误,请重新输入!您还有" + (3 - j) + "次机会!");
            } else {
                flag = true;
                break;
            }
            j++;
        }
        if (j >= 3)
            throw new LOginException("输入密码错误三次!机器锁定!");
        return flag;
    }

    public static void show() {// 界面展示,每次只用调用即可,不用每次都写
        String s = "";

        s = "*****************************************\n";
        s = s + "          河北省重大技术征集系统    \n";
        s = s + "****************************************\n";
        s = s + "    1、录入技术需求信息\n";
        s = s + "    2、删除技术需求信息\n";
        s = s + "    3、审核技术需求信息\n";
        s = s + "    4、显示技术需求信息\n";
        s = s + "    5、查询技术需求信息\n";
        s = s + "    6、统计技术需求信息\n";
        s = s + "    7、退出系统\n";
        System.out.print(s);
    }

}

 

JAVA课程结课实验代码(河北省重大技术需求征集系统设计)