Java -- book lending system

Posted by .evo. on Sat, 22 Feb 2020 07:25:20 +0100

**

Project requirements:

**
Develop a book borrowing system for the book reading room, which can store up to 50 books and realize the management of books. The book borrowing system has the following functions:
1. View Book Information
Select View function in the menu to display relevant information of all current books. The effect is as follows.

case 2:
				System.out.println("--->View books");
				System.out.println("Serial number\t state\t Name\t\t Lending date");
				for(int i = 0;names[i] != null;i ++){
					String situation = (states[i] == 0)?"Lent out":"May borrow";
					System.out.print((i+1) + "\t" + situation + "\t<" + names[i] + ">\t");
					if(states[i] == 0){
						System.out.println(dates[i]);
					}else{
						System.out.println();
					}
				}
				System.out.println("*************************");
				break;

2. new book information
Select the new function in the menu, input the new book information according to the prompt, and add to the inventory: the effect is as follows. If the book shelf is full, i.e. up to 50 books, you will be prompted to add the failed information.

case 1:
				System.out.println("--->New books");
				int a = 0;
				for(;a < names.length;a ++){
					if(names[a] == null){
					System.out.print("Please enter the book name:");
				    names[a]= input.next();//Enter the title of the book
					System.out.println("\n" + "Newly added" +names[a] +">Success!");
					
					//Store book information in the array
					states[a] = 1;
					times[a] = 0;
					dates[a] = null;
					break;
					}
				}
				if(a == 50){
					System.out.println("Shelf full, failed to add");
				}
				System.out.println("*************************");
				break;

3. Delete book information
Execute the "delete" command, enter the name of the book to be deleted and delete it. The effect is as follows. If the book is in lending status, deletion is not allowed. If the book information is not found in the book list, the prompt: "no matching information found!"


