作者:AlexTan
e-mail: [email protected]
聲明:此教程面向有編程基礎(其他語言也可以,不過最好是面向對象的語言)的同學。
由於這學期剛開始學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;
}
else
return -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 stub
new 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