| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2993 人关注过本帖
标题:ArraryList 食品仓库管理
只看楼主 加入收藏
飞逝的流年
Rank: 1
等 级:新手上路
帖 子:29
专家分:0
注 册:2020-8-27
结帖率:0
收藏
已结贴  问题点数:20 回复次数:1 
ArraryList 食品仓库管理

程序中有众多警告,怎么解决


package text;

import
import
import java.util.ArrayList;

public class Demo1 {


    public static void main(String[] args) throws Exception {
        shipintguanli.Spgl shiping = new shipintguanli.Spgl();
        BufferedReader sr = new BufferedReader(new InputStreamReader(System.in));
        while (true) {
            System.out.println("请按提示选择已下功能");
            System.out.println("添加食品请按1");
            System.out.println("查询食品信息请按2");
            System.out.println("修改食品价格请按3");
            System.out.println("删除食品请安4");
            System.out.println("退出请安0");
            String str = sr.readLine();
            if (str.equals("1")) {
                System.out.println("请输入食品编号:");
                String bh = sr.readLine();
                System.out.println("请输入食品名称: ");
                String mc = sr.readLine();
                System.out.println("请输入食品价格: ");
                float jg = Float.parseFloat(sr.readLine());
                shipintguanli.Sp sp = new shipintguanli.Sp(bh, mc, jg);
                shiping.addsp(sp);
            }
            else if (str.equals("2")) {
                System.out.println("请输入食品编号");
                String bh = sr.readLine();
                shiping.spxx(bh);
            }
            else if (str.equals("3")) {
                System.out.println("请输入食品编号");
                String bh = sr.readLine();
                System.out.println("请输入新的食品价格: ");
                float jg = Float.parseFloat(sr.readLine());
                shiping.spjg(bh, jg);
            }
            else if (str.equals("4")) {
                System.out.println("请输入食品编号");
                String bh = sr.readLine();
                shiping.delsp(bh);
            }
            else if (str.equals("0")) {
                System.out.println("感谢您的使用,再见");
                System.exit(0);
                break;
            }
            else {
                System.out.println("输入有误");

            }
        }
    }
    public static class Spgl {
        private ArrayList aa = null;

        public Spgl() {
            aa = new ArrayList();
        }

        public void addsp(shipintguanli.Sp sp) {
            aa.add(sp);
            System.out.println("添加新食品成功!");
        }

        public void spxx(String bh) {
            int i;
            for (i = 0; i < aa.size(); i++) {
                shipintguanli.Sp sp = (shipintguanli.Sp) aa.get(i);
                if (sp.getBiaonhao().equals(bh)) {
                    System.out.println("该食品名称为: ");
                    System.out.println("食品编号: " + sp.getBiaonhao());
                    System.out.println("食品名称: " + sp.getMingcheng());
                    System.out.println("食品价格: " + sp.getJiage());
                    break;
                }

            }
            if (i == aa.size()) {
                System.out.println("对不起,无此食品!");
            }
        }

        public void spjg(String bh, float jg) {
            int i;
            for (i = 0; i < aa.size(); i++) {
                shipintguanli.Sp sp = (shipintguanli.Sp) aa.get(i);
                if (sp.getBiaonhao().equals(bh)) {
                    sp.setJiage(jg);
                    System.out.println("修改食品价格成功!");
                    break;
                }
            }
            if (i == aa.size()) {
                System.out.println("对不起没有找到相应的食品,修改价格失败!");
            }
        }

        public void delsp(String bh) {
            int i;
            if (aa.size() == 0) {
                System.out.println("对不起,仓库中已经没有食品了!");
            }
            for (i = 0; i < aa.size(); i++) {
                shipintguanli.Sp sp = (shipintguanli.Sp) aa.get(i);
                if (sp.getBiaonhao().equals(bh)) {
                    aa.remove(i);
                    System.out.println("删除食品成功!");
                }
            }
                //这条if语句运行不出来,怎么解决
            if ((i == aa.size()) && (aa.size() != 0)) {
                System.out.println("对不起没有该食品!");
            }

        }

    }


    public static class Sp {
        private String biaonhao;
        private String mingcheng;
        private Float jiage;

        public String getBiaonhao() {
            return biaonhao;
        }

        public void setBiaonhao(String biaonhao) {
            this.biaonhao = biaonhao;
        }

        public String getMingcheng() {
            return mingcheng;
        }

        public void setMingcheng(String mingcheng) {
            this.mingcheng = mingcheng;
        }

        public Float getJiage() {
            return jiage;
        }

        public void setJiage(Float jiage) {
            this.jiage = jiage;
        }

        @Override
        public String toString() {
            return "Sp{" +
                    "biaonhao='" + biaonhao + '\'' +
                    ", mingcheng='" + mingcheng + '\'' +
                    ", jiage=" + jiage +
                    '}';
        }
    }
}
搜索更多相关主题的帖子: System public out String println 
2021-09-30 19:47
林月儿
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:湖南
等 级:版主
威 望:138
帖 子:2277
专家分:10647
注 册:2015-3-19
收藏
得分:20 
程序代码:
 public void delsp(String bh) {
            int i;
            if (aa.size() == 0) {
                System.out.println("对不起,仓库中已经没有食品了!");
            }
            for (i = 0; i < aa.size(); i++) {// 刪除操作应该是倒序遍历
                shipintguanli.Sp sp = (shipintguanli.Sp) aa.get(i);
                if (sp.getBiaonhao().equals(bh)) {
                    aa.remove(i);
                    System.out.println("删除食品成功!");
                }
            }
                //这条if语句运行不出来,怎么解决,既然是条件判断,得先触发条件
            if ((i == aa.size()) && (aa.size() != 0)) {
                System.out.println("对不起没有该食品!");
            }
        }

剑栈风樯各苦辛,别时冰雪到时春
2021-10-06 14:16
快速回复:ArraryList 食品仓库管理
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.067105 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved