/**
-
商品管理类
*/
public class ArticleManage {ArticleSet articleSet = new ArticleSet();
public void initial () {
Article xiyouji = new Article();
xiyouji.setArticle("西游记" , 10 , 1,0);
Article hongloumen = new Article();
hongloumen.setArticle("红楼梦",20 , 1, 0);
Article tutu = new Article();
tutu.setArticle("图图" ,10,1,0 );
articleSet.articles[0] = xiyouji;
articleSet.articles[1] = hongloumen;
articleSet.articles[2] = tutu;
}
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("新增图书");
add();
break;
case 2:
System.out.println("查看图书");
chakan();
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 ++ ;
}
}
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(){
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].number + maichu;
flag = true;
}else {
flag = false;
System.out.println("库存不够了");
}
break;
} else {
flag = false;
}
}
if(flag) {
System.out.println("卖出成功");
}else{
System.out.println("卖出失败");
}
}
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();
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;
}
}
}
}