发布网友 发布时间:2022-04-24 08:16
共2个回答
热心网友 时间:2022-06-17 23:01
写了三个类
Goods.java
Supermarket.java
Test.java
package web;
public class Goods
{
/**
* 具有商品名称Name,
* 商品价格Price,
* 商品数量number等属性,
* 操作Sale(销售商品,余额不足时给予提示)、
* Add(商品上架操作)
* 和ShowMe(显示商品信息)。
*/
private String name;
private float price;
private int number;
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public float getPrice()
{
return price;
}
public void setPrice(float price)
{
this.price = price;
}
public int getNumber()
{
return number;
}
public void setNumber(int number)
{
this.number = number;
}
}
package web;
import java.util.ArrayList;
import java.util.List;
/**
* 1、实现描述超市的的类Suppermacket类,
* 记录系统中现有商品(Goods类),
* 定义增加商品的函数Append,
* 删除商品的函数Delete,
* 查询商品的函数Query,
* 并显示查询结果;
* @author newuser
*
*/
public class Supermarket
{
private List<Goods> goodsList = new ArrayList<Goods>();
/**
* 增加商品的
* @param goods
* @return
*/
public String append(Goods goods)
{
goodsList.add(goods);
return "添加成功";
}
/**
* 删除商品
*/
public String delete(String name)
{
String flag = "删除商品["+name+"]不存在 ";
if(name != null)
{
for (int i = 0; i < goodsList.size(); i++)
{
Goods goods = goodsList.get(i);
if (goods.getName().equals(name))
{
goodsList.remove(i);
flag = "删除商品["+name+"]成功";
break;
}
}
}
return flag;
}
/**
* 销售
* @param goods
* @return
*/
public String sale(Goods goods)
{
String flag = "销售商品["+goods.getName()+"]失败";
if (goods!=null)
{
for (int i = 0; i < goodsList.size(); i++)
{
Goods goods_2 = goodsList.get(i);
if (goods_2.getName().equals(goods.getName()))
{
if (goods.getNumber() >goods_2.getNumber())
{
flag+=" 原因:库存不足";
}
else if ((goods_2.getPrice()*goods.getNumber()) > goods.getPrice())
{
flag+=" 原因:余额不足";
}
else
{
goods_2.setNumber(goods_2.getNumber()-goods.getNumber());
flag = "销售商品["+goods.getName()+"]成功";
}
break;
}
}
}
return flag;
} /**
* 查询商品
* @return
*/
public List<Goods> getGoodsList()
{
return goodsList;
}
public void setGoodsList(List<Goods> goodsList)
{
this.goodsList = goodsList;
}
}
package web;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Test
{
/**
* 1、实现描述超市的的类Suppermacket类, 记录系统中现有商品(Goods类), 定义增加商品的函数Append,
* 删除商品的函数Delete, 查询商品的函数Query, 并显示查询结果;
* 2、定义商品类Goods,具有商品名称Name,商品价格Price,商品数量number等属性,操作Sale(销售商品,余额不足时给予提示)、Add(商品上架操作)和ShowMe(显示商品信息)。
* 3、编写包含main方法的测试类,测试以上所要求的各种功能,即可以根据菜单命令增加、删除和查询商品,以及商品销售和商品上架的操作。
*/
public static void main(String[] args)
{
Supermarket market = new Supermarket();
String[] label =
{ "商品名称", "商品价格", "商品数量" };
int type = -1;
do
{
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 退出系统");
System.out.println("=================");
int i = 0;
do
{
try
{
type = Integer.parseInt(readString());
i = 0;
}
catch (Exception e)
{
i = 1;
System.err.println("输入有误");
}
}
while (i != 0);
switch (type)
{
case 1:
Goods goods = new Goods();
System.out.println("请输入商品名称");
goods.setName(readString());
do
{
try
{
System.out.println("请输入商品价格");
goods.setPrice(Integer.parseInt(readString()));
i = 0;
}
catch (Exception e)
{
i = 1;
System.err.println("输入有误");
}
}
while (i != 0);
do
{
try
{
System.out.println("请输入商品数量");
goods.setNumber(Integer.parseInt(readString()));
i = 0;
}
catch (Exception e)
{
i = 1;
System.err.println("输入有误");
}
}
while (i != 0);
System.out.println(market.append(goods));
break;
case 2:
System.out.println("请输入要删除商品多名称");
String name = readString();
System.out.println(market.delete(name));
break;
case 3:
System.out.println("商品名称\t商品价格\t商品数量");
for (int j = 0; j < market.getGoodsList().size(); j++)
{
Goods g = market.getGoodsList().get(j);
System.out.println(g.getName() + "\t" + g.getPrice() + "\t"
+ g.getNumber());
}
break;
case 4:
Goods goods_sals = new Goods();
System.out.println("请输入销售商品名称");
goods_sals.setName(readString());
do
{
try
{
System.out.println("请输入销售商品价格");
goods_sals.setPrice(Integer.parseInt(readString()));
i = 0;
}
catch (Exception e)
{
i = 1;
System.err.println("输入有误");
}
}
while (i != 0);
do
{
try
{
System.out.println("请输入销售商品数量");
goods_sals.setNumber(Integer.parseInt(readString()));
i = 0;
}
catch (Exception e)
{
i = 1;
System.err.println("输入有误");
}
}
while (i != 0);
System.out.println(market.sale(goods_sals));
break;
default:
System.out.println("选择不正确");
break;
}
}
while (type != 0);
}
/**
*
* @param prompt
* @return
*/
private static String readString()
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = null;
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
热心网友 时间:2022-06-17 23:01
考试题啊,选择题部分:dadcc
acbdc
cba,填空题:(1)class
interface
(2)0.67
后面的想要知道的话。。。麻烦先追加至少100分,不客气。