前言
在二十多岁的最后一年,决定再让自己任性一回,开始在 CMoney 参加工程师培训营,给自己半年,花费全部的心力在"写程式"这件我一直都很有兴趣的事情上。理工背景出生的我对于程式并不陌生,但多半是硬刻出一个可以运作的工具,一直很想要利用上班之余的时间多深入些什么,零碎的时间和人性的懒惰也常常让这事拖延。
从零开始学程式,但我们的战斗其实是从物件导向开始!第一週的複习週一点也不手软,很惊喜的发现同学们也都卯足了全力,几位程式小白们透过自学也都如火如荼的跟上进度。有一群抱有同样热情的伙伴们一起努力,实在很幸运,那就开始记录这一週忙碌的複习工程吧!
为了物件导向, 我们练习盖了一座图书馆
题目
图书馆中有数个书架,书架上有数本书,书本有许多出版资讯...
主程式
新增书柜新增书本查询指定分类的所有书籍修改书籍资讯指定书柜编号和书名,修改书本资讯印出所有书籍类别1: 图书馆(Library)
属性: 书柜(BookShelf[]), 书柜总数(int)方法:新增书柜: 指定书柜分类, 存书量上限寻找书柜: 指定书柜编号印出所有书籍: 指定书柜分类新增书籍:指定书柜分类, 查找相同分类的书柜, 按顺序存放在尚有空间的书柜中, 若书柜全满则回传书柜全满类别2: 书柜(BookShelf)
属性: 编号(int), 分类(String), 存书上限(int), 书总数(int), 书(Book[])方法:新增书籍: 指定书名, 作者, 出版日寻找书籍: 指定书名修改书籍: 指定书名, 查询书籍后让使用者修改书籍资讯类别3: 书(Book)
属性: 书名(String), 作者(String), 出版日期(Date)类别4: 日期(Date)
属性: 年(int), 月(int), 日(int)方法:建构子(Book)设定年/月/日软工笔记
就这样,我们在浩瀚的书海里,由浅入深,迷途忘返...
类别4: 日期(Date)
java的命名习惯:变数/方法: nameNameName类别: NameNameName封装技巧: 把属性设为pivate,以getter/ setter 存取,已利后续调整存取条件限制强制转型(整数->字串): ""+(int) public class Date{ private int year; private int month; private int date; public Date(int year, int month, int date){ setYear(year); setMonth(month); setDate(date); } public int getYear(){ return year; } public void setYear(int year){ this.year = year; } public String toString(){ int i = getYear()*10000 + getMonth()*100+ getDate(); return ""+i; } public int getMonth(){...} public void setMonth(int month){...} public int getDate(){...} public void setDate(int date){...} }
类别3: 书(Book)
使用 String.format() 将字串格式化 public class Book{ private String bookName; private Date publishDate; private String writer; public Book(String name, Date date, String writer){...} public String getBookName(){...} public void setBookName(String bookname){...} ... public void bookString(){ String info = Stirng.format("%-15s\t%-10s\t%s", getBookName(),getWriter(),getDate().toString); return info; } }
类别2: 书柜(BookShelf)
新增书籍:引数设定为Book 类别较为理想,将不属于BookShelf 该做的是撇除在BookShelf 的类别中,如果未来Book类别的属性有变化时才不会影响到BookShelf在void 方法中加入return,可以作为中断点寻找书籍: 暂时使用容易实作的循序搜寻,字串的相等必须使用equals().否则会比较记忆体位置修改书籍: 将新的书名等需要使用者输入的功能留BookShelf之外 public class BookShelf{ private int serial; private Sting index; private int bookMax; private int bookCount; private Book[] books; public BookShelf(int serial, String index, int bookMax){ setSerial(serial); ... bookCount = 0; books = new Book[bookMax]; } public int getSerial(){...} public void setSerial(int serial){...} ... public void addBook(Book book){ if(bookMax == bookCount){ return; } books[count++] = book; } public Book findByName(String bookName){ for(int i = 0; i < count; i++){ if(books[i].getBookName.equals(bookName)){ return books[i]; } } return null; } public void modifyByName(String bookName, String newBookName, String newWriter, Date newDate){ Book book = findByName(name); if(book != null){ book.setBookName(newBookName); ... return; } return; } public String bookShelfString(){ ... } }
类别1: 图书馆(Library)
书柜数量: 未知长度的arrary基础记忆体管理技巧: 不够用时在倍增, 在数量少时频繁运作, 数量大时减缓运作矩阵複製: 初新者应学习逐一複製资讯 public class Lirary{ private BookShelf[] bookShelfs; private int bsCount; public BookShelf[] getBookShelfs(){...} ... public Library(){ bookShelfs = new BookShelf[2]; bsCount = 0; } private void doubleBookShelfs(BookShelf[] arr){ BookShelf[] newArr = new Bookshelf[arr.length *2]; for(int i = 0; i < arr.length; i++){ newArr[i] = arr[i]; } arr = newArr; } public void addBookShelf(BookShelf bookshelf){ if(bsCount == bookshelfs.length){ doubleBookShelfs(bookshelfs); } bookshelfs[bsCount++] = bookshelf; } }
寻找书柜: 循序搜寻,回传一个找到的[书柜们],也就是另一个Library,因为里头 BookShelf 指向的 Book 相同,改变其中的资讯两个图书馆的藏书都会修改! public class Lirary{ public Library findByIndex(String index){ Lirary found = new Lirary() for(int i = 0; i < bsCount; i++) if(bookShelfs[i].getIndex().equals(index)){ found.addBookShelf(bookShelfs[i]); } } return found; } public void addBook(String index, Book book){ Lirary found = findByIndex(index); if(found.getBsCount() != 0){ for(int i = 0; i <= found.getBsCount(); i++){ if(found.getBookShelfs()[i].bookCount < found.getBookShelfs()[i].bookMax){ found.getBookShelfs()[i].addBook(book); return; } } System.out.println("书柜已满"); return; } System.out.println("输入错误"); } }