/*
* 商品实体类
* */
public class Goods {
//商品编号
private String productNum;
//商品名称
private String productName;
//商品价格
private Integer productPrice;
//商品类型
private String productTypes;
//商品数量
private Integer productQuantity;
public String getProductNum() {
return productNum;
}
public void setProductNum(String productNum) {
this.productNum = productNum;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public Integer getProductPrice() {
return productPrice;
}
public void setProductPrice(Integer productPrice) {
this.productPrice = productPrice;
}
public String getProductTypes() {
return productTypes;
}
public void setProductTypes(String productTypes) {
this.productTypes = productTypes;
}
public Integer getProductQuantity() {
return productQuantity;
}
public void setProductQuantity(Integer productQuantity) {
this.productQuantity = productQuantity;
}
public Goods(String productNum, String productName, Integer productPrice, String productTypes, Integer productQuantity) {
this.productNum = productNum;
this.productName = productName;
this.productPrice = productPrice;
this.productTypes = productTypes;
this.productQuantity = productQuantity;
}
public Goods() {
}
}
import java.util.ArrayList;
import java.util.Scanner;
public class GoodsWay {
/*
* 添加商品方法
* */
public static void addGoods(ArrayList<Goods> arrayGoods){
//键盘录入商品信息
Scanner scanner = new Scanner(System.in);
String productName;
GoodsWay goodsWay = new GoodsWay();
//判断商品名称是否重复使用
while (true){
System.out.println("请输入商品名称:");
productName = scanner.next();
boolean flag = isUsed(arrayGoods,productName);
if (flag){
System.out.println("商品名称已被使用,请重新输入");
}else {
break;
}
}
System.out.println("请输入商品编号:");
String productNum = scanner.next();
System.out.println("请输入商品价格:");
Integer productPrice = scanner.nextInt();
System.out.println("请输入商品类型:");
String productTypes = scanner.next();
System.out.println("请输入商品数量:");
Integer productQuantity = scanner.nextInt();
//创建商品对象,把键盘录入的数据赋值给商品对象的成员变量
Goods goods = new Goods();
goods.setProductNum(productNum);
goods.setProductName(productName);
goods.setProductPrice(productPrice);
goods.setProductTypes(productTypes);
goods.setProductQuantity(productQuantity);
//将商品对象添加到集合中
arrayGoods.add(goods);
//给出添加成功提示
System.out.println("添加学生成功.");
}
/*
* 查询商品方法
* */
public static void findAllGoods(ArrayList<Goods>arrayGoods){
if (arrayGoods.size() == 0){
System.out.println("没有商品信息,请先添加信息");
return;
}
//显示商品表头信息
System.out.println("商品编号\t商品名称\t商品价格\t商品类型\t商品数量");
//将集合数据取出按照对应格式显示商品信息
for (int i = 0; i < arrayGoods.size(); i++) {
Goods goods = arrayGoods.get(i);
System.out.println(goods.getProductNum()+"\t\t\t"+goods.getProductName()+"\t\t"+goods.getProductPrice()+"元/kg\t\t"+goods.getProductTypes()+"\t\t"+goods.getProductQuantity()+"个");
}
System.out.println("查询成功");
}
/*
* 修改商品方法
* */
public static void updateGoods(ArrayList<Goods>arrayGoods){
if (arrayGoods.size() == 0){
System.out.println("没有商品信息,请先添加信息");
return;
}
Scanner scanner = new Scanner(System.in);
String productName;
//判断商品名称是否存在
while (true){
//键盘录入商品名称
System.out.println("请输入商品名称");
productName = scanner.next();
boolean flag = isUsed(arrayGoods,productName);
if (flag == true){
break;
}else {
System.out.println("没有该商品信息,请重新输入");
}
}
//键盘录入需要修改的商品信息
System.out.println("请输入新的商品编号:");
String productNum = scanner.next();
System.out.println("请输入新的商品名称");
String productName1 = scanner.next();
System.out.println("请输入新的商品价格:");
Integer productPrice = scanner.nextInt();
System.out.println("请输入新的商品类型:");
String productTypes = scanner.next();
System.out.println("请输入新的商品数量:");
Integer productQuantity = scanner.nextInt();
//创建商品对象
Goods goods = new Goods();
goods.setProductNum(productNum);
goods.setProductName(productName1);
goods.setProductPrice(productPrice);
goods.setProductTypes(productTypes);
goods.setProductQuantity(productQuantity);
//将集合数据取出按照需要修改的商品名称进行修改
for (int i = 0; i < arrayGoods.size(); i++) {
Goods goods1 = arrayGoods.get(i);
if (goods1.getProductName().equals(productName)){
arrayGoods.set(i,goods);
System.out.println("修改商品成功!");
break;
}
}
}
/*
* 删除商品方法
* */
public static void deleteGoods(ArrayList<Goods>arrayGoods){
if (arrayGoods.size() == 0){
System.out.println("没有商品信息,请先添加信息");
return;
}
//键盘录入商品名称
Scanner scanner = new Scanner(System.in);
System.out.println("请输入需要删除的商品名称:");
String productName = scanner.next();
int index = -1;
for (int i = 0; i < arrayGoods.size(); i++) {
Goods goods = arrayGoods.get(i);
if (goods.getProductName().equals(productName)){
index =i;
break;
}
}
if (index == -1){
System.out.println("该信息不存在,请重新输入");
}else {
arrayGoods.remove(index);
System.out.println("删除商品成功");
}
}
/*
* 判断商品名称是否使用
* */
public static boolean isUsed(ArrayList<Goods> arrayGoods,String productName){
boolean flag = false;
for (int i = 0; i < arrayGoods.size(); i++) {
Goods goods = arrayGoods.get(i);
if (goods.getProductName().equals(productName)){
flag = true;
break;
}
}
return flag;
}
}
import java.util.ArrayList;
import java.util.Scanner;
public class GoodsManager {
public static void main(String[] args) {
//初始化商品集合
ArrayList<Goods> arrayGoods = new ArrayList<>();
//调用实现功能对象方法
GoodsWay goodsWay = new GoodsWay();
while (true){
//键盘录入用户输入的功能
Scanner scanner = new Scanner(System.in);
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("请选择需要使用的功能:");
int link = scanner.nextInt();
//根据用户选择功能进行相应的功能操作
switch (link){
case 1:
goodsWay.addGoods(arrayGoods);
break;
case 2:
goodsWay.updateGoods(arrayGoods);
break;
case 3:
goodsWay.deleteGoods(arrayGoods);
break;
case 4:
goodsWay.findAllGoods(arrayGoods);
break;
case 5:
System.out.println("谢谢使用");
System.exit(0);
break;
default:
System.out.println("您输入有误,请重新输入!");
}
}
}
}
功能介绍.png (3.01 KB, 下载次数: 35)
功能介绍
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) | 黑马程序员IT技术论坛 X3.2 |