商品后臺管理系統(tǒng)
系統(tǒng)概述
- 查看商品信息
- 新增商品信息
- 刪除商品信息
- 賣出商品
- 商品銷售排行榜
步驟1:初始化
package edu.xcdq;
/**
* @qvthor mazhongyuan
* @date 2021/4/6 18:43
*/
public class Article {
// 名字 單價 庫存 已賣數(shù)量
public String name; //商品名字
public double price; //商品單價
public int amount; //商品庫存數(shù)量
public int number; //商品銷售數(shù)量
/**
* 商品信息展示
*/
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 mazhongyuan
* @date 2021/4/6 18:46
*/
public class ArticleSet {
Article[] articles = new Article [30];
}
商品管理類
/**
* @qvthor mazhongyuan
* @date 2021/4/6 18:44
*/
public class ArticleManage {
//創(chuàng)建一個庫存,并且初始化
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:實現(xiàn)菜單轉(zhuǎn)換
// 啟動菜單
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:實現(xiàn)查看商品信息
/**
* 查看商品信息
*/
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:實現(xiàn)新增商品信息
/**
* 添加商品信息
*/
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("請輸入已賣的數(shù)量:");
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; //把新建的對象放在數(shù)組中的第一個空位置
break; // 后續(xù)的空位置直接跳過
}
}
}
}
步驟5:實現(xiàn)刪除商品信息
/**
* 刪除商品信息
*/
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; //備注下標(biāo)
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:實現(xiàn)商品銷售的業(yè)務(wù)處理
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("請輸入要賣出的數(shù)量");
int maichu = scanner.nextInt();
if (maichu < articleSet.articles[i].amount){ //賣出數(shù)量 < 庫存數(shù)
// 新庫存 = 舊庫存 - 賣出數(shù)量
articleSet.articles[i].amount = articleSet.articles[i].amount -maichu;
//新售出 = 舊售出 + 賣出數(shù)量
articleSet.articles[i].amount = articleSet.articles[i].amount + maichu;
flag = true ;
}else {
flag = false ;
System.out.println("庫存不夠了");
}
break; //找到相對應(yīng)的位置酥泛,已經(jīng)完成了修改月趟,后續(xù)的元素直接跳過杨幼,中端循環(huán)
}else {
flag = false;
//System.out.println("你要賣出的商品沒找到");
}
}
if (flag){
System.out.println("賣出成功");
}else {
System.out.println("賣出失敗");
}
}
步驟7:實現(xiàn)商品銷售排行榜
private void leaderboard(){
int cound = 0 ; //
for (int i = 0 ; i < articleSet.articles.length ; i++){
if (articleSet.articles[i] != null){
cound++ ;
}
}
//根據(jù)使用的長度臨時新建一個數(shù)組撇簿,這個數(shù)組元素存滿
Article[] newTemp = new Article[cound];
//把舊數(shù)組中的元素全部拷貝道新數(shù)組中,新數(shù)組裝滿元素
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 ++){ //讓當(dāng)前元素和它后面的元素對比
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 mazhongyuan
* @date 2021/4/6 18:44
*/
public class ArticleManage {
//創(chuàng)建一個庫存四瘫,并且初始化
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++ ;
}
}
//根據(jù)使用的長度臨時新建一個數(shù)組找蜜,這個數(shù)組元素存滿
Article[] newTemp = new Article[cound];
//把舊數(shù)組中的元素全部拷貝道新數(shù)組中,新數(shù)組裝滿元素
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 ++){ //讓當(dāng)前元素和它后面的元素對比
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 銷售數(shù)量 \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("請輸入要賣出的數(shù)量");
int maichu = scanner.nextInt();
if (maichu < articleSet.articles[i].amount){ //賣出數(shù)量 < 庫存數(shù)
// 新庫存 = 舊庫存 - 賣出數(shù)量
articleSet.articles[i].amount = articleSet.articles[i].amount -maichu;
//新售出 = 舊售出 + 賣出數(shù)量
articleSet.articles[i].amount = articleSet.articles[i].amount + maichu;
flag = true ;
}else {
flag = false ;
System.out.println("庫存不夠了");
}
break; //找到相對應(yīng)的位置,已經(jīng)完成了修改彰居,后續(xù)的元素直接跳過诚纸,中端循環(huán)
}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; //備注下標(biāo)
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("請輸入已賣的數(shù)量:");
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; //把新建的對象放在數(shù)組中的第一個空位置
break; // 后續(xù)的空位置直接跳過
}
}
}
}