1:Article
package xcdq.sgs;
/**
* @author xcdq.SGS
* @date 2021/4/6 14:41
*/
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 seArticle(String mingzi, int kucun,double danjian, int shouchu){
name = mingzi;
amount = kucun;
price = danjian;
number = shouchu;
}
}
2:ArticleSet
package xcdq.sgs;
/**
* @author xcdq.SGS
* @date 2021/4/6 14:49
*/
public class ArticleSet {
Article[] articles = new Article[30];
}
3:ArticleManage
package xcdq.sgs;
/**
* @author xcdq.SGS
* @date 2021/4/6 14:52
*/
public class ArticleManage {
//创建一个实体的仓库对象,并初始化
ArticleSet articleSet = new ArticleSet();
//初始化仓库,放进起始商品
public void initial() {
Article xiaomi11 = new Article();
xiaomi11.seArticle("小米11", 30, 1999, 0);
Article xiaomi11pro = new Article();
xiaomi11pro.seArticle("小米11pro", 40, 2999, 0);
Article xiaomuUtre = new Article();
xiaomuUtre.seArticle("小米增强版", 50, 3999, 0);
articleSet.articles[0] = xiaomi11;
articleSet.articles[1] = xiaomi11pro;
articleSet.articles[2] = xiaomuUtre;
}
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 gongnengBiaohao = scanner.nextInt();
switch (gongnengBiaohao) {
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(" 排行榜");
break;
case 6:
System.out.println("感谢使用,已经退出");
flag = false;
break;
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 - 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 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.seArticle(name,kucun,price,number);
for (int i = 0; i < articleSet.articles.length; i++) {
if (articleSet.articles[i] == null){ // 从前往后遍历数组,找到一个没有存数据的位置
articleSet.articles[i] = newArticle; // 找到空位置,把新商品存入
break; //只加入第一个位置,后续的位置不再判断
}
}
}
private void delete() {
System.out.println("请输入你要删除的商品编号:");
Scanner scanner = new Scanner(System.in);
int deLNO = scanner.nextInt();
boolean flag = true;
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];
j++;
}
articleSet.articles[j] = null;
flag = false;
break; //操作完成,直接中断for循环,后续的null元素无需操作
} else {
flag = false;
}
}
if (flag) {
System.out.println("删除失败!");
} else {
System.out.println("删除成功!");
}
}
public void sell(){
System.out.println("请输入你要卖出商品的名字:");
Scanner scanner = new Scanner(System.in);
String name = scanner.next();
boolean flag = true;
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].amount + maichu;
flag = true;
} else {
flag = false;
System.out.println("库存不够了");
}
break; // 找到对应的位置,已经完成了修改,后续的元素直接跳过,中断循环
}else {
flag = false;
}
}
if (flag){
System.out.println("卖出成功");
}else {
System.out.println("卖出失败");
}
}
}
4:demo
package xcdq.sgs;
/**
* @author xcdq.SGS
* @date 2021/4/6 15:36
*/
public class demo {
public static void main(String[] args) {
ArticleManage articleManage = new ArticleManage();
articleManage.initial();
articleManage.startMenu();
}
}