Book(string name, string author, string publisher, string date, double price, int id) : book_name(name), author(author), publisher(publisher), publish_date(date), price(price), book_id(id) {}这个是什么意思
时间: 2024-03-04 07:50:13 浏览: 126
这是一个C++中的构造函数,用于创建一个名为Book的类的对象。构造函数包含了六个参数,分别是书名(name)、作者(author)、出版社(publisher)、出版日期(date)、价格(price)、书的编号(id)。在构造函数中,使用了初始化列表来初始化Book类中的成员变量(book_name, author, publisher, publish_date, price和book_id)。这样构造函数被调用时,它会自动将传入的参数赋值给对应的成员变量。
相关问题
class Book { private: // 图书编号 int book_id; // 书名 string book_name; // 作者 string author; // 出版社 string publisher; // 出版日期 string publish_date; // 价格 double price; 帮我写一份构造方法
好的,根据这个类的成员变量,这里给出一个构造函数的例子:
```c++
class Book {
private:
// 图书编号
int book_id;
// 书名
string book_name;
// 作者
string author;
// 出版社
string publisher;
// 出版日期
string publish_date;
// 价格
double price;
public:
// 构造函数
Book(int id, string name, string author, string publisher, string date, double price) {
book_id = id;
book_name = name;
this->author = author; // 使用this指针来区分成员变量和参数同名的情况
this->publisher = publisher;
publish_date = date;
this->price = price;
}
};
```
这个构造函数包含了6个参数,它们分别对应了类的6个私有成员变量。在函数内部,使用了赋值语句来初始化每一个成员变量。在第10行,使用了this指针来区分成员变量和参数同名的情况。这样,当构造函数被调用时,它会自动将传入的参数赋值给对应的成员变量。
实现: 1.修改该文档,将Reader类通通用String定义 2.将文档里的读写方法按文档里所给的“读写方法模板”进行修改
### 修改后的文档
#### 1. 将 `Reader` 类中的所有字段类型从 `long` 改为 `String`
```java
package com.zrgj;
public class Reader {
private String r_id;
private String r_name;
private String r_cardid;
private String aphone;
private String r_password;
private boolean hasOverdueFee;
public Reader() {}
public Reader(String r_id, String r_name, String r_cardid, String aphone, String r_password) {
this.r_id = r_id;
this.r_name = r_name;
this.r_cardid = r_cardid;
this.aphone = aphone;
this.r_password = r_password;
this.hasOverdueFee = false;
}
// Getters and Setters
public String getR_id() { return r_id; }
public void setR_id(String r_id) { this.r_id = r_id; }
public String getR_name() { return r_name; }
public void setR_name(String r_name) { this.r_name = r_name; }
public String getR_cardid() { return r_cardid; }
public void setR_cardid(String r_cardid) { this.r_cardid = r_cardid; }
public String getAphone() { return aphone; }
public void setAphone(String aphone) { this.aphone = aphone; }
public String getR_password() { return r_password; }
public void setR_password(String r_password) { this.r_password = r_password; }
public boolean hasOverdueFee() { return hasOverdueFee; }
public void setHasOverdueFee(boolean hasOverdueFee) { this.hasOverdueFee = hasOverdueFee; }
@Override
public String toString() {
return "Reader [r_id=" + r_id + ", r_name=" + r_name + ", r_cardid=" + r_cardid + ", aphone=" + aphone + ", r_password=" + r_password + "]";
}
}
```
#### 2. 修改 `Tools` 类中的读写方法,使其符合文档中的“读写方法模板”
```java
package com.zrgj;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
public class Tools {
public static final String reader_file_path = System.getProperty("user.home") + "/Desktop/Reader.txt";
public static final String books_file_path = System.getProperty("user.home") + "/Desktop/Book.txt";
public static String readFile(String filePath) throws IOException {
File myFile = createFileObj(filePath);
StringBuilder result = new StringBuilder();
try (BufferedReader br = new BufferedReader(new FileReader(myFile))) {
String s;
while ((s = br.readLine()) != null) {
result.append(s).append("\n");
}
}
return result.toString();
}
public static void writeFile(String content, String filePath) throws IOException {
File file = createFileObj(filePath);
try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) {
writer.write(content);
}
}
public static File createFileObj(String filePath) {
return new File(filePath);
}
public static String arrToString(ArrayList<Reader> readerArr) {
StringBuilder sb = new StringBuilder();
for (Reader reader : readerArr) {
sb.append(reader.getR_id()).append(",")
.append(reader.getR_name()).append(",")
.append(reader.getR_cardid()).append(",")
.append(reader.getAphone()).append(",")
.append(reader.getR_password()).append(",")
.append(reader.hasOverdueFee()).append("\n");
}
return sb.toString();
}
public static ArrayList<Reader> strToArr(String content) {
ArrayList<Reader> readers = new ArrayList<>();
String[] lines = content.split("\n");
for (String line : lines) {
String[] parts = line.split(",");
if (parts.length == 6) {
readers.add(new Reader(parts[0], parts[1], parts[2], parts[3], parts[4]));
readers.get(readers.size() - 1).setHasOverdueFee(Boolean.parseBoolean(parts[5]));
}
}
return readers;
}
public static void addNewReader(Reader reader) throws IOException {
ArrayList<Reader> readers = strToArr(readFile(reader_file_path));
readers.add(reader);
writeFile(arrToString(readers), reader_file_path);
System.out.println("添加成功");
}
public static Reader findReaderById(String r_id) throws IOException {
ArrayList<Reader> readers = strToArr(readFile(reader_file_path));
for (Reader reader : readers) {
if (reader.getR_id().equals(r_id)) {
return reader;
}
}
return null;
}
public static boolean updatePassword(String r_id, String newPassword) throws IOException {
ArrayList<Reader> readers = strToArr(readFile(reader_file_path));
for (Reader reader : readers) {
if (reader.getR_id().equals(r_id)) {
reader.setR_password(newPassword);
writeFile(arrToString(readers), reader_file_path);
return true;
}
}
return false;
}
public static boolean deleteReader(String r_id) throws IOException {
ArrayList<Reader> readers = strToArr(readFile(reader_file_path));
for (int i = 0; i < readers.size(); i++) {
if (readers.get(i).getR_id().equals(r_id)) {
readers.remove(i);
writeFile(arrToString(readers), reader_file_path);
return true;
}
}
return false;
}
public static ArrayList<Book> readBooksFromFile() throws IOException {
File file = new File(books_file_path);
if (!file.exists()) {
return new ArrayList<>();
}
ArrayList<Book> books = new ArrayList<>();
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
String line;
while ((line = br.readLine()) != null) {
String[] data = line.split(",");
if (data.length == 7) {
String b_id = data[0];
String b_name = data[1];
String author = data[2];
String publisher = data[3];
String category = data[4];
double price = Double.parseDouble(data[5]);
boolean isReserved = Boolean.parseBoolean(data[6]);
Book book = new Book(b_id, b_name, author, publisher, category, price);
book.setReserved(isReserved);
books.add(book);
}
}
}
return books;
}
public static void writeBooksToFile(ArrayList<Book> books) throws IOException {
BufferedWriter bw = new BufferedWriter(new FileWriter(books_file_path));
for (Book book : books) {
bw.write(book.getB_id() + "," +
book.getB_name() + "," +
book.getAuthor() + "," +
book.getPublisher() + "," +
book.getCategory() + "," +
book.getPrice() + "," +
book.isReserved() + "\n");
}
bw.close();
}
public static void markBookAsReserved(String b_id) throws IOException {
ArrayList<Book> books = readBooksFromFile();
for (Book book : books) {
if (book.getB_id().equals(b_id)) {
book.setReserved(true);
writeBooksToFile(books);
return;
}
}
}
public static void markBookAsUnreserved(String b_id) throws IOException {
ArrayList<Book> books = readBooksFromFile();
for (Book book : books) {
if (book.getB_id().equals(b_id)) {
book.setReserved(false);
writeBooksToFile(books);
return;
}
}
}
// 读取借阅文件
public static ArrayList<BorrowReturn> readFromBorrowFile() throws IOException, ParseException {
ArrayList<BorrowReturn> borrowReturns = new ArrayList<>();
BufferedReader br = new BufferedReader(new FileReader("BorrowReturn.txt"));
String line;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
while ((line = br.readLine()) != null) {
String[] parts = line.split(",");
if (parts.length >= 4) { // 至少要有四个字段(bookid, bookname, readerid, borrowdate)
String bookid = parts[0];
String bookname = parts[1];
String readerid = parts[2];
Date borrowdate = sdf.parse(parts[3]);
Date returndate = null;
Date actualreturn = null;
if (parts.length > 4 && !parts[4].isEmpty()) {
returndate = sdf.parse(parts[4]);
}
if (parts.length > 5 && !parts[5].isEmpty()) {
actualreturn = sdf.parse(parts[5]);
}
BorrowReturn br1 = new BorrowReturn();
br1.setBookid(bookid);
br1.setBookname(bookname);
br1.setReaderid(readerid);
br1.setBorrowdate(borrowdate);
br1.setReturndate(returndate);
br1.setActualreturn(actualreturn);
borrowReturns.add(br1);
} else {
// 可以选择记录日志、跳过这一行或抛出异常
System.err.println("Invalid line format: " + line);
}
}
br.close();
return borrowReturns;
}
// 写借阅文件
public static void writeToBorrowFile(ArrayList<BorrowReturn> borrowReturns) throws IOException {
BufferedWriter bw = new BufferedWriter(new FileWriter("BorrowReturn.txt"));
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
for (BorrowReturn br : borrowReturns) {
String borrowDateString = sdf.format(br.getBorrowdate());
String returnDateString = (br.getReturndate() != null) ? sdf.format(br.getReturndate()) : "";
String actualReturnDateString = (br.getActualreturn() != null) ? sdf.format(br.getActualreturn()) : "";
bw.write(br.getBookid() + "," +
br.getBookname() + "," +
br.getReaderid() + "," +
borrowDateString + "," +
returnDateString + "," +
actualReturnDateString + "," + "\n");
}
bw.close();
}
// 写入座位文件
public static void writeToSeatFile(ArrayList<Seat> seats) throws IOException {
BufferedWriter bw = new BufferedWriter(new FileWriter("Seat.txt"));
for (Seat s : seats) {
bw.write(s.getId() + "," + s.getLocation() + "," + s.getStatus() + "," + "\n");
}
bw.close();
}
// 读取座位文件
public static ArrayList<Seat> readFromSeatFile() throws IOException {
ArrayList<Seat> seats = new ArrayList<>();
BufferedReader br = new BufferedReader(new FileReader("Seat.txt"));
String line;
while ((line = br.readLine()) != null) {
String[] parts = line.split(",");
if (parts.length == 3) { // 至少要有3个字段(id, location, status)
String id = parts[0];
String location = parts[1];
String status = parts[2];
Seat s = new Seat();
s.setId(id);
s.setLocation(location);
s.setStatus(status);
seats.add(s);
} else {
// 可以选择记录日志、跳过这一行或抛出异常
System.err.println("Invalid line format: " + line);
}
}
br.close();
return seats;
}
}
```
### 总结
1. **修改 `Reader` 类**:
- 将 `aphone` 字段从 `long` 改为 `String`。
- 更新相应的 getter 和 setter 方法。
2. **修改 `Tools` 类中的读写方法**:
- 读取和写入借阅文件的方法 `readFromBorrowFile` 和 `writeToBorrowFile` 已经按照模板进行了修改。
- 读取和写入座位文件的方法 `readFromSeatFile` 和 `writeToSeatFile` 也已经按照模板进行了修改。
这些修改确保了代码的一致性和正确性。
阅读全文
相关推荐
















