sy4_多态的应用_4_Book
(4) 在实验三对多本书完成增删改的基础上,应用函数重载、插入符(<<)的重载和提取符(>>)的重载进一步完善类的初始化和输出功能,并根据功能增加主菜单、二级菜单和清屏等功能提升用户体验。
整段代码:
Book.h
#pragma once
#ifndef BOOK_H
#define BOOK_H
#include <iostream>
#include <iomanip>
using namespace std;
class Book {
private:
string name; //图书名称
float price; //价格
string isbn; //ISBN
string publisher;//出版社
string author; //作者
public:
Book();//默认构造函数
Book(string Bname, float Bprice, string Bisbn, string Bpuvlisher, string Bauthor);//重载构造函数
string get_name();//获取书名
void setName(const string newName);//设置书名
float get_price();//获取价格
void setPrice(const float newPrice);//设置价格
string get_isbn();//获取ISBN
void setIsbn(const string newIsbn);//设置ISBN
string get_publisher();//获取出版社
void setPublisher(const string newPublisher);//设置出版社
string get_author();//获取作者
void setAuthor(const string newAuthor);//设置作者
friend istream& operator>>(istream& input, Book& book);//运算符重载方法,输入图书信息
friend ostream& operator<<(ostream& output, Book& book);//运算符重载方法,输出图书信息
};
#endif // !BOOK_H
BookList.h
#pragma once
#ifndef BOOKLIST_H
#define BOOKLIST_H
#include <iostream>
#include <iomanip>
#include "Book.h"
using namespace std;
class BookList
{
private:
Book bookList[100];
public:
int bookNum;
BookList();
void insert(Book b);//增加一本书
void showBook(int i);//打印下标为i的那本书的信息
void showBook_title();//打印表头
void showAll();//打印所有书
int search_name(string name);//根据书名去查找对应的第一本书
int search_price(float price);//根据价格去查找对应的第一本书
int search_isbn(string isbn);//根据isbn去查找对应的第一本书
int search_publisher(string publisher);//根据出版社去查找对应的第一本书
int search_author(string author);//根据作者去查找对应的第一本书
void update(int i, Book b);//修改时允许修改部分数据,这样就不需要修改所有的参数
Book bookInfor(int i);//为了修改时获取原始数据,可以获取某一本书的具体信息,然后返回
void deleted(int i);//删除第i本图书
};
#endif
Book.cpp
#include "Book.h"
Book::Book()//构造函数Book()
{
}
Book::Book(string Bname, float Bprice, string Bisbn, string Bpublisher, string Bauthor)//重载构造函数:形参初始化数据成员
{
name = Bname;
price = Bprice;
isbn = Bisbn;
publisher = Bpublisher;
author = Bauthor;
}
string Book::get_name() {
return name;
}
void Book::setName(const string newName) {
name = newName;
}
float Book::get_price() {
return price;
}
void Book::setPrice(const float newPrice) {
price = newPrice;
}
string Book::get_isbn()
{
return isbn;
}
void Book::setIsbn(const string newIsbn)
{
isbn = newIsbn;
}
string Book::get_publisher()
{
return publisher;
}
void Book::setPublisher(const string newPublisher)
{
publisher = newPublisher;
}
string Book::get_author()
{
return author;
}
void Book::setAuthor(const string newAuthor)
{
author = newAuthor;
}
istream& operator>>(istream& input, Book& book)
{
cout << "请输入书名:";
input >> book.name;
cout << "请输入图书价格:";
input >> book.price;
cout << "请输入图书ISBN:";
input >> book.isbn;
cout << "请输入图书的出版社:";
input >> book.publisher;
cout << "请输入作者:";
input >> book.author;
return input;
}
ostream& operator<<(ostream& output, Book& book)
{
output << left << setw(15) << book.name << setw(15) << book.price << setw(15) << book.isbn << setw(15) << book.publisher << setw(15) << book.author << endl;
return output;
}
BookList.cpp
#include "BookList.h"
BookList::BookList()
{
bookNum = 0;
}
void BookList::insert(Book b)//创建图书
{
bookList[bookNum].setName(b.get_name());//依次添加图书
bookList[bookNum].setPrice(b.get_price());
bookList[bookNum].setIsbn(b.get_isbn());
bookList[bookNum].setPublisher(b.get_publisher());
bookList[bookNum].setAuthor(b.get_author());
bookNum++;//图书添加成功后,现有图书数目bookNum+1
}
void BookList::showBook(int i)//打印某一本书
{
//cout << left << setw(15) << bookList[i].get_name() << setw(15) << bookList[i].get_price() << setw(15) << bookList[i].get_isbn() << setw(15) << bookList[i].get_publisher() << setw(15) << bookList[i].get_author() << endl;
cout << bookList[i];//使用输出流重载
}
void BookList::showBook_title()//打印表头
{
cout << left << setw(15) << "书名" << setw(15) << "价格" << setw(15) << "ISBN" << setw(15) << "出版社" << setw(15) << "作者" << endl;
}
void BookList::showAll()//打印所有书本
{
int j;
j = bookNum;
if (j == 0)
{
cout << "本系统暂未存储图书" << endl;
}
else
{
cout << "共有 " << j << " 本图书存储系统中," << "如下:" << endl;
showBook_title();
}
for (bookNum = 0; bookNum < j; bookNum++)
{
showBook(bookNum);
}
}//查找图书信息↓
int BookList::search_name(string Name)//书名查找
{
int i;
for (i = 0; i < bookNum; i++)
{
if (bookList[i].get_name() == Name)
return i;
}
if (i == bookNum)
return -1;
}
int BookList::search_price(float Price)//价格查找
{
int i;
for (i = 0; i < bookNum; i++)
{
if (bookList[i].get_price() == Price)
return i;
}
if (i == bookNum)
return -1;
}
int BookList::search_isbn(string Isbn)//isbn查找
{
int i;
for (i = 0; i < bookNum; i++)
{
if (bookList[i].get_isbn() == Isbn)
return i;
}
if (i == bookNum)
return -1;
}
int BookList::search_publisher(string Publisher)//出版社查找
{
int i;
for (i = 0; i < bookNum; i++)
{
if (bookList[i].get_publisher() == Publisher)
return i;
}
if (i == bookNum)
return -1;
}
int BookList::search_author(string Author)//作者查找
{
int i;
for (i = 0; i < bookNum; i++)
{
if (bookList[i].get_author() == Author)
return i;
}
if (i == bookNum)
return -1;
}
//查找图书信息↑
Book BookList::bookInfor(int i)//获取书本信息
{
return bookList[i];
}
void BookList::update(int i, Book b)//修改图书信息,指定下标
{
bookList[i].setName(b.get_name());//需要修改书的书名来自对象b输入的书名
bookList[i].setPrice(b.get_price());
bookList[i].setIsbn(b.get_isbn());
bookList[i].setPublisher(b.get_publisher());
bookList[i].setAuthor(b.get_author());
}
void BookList::deleted(int i)
{
int j;
for (j = i; j < (bookNum - 1); j++)
{
bookList[j].setName(bookList[j + 1].get_name());//删除后第j本书的新书名来自j+1本的书名,其他同理
bookList[j].setPrice(bookList[j + 1].get_price());
bookList[j].setIsbn(bookList[j + 1].get_isbn());
bookList[j].setPublisher(bookList[j + 1].get_publisher());
bookList[j].setAuthor(bookList[j + 1].get_author());
}
bookNum--;
}
main.cpp
#include <iostream>
#include <iomanip>
#include <conio.h>
#include <stdlib.h>
#include "Book.h"
#include "BookList.h"
using namespace std;
void Show_Menu()
{
cout << "\n===========================\n";
cout << " <<欢迎使用图书管理系统>> \n";
cout << "===========================\n";
cout << "[ 主菜单 ]\n";
cout << "===========================\n";
cout << "【1.添加图书】【2.打印书库】\n\n";
cout << "【3.查找图书】【4.删除图书】\n\n";
cout << "【5.修改图书】【6.退出系统】\n";
cout << "===========================\n";
cout << "请输入对应功能序号:\n";
}
void Show_LowMenu_select()
{
cout << "\n===========================\n";
cout << "[ 二级菜单 ]\n";
cout << "===========================\n";
cout << "1.[书名查询] 2.[价格查询] \n\n";
cout << "3.[ISBN查询] 4.[出版社查询]\n\n";
cout << "5.[作者查询] 6.[序号查询]\n\n";
cout << "7.[返回主菜单] \n";
cout << "===========================\n";
cout << "请输入对应功能序号:\n";
}
void Show_LowMenu_delect()
{
cout << "\n===========================\n";
cout << "[ 二级菜单 ]\n";
cout << "===========================\n";
cout << "1.[书名删除] 2.[ISBN删除] \n\n";
cout << "3.[序号删除] 4.[返回主菜单]\n";
cout << "===========================\n";
cout << "请输入对应功能序号:\n";
}
void Show_LowMenu_update()
{
cout << "\n===========================\n";
cout << "[ 二级菜单 ]\n";
cout << "===========================\n";
cout << "1.[修改书名] 2.[修改价格] \n\n";
cout << "3.[修改ISBN] 4.[修改出版社]\n\n";
cout << "5.[修改作者] 6.[返回主菜单]\n";
cout << "===========================\n";
cout << "请输入对应功能序号:\n";
}
int main()
{
string book_name, book_isbn, book_publisher, book_author;
string new_bookname, new_bookisbn, new_bookpublisher, new_bookauthor;
float book_price;
float new_bookprice;
Book b1;//初始化b1对象,书本
BookList list;//初始化list对象,书本的各项信息
char flag_insert = 'Y', flag_search = 'Y', flag_update = 'Y', flag_delete = 'Y';//设置flag值,用于控制循环
int k;
while (1)
{
//利用switch语句实现图书管理系统菜单
LOOP:Show_Menu();
cin >> k;
switch (k)
{
case 1:
{
cout << "功能:1.【图书录入】" << endl;
system("cls");
cin >> b1;//插入流重载
cout << "是否录入该图书(Y/N):";
cin >> flag_insert;
if (((flag_insert == 'Y') || (flag_insert == 'y')))
{
list.insert(b1);
system("cls");
cout << "已经录入该书本" << endl;
break;
}
else
{
system("cls");
cout << "未录入" << endl;
break;
}
}
case 2:
{
cout << "功能:2.【打印书库】" << endl;
system("cls");
list.showAll();
break;
}
case 3:
{
cout << "功能:3.【查找图书】" << endl;
system("cls");
while (1)
{
Show_LowMenu_select();
cin >> k;
switch (k)
{
case 1:
{
system("cls");
cout << "功能:1.[书名查找]" << endl;
cout << "请输入需要查找的图书名:";
cin >> book_name;
int sel_1 = list.search_name(book_name);
if (sel_1 >= 0)
{
system("cls");
cout << "查询到以下图书:" << endl;
list.showBook_title();
list.showBook(sel_1);
}
else
{
system("cls");
cout << "没有找到该图书信息" << endl;
}
break;
}
case 2:
{
system("cls");
cout << "功能:2.[价格查找]" << endl;
cout << "请输入需要查找的图书价格:";
cin >> book_price;
int sel_2 = list.search_price(book_price);
if (sel_2 >= 0)
{
system("cls");
cout << "查询到以下图书:" << endl;
list.showBook_title();
list.showBook(sel_2);
}
else
{
system("cls");
cout << "没有找到该图书信息" << endl;
}
break;
}
case 3:
{
system("cls");
cout << "功能:3.[ISBN查找]" << endl;
cout << "请输入需要查找的ISBN:";
cin >> book_isbn;
int sel_3 = list.search_isbn(book_isbn);
if (sel_3 >= 0)
{
system("cls");
cout << "查询到以下图书:" << endl;
list.showBook_title();
list.showBook(sel_3);
}
else
{
system("cls");
cout << "没有找到该图书信息" << endl;
}
break;
}
case 4:
{
system("cls");
cout << "功能:4.[出版社查找]" << endl;
cout << "请输入需要查找的出版社名:";
cin >> book_publisher;
int sel_4 = list.search_publisher(book_publisher);
if (sel_4 >= 0)
{
system("cls");
cout << "查询到以下图书:" << endl;
list.showBook_title();
list.showBook(sel_4);
}
else
{
system("cls");
cout << "没有找到该图书信息" << endl;
}
break;
}
case 5:
{
system("cls");
cout << "功能:5.[作者查找]" << endl;
cout << "请输入需要查找的作者名:";
cin >> book_author;
int sel_5 = list.search_author(book_author);
if (sel_5 >= 0)
{
system("cls");
cout << "查询到以下图书:" << endl;
list.showBook_title();
list.showBook(sel_5);
}
else
{
system("cls");
cout << "没有找到该图书信息" << endl;
}
break;
}
case 6:
{
system("cls");
cout << "功能:6.[序号查询]" << endl;
cout << "请输入需要查找的图书序号:";
int sel_6;
cin >> sel_6;
system("cls");
cout << "查询到以下图书:" << endl;
list.showBook(sel_6 - 1);
break;
}
case 7:
{
system("cls");
goto LOOP;
}
int n = getchar();
}
}
}
case 4:
{
cout << "功能:4.【删除图书】" << endl;
system("cls");
while (1)
{
Show_LowMenu_delect();
cin >> k;
switch (k)
{
case 1:
{
system("cls");
cout << "功能:1.[书名删除]" << endl;
cout << "请输入需要删除的书名:";
cin >> book_name;
int del_1 = list.search_name(book_name);
if (del_1 >= 0)
{
list.deleted(del_1);
system("cls");
cout << "图书已删除" << endl;
}
else
{
system("cls");
cout << "没有找到该图书" << endl;
}
break;
}
case 2:
{
system("cls");
cout << "功能:2.[ISBN删除]" << endl;
cout << "请输入需要删除的ISBN:";
cin >> book_isbn;
int del_2 = list.search_isbn(book_isbn);
if (del_2 >= 0)
{
list.deleted(del_2);
system("cls");
cout << "图书已删除" << endl;
}
else
{
system("cls");
cout << "没有找到该图书" << endl;
}
break;
}
case 3:
{
system("cls");
cout << "功能:3.[序号删除]" << endl;
cout << "请输入需要删除的图书序号:";
int del_3;
cin >> del_3;
list.deleted(del_3 - 1);
system("cls");
cout << "该图书已删除";
break;
}
case 4:
{
system("cls");
goto LOOP;
}
int n = getchar();
}
}
}
case 5:
{
cout << "功能:5.【修改图书】" << endl;
system("cls");
while (1)
{
Show_LowMenu_update();
cin >> k;
switch (k)
{
case 1:
{
system("cls");
cout << "功能:1.[修改书名]" << endl;
cout << "请输入需要修改的图书名:";
cin >> book_name;
int upd_1 = list.search_name(book_name);//查找要修改的相应图书信息
Book b = list.bookInfor(upd_1);//返回该本图书的信息
if (upd_1 >= 0)
{
cout << "请输入该图书新的图书名:";
cin >> new_bookname;
b.setName(new_bookname);
list.update(upd_1, b);
system("cls");
cout << "已修改,信息如下:" << endl;
list.showBook_title();
list.showBook(upd_1);
}
else
{
system("cls");
cout << "没有找到该图书信息" << endl;
}
break;
}
case 2:
{
system("cls");
cout << "功能:2.[修改价格]" << endl;
cout << "请输入需要修改价格的图书ISBN:";//因为isbn能够确定是哪本书
cin >> book_isbn;
int upd_2 = list.search_isbn(book_isbn);//查找要修改的相应图书信息
Book b = list.bookInfor(upd_2);//返回该本图书的信息
if (upd_2 >= 0)
{
cout << "请输入该图书新的价格:";
cin >> new_bookprice;
b.setPrice(new_bookprice);
list.update(upd_2, b);
system("cls");
cout << "已修改,信息如下:" << endl;
list.showBook_title();
list.showBook(upd_2);
}
else
{
system("cls");
cout << "没有找到该图书信息" << endl;
}
break;
}
case 3:
{
system("cls");
cout << "功能:3.[修改isbn]" << endl;
cout << "请输入需要修改的ISBN:";//因为isbn能够确定是哪本书
cin >> book_isbn;
int upd_3 = list.search_isbn(book_isbn);//查找要修改的相应图书信息
Book b = list.bookInfor(upd_3);//返回该本图书的信息
if (upd_3 >= 0)
{
cout << "请输入该图书新的ISBN:";
cin >> new_bookisbn;
b.setIsbn(new_bookisbn);
list.update(upd_3, b);
system("cls");
cout << "已修改,信息如下:" << endl;
list.showBook_title();
list.showBook(upd_3);
}
else
{
system("cls");
cout << "没有找到该图书信息" << endl;
}
break;
}
case 4:
{
system("cls");
cout << "功能:4.[修改出版社]" << endl;
cout << "请输入需要修改出版社的图书ISBN:";//因为isbn能够确定是哪本书
cin >> book_isbn;
int upd_4 = list.search_isbn(book_isbn);//查找要修改的相应图书信息
Book b = list.bookInfor(upd_4);//返回该本图书的信息
if (upd_4 >= 0)
{
cout << "请输入该图书新的出版社名:";
cin >> new_bookpublisher;
b.setPublisher(new_bookpublisher);
list.update(upd_4, b);
system("cls");
cout << "已修改,信息如下:" << endl;
list.showBook_title();
list.showBook(upd_4);
}
else
{
system("cls");
cout << "没有找到该图书信息" << endl;
}
break;
}
case 5:
{
system("cls");
cout << "功能:5.[修改作者]" << endl;
cout << "请输入需要修改作者的图书ISBN:";//因为isbn能够确定是哪本书
cin >> book_isbn;
int upd_5 = list.search_isbn(book_isbn);//查找要修改的相应图书信息
Book b = list.bookInfor(upd_5);//返回该本图书的信息
if (upd_5 >= 0)
{
cout << "请输入该图书新的作者名:";
cin >> new_bookauthor;
b.setAuthor(new_bookauthor);
list.update(upd_5, b);
system("cls");
cout << "已修改,信息如下:" << endl;
list.showBook_title();
list.showBook(upd_5);
}
else
{
system("cls");
cout << "没有找到该图书信息" << endl;
}
break;
}
case 6:
{
system("cls");
goto LOOP;
}
int n = getchar();
}
}
}
case 6:
{
system("cls");
cout << "已退出系统" << endl;
exit(0);
}
int n = getchar();
}
}
return 0;
}