[Java]---图书管理系统(一)

参考:http://blog.csdn.net/alextan_/article/details/65447446

本系列是通过学习AlexTan这位博主的文章搭建一个图书管理系统,顺便再学习一下java这门语言。除了大二水了一学期这门课,完全是菜鸡,所以注释很多自己的理解。

开发环境:window10+eclipse

一、准备
项目目录如下:
在这里插入图片描述
二、创建一个Book类,代码如下:

package model;

//创建一book类
public class Book {
	private String bookname;  //三个成员变量,定义图书有三种属性
	private String author;   //用private修饰,指定该变量只能自己类的方法访问,其他任何类(包括子类)的方法都不能访问
	private float price;
	
	//构造方法
	public Book(String bookname, String author, float price)
	{
		this.bookname = bookname;
		this.author = author;
		this.price = price;
	}
	
	//获取各个字段的值
	public String getBookname() {
		return bookname;
	}
	public String getAuthor() {
		return author;
	}
	public float getPrice() {
		return price;
	}
	
	//重置各个字段的值
	public void setBook(String bookname, String author, float price) {
		this.bookname = bookname;
		this.author = author;
		this.price = price;
	} 
}

先定义了四个简单的成员方法,分别为获取书名、获取作者、获取图书价格和重置图书属性。

三、创建MainClass.java文件,代码如下:

package ui;
 
import java.util.Scanner;
 
import model.Book;
 
public class MainClass {
	
	public static final int SIZE = 10;  //最大可存储图书的数量
	//public指定该变量为公共,static指定变量被所有对象共享,即所有实例都可以使用该变量
	//final为最终修饰符,指定此变量的值不能变
	Book[] booklist = new Book[SIZE]; //初始化Book类型的实例
	int count = 0;  //当前图书数量
	
	public MainClass()
	{
		
		Scanner scan = new Scanner(System.in);
		//初始化一个Scanner类实例,来获取用户的输入
		printMenu();  //调用打印菜单的方法
		
		while(true)
		{
			//读取用户输入
			int choice = scan.nextInt();  //获取一个int型输入
			
			if(choice == 5)
			{
				System.out.println("成功退出系统,欢迎再次光临!");
				break;
			}
			switch(choice)//switch形式
			{
			case 1: addBook(); break;
			case 2: deleteBoo(); break;
			case 3: changeBoo(); break;
			case 4: findBoo(); break;
			default: System.out.println("输入非法"); printMenu(); continue;//这个continue 是continue的while,
			}
		}
	}
	void printMenu()
	{
		//打印菜单
		System.out.println("欢迎...");
		System.out.println("增加图书...1");
		System.out.println("删除图书...2");
		System.out.println("修改图书...3");
		System.out.println("查询图书...4");
		System.out.println("退出系统...5");	
	}
	
	void addBook()//增加图书
	{
		if (count < SIZE)
		{
			System.out.println("当前共有:"+count+"本书!");
			Scanner scan = new Scanner(System.in);
			System.out.println("请输入图书名:");
			String bookname = scan.next();
			System.out.println("请输入作者:");
			String author = scan.next();
			System.out.println("请输入单价:");
			float price = scan.nextFloat();
			Book book = new Book(bookname,author,price);  //创建一本书
			booklist[count] = book;
			count++;
			System.out.println("增加成功!");
			printAllBook();  
		}
		else
		{
			System.out.println("图书库已满!");
		}
		
		
	}
	
	void deleteBoo()//删除图书
	{
		Scanner scan = new Scanner(System.in);
		while(true)
		{
			System.out.println("请输入按哪种方法删除图书:1、序号/2、书名/3、返回主菜单");
			int choose = scan.nextInt();
			if(choose == 1)
			{
				System.out.println("请输入要删除第几本书:");
				int id = scan.nextInt();
				id = orderFind(id);  
				//System.out.println(id);
				if(id > -1)
				{
					for(int i = id; i < count - 1 ; i++)//用for循环的形式实现对数组的删除
						booklist[i]=booklist[i+1];   //通过数组的后面元素覆盖前面的元素来实现删除
					count--;
					System.out.println("删除成功!");
					printAllBook();
				}
				else
				{
					System.out.println("输入错误!");
				}
			}
			else if(choose == 2)
			{
				System.out.println("请输入您要删除的书名:");
				String name = scan.next();
				int id = nameFind(name);    //通过方法将作者名转化为对应的id,但是这样有一个bug,要是作者名相同呢?
				if(id > -1)
				{
					for(int j = id; j<count-1; j++)//用for循环的形式实现对数组的删除
					{
						booklist[j]=booklist[j+1];
					}
					count --;
					System.out.println("删除成功!");
					printAllBook();
				}
				else
				{
						System.out.println("未查找到您想要的书名");
				}	
			}
			else if(choose == 3)
			{
				printMenu();
				break; //这个break会跳出当前循环,从而实现跳出当前函数,返回上一级循环,即主菜单。
			}
			else
			{
				System.out.println("输入非法!");
			}
		}
	}
	
