zoukankan      html  css  js  c++  java
  • 面向对象基础项目----图书管理系统(数组)

    登录: 判断用户输入的帐号密码是否正确。

    1 用户平台:1.1图书信息查询;1.2图书购买;1.3退出

    2 管理平台:2.1图书入库;2.2图书出库;2.3增加图书

    3 管理员:3.1会员信息查询;3.2会员信息增加;3.3会员信息修改;3.4会员信息删除;3.5退出

     

     

     

     

     User.class

     1 package com.jianglai.books;
     2 
     3 /**
     4  * 用户类
     5  * @author jl
     6  *
     7  */
     8 public class User {
     9 
    10     private String username;
    11     private String pwd;
    12     private int type;//1代表用户,2代表管理员
    13 
    14     public User() {
    15         super();
    16         // TODO Auto-generated constructor stub
    17     }
    18 
    19     
    20     
    21     public User(String username, String pwd, int type) {
    22         super();
    23         this.username = username;
    24         this.pwd = pwd;
    25         this.type = type;
    26     }
    27 
    28 
    29 
    30     @Override
    31     public String toString() {
    32         return "User [username=" + username + ", pwd=" + pwd + ", type=" + type + "]";
    33     }
    34 
    35     public int getType() {
    36         return type;
    37     }
    38 
    39     public void setType(int type) {
    40         this.type = type;
    41     }
    42 
    43     public String getUsername() {
    44         return username;
    45     }
    46 
    47     public void setUsername(String username) {
    48         this.username = username;
    49     }
    50 
    51     public String getPwd() {
    52         return pwd;
    53     }
    54 
    55     public void setPwd(String pwd) {
    56         this.pwd = pwd;
    57     }
    58 
    59 }

    Book.class

      1 package com.jianglai.books;
      2 
      3 /**
      4  * 书类
      5  * @author jl
      6  *
      7  */
      8 public class Book {
      9 
     10     private int book_Id;
     11     private String book_name;
     12     private String book_author;
     13     private String book_date;
     14     private double book_price;
     15     private int book_count;
     16     
     17     
     18     
     19     public Book() {
     20         super();
     21         // TODO Auto-generated constructor stub
     22     }
     23 
     24 
     25 
     26     public Book(int book_Id, String book_name, String book_author, String book_date, double book_price,
     27             int book_count) {
     28         super();
     29         this.book_Id = book_Id;
     30         this.book_name = book_name;
     31         this.book_author = book_author;
     32         this.book_date = book_date;
     33         this.book_price = book_price;
     34         this.book_count = book_count;
     35     }
     36 
     37 
     38     
     39 
     40     public int getBook_Id() {
     41         return book_Id;
     42     }
     43 
     44 
     45 
     46     public void setBook_Id(int book_Id) {
     47         this.book_Id = book_Id;
     48     }
     49 
     50 
     51 
     52     public String getBook_name() {
     53         return book_name;
     54     }
     55 
     56 
     57 
     58     public void setBook_name(String book_name) {
     59         this.book_name = book_name;
     60     }
     61 
     62 
     63 
     64     public String getBook_author() {
     65         return book_author;
     66     }
     67 
     68 
     69 
     70     public void setBook_author(String book_author) {
     71         this.book_author = book_author;
     72     }
     73 
     74 
     75 
     76     public String getBook_date() {
     77         return book_date;
     78     }
     79 
     80 
     81 
     82     public void setBook_date(String book_date) {
     83         this.book_date = book_date;
     84     }
     85 
     86 
     87 
     88     public double getBook_price() {
     89         return book_price;
     90     }
     91 
     92 
     93 
     94     public void setBook_price(double book_price) {
     95         this.book_price = book_price;
     96     }
     97 
     98 
     99 
    100     public int getBook_count() {
    101         return book_count;
    102     }
    103 
    104 
    105 
    106     public void setBook_count(int book_count) {
    107         this.book_count = book_count;
    108     }
    109 
    110 
    111 
    112     @Override
    113     public String toString() {
    114         return "Book [book_Id=" + book_Id + ", book_name=" + book_name + ", book_author=" + book_author + ", book_date="
    115                 + book_date + ", book_price=" + book_price + ", book_count=" + book_count + "]";
    116     }
    117     
    118     
    119 }

    DataList.class

     1 package com.jianglai.books;
     2 
     3 import java.util.Arrays;
     4 
     5 /**
     6  * 数据管理类
     7  * @author jl
     8  *
     9  */
    10 public class DataList {
    11 
    12 
    13     
    14     private User[] userList;
    15     private Book[] bookList;
    16 
    17     public DataList() {
    18         //初始化一个用户数组,并赋值
    19         this.userList = new User[]{
    20                 new User("user","user",1),
    21                 new User("jl", "000000", 2),
    22                 new User("admin", "admin", 0),
    23                 new User("root", "root", 0)};
    24         
    25         //初始化一个书的数组,并赋值
    26         this.bookList = new Book[]{
    27                 new Book(10001, "鬼吹灯", "天下霸唱", "2008-7-1", 27.8, 87),
    28                 new Book(10002, "魔戒1", "莫尔斯","2005-3-14", 54, 13),
    29                 new Book(10003, "华丽波特1", "罗琳","2003-2-14", 43.5, 99),
    30                 new Book(10004, "时间机器", "威尔","1965-3-3", 21.3, 75),
    31                 new Book(10005, "宇宙奥秘", "霍金","2003-12-23", 87.3, 14),};
    32     }
    33     
    34     public DataList(User[] userList, Book[] bookList) {
    35         super();
    36         this.userList = userList;
    37         this.bookList = bookList;
    38         
    39     }
    40     public User[] getUserList() {
    41         return userList;
    42     }
    43     public void setUserList(User[] userList) {
    44         this.userList = userList;
    45     }
    46     public Book[] getBookList() {
    47         return bookList;
    48     }
    49     public void setBookList(Book[] bookList) {
    50         this.bookList = bookList;
    51     }
    52     
    53     
    54     @Override
    55     public String toString() {
    56         return "DataList [userList=" + Arrays.toString(userList) + ", bookList=" + Arrays.toString(bookList) + "]";
    57     }
    58     
    59     
    60     
    61     
    62 
    63 }

    userManager.class

      1 package com.jianglai.books;
      2 
      3 import java.util.Arrays;
      4 
      5 /**
      6  * 用户管理类   ---   实现各种方法。
      7  * @author jl
      8  *
      9  */
     10 public class UserManager {
     11     
     12     
     13     DataList dl = new DataList();
     14     
     15     
     16     /**
     17      * 登录方法--  查询数据,返回查到的用户信息。
     18      * @param username   用户名
     19      * @param pwd           密码
     20      * @return            返回一个用户类,好用来判断登录角色
     21      */
     22     public User login(String username,String pwd) {
     23         User user = new User();
     24         for (int i = 0; i < dl.getUserList().length; i++) {
     25             if(username.equals(dl.getUserList()[i].getUsername())&&pwd.equals(dl.getUserList()[i].getPwd())){
     26                 return dl.getUserList()[i];
     27             }
     28         }
     29         
     30         return user;
     31     }
     32     
     33 
     34     /**
     35      *     用户类中查找整个书单列表
     36      */
     37     public void findBookList() {
     38         System.out.println("书号		书名		作者		发布日期			价格		库存");
     39         for (int i = 0; i < dl.getBookList().length; i++) {
     40             System.out.println(dl.getBookList()[i].getBook_Id()+"		"+dl.getBookList()[i].getBook_name()+"		"+dl.getBookList()[i].getBook_author()
     41                     +"		"+dl.getBookList()[i].getBook_date()+"		"+dl.getBookList()[i].getBook_price()+"		"+dl.getBookList()[i].getBook_count());
     42         }
     43     }
     44 
     45 
     46     /**
     47      *   购买图书
     48      * @param book_Id            购买图书的id
     49      * @param book_Count        购买图书的数量
     50      */
     51     public void moneyCount(int book_Id, int book_Count) {
     52         
     53         for (int i = 0; i < dl.getBookList().length; i++) {
     54             if(book_Id == dl.getBookList()[i].getBook_Id()){
     55                 dl.getBookList()[i].setBook_count(dl.getBookList()[i].getBook_count()-book_Count);
     56                 System.out.println(dl.getBookList()[i].getBook_name()+":"+
     57                 dl.getBookList()[i].getBook_price()+"
    数量:"+book_Count+"
    总计:"+(book_Count*dl.getBookList()[i].getBook_price()));
     58             }
     59         }
     60         
     61     }
     62 
     63 
     64     /**
     65      *    管理员查询用户全部信息的界面。
     66      */
     67     public void findUserList() {
     68         
     69         System.out.println("会员帐号			会员密码			会员角色");
     70         for (int i = 0; i < dl.getUserList().length; i++) {
     71             System.out.println(dl.getUserList()[i].getUsername()+"			"+dl.getUserList()[i].getPwd()+"			"
     72                             +dl.getUserList()[i].getType());
     73         }
     74         
     75     }
     76     
     77     /**
     78      *    注册会员功能
     79      * @param newUserName    新会员用户名
     80      * @param newPwd        新会员密码
     81      * @param newType        新会员类型(1-用户、2-管理员)
     82      */
     83 
     84 
     85     public void addUser(String newUserName, String newPwd, int newType) {
     86         
     87         User newUser = new User(newUserName, newPwd, newType);
     88         
     89         User[] userList = new User[dl.getUserList().length+1];
     90 
     91         int index = 0 ;
     92         
     93         for (int i = 0; i < userList.length-1; i++) {
     94             userList[index] = dl.getUserList()[i];
     95             index++;
     96         }
     97         
     98         userList[userList.length-1] = newUser;
     99         
    100         dl.setUserList(userList);
    101         
    102         for (int i = 0; i < userList.length; i++) {
    103             System.out.println(userList[i].getUsername()+"			"+userList[i].getPwd()+"			"+userList[i].getType());
    104         }
    105         
    106     }
    107 
    108 
    109     /**
    110      *     修改会员功能
    111      * @param uName
    112      * @param uType
    113      */
    114     public void updateUser(String uName, int uType) {
    115         for (int i = 0; i < dl.getUserList().length; i++) {
    116             if(uName.equals(dl.getUserList()[i].getUsername())){
    117                 System.out.println(dl.getUserList()[i]);
    118                 dl.getUserList()[i].setType(uType);
    119             }
    120         }
    121         
    122     }
    123 
    124 
    125     /**
    126      *         删除用户信息
    127      * @param uName        需要删除的用户信息的用户名
    128      */
    129     public void delUser(String uName) {
    130         // TODO Auto-generated method stub
    131         
    132         int index = dl.getUserList().length-1;
    133         User[] userList = new User[dl.getUserList().length-1];
    134         
    135         for (int i = 0; i < dl.getUserList().length; i++) {
    136             if(uName.equals(dl.getUserList()[i].getUsername())){
    137                 if(dl.getUserList()[i].getType()!=0){
    138                     index = i;
    139                     break;
    140                 }else {
    141                     System.out.println("您不能删除超级管理员用户!");
    142                     return;
    143                 }
    144 //                index = i;
    145             }
    146         }
    147         
    148         User[] uArr1 = Arrays.copyOf(dl.getUserList(), index);
    149         User[] uArr2 = Arrays.copyOfRange(dl.getUserList(), index+1, dl.getUserList().length);
    150         
    151         int count = 0;
    152         
    153         for (int i = 0; i < uArr1.length; i++) {
    154             userList[i] = uArr1[i];
    155             count++;
    156         }
    157 //        System.out.println(count);
    158         for (int i = 0; i < userList.length-uArr1.length; i++) {
    159             userList[count] = uArr2[i];
    160             count++;
    161         }
    162         
    163         dl.setUserList(userList);
    164         findUserList();
    165         
    166     }
    167     
    168     
    169     
    170     
    171 }

    bookManager.class

     1 package com.jianglai.books;
     2 
     3 import java.util.Arrays;
     4 
     5 /**
     6  *    图书管理类
     7  * 
     8  * @author jl
     9  *
    10  */
    11 public class BookManager {
    12 
    13     DataList dl = new DataList();
    14     /**
    15      *         管理员类中查找所有图书信息的方法
    16      */
    17     public void findBookList() {
    18         System.out.println("书号		书名		作者		发布日期			价格		库存");
    19         for (int i = 0; i < dl.getBookList().length; i++) {
    20             System.out.println(dl.getBookList()[i].getBook_Id()+"		"+dl.getBookList()[i].getBook_name()+"		"+dl.getBookList()[i].getBook_author()
    21                     +"		"+dl.getBookList()[i].getBook_date()+"		"+dl.getBookList()[i].getBook_price()+"		"+dl.getBookList()[i].getBook_count());
    22         }
    23     }
    24     
    25     /**
    26      *  图书入库的方法
    27      * @param book_Id    需要增加数量的图书的id
    28      * @param book_Count 增加图书的数量
    29      */
    30     public void addBookCount(int book_Id,int book_Count) {
    31         
    32         Book[] bookList = dl.getBookList();
    33         
    34         for (int i = 0; i < dl.getBookList().length; i++) {
    35             if(book_Id == dl.getBookList()[i].getBook_Id()){
    36                 bookList[i].setBook_count(bookList[i].getBook_count()+book_Count);
    37             }
    38         }
    39         
    40     }
    41 
    42     /**
    43      *  图书出库的方法
    44      * @param book_Id     需要删除数量的图书的id
    45      * @param book_Count  删除图书的数量
    46      */
    47     public void delBookCount(int book_Id,int book_Count) {
    48 
    49         Book[] bookList = dl.getBookList();
    50 
    51         for (int i = 0; i < dl.getBookList().length; i++) {
    52             if (book_Id == dl.getBookList()[i].getBook_Id()) {
    53                 bookList[i].setBook_count(bookList[i].getBook_count() - book_Count);
    54             }
    55         }
    56     }
    57     
    58 
    59     /**
    60      *     新增图书
    61      * @param book_name            增加图书的图书名
    62      * @param book_author        增加图书的作者
    63      * @param book_date            增加图书的出版日期
    64      * @param book_price        增加图书的价格
    65      * @param book_count        增加图书的数量
    66      */
    67     public void addBookList(String book_name, String book_author, String book_date, double book_price, int book_count) {
    68         
    69         int book_Id = 10000+(dl.getBookList().length+1);
    70         
    71         int index = 0;
    72         Book newBook = new Book(book_Id, book_name, book_author, book_date, book_price, book_count);
    73         
    74         Book[] bookList = new Book[dl.getBookList().length+1];
    75         
    76         for (int i = 0; i < dl.getBookList().length; i++) {
    77             bookList[index] = dl.getBookList()[index];
    78             index ++;
    79         }
    80         
    81         bookList[dl.getBookList().length] = newBook;
    82         
    83         
    84         dl.setBookList(bookList); 
    85         
    86         System.out.println("书号		书名		作者		发布日期			价格		库存");
    87         for (int i = 0; i < bookList.length; i++) {
    88             System.out.println(bookList[i].getBook_Id()+"		"+bookList[i].getBook_name()+"		"+bookList[i].getBook_author()
    89                     +"		"+bookList[i].getBook_date()+"		"+bookList[i].getBook_price()+"		"+bookList[i].getBook_count());
    90         }
    91 
    92     }
    93 }

     Test.class

      1 package com.jianglai.books;
      2 
      3 import java.util.Scanner;
      4 
      5 public class Test {
      6     
      7     public static void main(String[] args) {
      8         
      9         UserManager um = new UserManager();
     10         BookManager bm = new BookManager();
     11         
     12         Scanner in = new Scanner(System.in);
     13         
     14         DataList dl = new DataList();
     15         
     16         int num =0;//操作选择计数器
     17         
     18         
     19         while (true) {
     20             
     21             System.out.println("************欢迎使用图书书店************");
     22             //1.输入用户名和密码进行判断登录
     23             System.out.print("请输入用户名:");
     24             String username = in.next();
     25             System.out.print("请输入密码:");
     26             String pwd = in.next();
     27             
     28             //2.用户登录
     29             User u = um.login(username, pwd);
     30             
     31             //判断用户的属性
     32             if(u.getType() == 1){//用户操作界面
     33                 
     34                 System.out.println("************欢迎光临图书系统************");
     35                 
     36                 //2-1-1.用户显示书目列表
     37                 um.findBookList();
     38                 System.out.println();
     39                 
     40                 
     41                 int book_Id;// 书的ID
     42                 int book_Count;// 数的数量
     43                 
     44                 
     45                 boolean flage = true;
     46                 
     47                 while (flage) {
     48                     System.out.println("请选择进行的操作:1.查看图书	2.结账	3.退出");
     49                     num = in.nextInt();
     50                     
     51                     switch (num) {
     52                     case 1:
     53                         um.findBookList();
     54                         break;
     55                     case 2:
     56                         System.out.println("请输入想要购买的图书信息-->");
     57                         System.out.print("请输入图书ID:");
     58                         book_Id = in.nextInt();
     59                         System.out.print("请输入出库的数量:");
     60                         book_Count = in.nextInt();
     61                         
     62                         //购买图书,并且计算图书价格再显示
     63                         um.moneyCount(book_Id,book_Count);
     64                         
     65                         break;
     66                     case 3:
     67                         System.out.println("感谢使用,退出系统。");
     68                         flage = false;
     69                         break;
     70                     default:
     71                         System.out.println("您的输入有误!");
     72                         break;
     73                     }
     74                     
     75                 }
     76                 
     77                 
     78             }else if(u.getType()==2){//管理员操作界面
     79                 
     80                 System.out.println("************欢迎登录库存管理系统************");
     81 
     82                 // 2-2-1.管理员显示书目列表
     83                 bm.findBookList();
     84                 System.out.println();
     85 
     86                 boolean flag = true;
     87                 
     88                 while (flag) {
     89 
     90                     // 2-2-2.管理员操作
     91                     System.out.println("请选择进行的操作:1.图书入库	2.图书出库	3.查询全部图书	4.新增图书	5.退出");
     92                     num = in.nextInt();
     93 
     94                     int book_Id;// 书的ID
     95                     int book_Count;// 数的数量
     96 
     97                     String book_name;
     98                     String book_author;
     99                     String book_date;
    100                     double book_price;
    101                     int book_count;
    102 
    103                     switch (num) {
    104                     case 1:
    105                         System.out.println("请输入图书信息-->");
    106                         System.out.print("请输入图书ID:");
    107                         book_Id = in.nextInt();
    108                         System.out.print("请输入入库的数量:");
    109                         book_Count = in.nextInt();
    110                         // 增加图书数量
    111                         bm.addBookCount(book_Id, book_Count);
    112 
    113                         bm.findBookList();
    114 
    115                         break;
    116                     case 2:
    117 
    118                         System.out.println("请输入图书信息-->");
    119                         System.out.print("请输入图书ID:");
    120                         book_Id = in.nextInt();
    121                         System.out.print("请输入出库的数量:");
    122                         book_Count = in.nextInt();
    123                         // 删除图书数量
    124                         bm.delBookCount(book_Id, book_Count);
    125 
    126                         bm.findBookList();
    127                         break;
    128                     case 3:
    129                         bm.findBookList();
    130                         break;
    131                     case 4:
    132                         System.out.println("请输入图书信息-->");
    133                         System.out.print("请输入书名:");
    134                         book_name = in.next();
    135                         System.out.print("请输入作者:");
    136                         book_author = in.next();
    137                         System.out.print("请输入出版时间:");
    138                         book_date = in.next();
    139                         System.out.print("请输入价格:");
    140                         book_price = in.nextDouble();
    141                         System.out.print("请输入库存:");
    142                         book_count = in.nextInt();
    143                         
    144                         //新增图书
    145                         bm.addBookList(book_name, book_author, book_date, book_price, book_count);
    146 
    147                         break;
    148                     case 5:
    149                         System.out.println("感谢使用,退出系统。");
    150                         flag = false;
    151                         break;
    152 
    153                     default:
    154                         System.out.println("您的输入有误!");
    155                         break;
    156                     }
    157 
    158                 }
    159                 
    160             }else if (u.getType()==0 && u.getUsername()!=null) {//超级管理员界面
    161                 System.out.println("************欢迎使用管理员系统************");
    162                 
    163                 //查询所有用户
    164                 um.findUserList();
    165                 System.out.println();
    166                 
    167                 //新增用户的信息
    168                 String newUserName;
    169                 String newPwd;
    170                 int newType;
    171                 
    172                 //修改用户
    173                 String uName = null;
    174                 int uType = 0;
    175                 
    176                 
    177                 boolean flag = true;
    178                 
    179                 //2-0超级管理员的操作
    180                 while (flag) {
    181                     System.out.println("请选择进行的操作:1.查看用户列表	2.注册新用户	3.修改用户	4.删除用户	5.退出");
    182                     num = in.nextInt();
    183                     
    184                     switch (num) {
    185                     case 1:
    186                         um.findUserList();
    187                         break;
    188                     case 2:
    189                         System.out.println("请输入注册用户信息-->");
    190                         System.out.print("请输出用户名:");
    191                         newUserName = in.next();
    192                         System.out.print("请输入密码:");
    193                         newPwd = in.next();
    194                         System.out.print("请输入会员角色类型(1-用户、2-管理员):");
    195                         newType = in.nextInt();
    196                         
    197                         um.addUser(newUserName,newPwd,newType);
    198                         
    199                         break;
    200                     case 3:
    201                         System.out.println("请输入选择用户信息-->");
    202                         System.out.print("请选择用户名:");
    203                         uName = in.next();
    204                         System.out.print("请修改会员角色类型(1-用户、2-管理员):");
    205                         uType = in.nextInt();
    206                         
    207                         //修改用户的角色
    208                         um.updateUser(uName,uType);
    209                         um.findUserList();
    210                         break;
    211                     case 4:
    212                         
    213                         System.out.println("请输入要删除的用户信息-->");
    214                         System.out.println("请输入要删除的用户的用户名:");
    215                         
    216                         uName = in.next();
    217                         
    218                         um.delUser(uName);
    219                         break;
    220                     case 5:
    221                         System.out.println("感谢使用,退出系统。");
    222                         flag = false;
    223                         break;
    224                     default:
    225                         System.out.println("您的输入有误!");
    226                         break;
    227                     }
    228                     
    229                 }
    230                 
    231                 
    232             }else if (u.getUsername()== null) {//用户名或者密码输入错误的情况
    233                 System.err.println("用户名或密码错误,请核对后重新启动系统进行登录!");
    234             }
    235         }
    236         
    237     }
    238 
    239 }
  • 相关阅读:
    IE绝对定位元素神秘消失或被遮挡的解决
    14条最佳JS代码编写技巧
    Query中的$.getJSON
    理解JavaScript中的对象
    Javascript的IE和Firefox兼容性汇编
    window.location 与 window.location.href
    认识hasLayout主要IE浏览器css bug的一大罪恶根源
    C/C++版数据结构之排序算法
    C++关键字mutable
    C++关键字explicit
  • 原文地址:https://www.cnblogs.com/JiangLai/p/8675136.html
Copyright © 2011-2022 走看看