case 3:
				System.out.println("--->Delete books");
				System.out.println("Please enter the book name");
				String book = input.next();
				boolean check1 = false;//Determine whether the bibliography to be deleted is found. false means no bibliography is found
				for(int b = 0;names[b] != null;b ++){
					if(names[b].equals(book)){
						check1 = true;
						if(states[b] == 1){//Books are not lent, can be deleted
							System.out.println("Delete "" +book + ">Success!");
							int i = b;
							for(;i <names.length-1;i ++){
								states[i] = states[i+1];//The third book is not lent
								names[i] = names[i+1];
								dates[i] = dates[i+1];
								times[i] = times[i+1];
							}//Move array contents forward
							
							//Initialize the location of the last book
							states[i] = 1;//The third book is not lent
							names[i] = null;
							dates[i] = null;
							times[i] = 0;
							break;
						}else{
							System.out.println("The book has been lent and cannot be deleted!");
							break;
						}
				    }
				}
				if(check1 == false){
					System.out.println("No matching information found!");
				}
				System.out.println("*************************");
				break;

4. Lending books
Execute the "lend" command to realize the lending of books. The effect is as follows. If the book has been lent, the system will prompt "XXX has been lent". If the book information is not found, the system will prompt: "no matching information found!" The lending date is entered in the format (year month day).

case 4:
				System.out.println("--->Lending books");
				System.out.println("Please enter the book name:");
				String lend = input.next();
				boolean check2 = false;//Judge whether the book you want to lend can be found. false means not found, and true means found
				for(int b = 0;names[b] != null;b ++){
					if(names[b].equals(lend)){//If the book exists
						check2 = true;//Books you want to borrow can be found
						if(states[b] == 1){//If the book is not lent
							System.out.println("Please enter the lending date (year-month-Day):");
							dates[b] = input.next();
							while(judge(dates[b]) == false){
								System.out.println("Illegal date, please re-enter!");
								dates[b] = input.next();
							}
							states[b] = 0;//Convert the current book status to lending
							times[b] ++;//Current book lending times plus one
						}else{//If the book has been lent
							System.out.println(names[b] + "Has been lent!");						
						}
						break;
					}
				}
				if(check2 == false){
					System.out.println("No matching information found!");
				}
				System.out.println("*************************");
				break;

5. Return books
Execute the "return" command to return the books and calculate the rent (1 yuan / day). The effect is as follows. If the returned book is not lent, the system will prompt: "the book is not lent! The return operation cannot be performed. " If the returned books do not match the books in the list, the system will prompt: "no matching information found!"

case 5:
				System.out.println("--->Return books");
				System.out.println("Please enter the book name:");
				String back = input.next();
				boolean check3 = false;//Judge whether the book you want to return can be found. false means not found, and true means found
				for(int b = 0;names[b] != null;b ++){
					if(names[b].equals(back)){//If the book exists
						check3 = true;//Books you want to borrow can be found
						if(states[b] == 0){//If the book has been lent
							System.out.println("Please enter return date (year-month-Day):");
							String returnDate = input.next();
							while(judge(returnDate) == false){
								System.out.println("Illegal date, please re-enter!");
								returnDate = input.next();
							}
							System.out.println("Return" + back + ">Success!");
							System.out.println("Lending date:" + dates[b]);
							System.out.println("Return date:" + returnDate);
							int money = 0;
							try {
								money = daysBetween(dates[b],returnDate);
							} catch (ParseException e) {
								e.printStackTrace();
							}
							System.out.println("Rent payable (yuan):" + money );
							states[b] = 1;//Convert the current book status to not lent							
						}else{//If the book is not lent
							System.out.println("The book has not been lent and cannot be returned!");						
						}
						break;
					}
				}
				if(check3 == false){
					System.out.println("No matching information found!");
				}
				System.out.println("*************************");
				break;
public class bookLending {
	//Define a function to determine whether the date is legal
	static boolean judge (String str) {
		SimpleDateFormat sd=new SimpleDateFormat("yyyy-MM-dd");//In parentheses is the date format, y represents the year, M represents the month in the year (to avoid conflicts with the minutes in the hour, M is used here), d represents the days in the month
		try {
			sd.setLenient(false);//Whether the date / time resolution is not strict is specified here. It is not strict when it is true or strict when it is false
			sd.parse(str);//Parses text from the beginning of a given string to produce a date
		}
		catch (Exception e) {
			return false;
		}
		return true;
	}
public static int daysBetween(String smdate,String bdate) throws ParseException{
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
			Calendar cal = Calendar.getInstance();
			cal.setTime(sdf.parse(smdate));
			long time1 = cal.getTimeInMillis();
			cal.setTime(sdf.parse(bdate));
			long time2 = cal.getTimeInMillis();
			long between_days=(time2-time1)/(1000*3600*24);
		return Integer.parseInt(String.valueOf(between_days));
		}

6. Lending ranking
Sort all numbers by lending times

	public static void printBook(String[] names, int[] times, int[] sortBook, int number){
		int[] another = times.clone();//Replicated array
		int i = 0;
		int max = another[0];//Find the maximum lending times, used to set cycle conditions
		for(int p = 0;p <= max;p ++){//From 0 to the maximum lending times, query the subscript of the book corresponding to the lending times in turn, and store it in the sortBook array
			for(int q = 0;q < number;q ++){//Traversal times array, which stores the subscript of qualified array value in sortBook array
				if(times[q] == p){
					sortBook[i++] = q;
				}
			}
		}
		System.out.println("Serial number\t" + "Title\t\t" + "Lending times");
		for(int x = (number-1);x >= 0;x --){//Print lending Times Ranking
			System.out.println((number-x) + "\t" + names[sortBook[x]] + "\t\t" + times[sortBook[x]]);
		}
		System.out.println("*************************");
	}
case 0:
				int number = 0;//The total number of initialized titles is not 0
				for(;names[number] != null;number ++){}//Find the total number of current books
				int[] sortBook = new int[number];
	            printBook(names,times,sortBook, number);//Call function to export lending Leaderboard
				break;

7. exit
When the user executes the "exit" command, the program is ended;

The following is the total code:

package bookLending;

import java.util.Scanner;
import java.text.SimpleDateFormat;
import java.util.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class bookLending {
	//Define a function to determine whether the date is legal
	static boolean judge (String str) {
		SimpleDateFormat sd=new SimpleDateFormat("yyyy-MM-dd");//In parentheses is the date format, y represents the year, M represents the month in the year (to avoid conflicts with the minutes in the hour, M is used here), d represents the days in the month
		try {
			sd.setLenient(false);//Whether the date / time resolution is not strict is specified here. It is not strict when it is true or strict when it is false
			sd.parse(str);//Parses text from the beginning of a given string to produce a date
		}
		catch (Exception e) {
			return false;
		}
		return true;
	}
	public static void printBook(String[] names, int[] times, int[] sortBook, int number){
		int[] another = times.clone();//Replicated array
		int i = 0;
		int max = another[0];//Find the maximum lending times, used to set cycle conditions
		for(int p = 0;p <= max;p ++){//From 0 to the maximum lending times, query the subscript of the book corresponding to the lending times in turn, and store it in the sortBook array
			for(int q = 0;q < number;q ++){//Traversal times array, which stores the subscript of qualified array value in sortBook array
				if(times[q] == p){
					sortBook[i++] = q;
				}
			}
		}
		System.out.println("Serial number\t" + "Title\t\t" + "Lending times");
		for(int x = (number-1);x >= 0;x --){//Print lending Times Ranking
			System.out.println((number-x) + "\t" + names[sortBook[x]] + "\t\t" + times[sortBook[x]]);
		}
		System.out.println("*************************");
	}
	public static int daysBetween(String smdate,String bdate) throws ParseException{
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
			Calendar cal = Calendar.getInstance();
			cal.setTime(sdf.parse(smdate));
			long time1 = cal.getTimeInMillis();
			cal.setTime(sdf.parse(bdate));
			long time2 = cal.getTimeInMillis();
			long between_days=(time2-time1)/(1000*3600*24);
		return Integer.parseInt(String.valueOf(between_days));
		}

	public static void main(String[] args) {
		//Define data subject: Books
		int[] states = new int[50];//Status, 0: loaned, 1: loanable
		String[] names = new String[50];//Name
		String[] dates = new String[50];//Lending date, two-dimensional array stores year, month and day respectively, convenient for calculating date
		int[] times = new int[50];//Lending times

		//Initialize four data
		states[0] = 0;//The first book has been lent
		names[0] = "data structure";
		dates[0] = "2018-7-1";//The first book was loaned on July 1, 2018
		times[0] = 1;//The first book has been lent once
		
		states[1] = 1;//The second book is not lent
		names[1] = "data base";
		dates[1] = null;
		times[1] = 0;//The second book hasn't been lent yet
		
		states[2] = 1;//The third book is not lent
		names[2] = "discrete mathematics";
		dates[2] = null;
		times[2] = 0;//The third book hasn't been lent yet
		
		states[3] = 1;//The fourth book is not lent
		names[3] = "Compiling principle";
		dates[3] = null;
		times[3] = 0;//The fourth book hasn't been lent yet
		
		//Build process framework
		Scanner input = new Scanner(System.in);
		int num = -1;//When the user enters 0, it will return to the main menu, and num will be reassigned after borrowing
		boolean flag = false;//Record whether the user exits the system. true means to exit, false means not to exit
		System.out.println("Welcome to the book lending system");
		
		do{
			System.out.println("---------------------");
			System.out.println("0.Lending Leaderboard");
			System.out.println("1.New books");
			System.out.println("2.View books");
			System.out.println("3.Delete books");
			System.out.println("4.Lending books");
			System.out.println("5.Return books");
			System.out.println("6.Sign out");
			System.out.println("---------------------");
			System.out.println("Please choose:");
			int choose = input.nextInt();
			switch(choose){
			case 0:
				int number = 0;//The total number of initialized titles is not 0
				for(;names[number] != null;number ++){}//Find the total number of current books
				int[] sortBook = new int[number];
	            printBook(names,times,sortBook, number);//Call function to export lending Leaderboard
				break;
			case 1:
				System.out.println("--->New books");
				int a = 0;
				for(;a < names.length;a ++){
					if(names[a] == null){
					System.out.print("Please enter the book name:");
				    names[a]= input.next();//Enter the title of the book
					System.out.println("\n" + "Newly added" +names[a] +">Success!");
					
					//Store book information in the array
					states[a] = 1;
					times[a] = 0;
					dates[a] = null;
					break;
					}
				}
				if(a == 50){
					System.out.println("Shelf full, failed to add");
				}
				System.out.println("*************************");
				break;
			case 2:
				System.out.println("--->View books");
				System.out.println("Serial number\t state\t Name\t\t Lending date");
				for(int i = 0;names[i] != null;i ++){
					String situation = (states[i] == 0)?"Lent out":"May borrow";
					System.out.print((i+1) + "\t" + situation + "\t<" + names[i] + ">\t");
					if(states[i] == 0){
						System.out.println(dates[i]);
					}else{
						System.out.println();
					}
				}
				System.out.println("*************************");
				break;
			case 3:
				System.out.println("--->Delete books");
				System.out.println("Please enter the book name");
				String book = input.next();
				boolean check1 = false;//Determine whether the bibliography to be deleted is found. false means no bibliography is found
				for(int b = 0;names[b] != null;b ++){
					if(names[b].equals(book)){
						check1 = true;
						if(states[b] == 1){//Books are not lent, can be deleted
							System.out.println("Delete "" +book + ">Success!");
							int i = b;
							for(;i <names.length-1;i ++){
								states[i] = states[i+1];//The third book is not lent
								names[i] = names[i+1];
								dates[i] = dates[i+1];
								times[i] = times[i+1];
							}//Move array contents forward
							
							//Initialize the location of the last book
							states[i] = 1;//The third book is not lent
							names[i] = null;
							dates[i] = null;
							times[i] = 0;
							break;
						}else{
							System.out.println("The book has been lent and cannot be deleted!");
							break;
						}
				    }
				}
				if(check1 == false){
					System.out.println("No matching information found!");
				}
				System.out.println("*************************");
				break;
			case 4:
				System.out.println("--->Lending books");
				System.out.println("Please enter the book name:");
				String lend = input.next();
				boolean check2 = false;//Judge whether the book you want to lend can be found. false means not found, and true means found
				for(int b = 0;names[b] != null;b ++){
					if(names[b].equals(lend)){//If the book exists
						check2 = true;//Books you want to borrow can be found
						if(states[b] == 1){//If the book is not lent
							System.out.println("Please enter the lending date (year-month-Day):");
							dates[b] = input.next();
							while(judge(dates[b]) == false){
								System.out.println("Illegal date, please re-enter!");
								dates[b] = input.next();
							}
							states[b] = 0;//Convert the current book status to lending
							times[b] ++;//Current book lending times plus one
						}else{//If the book has been lent
							System.out.println(names[b] + "Has been lent!");						
						}
						break;
					}
				}
				if(check2 == false){
					System.out.println("No matching information found!");
				}
				System.out.println("*************************");
				break;
			case 5:
				System.out.println("--->Return books");
				System.out.println("Please enter the book name:");
				String back = input.next();
				boolean check3 = false;//Judge whether the book you want to return can be found. false means not found, and true means found
				for(int b = 0;names[b] != null;b ++){
					if(names[b].equals(back)){//If the book exists
						check3 = true;//Books you want to borrow can be found
						if(states[b] == 0){//If the book has been lent
							System.out.println("Please enter return date (year-month-Day):");
							String returnDate = input.next();
							while(judge(returnDate) == false){
								System.out.println("Illegal date, please re-enter!");
								returnDate = input.next();
							}
							System.out.println("Return" + back + ">Success!");
							System.out.println("Lending date:" + dates[b]);
							System.out.println("Return date:" + returnDate);
							int money = 0;
							try {
								money = daysBetween(dates[b],returnDate);
							} catch (ParseException e) {
								e.printStackTrace();
							}
							System.out.println("Rent payable (yuan):" + money );
							states[b] = 1;//Convert the current book status to not lent							
						}else{//If the book is not lent
							System.out.println("The book has not been lent and cannot be returned!");						
						}
						break;
					}
				}
				if(check3 == false){
					System.out.println("No matching information found!");
				}
				System.out.println("*************************");
				break;
			case 6:
				flag = true;
				break;
			default:
				flag = true;
				break;
		
			}
			if(flag == true){
				break;
			}else{
				System.out.println("Enter 0 to return");
				num = input.nextInt();
			}
		}while(num == 0);
		System.out.println("Thank you for using!");
	}
}

Published 1 original article, praised 0 and visited 5
Private letter follow

Topics: Java