软工导论测试代码(归档):设备管理系统

设备管理系统

部分功能实现代码,用来完成导论测试

#6.9更新#实现list除重

package com;
import jdk.swing.interop.LightweightContentWrapper;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.sql.SQLOutput;

public class experiment09 {

    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);
        String fileName = "D:/exampleSos/information.txt";
        File file = new File(fileName);
        List<String> list = new ArrayList<>();
        loadData(file);
        loadList(fileName,list);
        while(true){
            System.out.println("------欢迎进入设备管理系统------");
            System.out.println(" 1 新建设备");
            System.out.println(" 2 删除设备");
            System.out.println(" 3 查看所有设备");
            System.out.println(" 4 设备蜂鸣与定位");
            System.out.println(" 5 退出系统");
            System.out.println("-------请选择服务,输入序号--------");

            String choice = sc.next();
            switch (choice){
                case "1":
                    add(list);
                    storeData(list,fileName);
                    break;
                case "2" :
                    delete(list);
                    storeData(list,fileName);
                    break;
                case "3":
                    all(fileName,list);
                    break;
                case "4":
                    ring(list);
                    break;
                case "5":
                    System.out.println("感谢使用(●'◡'●)");
                    System.exit(0);
                    break;
                default:
                    System.out.println("输入错误,请重试!");
                    break;
            }
        }

    }

    //    初始化
    private static void loadList(String fileName,List<String>l2) throws IOException{
        BufferedReader br = new BufferedReader(new FileReader(fileName));
        String line;
//        List <String> l2 = new ArrayList<>();
        while((line = br.readLine())!=null){
            //System.out.println(line);
            l2.add(line);
        }
        br.close();
    }



    //      初始化
    private static void loadData(File file){

        try {
            if (file.createNewFile()){
                System.out.println("File is created!");
            }else{
                System.out.println("File already exists.");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("初始化完成");
    }
    //    更新
    private static void storeData(List<String> list,String fileName) throws IOException {
        BufferedWriter writer = new BufferedWriter(new FileWriter(fileName));
        try{
            for(String l : list){
                writer.write(l + "\r\n");
                //bw.flush();
            }
        }catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    //    设备添加与显示
    private  static  void  add(List<String>information){

        Scanner sc = new Scanner(System.in);
        System.out.println("----- 是否新建设备:新建1/退出0 -------");
        String shuru=sc.next();    //1
        // \r\n在txt文本中换行
        //List<String> information = new ArrayList<>();
        while(shuru.equals("1")){           //2
            System.out.println("----- 请输入设备名称 ------");
            String objName = sc.next();

            while (true) {
                boolean f = false;
                for(String i :information){
                    if(objName.equals(i)){//重名
                        f = true;
                    }
                }
                if(f) {
                    System.out.println("该设备名称已存在,请重新输入:");
                    objName = sc.next();
                }
                else
                    break;
            }

            information.add(objName);
            System.out.println("----- 是否继续新建设备:新建1/退出0 -------");
            shuru=sc.next();   // 3
        }
        if(shuru.equals("0")){
            System.out.println("成功退出了新建界面");
        }else{
            if(!shuru.equals("1")){
                System.out.println("输入的命令非法,已退出新建界面,请重试!");
            }
        }
    }

    private static void delete(List<String>information){
        Scanner sc = new Scanner(System.in);
        System.out.println("----- 是否删除设备:删除1/退出0 -------");
        String shuru=sc.next();
        // \r\n在txt文本中换行
        //List<String> information = new ArrayList<>();
        while(shuru.equals("1")){
            System.out.println("----- 请输入设备名称 ------");
            String objName = sc.next();

            if(information.removeIf(s->objName.equals(s))){
                System.out.println("该设备已删除!");
            }else
                System.out.println("未找到该设备,删除失败!");
            System.out.println("----- 是否继续删除设备:删除1/退出0 -------");
            shuru=sc.next();
        }
        if(shuru.equals("0")){
            System.out.println("成功退出了删除界面");
        }else{
            if(!shuru.equals("1")){
                System.out.println("输入的命令非法,已退出删除界面,请重试!");
            }
        }

    }
    private static void ring(List<String>list){
        Scanner sc = new Scanner(System.in);
        System.out.println("----- 是否蜂鸣/定位设备:确认1/退出0 -------");
        String shuru=sc.next();
        // \r\n在txt文本中换行
        //List<String> information = new ArrayList<>();
        while(shuru.equals("1")){
            System.out.println("----- 请输入设备名称 ------");
            String objName = sc.next();
            int f = 0;
            for(String i :list){
                if(objName.equals(i)){
                    f = 1;
                    System.out.println("----- 已匹配该设备,请选择服务: ------");
                    System.out.println(" 1 蜂鸣 ------");
                    System.out.println(" 2 定位 ------");
                    System.out.println(" 3 蜂鸣并定位 ------");
                    String c =  sc.next();
                    switch (c){
                        case "1":
                            System.out.println("----- 该设备正在蜂鸣 ------");
                            break;
                        case "2":
                            System.out.println("----- 该设备正在定位 ------");
                            break;
                        case "3":
                            System.out.println("----- 该设备正在蜂鸣,定位 ------");
                            break;
                        default:
                            System.out.println("命令错误,请重试!");
                            break;
                    }
                }
            }
            if(f==0) System.out.println("未找到该设备!");
            System.out.println("----- 是否继续蜂鸣/定位设备:确认1/退出0 -------");
            shuru=sc.next();
        }
        if(shuru.equals("0")){
            System.out.println("成功退出了此界面");
        }else{
            if(!shuru.equals("1")){
                System.out.println("输入的命令非法,已退出此界面,请重试!");
            }
        }
    }

    private static void all(String fileName,List<String>list)throws Exception{
//        if(!file.exists()){
//            System.out.println("该文件为空");
//        }

        BufferedReader br = new BufferedReader(new FileReader(fileName));
        String line;
        List <String> l2 = new ArrayList<>();
        while((line = br.readLine())!=null){
            //System.out.println(line);
            l2.add(line);
        }
        br.close();
        for(String l :l2){
            System.out.println(l);
        }
    }
    private static void walkDirectories(String dir) throws IOException {
        File file = new File(dir);
        if(!file.exists()){
            System.out.println("该文件为空");
        }
        BufferedReader br = new BufferedReader(new FileReader(dir));
        String line;
        List <String> l2 = new ArrayList<>();
        while((line = br.readLine())!=null){
            System.out.println(line);
            // l2.add(line);
        }
        br.close();

    }
}


更新了list除重

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值