定义图书类Book,具有属性账号id,书名name、作者author 和价格price和一个图书馆类Library保存新书

    技术2022-07-11  148

    1.定义图书类Book,具有属性账号id,书名name、作者author 和价格price,在创建图书对象时要求通过构造器进行创建,一次性将四个属性全部赋值, 要求账户属性是int型,名称是String型,作者是String型,价格是double,请合理进行封装。

    在Book类,添加toString方法,要求返回图书信息字符串,使用\t隔开各信息要求定义一个图书馆Library类,在图书馆类中添加一个HashSet集合用于保存多本图书 3)在图书馆类中要求能够新增图书 4)在图书馆类中要求可以查看所有添加过的图书 5)不允许添加重复的图书(如果账号id和书名name相同,则认为两本书是相同的) 6)可以根据id删除图书

    没想到还有一天会写这些代码(笑)。 Book类里面,几个简单的丶都在注释里提及了

    Book.java

    /** * @author:fang */ package shixun.project1.zuoye1; //Comparable继承用于排序 public class Book implements Comparable<Book>{ private Integer id; private String name; private String author; private double price; public Book(){ super(); } //赋值 public Book(int id,String name,String author,double price){ super(); this.id=id; this.name=name; this.author=author; this.price=price; } public int getId(){ return id; } //重写toString()方法,实现格式化输出 @Override public String toString(){ return "\nbook\tid=" + id +"\t" + "name=" + name + "\t" + "author=" + author +"\t" + "price=" + price + "\n"; } //重写hashCode()和equals()避免重复 @Override public int hashCode(){ return this.id+this.name.hashCode(); } @Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null || !(obj instanceof Book)){ return false; } Book b = (Book) obj; if (name == null) { if (b.name != null) { return false; } } else if (id != b.id&&!name.equals(b.name)) { return false; } return true; } @Override public int compareTo(Book o) { return this.getId()-o.getId(); } }

    Library类没什么好说的,无非增删改查和一个选择控制器,修改没有实现,不是因为我懒,而是要求里没有提及(笑) Library.java

    /** * @author:fang */ package shixun.project1.zuoye1; import java.util.*; public class Library { public static void main(String[] args) { HashSet<Book> hashSet=new HashSet<>(); select(hashSet); // addBook(); } //功能选择 private static void select(HashSet<Book> hashSet) { Scanner s=new Scanner(System.in); System.out.println("功能列表:\n1.插入图书\n2.删除图书\n3.修改图书\n4.查看图书\n请输入对应id选择功能:"); String opt=s.next(); switch (opt){ case "1": hashSet=addBook(hashSet); break; case "2": deleteBook(hashSet); break; case "3": changeBook(hashSet); break; case "4": checkBook(hashSet); break; default: System.out.println("输入错误,请重新输入!"); select(hashSet); } } //增 private static HashSet<Book> addBook(HashSet<Book> hashSet) { ArrayList<Book> arrayList=new ArrayList<>(hashSet); Scanner s=new Scanner(System.in); do { System.out.println("插入图书:"); System.out.println("input bookId:"); int id=s.nextInt(); System.out.println("input bookName:"); String name=s.next(); System.out.println("input bookAuthor:"); String author=s.next(); System.out.println("input bookPrice"); double price=s.nextDouble(); hashSet.add(new Book(id,name,author,price)); System.out.println("是否继续插入y/N?"); String isNext=s.next(); if (isNext.equals("n")){ System.out.println("插入完毕!"); select(hashSet); break; } else { if (!isNext.equals("y")) { System.out.println("请输入正确的指令!"); select(hashSet); break; } System.out.println(hashSet); } }while (true); checkBook(hashSet); return hashSet; } //删 private static HashSet<Book> deleteBook(HashSet<Book> hashSet) { System.out.println("删除图书"); System.out.println("请输入要删除书籍的ID:"); Scanner s=new Scanner(System.in); int deleteId=s.nextInt(); ArrayList<Book> arrayList=new ArrayList<>(hashSet); Collections.sort(arrayList); Iterator<Book> iterator=arrayList.iterator(); while (iterator.hasNext()){ Book b=(Book) iterator.next(); if(b.getId()==deleteId){ iterator.remove(); } } //这里稍微需要注意一下,因为CRUD返回的都是HashSet,所以把删除后的arrayList里面的值重新倒给hashSet,进行输出和返回 hashSet=new HashSet<Book>(arrayList); checkBook(hashSet); select(hashSet); return hashSet; } //改 private static void changeBook(HashSet<Book> hashSet) { System.out.println("修改图书"); select(hashSet); } //查 private static void checkBook(HashSet<Book> hashSet) {//,HashSet<Book> hashSet System.out.println("查看图书"); ArrayList<Book> arrayList=new ArrayList<>(hashSet); Collections.sort(arrayList); Iterator<Book> iterator=arrayList.iterator(); while (iterator.hasNext()){ Book book = (Book) iterator.next(); System.out.println(book); } select(hashSet); } }

    不足之处,欢迎指正

    Processed: 0.010, SQL: 9