	void changeBoo()
	{
		Scanner scan = new Scanner(System.in);
		while(true)
		{
			System.out.println("请输入按哪种方法修改图书:1、序号/2、书名/3、返回主菜单");
			int choose = scan.nextInt();
			if(choose == 1)
			{
				System.out.println("请输入要修改第几本书:");
				int number = scan.nextInt();
				int id = orderFind(number);  //将输入的数字转换为id,因为数组下标从0开始,所以id和number差了1
				if(id > -1)
				{
					System.out.println("原书名为:"+booklist[id].getBookname()+" 请输入你要修改为什么书名:");
					String str = scan.next();
					System.out.println("请输入作者:");
					String author = scan.next();
					System.out.println("请输入单价:");
					float price = scan.nextFloat();
					booklist[id].setBook(str,author,price);
					System.out.println("修改成功!");
					printAllBook();
				}
				else
				{
					System.out.println("输入错误!");
				}
			}
			else if(choose == 2)
			{
				System.out.println("请输入您要修改的书名:");
				String name = scan.next();
				int id = nameFind(name);   //将书名转化为对应的id
				if(id > -1)
				{
					System.out.println("原书名为:"+booklist[id].getBookname()+" 请输入你要修改为什么书名:");
					String str = scan.next();
					System.out.println("请输入作者:");
					String author = scan.next();
					System.out.println("请输入单价:");
					float price = scan.nextFloat();
					booklist[id].setBook(str,author,price);
					System.out.println("修改成功!");
					printAllBook();		
				}
			}
			else if(choose == 3)
			{
				printMenu();
				break;
			}
			else
			{
				System.out.println("输入非法!");
			}
		}
	}
	
	void findBoo()
	{
		Scanner scan = new Scanner(System.in);
		while(true)
		{
			System.out.println("请输入按哪种方法查找图书:1、序号/2、书名/3、返回主菜单");
			int choose = scan.nextInt();
			if(choose == 1)
			{
				System.out.println("请输入要查找第几本书:");
				int number = scan.nextInt();
				int id = orderFind(number);
				if(id > -1)
				{
					System.out.println("你要查找的书名为:"+booklist[id].getBookname()+" 作者:"+booklist[id].getAuthor()+" 单价:"+booklist[id].getPrice()+"元/本");
				}
				else
				{
					System.out.println("输入错误!");
				}
			}
			else if(choose == 2)
			{
				System.out.println("请输入您要查找的书名:");
				String name = scan.next();
				int id = nameFind(name);
				if(id > -1)
				{
					System.out.println("查找成功,您查找到的书为第"+(id+1)+"本书!"+"书名为:"+booklist[id].getBookname()+" 作者:"+booklist[id].getAuthor()+" 单价:"+booklist[id].getPrice()+"元/本");		
				}
			}
			else if(choose == 3)
			{
				printMenu();
				break;
			}
			else
			{
				System.out.println("输入非法!");
			}
		}
	}
	
	void printAllBook() //循环打印所有的图书
	{
		for (int i = 0; i < count; i++)
		{
			System.out.println("第"+(i+1)+"本书名:"+booklist[i].getBookname()+" 作者:"+booklist[i].getAuthor()+" 单价:"+booklist[i].getPrice()+"元/本");
		}
	}
	
	int orderFind(int number)	//按序号查找,返回id
	{
		//System.out.println(number);
		if(number <= count)
		{
			int id = number - 1;
			return id;
		}
		else
			return -1;
	}
	
	int nameFind(String name)//按书名查找,返回id
	{
		int id = -1;
		for(int i = 0; i < count; i++)
		{
			if(booklist[i].getBookname().equals(name))  //调用Book类中的获取书名方法
			{
				id = i;
				break;
			}
			else if(i<count)
				continue;
			else
			{
				System.out.println("未查找到您想要的书名");
				break;
			}
		}
		return id;
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new MainClass();
	}
 
}

运行结果:
在这里插入图片描述

笔记:

Scanner类
1.实例化:

Scanner scan = new Scanner(System.in)

2.next方法
从键盘接收数据,next方法接收字符串

if(scan.hasNext()){
	String str = scan.next();
}

3.nextLine方法
从键盘接收数据,nextLine方法接收字符串

if(scan.hasNextLine()){
	String str = scan.nextLine();
}

4.next()和nextLine()的区别

next():

1)一定要读取到有效字符后才可以结束输入。
2)对输入有效字符之前遇到的空白,next() 方法会自动将其去掉。
3)只有输入有效字符后才将其后面输入的空白作为分隔符或者结束符。
next() 不能得到带有空格的字符串。

nextLine():

1)以Enter为结束符,也就是说 nextLine()方法返回的是输入回车之前的所有字符。
2)可以获得空白。

5.获取其他数据类型
scan.nextInt()
scan.nextFloat()
scan.nextDouble()等

猜你喜欢

转载自blog.csdn.net/Duang_993/article/details/86822830