java简单的食品管理系统
实现功能如下:
1、添加食品
2、查找食品信息
3、修改食品价格
4、删除食品
5、退出此系统
package study5;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;
public class FoodManmge {
public static void main(String[] args) throws Exception{
Manmge shipin=new Manmge();
BufferedReader sr=new BufferedReader(new InputStreamReader(System.in));
while(true){
System.out.println("----食品管理系统----");
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 name=sr.readLine();
System.out.println("请输入食品价格");
float price=Float.parseFloat(sr.readLine());
Food food=new Food(bh,name,price);
shipin.addFood(food);
}else if(str.equals("2")){
System.out.println("请输入食品编号");
String bh=sr.readLine();
shipin.FoodFind(bh);
}
else if(str.equals("3")){
System.out.println("请输入食品编号");
String bh=sr.readLine();
System.out.println("请输入新的价格");
float jg=Float.parseFloat(sr.readLine());
shipin.doPrice(bh, jg);
}else if(str.equals("4")){
System.out.println("请输入食品编号");
String bh=sr.readLine();
shipin.delFood(bh);
}else if(str.equals("0")){
System.out.println("感谢您的使用,再见!");
System.exit(0);
}else{
System.out.println("输入有误,看提示好吧!");
}
}
}
}
class Food{
private String name;
private String bianhao;
private float price;
Food(String bianhao,String name,float price ){
this.name=name;
this.bianhao=bianhao;
this.price=price;
}
public String getName(){
return name;
}
public String getBianhao(){
return bianhao;
}
public float getPrice(){
return price;
}
public void setName(String name){
this.name=name;
}
public void setBianhao(String bianhao){
this.bianhao=bianhao;
}
public void setPrice(float price){
this.price=price;
}
}
class Manmge{
private ArrayList<Food> aaa;
Manmge(){
aaa=new ArrayList<Food>();
}
public void addFood(Food food){
aaa.add(food);
System.out.println("食品添加成功!");
for(int i=0;i<aaa.size();i++){
Food tv=(Food)aaa.get(i);
System.out.println("第"+(i+1)+"个食品为:"+tv.getName());
}
}
public void FoodFind(String bh){
int i;
for(i=0; i< aaa.size();i++){
Food food=(Food)aaa.get(i);
if(food.getBianhao().equals(bh)){
System.out.println();
System.out.println("该食品的信息为如下");
System.out.println("该食品的编号为:"+bh);
System.out.println("该食品的名称为:"+food.getName());
System.out.println("该食品的价格为:"+food.getPrice());
break;
}
}
if(i==aaa.size()){
System.out.println("对不起,暂无此食品!");
}
}
public void doPrice(String bh,float price){
int i;
for(i=0;i<aaa.size();i++){
Food food=(Food)aaa.get(i);
if(food.getBianhao().equals(bh)){
food.setPrice(price);
System.out.println("修改食品价格成功!");
break;
}
}
if(i==aaa.size()){
System.out.println("对不起,没有找到相应的食品,修改失败!");
}
}
public void delFood(String bh){
int i;
if(aaa.size()==0){
System.out.println("对不起,仓库中已没有任何食品!");
}
for(i=0;i<aaa.size();i++){
Food food=(Food)aaa.get(i);
if(food.getBianhao().equals(bh)){
aaa.remove(i);
System.out.println("删除食品成功!");
break;
}
}
if( (i==aaa.size()) && (aaa.size()!=0 )){
System.out.println("对不起,没有该食品!");
}
}
}