Java入门教程之图书管理系统(由简入繁)(一)

来源:互联网 发布:少儿围棋入门软件 编辑:程序博客网 时间:2024/05/16 19:44

作者:AlexTan

e-mail: alextanbz@gmail.com


声明:此教程面向有编程基础(其他语言也可以,不过最好是面向对象的语言)的同学。


由于这学期刚开始学java课,无意间觉得自己很幸运:选到一个很不错的java老师,不过也可能是我个人比较适合这种教学方法吧! 所以自己打算写一个教程来分享一下,非常适合有编程基础的同学哟!特别是刚接触java的同学!废话不多说,我们开始吧:


先介绍一下整个教程的内容吧,这个教程主要是通过一个具体的项目,来学习java的知识,但仅凭这个项目,想成为一个java大牛,还是远远不够的,所以叫java入门嘛!

大概整个项目的思路就是:从最简单的通过数组来实现图书管理系统,到最后的通过数据库来实现,再加上一个漂亮的UI界面!最后,一个小的图书管理系统项目,一点一点的改,不管从用户体验上来说,还是从代码上来说,变成一个相当不错的图书管理系统。总之,就是一个精益求精的过程!


下面开始吧:


Java入门之图书管理系统一(数组实现):


首先,建立一个Book类,在model包下的Book.java文件中,这里的语法和c++差不多:

package model;public class Book {private String bookname;private String author;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;}//重置各个字段的值}

接着是主文件,在ui包下的MainClass.java文件:

package ui;import java.util.ArrayList;import java.util.Scanner;import model.Book;public class MainClass {public static final int SIZE = 10;Book[] booklist = new Book[SIZE];int count = 0;public MainClass(){Scanner scan = new Scanner(System.in);printMenu();while(true){//读取用户输入int choice = scan.nextInt();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,}}/* * if形式:while(true){//根据用户输入调用不同方法if(choice == 1){addBook();}else if(choice == 2){deleteBoo();}else if(choice == 3){changeBoo();}else if(choice == 4){findBoo();}else if(choice == 5){System.out.println("成功退出系统,欢迎再次光临!");break;}}*/}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);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);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);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;}elsereturn -1;}int nameFind(String name)//按书名查找,返回id{int id = -1;for(int i = 0; i < count; i++){if(booklist[i].getBookname().equals(name)){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 stubnew MainClass();}}

代码比较简单,所以注释没怎么写,不过如果有C++基础,或则其他面向对象编程语言的基础的话应该很容易看懂。笔者是有C/C++,Python,以及PHP基础的。

运行截图:


总结:

这个是上JAVA课的第一节课就写好了,感觉JAVA和C++语法很相似,程序比较简单,分享给大家学习学习!

但是这种方式实现图书的查找与删除比较麻烦,下一章博客将会用Java里的ArrayList来实现图书管理系统,查找删除会简便很多!

请阅读下一篇博客:Java入门教程之图书管理系统(由简入繁)(二)( http://blog.csdn.net/alextan_/article/details/65449333 )

转载请注明出处:http://blog.csdn.net/alextan_/article/details/65447446


3 1
原创粉丝点击