赞
踩
- public class Goods {
- private String Name;
- private String State; //促销状态
- private double listPrice; //原价
- private double promoPrice; //促销价
- private int Inventory; //库存
- private int SV; //销量
-
- //无参构造
- public Goods() {
- }
-
- //全参构造
-
-
- public Goods(String name, String state, double listPrice, double promoPrice, int inventory, int SV) {
- Name = name;
- State = state;
- this.listPrice = listPrice;
- this.promoPrice = promoPrice;
- Inventory = inventory;
- this.SV = SV;
- }
-
- public String getName() {
- return Name;
- }
-
- public void setName(String name) {
- Name = name;
- }
-
- public String getState() {
- return State;
- }
-
- public void setState(String state) {
- State = state;
- }
-
- public double getListPrice() {
- return listPrice;
- }
-
- public void setListPrice(double listPrice) {
- this.listPrice = listPrice;
- }
-
- public double getPromoPrice() {
- return promoPrice;
- }
-
- public void setPromoPrice(double promoPrice) {
- this.promoPrice = promoPrice;
- }
-
- public int getInventory() {
- return Inventory;
- }
-
- public void setInventory(int inventory) {
- Inventory = inventory;
- }
-
- public int getSV() {
- return SV;
- }
-
- public void setSV(int SV) {
- this.SV = SV;
- }
-
- @Override
- public String toString() {
- return "Goods{" +
- "Name='" + Name + '\'' +
- ", State='" + State + '\'' +
- ", listPrice='" + listPrice + '\'' +
- ", promoPrice='" + promoPrice + '\'' +
- ", Inventory=" + Inventory +
- ", SV=" + SV +
- '}';
- }
- }

