import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
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();
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.sqjg(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(); //把new集合类对象放在管理类的构造方法中
//等于在new管理类对象的时候直接new了集合类对象
}
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 sqjg(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("对不起,没有该食品!");
}
}
}
L4_2
最新推荐文章于 2023-03-30 11:56:14 发布