系统述
查看商品信息概
新增商品信息
删除商品信息
卖出商品
商品销售排行榜
步骤1:初始化
package edu.xcdq;
/**
* @qvthor liuwenzheng
* @date 2021/4/6 18:43
*/
public class Article {
// 名字 单价 库存 已卖数量
public String name; //商品名字
public double price; //商品单价
public int amount; //商品库存数量
public int number; //商品销售数量
/**
* 商品信息展示
*/
public void print( int index ) {
System.out.println(index +"\t" + name + "\t" + price
+ "\t" + amount + "\t" + number );
}
public void setArticle(String mingzi , double danjia , int kucun , int yishou){
name = mingzi;
price = danjia;
amount = kucun;
number = yishou;
}
}
商品集合类
package edu.xcdq;
/**
* @qvthor liuwenzheng
* @date 2021/4/6 18:46
*/
public class ArticleSet {
Article[] articles = new Article [30];
}
商品管理类
/**
* @qvthor liuwenzheng
* @date 2021/4/6 18:44
*/
public class ArticleManage {
//创建一个库存,并且初始化
ArticleSet articleSet = new ArticleSet();
// 仓库初始化,放入一些商品
public void initial(){
Article xiaomi11 = new Article();
/*xiaomi11.name = "小米11";
xiaomi11.number = 30;
xiaomi11.amount = 0;
xiaomi11.price = 1999;*/
xiaomi11.setArticle("小米11",1999,30,0);
Article xiaomi11Pro = new Article();
xiaomi11Pro.setArticle("小米11pro",2999,10,0);
Article xiaomiUltra = new Article();
xiaomiUltra.setArticle("小米至尊版",3999,20,0);
articleSet.articles[0] = xiaomi11;
articleSet.articles[1] = xiaomi11Pro;
articleSet.articles[2] = xiaomiUltra;
}
步骤2:实现菜单转换
// 启动菜单
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 funNo = scanner.nextInt();
switch (funNo) {
case 1:
System.out.println("查看商品信息");
chakan();
break;
case 2:
System.out.println("2 新增商品信息");
add();
break;
case 3:
System.out.println("3 删除商品信息");
dele();
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 );
}
测试类
package edu.xcdq;
public class Main {
public static void main(String[] args) {
ArticleManage articleManage = new ArticleManage();
articleManage.initial();
articleManage.startMenu();
}
}
步骤3:实现查看商品信息
/**
* 查看商品信息
*/
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);
}
}
}
步骤4:实现新增商品信息
/**
* 添加商品信息
*/
private 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 count = scanner.nextInt();
System.out.println("请输入已卖的数量:");
int number = scanner.nextInt();
Article newArticle = new Article();
newArticle.setArticle(name , price ,count , number );
for ( int i = 0 ; i < articleSet.articles.length ; i++ ) {
if ( articleSet.articles[i] == null ) {
articleSet.articles[i] = newArticle; //把新建的对象放在数组中的第一个空位置
break; // 后续的空位置直接跳过
}
}
}
}
步骤5:实现删除商品信息
/**
* 删除商品信息
*/
private void dele(){
System.out.println("请输入你要删除的商品编号");
Scanner scanner = new Scanner(System.in);
int delNo = scanner.nextInt();
boolean falg = 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 ;
falg = true ;
break;
}else {
falg = false ;
}
}
if (falg){
System.out.println("删除成功");
}else {
System.out.println("删除失败 ");
}
}
步骤6:实现商品销售的业务处理
private 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].amount = articleSet.articles[i].amount + 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("卖出失败");
}
}
步骤7:实现商品销售排行榜
private void leaderboard(){
int cound = 0 ; //
for (int i = 0 ; i < articleSet.articles.length ; i++){
if (articleSet.articles[i] != null){
cound++ ;
}
}
//根据使用的长度临时新建一个数组,这个数组元素存满
Article[] newTemp = new Article[cound];
//把旧数组中的元素全部拷贝道新数组中,新数组装满元素
for (int i = 0 ; i < cound ; i++){
newTemp[i] = articleSet.articles[i];
}
//冒泡排序
for (int i = 0 ; i < newTemp.length-1 ; i++){ //所有元素参与排序
for (int j = 0 ; j < newTemp.length - i - 1 ; i ++){ //让当前元素和它后面的元素对比
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 ;
}
}
}
}
总代码
package edu.xcdq;
import java.util.Scanner;
/**
* @qvthor liuwenzheng
* @date 2021/4/6 18:44
*/
public class ArticleManage {
//创建一个库存,并且初始化
ArticleSet articleSet = new ArticleSet();
// 仓库初始化,放入一些商品
public void initial(){
Article xiaomi11 = new Article();
/*xiaomi11.name = "小米11";
xiaomi11.number = 30;
xiaomi11.amount = 0;
xiaomi11.price = 1999;*/
xiaomi11.setArticle("小米11",1999,30,0);
Article xiaomi11Pro = new Article();
xiaomi11Pro.setArticle("小米11pro",2999,10,0);
Article xiaomiUltra = new Article();
xiaomiUltra.setArticle("小米至尊版",3999,20,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 funNo = scanner.nextInt();
switch (funNo) {
case 1:
System.out.println("查看商品信息");
chakan();
break;
case 2:
System.out.println("2 新增商品信息");
add();
break;
case 3:
System.out.println("3 删除商品信息");
dele();
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 );
}
//排行榜
private void leaderboard(){
int cound = 0 ; //
for (int i = 0 ; i < articleSet.articles.length ; i++){
if (articleSet.articles[i] != null){
cound++ ;
}
}
//根据使用的长度临时新建一个数组,这个数组元素存满
Article[] newTemp = new Article[cound];
//把旧数组中的元素全部拷贝道新数组中,新数组装满元素
for (int i = 0 ; i < cound ; i++){
newTemp[i] = articleSet.articles[i];
}
//冒泡排序
for (int i = 0 ; i < newTemp.length-1 ; i++){ //所有元素参与排序
for (int j = 0 ; j < newTemp.length - i - 1 ; i ++){ //让当前元素和它后面的元素对比
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);
}
}
/**
* 卖出商品
*/
private 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].amount = articleSet.articles[i].amount + 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("卖出失败");
}
}
/**
* 删除商品信息
*/
private void dele(){
System.out.println("请输入你要删除的商品编号");
Scanner scanner = new Scanner(System.in);
int delNo = scanner.nextInt();
boolean falg = 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 ;
falg = true ;
break;
}else {
falg = false ;
}
}
if (falg){
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);
}
}
}
/**
* 添加商品信息
*/
private 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 count = scanner.nextInt();
System.out.println("请输入已卖的数量:");
int number = scanner.nextInt();
Article newArticle = new Article();
newArticle.setArticle(name , price ,count , number );
for ( int i = 0 ; i < articleSet.articles.length ; i++ ) {
if ( articleSet.articles[i] == null ) {
articleSet.articles[i] = newArticle; //把新建的对象放在数组中的第一个空位置
break; // 后续的空位置直接跳过
}
}
}
}