- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我必须创建并使用 LinkedList(从头开始实现)才能与图书库管理程序配合使用。我有 3 个包含不同类的文件,3 个文件中的 3 个主要类是 BookList(书籍列表)、ReaderList(存储读者列表)和 LendingList(用于存储借阅目的列表)。 BookList和ReaderList分别是Book和Reader的类型,我想从Book类的bookCode属性和Reader类的readerCode属性中提取当前数据。
输入数据允许用户输入借出项目。运行时,屏幕显示如下:
输入图书代码:
输入阅读器代码:
输入状态:
用户输入bcode和rcode后,程序检查并执行以下操作:
书籍文件:
package BooksPackage;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Scanner;
import java.util.Set;
/**
*
* @author Do Van Nam
*/
class Book {
String bcode;
String btitle;
int quantity;
int lended;
double price;
Book(String code, String title, int quantity, int lended, double price) {
this.bcode = code;
this.btitle = title;
this.quantity = quantity;
this.lended = lended;
this.price = price;
}
@Override
public String toString() {
return "Book{" + "bcode=" + bcode + ", btitle=" + btitle + ", quantity=" + quantity + ", lended=" + lended + ", price=" + price + '}';
}
}
class List {
private static class Node {
Book element;
Node next;
public Node(Book e, Node next) {
this.element = e;
this.next = next;
}
public Node(Book e) {
this(e, null);
}
public Node getNext() {
return next;
}
public Book getElement() {
return element;
}
public void setNext(Node e) {
this.next = e;
}
}
Node head = null;
Node tail = null;
int size = 0;
public List() {
}
public boolean isEmpty() {
return size == 0;
}
public Book getFirst() {
if (isEmpty()) {
return null;
}
return head.element;
}
public Book getLast() {
if (isEmpty()) {
return null;
}
return tail.element;
}
public void addFirst(Book e) {
head = new Node(e, head);
if (size == 0) {
tail = head;
}
size++;
}
public void addLast(Book e) {
Node last = new Node(e, null);
if (isEmpty()) {
head = last;
} else if (size == 1) {
head.setNext(last);
tail = last;
} else {
tail.setNext(last);
tail = last;
}
size++;
}
public void removeFirst() {
if (isEmpty()) {
return;
}
head = head.getNext();
size--;
}
public void removeLast() {
if (isEmpty()) {
return;
}
Node secondLast = head;
while (secondLast.next.next != null) {
secondLast = secondLast.next;
}
secondLast.next = null;
size--;
}
public boolean isDuplicate(String code) {
Node node = head;
while (node != null) {
if (node.element.bcode.equals(code)) {
return true;
}
node = node.next;
}
return false;
}
public String displayNode() {
Node node = head;
double value;
String a = "";
while (node != null) {
value = node.element.price * node.element.quantity;
a += node.element.bcode + "\t" + node.element.btitle + "\t" + node.element.quantity + "\t" + node.element.lended + "\t" + node.element.price + "\t" + value + "\n";
node = node.getNext();
}
return a;
}
public Node searchByCode(String code) {
Node x = head;
while (x != null) {
if (x.element.bcode.equals(code)) {
return x;
}
x = x.getNext();
}
return null;
}
public void deleteByCode(String code) {
Node x = head;
if (x.element.bcode.equals(code)) {
head = head.next;
size--;
return;
}
while (x.next != null) {
if (x.next.element.bcode.equals(code)) {
x.next = x.next.next;
size--;
return;
}
x = x.next;
}
}
public void sortByBCode() {
Node a, b;
Book obj;
a = head;
while (a != null) {
b = a.next;
while (b != null) {
if (b.element.bcode.compareTo(a.element.bcode) < 0) {
obj = a.element;
a.element = b.element;
b.element = obj;
}
b = b.next;
}
a = a.next;
}
}
public void insertAfter(Node node, Book book) {
if (isEmpty() || node == null) {
return;
}
Node after = node.next;
Node newNode = new Node(book, after);
node.next = newNode;
if (tail == node) {
tail = newNode;
}
size++;
}
public Node nodeAtPos(int pos) {
int i = 0;
Node init = head;
while (init != null) {
if (i == pos) {
return init;
}
i++;
init = init.next;
}
return null;
}
public void deleteAtPostion(int pos) {
if (isEmpty()) {
return;
}
Node temp = head;
if (pos == 0) {
head = head.next;
return;
}
for (int i = 0; temp != null && i < pos - 1; i++) {
temp = temp.next;
}
if (temp == null || temp.next == null) {
return;
}
Node next = temp.next.next;
temp.next = next;
}
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
// 1
List bookList = new List();
// 2
// a.addFirst(new Book("SA", "SOMETHING", 12, 23, 122));
// a.addFirst(new Book("SAX", "SOMETHING", 12, 23, 122));
// a.addFirst(new Book("SAC", "SOMETHING", 12, 23, 122));
// a.addFirst(new Book("SAD", "SOMETHING", 12, 23, 122));
System.out.println("1.1. Load data from file\n"
+ "1.2. Input & add to the end\n"
+ "1.3. Display data\n"
+ "1.4. Save book list to file\n"
+ "1.5. Search by bcode\n"
+ "1.6. Delete by bcode\n"
+ "1.7. Sort by bcode\n"
+ "1.8. Input & add to beginning\n"
+ "1.9. Add after position k\n"
+ "1.10. Delete position k");
int option;
do {
System.out.println("Choose an option from 1 to 10, press 0 to stop");
option = sc.nextInt();
if (option == 1) {
System.out.println("Enter the file you want to read");
String file = sc.next();
// the file will be using here is test.txt, which is existed on my local computer, you should try by entering the file you want to read on your computer instead.
BufferedReader read = new BufferedReader(new FileReader(file));
String str;
while ((str = read.readLine()) != null) {
System.out.println(str);
}
}
if (option == 2) {
System.out.println("Enter the book");
String bcode = sc.next();
String btitle = sc.next();
int quantity = sc.nextInt();
int lended = sc.nextInt();
double price = sc.nextDouble();
if (!bookList.isDuplicate(bcode)) {
bookList.addLast(new Book(bcode, btitle, quantity, lended, price));
} else {
System.out.println("This book is already in the list.");
}
}
if (option == 3) {
System.out.println("code" + "\t" + "Title" + "\t" + "Quantity" + "\t" + "Lended" + "\t" + "Price" + "\t" + "Value");
System.out.println("-------------------------------------------------------------------");
System.out.println(bookList.displayNode());
}
if (option == 4) {
System.out.println("Enter the file name");
String fileName = sc.next();
File input = new File(fileName);
if (input.createNewFile()) {
FileWriter fr = null;
BufferedWriter br = null;
String content = bookList.displayNode();
try {
fr = new FileWriter(input);
br = new BufferedWriter(fr);
// String[] lines = content.split("\r\n|\r|\n");
// int linesNums = lines.length;
br.write(content);
} catch (IOException e) {
e.printStackTrace();
} finally {
br.close();
fr.close();
}
}
}
if (option == 5) {
System.out.println("Enter the code of the book you are searching");
String code = sc.next();
if (bookList.searchByCode(code) != null) {
System.out.println(bookList.searchByCode(code));
} else {
System.out.println("Not found");
}
}
if (option == 6) {
System.out.println("Enter the code of the book you want to delete");
String code = sc.next();
bookList.deleteByCode(code);
}
if (option == 7) {
bookList.sortByBCode();
}
if (option == 8) {
System.out.println("Enter the book you want to add to the beginning of the list");
System.out.println("How many books you want to add?");
int nums = sc.nextInt();
while (nums != 0) {
System.out.println("Enter bcode, title, quantity, lended and price for this book");
String bcode = sc.next();
String title = sc.next();
int quantity = sc.nextInt();
int lended = sc.nextInt();
double price = sc.nextDouble();
bookList.addFirst(new Book(bcode, title, quantity, lended, price));
nums--;
}
}
if (option == 9) {
System.out.println("Insert a new node after the bcode: ");
String code = sc.next();
System.out.println("Enter the book");
String bcode = sc.next();
String title = sc.next();
int quantity = sc.nextInt();
int lended = sc.nextInt();
double price = sc.nextDouble();
Book newBook = new Book(bcode, title, quantity, lended, price);
bookList.insertAfter(bookList.searchByCode(code), newBook);
}
if (option == 10) {
System.out.println("Enter the position you want to delete");
int pos = sc.nextInt();
bookList.deleteAtPostion(pos);
}
} while (option != 0);
}
}
阅读器文件:
package BooksPackage;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author VanNam
*/
class Reader {
private String rcode;
private String name;
private int byear;
Reader(String rcode, String name, int byear) {
this.rcode = rcode;
this.name = name;
this.byear = byear;
}
@Override
public String toString() {
return "Reader{" + "rcode=" + rcode + ", name=" + name + ", byear=" + byear + '}';
}
public String getRcode() {
return rcode;
}
public void setRcode(String rcode) {
this.rcode = rcode;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getByear() {
return byear;
}
public void setByear(int byear) {
this.byear = byear;
}
}
class XList {
private static class Node {
Reader element;
Node next;
public Node(Reader e, Node next) {
this.element = e;
this.next = next;
}
public Node(Reader e) {
this(e, null);
}
public Node getNext() {
return next;
}
public Reader getElement() {
return element;
}
public void setNext(Node e) {
this.next = e;
}
}
public XList() {
head = tail = null;
}
Node head = null;
Node tail = null;
int size = 0;
public boolean isEmpty() {
return size == 0;
}
public Reader getFirst() {
return head.getElement();
}
public Reader getLast() {
return tail.getElement();
}
public void addFirst(Reader e) {
head = new Node(e, head);
if (size == 0) {
tail = head;
}
size++;
}
public void addLast(Reader e) {
Node last = new Node(e, null);
if (size == 0) {
head = last;
} else if (size == 1) {
head.setNext(last);
tail = last;
} else {
tail.setNext(last);
tail = last;
}
size++;
}
public String displayNode() {
Node node = head;
String a = "";
while (node != null) {
a += node.element.getRcode() + "\t" + node.element.getName() + "\t" + node.element.getByear() + "\n";
node = node.getNext();
}
return a;
}
public Node searchByCode(String code) {
Node x = head;
while (x != null) {
if (x.element.getRcode().equals(code)) {
return x;
}
x = x.getNext();
}
return null;
}
public void deleteByCode(String code) {
Node x = head;
if (x.element.getRcode().equals(code)) {
head = head.next;
size--;
return;
}
while (x.next != null) {
if (x.next.element.getRcode().equals(code)) {
x.next = x.next.next;
size--;
return;
}
x = x.next;
}
System.out.println("Not found this reader on the list");
}
public boolean isDuplicate(String code) {
Node node = head;
while (node != null) {
if (node.element.getRcode().equals(code)) {
return true;
}
node = node.getNext();
}
return false;
}
public static void main(String[] args) throws FileNotFoundException, IOException {
XList readerList = new XList();
int option;
System.out.println("2.1. Load data from file\n"
+ "2.2. Input & add to the end\n"
+ "2.3. Display data\n"
+ "2.4. Save reader list to file\n"
+ "2.5. Search by rcode\n"
+ "2.6. Delete by rcode");
do {
Scanner sc = new Scanner(System.in);
System.out.println("Choose an option");
option = sc.nextInt();
if (option == 1) {
System.out.println("Enter the file you wanna read");
String file = sc.next();
// the file will be using here is testReader.txt, which is existed on my local computer, you should try by entering the file you want to read on your computer instead.
BufferedReader read = new BufferedReader(new FileReader(file));
String str;
while ((str = read.readLine()) != null) {
System.out.println(str);
}
}
if (option == 2) {
System.out.println("Enter the reader");
String code = sc.next();
String name = sc.next();
int year = sc.nextInt();
if (!readerList.isDuplicate(code)) {
readerList.addLast(new Reader(code, name, year));
} else {
System.out.println("This reader is already in the list.");
}
}
if (option == 3) {
System.out.println(readerList.displayNode());
}
if (option == 4) {
System.out.println("Enter the file name");
String fileName = sc.next();
File input = new File(fileName);
if (input.createNewFile()) {
FileWriter fr = null;
BufferedWriter br = null;
String content = readerList.displayNode();
try {
fr = new FileWriter(input);
br = new BufferedWriter(fr);
br.write(content);
} catch (IOException e) {
e.printStackTrace();
} finally {
br.close();
fr.close();
}
}
}
if (option == 5) {
System.out.println("Find the reader by entering the code");
String code = sc.next();
if (readerList.searchByCode(code) != null) {
System.out.println("Found at this address: " + readerList.searchByCode(code));
} else {
System.out.println("Not found");
};
}
if (option == 6) {
System.out.println("Enter the reader you want to delete by entering the code");
String code = sc.next();
readerList.deleteByCode(code);
}
} while (option != 0);
}
}
LendingBook 文件:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package BooksPackage;
import java.util.Scanner;
/**
*
* @author VanNam
*/
public class LendingBook {
private String bcode;
private String rcode;
private int state;
public LendingBook(String bcode, String rcode, int state) {
this.bcode = bcode;
this.rcode = rcode;
this.state = state;
}
@Override
public String toString() {
return "LendingBook{" + "bcode=" + bcode + ", rcode=" + rcode + ", state=" + state + '}';
}
public String getBcode() {
return bcode;
}
public void setBcode(String bcode) {
this.bcode = bcode;
}
public String getRcode() {
return rcode;
}
public void setRcode(String rcode) {
this.rcode = rcode;
}
public int getState() {
return state;
}
public void setState(int state) {
this.state = state;
}
}
class LendingList {
private static class Node{
private LendingBook element;
private Node next;
public Node(LendingBook e, Node n){
this.element = e;
this.next = n;
}
public Node(LendingBook e){
this(e, null);
}
public Node getNext() {
return next;
}
public LendingBook getElement() {
return element;
}
public void setNext(Node n){
this.next = n;
}
}
Node head = null;
Node tail = null;
int size = 0;
public boolean isEmpty(){
return size == 0;
}
public LendingBook getFirst() {
if(isEmpty()) return null;
return head.getElement();
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
LendingList lendingList = new LendingList();
int option;
do{
System.out.println("Enter the option" + "\n" + "1.Input data 2. Display lending data 3. Sort by bcode + rcode" + "\n" + "Press 0 to exit");
option = sc.nextInt();
if(option == 1){
String bookCode; // -> Here, I want to check it with the existing bookCode from the Book file
String readerCode; // // -> Here, I want to check it with the existing readerCode from the Reader file
int state;
System.out.println("Enter book code");
bookCode = sc.next();
System.out.println("Enter the reader code");
readerCode = sc.next();
}
}while(option != 0);
}
}
我想获取该信息并在我的 LendingList 中使用上述两个类的数据。非常感谢。
最佳答案
您只能使用构造函数或实例方法传递数据。
或者做
filename bob = new filename(object or data type);
bob.method(object or data type);
PS:不确定这是否有效。
关于java - 如何获取另一个文件中的一个类中已存在的对象的数据并将其用于Java中另一个文件中的另一个类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60377990/
我的一位教授给了我们一些考试练习题,其中一个问题类似于下面(伪代码): a.setColor(blue); b.setColor(red); a = b; b.setColor(purple); b
我似乎经常使用这个测试 if( object && object !== "null" && object !== "undefined" ){ doSomething(); } 在对象上,我
C# Object/object 是值类型还是引用类型? 我检查过它们可以保留引用,但是这个引用不能用于更改对象。 using System; class MyClass { public s
我在通过 AJAX 发送 json 时遇到问题。 var data = [{"name": "Will", "surname": "Smith", "age": "40"},{"name": "Wil
当我尝试访问我的 View 中的对象 {{result}} 时(我从 Express js 服务器发送该对象),它只显示 [object][object]有谁知道如何获取 JSON 格式的值吗? 这是
我有不同类型的数据(可能是字符串、整数......)。这是一个简单的例子: public static void main(String[] args) { before("one"); }
嗨,我是 json 和 javascript 的新手。 我在这个网站找到了使用json数据作为表格的方法。 我很好奇为什么当我尝试使用 json 数据作为表时,我得到 [Object,Object]
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
我听别人说 null == object 比 object == null check 例如: void m1(Object obj ) { if(null == obj) // Is thi
Match 对象 提供了对正则表达式匹配的只读属性的访问。 说明 Match 对象只能通过 RegExp 对象的 Execute 方法来创建,该方法实际上返回了 Match 对象的集合。所有的
Class 对象 使用 Class 语句创建的对象。提供了对类的各种事件的访问。 说明 不允许显式地将一个变量声明为 Class 类型。在 VBScript 的上下文中,“类对象”一词指的是用
Folder 对象 提供对文件夹所有属性的访问。 说明 以下代码举例说明如何获得 Folder 对象并查看它的属性: Function ShowDateCreated(f
File 对象 提供对文件的所有属性的访问。 说明 以下代码举例说明如何获得一个 File 对象并查看它的属性: Function ShowDateCreated(fil
Drive 对象 提供对磁盘驱动器或网络共享的属性的访问。 说明 以下代码举例说明如何使用 Drive 对象访问驱动器的属性: Function ShowFreeSpac
FileSystemObject 对象 提供对计算机文件系统的访问。 说明 以下代码举例说明如何使用 FileSystemObject 对象返回一个 TextStream 对象,此对象可以被读
我是 javascript OOP 的新手,我认为这是一个相对基本的问题,但我无法通过搜索网络找到任何帮助。我是否遗漏了什么,或者我只是以错误的方式解决了这个问题? 这是我的示例代码: functio
我可以很容易地创造出很多不同的对象。例如像这样: var myObject = { myFunction: function () { return ""; } };
function Person(fname, lname) { this.fname = fname, this.lname = lname, this.getName = function()
任何人都可以向我解释为什么下面的代码给出 (object, Object) 吗? (console.log(dope) 给出了它应该的内容,但在 JSON.stringify 和 JSON.parse
我正在尝试完成散点图 exercise来自免费代码营。然而,我现在只自己学习了 d3 几个小时,在遵循 lynda.com 的教程后,我一直在尝试确定如何在工具提示中显示特定数据。 This code
我是一名优秀的程序员,十分优秀!