我刚学Java不久这是我写的一个简单的控制台程序(图书管理)

作者在 2010-04-17 17:45:33 发布以下内容
import java.util.ArrayList;
import java.util.Scanner;


public class Prictice {
    static ArrayList admin=new ArrayList();
    static ArrayList book=new ArrayList();
    //初始化数据
    static void source(){
        ArrayList admin1=new ArrayList();
        admin1.add("abc");
        admin1.add("123");
        ArrayList admin2 =new ArrayList();
        admin2.add("cba");
        admin2.add("321");
        
        admin.add(admin1);
        admin.add(admin2);
        
        ArrayList book1=new ArrayList();
        book1.add("C#2.0宝典");
        book1.add("88");
        ArrayList book2=new ArrayList();
        book2.add("Java编程基础");
        book2.add("55");
        ArrayList book3=new ArrayList();
        book3.add("J2SE桌面应用程序开发");
        book3.add("60");
        ArrayList book4=new ArrayList();
        book4.add("数据库设计和应用");
        book4.add("45");
        ArrayList book5=new ArrayList();
        book5.add("水浒传");
        book5.add("55.5");
        ArrayList book6=new ArrayList();
        book6.add("红楼梦");
        book6.add("68");
        ArrayList book7=new ArrayList();
        book7.add("三国演义");
        book7.add("78");
        ArrayList book8=new ArrayList();
        book8.add("西游记");
        book8.add("46");
        
        book.add(book1);
        book.add(book2);
        book.add(book3);
        book.add(book4);
        book.add(book5);
        book.add(book6);
        book.add(book7);
        book.add(book8);
    }
    //登陆
    static boolean login(String userName,String userPw){
        boolean bool=false;
        for(int i=0;i<admin.size();i++){
            ArrayList user=(ArrayList)admin.get(i);
            String name=(String)user.get(0);
            String password=(String)user.get(1);
            if(userName.equals(name)&&userPw.equals(password)){
                bool=true;
                break;
            }
        }
        return bool;
    }
    //根据价格范围查找
    static ArrayList searchByPrice(float price_A,float price_B){
        ArrayList result=new ArrayList();
        for(int i=0;i<book.size();i++){
            ArrayList bookInfo=(ArrayList)book.get(i);
            String price=(String)bookInfo.get(1);
            float price1 = Float.parseFloat(price);
            
            if(price1>price_A &&price1<price_B){
                result.add(bookInfo);
            }
        }
        return result;
    }
    //根据图书种类查询
    static ArrayList searchBySort(int opt){
        ArrayList result=new ArrayList();
        if(opt==1){
            result.add(book.get(0));
            result.add(book.get(1));
            result.add(book.get(2));
            result.add(book.get(3));
        }else if(opt==2){
            result.add(book.get(4));
            result.add(book.get(5));
            result.add(book.get(6));
            result.add(book.get(7));
        }
        return result;
    }
    //添加
    static void addBook(String newBookName,String newBookPrice){
        ArrayList newBook=new ArrayList();
        newBook.add(newBookName);
        newBook.add(newBookPrice);
        book.add(newBook);
    }
    //删除(根据书名删)
    static void deletebook(String deleteBookName){
        
        for(int i=0;i<book.size();i++){
            ArrayList bookInfo=(ArrayList)book.get(i);
            String bookName=(String)bookInfo.get(0);
            if(bookName.equals(deleteBookName)){
                book.remove(i);
                break;
            }
        }
    }
    //要更改书价的书的原价
    static String oldPrice(String rePriBookName){
        String oldPri="";
        for(int i=0;i<book.size();i++){
            ArrayList bookInfo=(ArrayList)book.get(i);
            String bookName=(String)bookInfo.get(0);
            String pri=(String)bookInfo.get(1);
            if(bookName.equals(rePriBookName)){
                oldPri=pri;
                break;
            }
        }
        return oldPri;
    }
    //更改书价(根据书名)
    static void rejiggerPrice(String rePriBookName,String newBookPrice){
        int i;
        for(i=0;i<book.size();i++){
            ArrayList bookInfo=(ArrayList)book.get(i);
            String bookName=(String)bookInfo.get(0);
            if(bookName.equals(rePriBookName)){
                break;
            }
        }
        ArrayList bookInfo = (ArrayList) book.get(i);
        bookInfo.set(0, rePriBookName);
        bookInfo.set(1, newBookPrice);
        
        
        // 重新设置到book中
        book.set(i, bookInfo);
    }
    //输出
    static void showInfo (ArrayList book){
        for(int i=0;i<book.size();i++){
            ArrayList shu=(ArrayList)book.get(i);
            String bookName=(String)shu.get(0);
            String bookPrice=(String)shu.get(1);
            System.out.println(bookName+"\t"+bookPrice);
        }
    }
    public static void main(String[] args) {
        source();
        ArrayList result=new ArrayList();
        Scanner q=new Scanner(System.in);
        System.out.println("**********书店管理系统**********");
        System.out.println("管理员登陆");
        for(int i=1;;i++){
            boolean isLogin=false;
            System.out.print("请输入用户名:");
            String userName=q.next();
            System.out.print("请输入密码:");
            String userPw=q.next();
            isLogin=login(userName,userPw);
            if(isLogin){
                System.out.println("欢迎进入书店管理系统!!!");
                break;
            }else{
                System.out.println("账号或密码错误,请选择:");
                System.out.println("1.重新登陆\t2.退出");
                int opt=q.nextInt();
                if(opt==1){
                    continue;
                }else if(opt==2){
                    System.exit(0);
                }else{
                    System.out.println("选择错误!!!");
                    continue;
                }
            }
        }
        for(int t=0;;t++){
            
        System.out.println("请选择功能:");
        System.out.println("1.添加书籍\n2.查看书目\n3.删除书籍\n4.更改书价\n5.退出");
        int a=q.nextInt();
        if(a==1){
            //添加书籍
            for(int j=0;;j++){
                
            System.out.println("请输入要添加的书名:");
            String newBookName=q.next();
            System.out.println("请输入该书的价格:");
            String newBookPrice=q.next();
            addBook(newBookName,newBookPrice);
            showInfo(book);
            int opt;
            for(int k=0;;k++){
                
            System.out.println("请选择:");
            System.out.println("1.继续添加\t2.返回上一级\t3.退出");
             opt=q.nextInt();
            if(opt==1){
                break;
            }else if(opt==2){
                break;
            }else if(opt==3){
                System.exit(0);
            }else{
                System.out.println("选择错误!!!");
                continue;
            }
            }
            if(opt==2){
                break;
            }
            continue;
            }
            continue;
        }else if(a==2){
            for(int j=0;;j++){
                
            System.out.println("请选择查询方式:");
            System.out.println("1.按价格范围查询\n2.按图书类型查询\n3.返回上一级\n4.退出");
            int b=q.nextInt();
            if(b==1){
                //按价格
                System.out.print("请输入低价格:");
                float price_A=q.nextFloat();
                System.out.print("请输入高价格:");
                float price_B=q.nextFloat();
                result=searchByPrice(price_A,price_B);
                showInfo(result);
                for(int k=0;;k++){
                    
                System.out.println("请选择:");
                System.out.println("1.返回上一级\t2.退出");
                int opt=q.nextInt();
                if(opt==1){
                    break;
                }else if(opt==2){
                    System.exit(0);
                }else{
                    System.out.println("选择错误!!!");
                    continue;
                }
                }
                continue;
            }else if(b==2){
                //按类型
                for(int i=0;;i++){
                    
                System.out.println("请选择类型:");
                System.out.println("1.计算机类\n2.文学类\n3.返回上一级\n4.退出");
                int c=q.nextInt();
                if(c==1){
                    //计算机类
                    result=searchBySort(1);
                    showInfo(result);
                    for(int k=0;;k++){
                        
                    System.out.println("请选择:");
                    System.out.println("1.返回上一级\t2.退出");
                    int opt=q.nextInt();
                    if(opt==1){
                        break;
                    }else if(opt==2){
                        System.exit(0);
                    }else{
                        System.out.println("选择错误!!!");
                        continue;
                    }
                    }
                    continue;
                }else if(c==2){
                    //文学类
                    result=searchBySort(2);
                    showInfo(result);
                    for(int k=0;;k++){
                        
                    System.out.println("请选择:");
                    System.out.println("1.返回上一级\t2.退出");
                    int opt=q.nextInt();
                    if(opt==1){
                        break;
                    }else if(opt==2){
                        System.exit(0);
                    }else{
                        System.out.println("选择错误!!!");
                        continue;
                    }
                    }
                    continue;
                }else if(c==3){
                    //返回上一级
                    break;
                }else if(c==4){
                    //退出
                    System.exit(0);
                }else{
                    System.out.println("选择错误!!!");
                    continue;
                }
                }
                continue;
                
            }else if(b==3){
                break;
            }
            else if(b==4){
                //退出
                System.exit(0);
            }else{
                System.out.println("选择错误!!!");
                continue;
            }
            }
            continue;
        }else if(a==3){
            //删除书籍
            for(int j=0;;j++){
            System.out.println("请输入要删除的书的书名:");
            String deleteBookName=q.next();
            deletebook(deleteBookName);
            showInfo(book);
            int opt;
            for(int k=0;;k++){
                
            System.out.println("请选择:");
            System.out.println("1.继续删除\t2.返回上一级\t3.退出");
             opt=q.nextInt();
            if(opt==1){
                break;
            }else if(opt==2){
                break;
            }else if(opt==3){
                System.exit(0);
            }else{
                System.out.println("选择错误!!!");
                continue;
            }
            }
            if(opt==2){
                break;
            }
            continue;
            }
            continue;
        }else if(a==4){
            //更改书价
            for(int j=0;;j++){
                
            String pri="";
            System.out.println("请输入要更改书价的书的书名:");
            String rePriBookName=q.next();
            pri=oldPrice(rePriBookName);
            System.out.println("旧价格为:"+pri);
            System.out.println("请输入新价格:");
            String newBookPrice=q.next();
            rejiggerPrice(rePriBookName,newBookPrice);
            showInfo(book);
            int opt;
            for(int k=0;;k++){
                
            System.out.println("请选择:");
            System.out.println("1.继续更改\t2.返回上一级\t3.退出");
             opt=q.nextInt();
            if(opt==1){
                break;
            }else if(opt==2){
                break;
            }else if(opt==3){
                System.exit(0);
            }else{
                System.out.println("选择错误!!!");
                continue;
            }
            }
            if(opt==2){
                break;
            }
            continue;
            }
            continue;
        }else if(a==5){
            //退出
            System.exit(0);
        }else{
            System.out.println("选择错误!!!");
            continue;
        }
        continue;
        }
            
        }
    }