- import java.util.Arrays;
- import java.util.Scanner;
-
- public class GoodsManager {
- //全局静态变量
- static Goods[] goods; //商品数组
- static Scanner scanner = new Scanner(System.in);
-
- //静态代码块:初始化商品数据
- static {
- goods = new Goods[4];
-
- goods[0]=new Goods("苹果","促销",5,2,20,10);
- goods[1]=new Goods("方便面","未促销",4,3.5,30,20);
- goods[2]=new Goods("矿泉水","促销",2,1.5,40,30);
- goods[3]=new Goods("火腿肠","未促销",1.5,1,50,40);
-
- }
-
- //展示商品列表
- static void disPlay(){
- System.out.println("-------商品列表-------");
- for (int i = 0; i < goods.length; i++) {
- if(goods[i]!=null){
- System.out.println(goods[i].toString());
- }
- }
- }
-
- //添加商品
- static void addGoods(){
- System.out.println("********添加商品********");
- //添加前的扩容工作
- if(goods[goods.length-1]!=null){ //判断商品数组最后一个位置是否为空,不为空则满,需要扩容
- goods = Arrays.copyOf(goods,goods.length+5); //扩容
- }
-
- //创建一个新的商品对象
- Goods good = new Goods();
- //录入商品信息
- System.out.println("输入商品名称:");
- good.setName(scanner.next());
- System.out.println("输入商品状态:");
- good.setState(scanner.next());
- System.out.println("输入商品原价:");
- good.setListPrice(scanner.nextDouble());
- System.out.println("输入商品促销价:");
- good.setPromoPrice(scanner.nextDouble());
- System.out.println("输入商品库存:");
- good.setInventory(scanner.nextInt());
- System.out.println("输入商品销量:");
- good.setSV(scanner.nextInt());
-
- //遍历商品数组寻找空位置插入商品
- for (int i = 0; i < goods.length; i++) {
- if(goods[i]==null){
- goods[i]=good;
- break;
- }
- }
- System.out.println("商品添加成功!");
- }
-
- //删除商品
- static void removeGoods(){
- System.out.println("*********删除商品*********");
- disPlay(); //展示商品列表
-
- //接收待删除商品的名称
- System.out.println("请输入待删除的商品名称");
- String name = scanner.next();
-
- //定义存放待删除商品的下标
- int removeIndex = -1;
-
- //先在数组中寻找这个商品
- for (int i = 0; i < goods.length; i++) {
- if(goods[i].getName().equals(name)){ //找到这个商品
- removeIndex = i;
- break;
- }
- }
-
- if(removeIndex == -1 ){ //没找到这个商品
- System.out.println("该商品不存在!");
- return;
- }else { //商品存在,开始执行删除操作,并且将其之后的元素前移一位
- if(removeIndex == goods.length - 1 ){ //判断待删除元素是不是数组的最后一位
- goods[goods.length-1] = null ; //是的话直接置空即可
- }else{ //不在数组的最后一位,执行前移操作
- for (int i = removeIndex; i < goods.length-1; i++) {
- if(goods[i]==null){ //前移到为空为止
- break;
- }
- goods[i]=goods[i+1]; //前移操作
- }
- goods[goods.length-1]=null; //当数组为满情况删除非最后一位元素时,后移完需要将最后一个元素置空,否则会重复
- }
-
- }
- System.out.println("删除成功!");
- disPlay();
- }
-
- //购买商品
- static void buyGoods(){
- System.out.println("********购买商品*********");
- disPlay();
- double rental = 0.0; //应付款金额
-
- //接收购买的商品名
- System.out.println("输入购买的商品的名称:");
- String name = scanner.next();
- boolean exit = false; //判断是否找到的标志
-
- //接收购买的数量
- System.out.println("请输入你需要购买该商品的数量:");
- int buyStock = scanner.nextInt();
-
- //判断数量是否合法
- while(buyStock<0){
- System.out.println("购买数量不合法,请从新输入一个大于0的整数:");
- buyStock = scanner.nextInt();
- }
-
- //判断商品是否存在
- for (int i = 0; i < goods.length; i++) {
- if(goods[i]==null){
- break;
- }
- if(goods[i].getName().equals(name)){ //找到该商品
- exit = true;
-
-
- //判断库存是否足够
- while(goods[i].getInventory() < buyStock){
- System.out.println("购买数量超过库存!请重新购买");
- buyStock = scanner.nextInt();
-
- while(buyStock<0){
- System.out.println("购买数量不合法,请从新输入一个大于0的整数:");
- buyStock = scanner.nextInt();
- }
- }
- goods[i].setSV(goods[i].getSV()+buyStock);
- goods[i].setInventory(goods[i].getInventory() - buyStock);
-
- disPlay();
- //打印小票
- if(goods[i].getState().equals("促销")){
- rental = buyStock * goods[i].getPromoPrice(); //促销价
- System.out.println("您购买了"+buyStock+"个"+name+"促销单价为:"+goods[i].getPromoPrice()+"需要支付:"+rental);
- }
- if(goods[i].getState().equals("未促销")){
- rental = buyStock * goods[i].getListPrice(); //原价
- System.out.println("您购买了"+buyStock+"个"+name+"原价为:"+goods[i].getListPrice()+"需要支付:"+rental);
- }
-
- System.out.println("请输入您的支付金额");
- double pay = scanner.nextDouble();
-
- while (pay<rental){
- System.out.println("您支付的金额小于总租金,请重新输入");
- pay = scanner.nextDouble();
- }
-
- System.out.println("********购物小票********");
- System.out.println("购买商品:" + name);
- if(goods[i].getState().equals("促销")){
- System.out.println("购买商品单价为:" + goods[i].getPromoPrice());
- }else
- {
- System.out.println("购买商品单价为:" + goods[i].getListPrice());
-
- }
- System.out.println("购买数量为:"+buyStock);
- System.out.println("需支付:" + rental);
- System.out.println("实际支付"+ pay);
- System.out.println("找零:" + (pay-rental));
- System.out.println("**********欢迎下次光临*********");
- }
- }
- if(!exit){
- System.out.println("没找到该商品!");
- }
- }
-
-
- //修改商品促销状态
- static void updateState(){
- System.out.println("********修改商品的促销状态*********");
- disPlay();
-
- //接收待修改商品名
- System.out.println("输入需要修改商品的名称:");
- String name = scanner.next();
- boolean b2 = false; //判断是否找到的标志
-
- //接收新的状态
- System.out.println("请输入该商品的状态(促销 & 未促销):");
- String state = scanner.next();
- //判断状态是否合法
- boolean s1 = true;
- while(s1){
- if(state.equals("促销") || state.equals("未促销")){
- break;
- }
- System.out.println("状态不合法,请从新输入:");
- state = scanner.next();
- }
-
- //判断是否存在
- for (int i = 0; i < goods.length; i++) {
- if(goods[i]==null){
- break;
- }
- if(goods[i].getName().equals(name)){ //找到该元素并进行修改
- b2 = true;
- goods[i].setState(state);
- System.out.println("修改成功!");
- disPlay();
- }
- }
- if(!b2){
- System.out.println("没找到该商品!");
- }
- }
-
- //补充库存函数
- static void restock(){
- System.out.println("********补充商品的库存*********");
- disPlay();
-
- //接收待补充商品名
- System.out.println("输入需要补充库存的商品的名称:");
- String name = scanner.next();
- boolean b2 = false; //判断是否找到的标志
-
- //接收需要增加的量
- System.out.println("请输入你需要补充该商品的数量:");
- int addStock = scanner.nextInt();
- //判断数量是否合法
- boolean s1 = true;
- while(s1){
- if(addStock>=0){
- break;
- }
- System.out.println("数量不合法,请从新输入:");
- addStock = scanner.nextInt();
- }
-
- //判断是否存在
- for (int i = 0; i < goods.length; i++) {
- if(goods[i]==null){
- break;
- }
- if(goods[i].getName().equals(name)){ //找到该元素并进行修改
- b2 = true;
- goods[i].setInventory(goods[i].getInventory()+addStock);
- System.out.println("增加成功!");
- disPlay();
- }
- }
- if(!b2){
- System.out.println("没找到该商品!");
- }
- }
-
- //打印菜单函数
- static void printMenu(){
- boolean exit = false;
- do{
- System.out.println("************欢迎进入超市***********");
- System.out.println("1、查看商品列表");
- System.out.println("2、添加商品");
- System.out.println("3、删除商品");
- System.out.println("4、购买商品");
- System.out.println("5、修改商品促销状态");
- System.out.println("6、增加商品库存");
- System.out.println("7、退出超市");
- System.out.println("/请选择菜单编号进行选择/:");
-
- int c = scanner.nextInt();
- switch (c){
- case 1 :
- disPlay();
- break;
- case 2 :
- addGoods();
- break;
- case 3 :
- removeGoods();
- break;
- case 4 :
- buyGoods();
- break;
- case 5 :
- updateState();
- break;
- case 6 :
- restock();
- break;
- case 7 :
- exit = true ;
- break;
- default:
- System.out.println("输入的菜单编号错误!请重新输入");
- break;
- }
-
- }while (!exit);
- System.out.println("!Bye!");
- }
-
-
-
- public static void main(String[] args) {
- //removeGoods();
- //updateState();
- printMenu();
- }
-
- }

具体功能不在演示
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。