图书管理小程序(简易版)
我小学期选的是JAVA课,这是第三天上完课留得一个作业,实现如下功能






由于我还是初学者,所以就手撕了将近两个小时才写完整个项目(有时候写写就玩去了),整体功能是全部实现,代码如下:
1 package 小学期.day03; 2 3 import java.text.ParseException; 4 import java.text.SimpleDateFormat; 5 import java.util.*; 6 7 class BookInformation{ //图书信息类 8 String state = null; 9 String name = null; 10 String date = null; 11 12 BookInformation(){} //定义构造函数 13 BookInformation(String state, String name, String date){ 14 this.state = state; 15 this.name = name; 16 this.date = date; 17 } 18 } 19 20 class BookManger{ //图书管理类 21 22 Scanner input = new Scanner(System.in); //输入函数input 23 24 List<BookInformation> list = new ArrayList(50); //保存图书信息,把默认数组容量改为50 25 private final String[] choice = {"欢迎使用图书借阅系统", "-------------------------------------", "0. 借出排行榜", "1. 新增图书", 26 "2. 查看图书", "3. 删除图书", "4. 借出图书", "5. 归还图书", "6. 退出", 27 "--------------------------------------", "请选择:"}; //保存选项 2 3 4 5 6 7 8 28 29 public void initialize(){ //初始化图书信息,初始图书为三本 30 BookInformation B1 = new BookInformation("已借出", "数据结构", "2018-7-1"); 31 BookInformation B2 = new BookInformation("可借", "数据库", null); 32 BookInformation B3 = new BookInformation("可借", "离散数学", null); 33 list.add(B1); 34 list.add(B2); 35 list.add(B3); 36 } 37 38 public int showMenu(){ //菜单展示 39 for (String s : choice) { 40 System.out.println(s); 41 } 42 return input.nextInt(); 43 } 44 public void viewBooks(){ //查看图书函数 45 System.out.println("---> 查看图书"); 46 System.out.println("序号 状态 名称 借出日期"); 47 for (int count = 0;count < list.size();count++){ 48 System.out.println( count+1 + " \t" + list.get(count).state +" \t《" + list.get(count).name + "》 \t" + list.get(count).date); 49 } 50 } 51 public void addBooks() { //新增图书函数 52 System.out.print("---> 新增图书\n\n请输入图书名称:"); 53 String string = input.next(); 54 if (list.size() < 50) { 55 BookInformation B = new BookInformation("可借", string, null); 56 list.add(B); 57 System.out.println("新增《" + list.get(list.size()-1).name+ "》成功!"); 58 }else { 59 System.out.println("新增《" + list.get(list.size()-1).name+ "》失败!"); 60 } 61 } 62 public void deleteBooks() { //删除图书函数 63 System.out.print("---> 删除图书\n\n请输入图书名称:"); 64 String string = input.next(); 65 int flag = 1; //flag标记位 66 for(int count=0;count<list.size();count++){ 67 if (list.get(count).name.equals(string)) { 68 flag = 0; 69 if ("可借".equals(list.get(count).state)) { 70 System.out.println("删除《" + list.get(count).name + "》成功!"); 71 list.remove(count); 72 } else { 73 System.out.println("《" + list.get(count).name + "》为借出状态,不可被删除!"); 74 } 75 } 76 } 77 if(flag == 1){ 78 System.out.println("“没有找到匹配信息!"); 79 } 80 } 81 public void borrowBooks(){ //借出图书函数 82 System.out.print("---> 借出图书\n\n请输入图书名称:"); 83 String string1 = input.next(); 84 System.out.print("请输入借出日期(年-月-日):"); 85 String string2 = input.next(); 86 int flag = 1; //flag标记位 87 for (BookInformation bookInformation : list) { 88 if (bookInformation.name.equals(string1)) { 89 flag = 0; 90 if ("可借".equals(bookInformation.state)) { 91 System.out.println("借出《" + bookInformation.name + "》成功!"); 92 bookInformation.date = string2; 93 bookInformation.state = "已借出"; 94 } else { 95 System.out.println("《" + bookInformation.name + "》已被借出!"); 96 } 97 } 98 } 99 if(flag == 1){ 100 System.out.println("“没有找到匹配信息!"); 101 } 102 } 103 public void returnBook() throws ParseException { //归还图书函数 104 System.out.print("---> 归还图书\n\n请输入图书名称:"); 105 String string1 = input.next(); 106 System.out.print("请输入归还日期(年-月-日):"); 107 String string2 = input.next(); 108 int flag = 1; //flag标记位 109 for (BookInformation bookInformation : list) { 110 if (bookInformation.name.equals(string1)) { 111 flag = 0; 112 if ("已借出".equals(bookInformation.state)) { 113 System.out.println("归还《" + bookInformation.name + "》成功!"); 114 System.out.println("借出日期:" + bookInformation.date); 115 System.out.println("归还日期:" + string2); 116 117 //求相隔天数 118 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); 119 Date beginDate = format.parse(bookInformation.date); 120 Date endDate = format.parse(string2); 121 long day = (endDate.getTime() - beginDate.getTime()) / (24 * 60 * 60 * 1000); 122 System.out.println("应付租金(元):" + day); 123 bookInformation.date = null; 124 bookInformation.state = "可借"; 125 } else { 126 System.out.println("《" + bookInformation.name + "》该图书没有被借出!无法进行归还操作。"); 127 } 128 } 129 } 130 if(flag == 1){ 131 System.out.println("没有找到匹配信息!"); 132 } 133 } 134 public void quit(){ //退出程序 135 System.exit(0); //正常退出程序 136 } 137 } 138 139 public class 图书馆 { 140 public static void main(String[] args) throws ParseException { //类内主函数调用 141 Scanner input = new Scanner(System.in); 142 BookManger B1 = new BookManger(); //创建图书馆管理系统对象 143 B1.initialize(); //初始化 144 while(true) { 145 switch (B1.showMenu()) { 146 case 1 -> B1.addBooks(); 147 case 2 -> B1.viewBooks(); 148 case 3 -> B1.deleteBooks(); 149 case 4 -> B1.borrowBooks(); 150 case 5 -> B1.returnBook(); 151 case 6 -> B1.quit(); 152 } 153 System.out.print("**************************\n输入 0 返回:"); 154 input.nextInt(); 155 } 156 } 157 }

浙公网安备 33010602011771号