默认分类 | 阅读 958 次
文章评论,共9条
红色杀戮
2010-04-17 18:58
1
<img src="image/face/1.gif" class="face">刚学不久就能写出这么长的东东来,你真厉害啊。。。
爱OO你
2010-04-21 23:35
2
<img src="image/face/24.gif" class="face">是刚学的吗????
linhui10(作者)
2010-04-21 23:47
3
<div class="quote"><span class="q"><b>爱OO你</b>: <img src="image/face/24.gif" class="face">是刚学的吗????</span></div>学了大概二三十来节课吧!
JAVATWO
2010-04-22 11:31
4
有一套
爱OO你
2010-04-22 18:23
5
差不多了<br />
<img src="image/face/3.gif" class="face">
龙林
2010-04-25 22:33
6
你太猛了,刚学就写的出这样的了。天才
lixiaolou
2010-04-26 11:45
7
咋么我登陆不进去啊&nbsp;&nbsp;用户名和密码是啥啊
linhui10(作者)
2010-04-27 00:13
8
初始化数据里不有吗<br />
用户名:abc<br />
密码:123<br />
或<br />
用户名:cba<br />
密码:321
冷无霜
2010-05-10 11:11
9
厉害·····佩服····
游客请输入验证码
浏览1629次
文章分类
文章归档