Articl
package com.company;
public class Articl {
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;
}
}
ArticleManage
package com.company;
import java.util.Scanner;
public class ArticleManage {
//创建一个实体的仓库对象,并初始化
ArticleSet articleSet = new ArticleSet();
// 初始化仓库, 放入起始商品
public void initial() {
articl xiaomi11 = new articl();
xiaomi11.setArticle("小米11", 30, 1999, 0);
articl xiaomi11pro = new articl();
xiaomi11pro.setArticle("小米11pro", 40, 2999, 0);
articl xiaomiUltra = new articl();
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;
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 ++;
}
}
// 根据使用的长度临时新建一个数组,这个数组元素存满
articl[] newTemp = new articl[count];
// 把旧数组中的元素全部拷贝到新数组中,新数组装满元素
for (int i =0 ; i< count ; i ++) {
newTemp[i] = articleSet.articles[i];
}
for ( int i= 0 ; i< newTemp.length; i++){
for ( int j = 0 ; j < newTemp.length -j ; j++){
if (newTemp[j].number< newTemp[j+1].number){
// 两个元素交换位置,需要借助第三方临时变量做存储
articl temp = newTemp[j];
newTemp[j]= newTemp[j+1];
newTemp[j+1] = temp;
}
}
}
}
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 = true;
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();
articl articl = new articl();
articl.setArticle(name, kucun, price, number);
for (int i = 0; i < articleSet.articles.length; i++) {
if (articleSet.articles[i] == null) {
articleSet.articles[i] = articl;
break;
}
}
}
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++) { //小米11 小米11pro 小米增强版
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;
System.out.println("你要卖出的商品没找到");
}
}
if (flag) {
System.out.println("卖出成功");
} else {
System.out.println("卖出失败");
}
}
}
ArticleSet
public class ArticleSet {
articl[] articles = new articl[30];
}
Dome
public class Demo {
public static void main(String[] args) {
ArticleManage articleManage = new ArticleManage();
articleManage.initial();
articleManage.startMenu();
}
}