如何获取另一个文件中一个类中已经存在的对象的数据,并将其用于Java中另一个文件中的另一个类?

来源:爱站网时间:2021-11-13编辑:网友分享
如何获取另一个文件中一个类中已经存在的对象的数据,并将其用于Java中另一个文件中的另一个类?这个问题很长吧,哈哈哈,爱站技术小编提供的答案也很长,朋友们慢慢看吧,希望能对你们有帮助。

问题描述


我必须创建并使用LinkedList(从头开始实现)才能与图书库管理程序配合使用。我有3个文件,这些文件包含不同的类,其中3个文件中的3个主要类是BookList(这是书籍的列表),ReaderList(用于存储阅读者的列表)和LendingList(用于存储借出列表)。 BookList和ReaderList分别是Book和Reader的类型,我想从Book类的bookCode属性和Reader类的readerCode属性中提取当前数据。

输入数据允许用户输入借出项目。运行时,屏幕如下所示:

输入书号:

输入阅读器代码:

进入状态:

在用户输入bcode和rcode之后,程序检查并执行以下操作:

  • 如果在书籍列表中未找到bcode或在读者列表中未找到rcode,则不接受数据。
  • 如果在借出列表中找到bcode和rcode且状态均为1,则不接受数据。
  • 如果找到bcode和rcode但已借出=数量,则状态为0的新借出项目将添加到“借出”列表的末尾。
  • 如果找到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) 

阅读器文件:

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中另一个文件中的另一个类?看完以上分享之后,大家应该都知道怎么操作了吧。

上一篇:Jpa命名本机查询无结果怎么办呢?

下一篇:如何判断Sting是否自然地按字母顺序排序?

您可能感兴趣的文章

相关阅读

热门软件源码

最新软件源码下载