请您帮我弄清楚如何才能创建删除命令?
在选择5.我必须使用数组而不是ArrayList
Vector
?
//包ask1主类库
package ask1;
import java.lang.Object.*;
import java.util.Scanner;
import java.util.Arrays;
import java.io.*;
public class library {
public static void main(String[] args) throws InstantiationException, IllegalAccessException {
Management manager = new Management();
Scanner input = new Scanner( System.in );
Book vivlio = new Book();
System.out.println("\n\t\t^*^*^*^*^*^*^* LIBRARY MANAGEMENT ^*^*^*^*^*^*^");
while(true){
System.out.println("------------------MENU-------------------------------");
System.out.print("\nENTER UR CHOICE\n\t1:Add a new Book\n\t2:Edit Book Infos\n\t3:Search a Book (with ISBN)\n\t4:Show all the Books\n\t5:Delete a Book (with ISBN)\n\t6:Exit \n :");
int selection;
selection=input.nextInt();
if (selection == 1)
{
System.out.println("Adding a new book ");
String empty = input.nextLine();
System.out.println("name of book:");
vivlio.name = input.nextLine();
System.out.println("Author:");
vivlio.author = input.nextLine();
System.out.println("ISBN:");
vivlio.isbn = input.nextLine();
System.out.println("Number of copies:");
vivlio.number = input.nextInt();
System.out.println("");
manager.AddBook(vivlio);
}
else
if (selection == 2) {
System.out.println("Editing a Book ");
System.out.println("Please enter title of book to edit:");
String title = input.next();
Book editingBook = findBookByTitle(title);
if(editingBook == null) {
System.out.println("Sorry no book found with title name = " + title);
} else {
//ask user for new price etc what ever you want to edit.
System.out.println("Please enter new values:");
String newValue = input.nextLine();
editingBook.setPrice(newValue);
// etc. other editing to book.
}
}
else
if (selection == 3)
{
System.out.println("Searching a Book ");
}
else
if (selection == 4)
{
System.out.println("You Choose to view all the Books ");
manager.PrintAllBooks();
}
else
if (selection == 5)
{
System.out.println("You Choose to Delete a Book ");
String empty = input.nextLine();
}
else
if(selection == 6)
{
System.out.println("Library System Terminated!!! ");
String empty = input.nextLine();
System.exit(0);
}
else
{
System.out.println("Wrong Choice");
}
}
}
private static Book findBookByTitle(String title) {
// TODO Auto-generated method stub
return null;
}
这是第二个名为Book
的类package ask1;
import java.util.Scanner;
public class Book {
Scanner input = new Scanner( System.in );
public String isbn;
public String name;
public String author;
public int number;
public String getIsbn() {
return isbn;
}
public void setIsbn(String isbn) {
this.isbn = isbn;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
String bookinfo = name + " ," + author + " ," + isbn;
public void setPrice(String newPrice) {
// TODO Auto-generated method stub
}
}
第三节叫做管理
package ask1;
import java.lang.reflect.Array;
import java.util.Scanner;
import java.util.Arrays;
public class Management {
public Book[] books =new Book [60];
String Sisbn;
int current = 0;
Library lib = new Library ();
Scanner input = new Scanner( System.in );
//Number 1
public void AddBook(Book vivlio)
{
books[current]=vivlio;
current++;
}
//Number 2
public Book findBookByTitle(String title)
{
Book searchBook = null;
for (Book vivlio : books)
{
if(vivlio.getName().equalsIgnoreCase(title))
{
searchBook = vivlio;
System.out.println("New name of book:");
vivlio.setName(input.nextLine());
System.out.println("New Author of book:");
vivlio.setAuthor(input.nextLine());
System.out.println("New isbn of book:");
vivlio.setIsbn(input.nextLine());
System.out.println("New number of copies of book:");
vivlio.setNumber(input.nextInt());
break; }
}
return searchBook;
}
public String getSisbn() {
return Sisbn;
}
public void setSisbn(String sisbn) {
Sisbn = sisbn;
}
public Book[] getBooks() {
return books;
}
public void setBooks(Book[] books) {
this.books = books;
}
public int getCurrent() {
return current;
}
public void setCurrent(int current) {
this.current = current;
}
//Number3
public Book findBookByISBN(String isbn)
{
Book searchBook = null;
for (Book vivlio : books)
{
if(vivlio.getIsbn().equalsIgnoreCase(isbn))
{
searchBook = vivlio;
String book = vivlio.getName();
lib.once=vivlio.getName();
System.out.println(lib.once);
break;
}
}
return searchBook;
}
//Number 4
public void PrintAllBooks()
{
for (int i=0;i<current;i++)
{
Book b = books[i];
System.out.println(b.name);
}
}
//number 5
public Book DeleteWithIsbn(String isbn)
{
Book searchBook = null;
for (Book vivlio : books)
{
if(vivlio.getIsbn().equalsIgnoreCase(isbn))
{
searchBook = vivlio;
books[current]=null;
for (int i=0;i<current;current--)
{
Book b = books[i];
System.out.println(b.name);
break;
}
}
}
return searchBook;
}
}
答案 0 :(得分:0)
您的代码不会尝试“删除”它仍然找到。请尝试以下方法:
(未经测试,但给你的想法)
public boolean DeleteWithIsbn(String isbn) {
int index = 0;
for (Book vivlio : books) {
if(vivlio.getIsbn().equalsIgnoreCase(isbn)) { //find the right book
for (int i = index; i < books.length - 1; i++) {
books[i] = books[i+1]; //move all the other books up
}
books[books.length -1] = null; //reset the last one in the array
return true;
}
count++;
}
return false;
}
如果无法找到该书,此方法将返回false
。否则返回true
。
当然这一切都假定这本书只在阵列中存在过一次。
答案 1 :(得分:0)
首先,修改if / else语句的缩进,并考虑使用case语句。
其次,你的选择5实际上并没有删除任何内容 - 它只是将String设置为空白到下一行。使用Scanner获取用户输入的ISBN,并确保使用该isbn调用Book.DeleteBookWithISBN(isbn)。然后它会做你想要的:)