数组类
public class Demo02 {
Article[] articles = new Article[30];
}
商品类
/*商品类,对商品属性的封装*/
public class Article {
public String name; // 商品的名称
public int amount; //库存
public double price;//单价
public int number;//售出的数量
public void print(int index){ //打印: 为了便于查看商品类的详情
System.out.println(index+"\t" + name+ "\t" + amount +" \t" + price+"\t" +number);
}
//初始化商品信息
public void setArticle(String mingzi,int kucun, double danjia,int shouchu){
name = mingzi;
amount = kucun;
price = danjia;
number = shouchu;
}
}
代码类
/*商品管理类*/
public class ArticleMange {
//创建一个实体的仓库对象 并实体化
Demo02 articleSet = new Demo02();
//初始化仓库,并放入起始商品
public void inital() {
Article xiaomi11 = new Article();
xiaomi11.setArticle("小米11", 30, 1999, 0);
Article xiaomi11pro = new Article();
xiaomi11pro.setArticle("小米11pro", 40, 2999, 0);
Article xiaomiUltra = new Article();
xiaomiUltra.setArticle("小米增强版", 50, 3999, 0);
articleSet.articles[0] = xiaomi11;
articleSet.articles[1] = xiaomi11pro;
articleSet.articles[2] = xiaomiUltra;
}
public void startMenu() {
boolean flag = true;
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("*********************************");
System.out.println("请输入你要执行的功能编号");
Scanner scanner = new Scanner(System.in);
int gongnengbianhao = scanner.nextInt();
switch (gongnengbianhao) {
case 1:
System.out.println("查看商品信息");
chakan();
break;
case 2:
System.out.println("新增商品信息");
add();
break;
case 3:
System.out.println("删除商品信息");
delete();
break;
case 4:
System.out.println("卖出商品");
sell();
break;
case 5:
System.out.println("商品销售排行榜");
leaderboard();
break;
case 6:
System.out.println("退出");
flag = false;
default:
System.out.println("你输入的编号有无");
break;
}
} while (flag);
}
//
// //排行榜
//销售排行榜
public void leaderboard() {
int count = 0; // 统计原始数组使用的长度
for (int i = 0; i < articleSet.articles.length; i++) {
if (articleSet.articles[i] != null) {
count++;
}
}
//根据使用的长度临时新建一个数组,这个数组元素存满
Article[] newTemp = new Article[count];
// 吧就数组中的元素全部拷贝到新数组中,寻数组装满元素
for (int i = 0; i < count; i++) {
newTemp[i] = articleSet.articles[i];
}
// 排序(冒泡排序)
for (int i = 0; i < newTemp.length - 1; i++) { // 让所有元素参与排序
for (int j = 0; j < newTemp.length - i - 1; j++) { //让当前元素和他后面的元素对比
if (newTemp[j + 1] != null) {// 保证下一个要对比的元素存在
if (newTemp[j].number < newTemp[j + 1].number) {
//两个元素交换位置,需要借助第三方临时变量存储
Article temp = newTemp[j];
newTemp[j] = newTemp[j + 1];
newTemp[j + 1] = temp;
}
}
}
}
// 显示名次
System.out.println("名次:\t销售数量 \t 商品名称");
for (int i = 0; i < newTemp.length; i++) {
System.out.println(i + 1 + "\t" + newTemp[i].number + "\t" + newTemp[i].name);
}
}
//卖出商品
public void sell() {
boolean flag = true;
System.out.println("请输入你要卖出的商品的名字");
Scanner scanner = new Scanner(System.in);
String name = scanner.next();
for (int i = 0; i < articleSet.articles.length; i++) {
if (articleSet.articles[i] != null && articleSet.articles[i].name.equals(name)) {
System.out.println("请输入要卖出的数量");
int maichu = scanner.nextInt();
if (maichu < articleSet.articles[i].amount) { //卖出数量要小于库存数
//新库存等于就库存
articleSet.articles[i].amount = articleSet.articles[i].amount - maichu;
//新售出 = 就售出 + 卖出数量
articleSet.articles[i].number = articleSet.articles[i].number + maichu;
flag = true;
} else {
flag = false;
System.out.println("库存不够了");
}
break; //找到对应的位置,已经完成了修改,就绪的元素直接跳过,终端循环
} else {
flag = false;
}
}
if (flag) {
System.out.println("卖出成功");
} else {
System.out.println("卖出失败");
}
}
//查看商品信息
public void chakan() {
System.out.println("编号\t 名称 \t 库存 \t 价格 \t 售出数量");
for (int i = 0; i < articleSet.articles.length; i++) {
if (articleSet.articles[i] != null) {
articleSet.articles[i].print(i + 1);
}
}
}
//新增商品
public void add() {
//接受用户的输入
Scanner scanner = new Scanner(System.in);
System.out.println("请输入商品的名字:");
String name = scanner.next();
System.out.println("价格:");
double price = scanner.nextDouble();
System.out.println("库存");
int kucun = scanner.nextInt();
System.out.println("售出数量");
int number = scanner.nextInt();
//把接收到的数据封装到对象中
Article newArticle = new Article();
newArticle.setArticle(name, kucun, price, number);
for (int i = 0; i < articleSet.articles.length; i++) {
if (articleSet.articles[i] == null){ //从前往后遍历数组,找到第一个没有元素的位置
articleSet.articles[i] = newArticle;// 找到空位置,吧新商品存入
break; //后续的位置不加入
}
}
}
//删除商品
public void delete() {
boolean flag = true;
System.out.println("输入你要删除的商品编号");
Scanner scanner = new Scanner(System.in);
int delNo = scanner.nextInt();
for (int i = 0; i < articleSet.articles.length; i++) {
if (delNo == (i + 1) && articleSet.articles[i] != null) {
int j = i; // 备份要删除元素的下表
while (articleSet.articles[j + 1] != null) {
articleSet.articles[j] = articleSet.articles[j + 1];
}
articleSet.articles[j] = null;
flag = true;
j++;
break; // 操作完成 直接中断for循环 后续的null元素无需操作
} else {
flag = false;
}
}
if (flag) {
System.out.println("删除成功");
} else {
System.out.println("删除失败!");
}
}
}
测试类
public class Demo {
public static void main(String[] args) {
ArticleMange articleMange = new ArticleMange();
articleMange.inital();
articleMange.startMenu();
}
}