集合类---食品库管理

import java.util.*;
import java.io.*;
public class L4_2 {
     public static void main(String[] args) throws Exception{
         Spgl shipin=new 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();//读取一行,接收用户收入的数字,必须是String类型
             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());//返回一个新的 float 值,该值被初始化为用指定 String 表示的值
                 Sp sp=new Sp(bh,mc,jg);
                 shipin.addsp(sp);
             }else if(str.equals("2")){
                 System.out.println("请输入食品的编号");
                 String bh=sr.readLine();
                 shipin.spxx(bh);
             }else if(str.equals("3")){
                 System.out.println("请输入食品的编号");
                 String bh=sr.readLine();
                 System.out.println("请输入新的价格");
                 float jg=Float.parseFloat(sr.readLine());
                 shipin.spjg(bh,jg);     
             }else if(str.equals("4")){
                 System.out.println("请输入食品的编号");
                 String bh=sr.readLine();
                 shipin.delsp(bh);
             }else if(str.equals("0")){
                 System.out.println("感谢您的使用,再见");
                 System.exit(0);
             }else{
                 System.out.println("输入有误");
             }
         }
     }
}
class Sp{//定义一个商品类
    private String bianhao;
    private String mingcheng;
    private float jiage;
    Sp(String bianhao,String mingcheng,float jiage){//构造方法,进行初始化操作
        this.bianhao=bianhao;
        this.mingcheng=mingcheng;
        this.jiage=jiage;
    }
    public String getBianhao() {
        return bianhao;
    }
    public void setBianhao(String bianhao) {
        this.bianhao = bianhao;
    }
    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;
    }   
}
class Spgl{//定义一个食品管理类
    private ArrayList aa=null;
    Spgl(){
        aa=new ArrayList();
    }
    public void addsp(Sp sp){//添加食品
        aa.add(sp);
        System.out.println("添加新的商品成功");
    }
    public void spxx(String bh){//查询食品
        int i;
        for(i=0;i<aa.size();i++){
            Sp sp=(Sp)aa.get(i);
            if(sp.getBianhao().equals(bh)){
                System.out.println("该食品的信息为:");
                System.out.println("食品的编号为:"+bh);
                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++){
            Sp sp=(Sp)aa.get(i);
            if(sp.getBianhao().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++){
            Sp sp=(Sp)aa.get(i);
            if(sp.getBianhao().equals(bh)){
                aa.remove(i);
                System.out.println("删除食品成功");
                break;
            }
        }
        if((aa.size()==0)&&(aa.size()!=0)){
            System.out.println("对不起。没有该食品");
        }
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值