2301_81289092 2024-09-05 21:26 采纳率: 0%
浏览 4

系统运行不对怎么办啊

img

img


show不出来文件内容,是写的有什么问题吗?服了呀改也改不对

  • 写回答

1条回答 默认 最新

  • 星落.. 2024-09-06 00:35
    关注
    
    package com.star.cmsdemo02;
    
    /*
     * @Author: wangjiawei
     * @Date: 2024/9/5 22:39
     * @Description:
     */
    
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.util.Scanner;
    public class productView {
        private productlist productlist = new productlist(200);
    
        public void printmenu() {
            System.out.println("------品牌鞋信息管理系统------");
            System.out.println("info:----品牌鞋基本信息管理----");
            System.out.println("quit:----退出系统");
            System.out.println("---------------------------");
            while (true) {
                Scanner sc = new Scanner(System.in);
                System.out.print("main>");
                String choice = sc.next();
                switch (choice) {
                    case "info":
                        System.out.println("---品牌鞋基本信息管理---");
                        System.out.println("show:----显示品牌信息");
                        System.out.println("add:-----添加品牌信息");
                        System.out.println("delete:--删除品牌信息");
                        System.out.println("modify:--修改品牌信息");
                        System.out.println("save:--导出文件");
                        System.out.println("load:---导入文件");
                        System.out.println("quit:---退出-----");
                        System.out.println("------------------");
                        while (true) {
                            sc = new Scanner(System.in);
                            System.out.print("info>");
                            String choice1 = sc.next();
                            switch (choice1) {
                                case "show":
                                    System.out.println("---展示产品信息---");
                                    show();
                                    break;
                                case "add":
                                    System.out.println("---添加产品信息---");
                                    add();
                                    break;
                                case "delete":
                                    System.out.println("---删除品牌信息---");
                                    delete();
                                    break;
                                case "modify":
                                    System.out.println("---修改品牌信息---");
                                    modify();
                                    System.out.println("---修改品牌信息---");
                                    modify();
                                    break;
                                case "save":
                                    System.out.println("---导出文件---");
                                    exportFile();
                                    break;
                                case "load":
                                    System.out.println("---导入文件---");
                                    try {
                                        load();
                                    } catch (IOException e) {
                                        throw new RuntimeException(e);
                                    }
                                    break;
                                case "return":
                                    return;
                            }
                        }
                    case "quit":
                        System.exit(0);
    
                }
            }
        }
    
        public void process() {
            printmenu();
            while (true) {
                Scanner sc = new Scanner(System.in);
                System.out.print("info>");
                String choice = sc.next();
                switch (choice) {
                    case "add":
                        add();
                        break;
                    case "delete":
                        delete();
                        break;
                    case "modify":
                        modify();
                        break;
                    case "load":
                        try {
                            load();
                        } catch (IOException e) {
                            throw new RuntimeException(e);
                        }
                        break;
                    case "return":
                        return;
    
                }
    
    
            }
    
        }
    
        public static void main(String args[]) {
            productView st = new productView();
            st.process();
        }
    
        public int find(String id) {
            product[] pros =  productlist.getallproduct();
            int k = -1;
            for (int i = 0; i < pros.length; i++) {
                if (id.equals(pros[i].getId())) {
                    k = i;
                    break;
                }
            }
            return k;
    
        }
    
        public void add() {
            Scanner sc = new Scanner(System.in);
            System.out.println("序号");
            String id = sc.next();
            System.out.println("品牌名称");
            String name = sc.next();
            System.out.println("价钱");
            String price = sc.next();
            System.out.println("颜色");
            String colour = sc.next();
            System.out.println("产地");
            String producer = sc.next();
            product pro = new product(id, name, price, colour, producer);
            boolean flag = productlist.addproduct(pro);
            if (flag) {
                System.out.println("---添加成功---");
            } else {
                System.out.println("---添加失败---");
            }
        }
    
        public void delete() {
            System.out.println("请输入要删除的序号:");
            Scanner sc = new Scanner(System.in);
            String id = sc.next();
            int index = find(id);
            if (index == -1) {
                System.out.println("---该品牌鞋不存在---");
                return;
            }
            boolean flag = productlist.delproduct(index);
            if (flag) {
                System.out.println("---删除成功---");
            } else {
                System.out.println("---删除失败---");
            }
        }
    
        public void modify() {
            System.out.print("请输入要修改的序号:");
            Scanner sc = new Scanner(System.in);
            String id = sc.next();
            int index = find(id);
            if (index == -1) {
                System.out.println("---该品牌不存在---");
                return;
            }
            System.out.println("品牌名称");
            String name = sc.next();
            System.out.println("价钱");
            String price = sc.next();
            System.out.println("颜色");
            String colour = sc.next();
            System.out.println("产地");
            String producer = sc.next();
            product pro = new product(index);
            boolean flag = productlist.modifyproduct(index, pro);
            if (flag) {
                System.out.println("---修改成功---");
            } else {
                System.out.println("---修改失败---");
            }
        }
    
        public void show() {
            System.out.println("----品牌信息----");
            // product[] pros;
            product[] pros = productlist.getallproduct();
    
            // TODO 这里不应该是判断product.length == 0,你product里的length属性没赋初值且没用过默认是0,应该是判断product[]数组的长度
            if (pros.length == 0) {
                System.out.println("尚没有该品牌记录");
            } else {
                System.out.println("序号\t名称\t价钱\t颜色\t产地");
                for (int i = 0; i <= pros.length - 1; i++) {
                    System.out.println(pros[i].getId() + "\t"
                            + pros[i].getName() + "\t"
                            + pros[i].getPrice() + "\t"
                            +pros[i].getColour() + "\t"
                            + pros[i].getProducer() + "\t");
                }
            }
    
            System.out.println("------------------------");
        }
    
        public void exportFile() {
            String filePath = "D:\\product.txt";
            product[] pros = productlist.getallproduct();
            try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath))) {
                writer.write("序号\t\t品牌名称\t\t价格\t\t颜色\t\t产地\n");
                for (int i = 0; i < pros.length; i++) {
                    writer.write(pros[i].getId() + "\t\t\t"
                            + pros[i].getName() + "\t\t"
                            + pros[i].getPrice() + "\t\t"
                            + pros[i].getColour() + "\t\t"
                            + pros[i].getProducer() + "\n");
                }
                System.out.println("品牌信息已成功写入文件!");
            } catch (IOException e) {
                System.err.println("写入文件时发生错误:" + e.getMessage());
            }
        }
    
        public void load1() throws IOException{
            product s1 = new product("1001", "安踏","200¥", "白色","江西");
            productlist.addproduct(s1);
    
    //        System.out.println("load()方法:"+s1);
            product[] pros = productlist.getallproduct();
            for (product pro : pros) {
                System.out.println("load list:" + pro);
            }
    
        }
        public void load() throws IOException {
            Scanner sc = new Scanner(System.in);
            System.out.print("请输入要导入的文件名:");
    
    //        String fn = sc.next();
            //文件路径得用"/",不要用"\\"
            String fn = "E:/IdeaProjects/code/cms-demo02/src/test/java/com/star/cmsdemo02/a.txt";
            File file = new File(fn);
    
            System.out.println(file.exists());//true
    
            if (file.exists()) {
                BufferedReader br = new BufferedReader(new FileReader(fn));
                String str;
                // TODO 你while这句多了分号
                while ((str = br.readLine()) != null)
                {String id = str.split(",")[0];
                    String name = str.split(",")[1];
                    String price = str.split(",")[2];
                    String colour = str.split(",")[3];
                    String producer = str.split(",")[4];
                    product s = new product(id, name,price, colour,producer);
                    System.out.println("产品:"+s.name);
    
                    if (idExists(id)) {
                        System.out.println("---该顾客已存在---");
                    } else {
                        productlist.addproduct(s);
                    }
                }
                br.close();
                System.out.println("--导入成功--");
            } else {
                System.out.println("---要导入的文件不存在--");
            }
        }
    
        private boolean idExists(String id) {
            return false;
        }
        public class product {
            //这个静态属性没有初始化过 length默认等于0 show()方法中的(product.length == 0)判断恒等于true
            public static int length;
    
            private String id;
            private String name;
            private String price;
            private String colour;
            private String producer;
    
            //单参构造器
            public product(int i) {
                this.name = this.id;
                this.id = this.name;
                this.price= this.price;
                this.colour= this.colour;
                this.producer= this.producer;
            }
    
            //全参构造器
            public product(String id, String name, String price, String colour, String producer) {
                this.id = id;
                this.name = name;
                this.price= price;
                this.colour= colour;
                this.producer= producer;
            }
    
    
            public static product[] addproduct() {
                return new product[0];
            }
    
            product get(int i) {
                return new product(i);
            }
            public String getName(){
                return name;
            }
            public void setName(String name){
                this.name = name;
            }
            public String getPrice() {
                return price;
            }
            public void setPrice(String price){
                this.price= price;
            }
            public String getId() {
                return id;
            }
            public void setId(String id) {
                this.id = id;
            }
            public String getColour() {
                return colour;
            }
            public void setColour(String colour) {
                this.colour = colour;
            }
            public String getProducer() {
                return producer;
            }
            public void setProducer(String  producer) {
                this.producer = producer;
            }
    
            @Override
            public String toString() {
                return "Product{" +
                        "id='" + id + '\'' +
                        ", name=" + name +
                        ", price=" + price+
                        ", colour='" + colour + '\'' +
                        ",producer=" + producer +
                        '}';
            }
    
    
        }
    
    //    public class productlist {
    //        public static Object remove;
    //        private product[] products;
    //        private static int total = 0;
    //
    //        public productlist(int maxlength) {
    //            products = new product[maxlength];
    //        }
    //
    //        public boolean modifyproduct(int index, product pro) {
    //            // TODO Auto-generated method stub
    //            return false;
    //        }
    //
    //        public product[] getallproduct() {
    //            // TODO Auto-generated method stub
    //            return null;
    //        }
    //
    //        public static void remove(int i) {
    //        }
    //
    //        public boolean addproduct(product pro) {
    //            if (total >= products.length) {
    //                return false;
    //            }
    //            products[total++] = pro;
    //            return true;
    //        }
    //
    //        public boolean delproduct(int index) {
    //            if (total < 0 || index >= total) {
    //                return false;
    //
    //            }
    //            for (int i = index; i < total - 1; i++) {
    //                products[i] = products[i++];
    //            }
    //            products[--total] = null;
    //            return true;
    //        }
    //    }
        public class productlist {
            public static Object remove;
            private product[] products;
            private static int total = 0;
    
            public productlist(int maxlength) {
                products = new product[maxlength];
            }
    
            public static void remove(int i) {
            }
    
            public boolean addproduct(product pro) {
                if (total >= products.length) {
                    return false;
                }
                products[total++] = pro;
                return true;
            }
    
            public boolean delproduct(int index) {
                if (total < 0 || index >= total) {
                    return false;
    
                }
                for (int i = index; i < total - 1; i++) {
                    products[i] = products[i++];
                }
                products[--total] = null;
                return true;
            }
    
            public boolean modifyproduct(int index, product pro) {
                if (index < 0 || index >= total) {
                    return false;
                }
                products[index] = pro;
                return true;
            }
    
            public product[] getallproduct() {
                product[] pros = new product[total];
                for (int i = 0; i < total; i++) {
                    pros[i] = products[i];
                }
                return pros;
            }
        }
    }
    
    
    评论

报告相同问题?

问题事件

  • 创建了问题 9月5日

悬赏问题

  • ¥15 Mac版Fiddler Everywhere4.0.1提示强制更新
  • ¥15 android 集成sentry上报时报错。
  • ¥50 win10链接MySQL
  • ¥35 跳过我的世界插件ip验证
  • ¥15 抖音看过的视频,缓存在哪个文件
  • ¥15 自定义损失函数报输入参数的数目不足
  • ¥15 如果我想学习C大家有是的的资料吗
  • ¥15 根据文件名称对文件进行排序
  • ¥15 deploylinux的ubuntu系统无法成功安装使用MySQL❓
  • ¥15 有人会用py或者r画这种图吗