食品庫存管理

託帕發表於2018-09-04

新增、查詢、修改價格、刪除商品。

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;
public class Test1 {
	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();//讀取一行
			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());

				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(i==aa.size()&&(aa.size()!=0)){
				System.out.println("對不起,沒有該食品");
			}
		}
	}
}

 

相關文章