Using the classes designed in PE 6 and 7, write a program to simulate a bookstor
ID: 3627895 • Letter: U
Question
Using the classes designed in PE 6 and 7, write a program to simulate a bookstore. The bookstore has two types of customers: those who are members of the bookstore and those who buy books from the bookstore only ocasionally. Each member has to pay a 10$ yearly membership fee and recieves a 5% discount on each book purchased.For each member, the bookstore keeps track of the number of books purchased and the total amount spent. For every eleventh book that a member buys, the bookstore takes the average of the total amount of the last 10 books purchased, applies this amount as a discount, and then resets the total amount spent to 0.
Write a program that can process up to 1000 book titles and 500 members. Your program should contain a menu that gives the user different choices to effectively run the program; in other words, your program should be user driven.
Here is the code for the bookType class:
#include <string>
using namespace std;
class bookType
{
public :
bookType();
void setTitle( string );
string getTile();
bool compareTitle( string );
void setAuthor( string = "");
void showAuthors();
void updateAuthor( string = "");
string *getAuthors();
void setCopies( int );
void showCopies();
void updateCopies( int );
int getCopies();
void setPublisher( string );
void showISBN();
void updateISBN( string );
string getISBN()
bool compareISBN(string);
void setPrice(double);
void showPrice();
void updatePrice(double);
double getPrice();
private:
string title;
string authors[4];
string publisher;
string ISBN;
double price;
int copies;
int authorNo;
};//ends class definition of booktype
//implements file bookTypeImp.cpp
#include<iostream>
#include"bookType.h"
using namespace std;
bookType ::bookType()
{
title = " ";
for (int i=0; i<4;i++)
authors[i] = "";
publisher = "";
ISBN = "";
price = 0;
copies = 0;
authorsNo = 0;
}//end constructor bookType
void bookType::setTitle(string myTitle)
{
title = myTitle;
}//end function setTitle
string bookType:: getTitle()
{
return title;
}//end function getTitle;
bool bookType::compareTitle(string otherTitle)
{
return (title.compare(otherTitle)==0);
}//end function compareTitle
void bookType::setAuthor(string myAuthor)
{
authorsNo = authorsNo % 4;
// check for valid author name
if(myAuthor.compare("")==0)
return;
else
{
//store the author name
authors[authorsNo] = myAuthor;
//keep track of authors count
authorsNo++
}//end else
}//end function authors
void bookType::showAuthors()
{
for (int i=0; i<authorsNo; i++)
cout << authors[i] << ", ";
cout << " ";
}//end function showAuthors
void bookType::updateAuthor(string myAuthor)
{
setAuthor(myAuthor);
}//end function updateAuthor
string *bookType::getAuthors()
{
return authors;
}//end function get authors
void bookType::setCopies(int myCopies)
{
copies = myCopies;
}//end function setCopies
void bookType::showCopies()
{
cout << " The number of copies is " << copies;
}//end function showCopies
void bookType::updateCopies(int myCopies)
{
copies = myCopies;
}//end function updateCopies
int bookType::getCopies()
{
return copies;
}//end function get copies
void bookType::setPublisher(string myPublisher)
{
publisher = myPublisher;
}//end function setPublisher
void bookType::showPublisher()
{
cout << publisher;
}//end function showPublisher
void bookType::updatePublisher(string myPublisher)
{
publisher = myPublisher;
}//end function updatePublisher
string bookType::getPublisher()
{
return publisher;
}//end function getPublisher
void bookType::setISBN(string myISBN)
{
ISBN = myISBN;
}//end function setISBN
void bookType::showISBN()
{
cout << ISBN;
}//end function showISBN
void bookType::update ISBN(string myISBN)
{
ISBN = myISBN;
}//end function updateISBN
string bookType::getISBN()
{
return ISBN;
}//end function getISBN
bool bookType::compareISBN(string my ISBN)
{
return (myISBN.compare(ISBN)==0);
}//end function compareISBN
void bookType::setPrice(double myPrice)
{
price = myPrice;
}//end function setPrice
void bookType::showPrice()
{
cout<<" The book price is " << price;
}//end function showPrice
void bookType::update(double myPrice)
{
price = myPrice;
}// end function updatePrice
double bookType::getPrice()
{
return price;
}//end function get price
And here is the code for the memberType class
//Header File Section
#include "stdafax.h"
#include <iostream>
#include <string>
using namespace std;
class memberType
{
public: string Name;
string ID;
int no_books;
float amount;
memberType()
{
Name=" ";
ID = " ";
no_books = 0;
amount = 0.0;
}
public void set(string name, string id, int no, float a);
void modify(string name);
void modify(int no, float a);
void show();
};
void memberType::set(string n, string id, int no, float a)
{
Name = n;
ID = id;
no_books = no;
amount = a;
}
void memberType::modify(string name)
{
Name=name;
}
void memberType::modify(int no,float a)
{
no_books=no;
amount = a;
}
void memberType::show()
{
cout<<"Person's Name is: " << Name << endl;
cout<<"Number of Books: " << no_books << endl;
cout<<"Amount Spent: " << amount<<endl;
}
Explanation / Answer
/** The TestScoreReader class reads test scores as tokens from a file and calculates the average of each line of scores. */ public class Book { private FileReader freader; private BufferedReader inputFile; private String line; /** The constructor opens a file to read the grades from. @param filename The file to open. */ public Book(String filename) throws IOException { freader = new FileReader(filename); inputFile = new BufferedReader(freader); } /** The readNextLine method reads the next line from the file. @return true if the line was read, false otherwise. */ public boolean readNextLine() throws IOException { boolean lineRead; // Flag variable // Get the next line. line = inputFile.readLine(); // Determine whether the line was read. if (line != null) lineRead = true; else lineRead = false; return lineRead; } public String tokenTime() throws IOException { String name; String author; String publisher; String isbn; String price; String str; StringTokenizer token = new StringTokenizer(line, ","); name = token.nextToken(); author = token.nextToken(); publisher = token.nextToken(); isbn = token.nextToken(); price = token.nextToken(); str = "Name: " + name + " Author: " + author + " Publisher: " + publisher + " ISBN: " + isbn + " Price: RM" + price; return str; } /** The close method closes the file. */ public void close() throws IOException { inputFile.close(); } } the body... import java.io.*; // Needed for IOException import javax.swing.JOptionPane; /** This program uses the TestScoreReader class to read test scores from a file and get their averages. */ public class Demo { public static void main(String[] args) throws IOException { // Create a TestScoreReader object. Book scoreReader = new Book("as.txt"); Member member = new Member("mem.txt"); // Display the averages. while (scoreReader.readNextLine()) { JOptionPane.showMessageDialog(null, scoreReader.tokenTime() , "BOOK", JOptionPane.INFORMATION_MESSAGE); while (member.readNextLine()) { //JOptionPane.showMessageDialog(null, member.tokenTime() , "MEMBER", JOptionPane.INFORMATION_MESSAGE); } } // Close the TestScoreReader. scoreReader.close(); } } the books is refer to here... Starting Out with Java, Gaddis and Muganda, Pearson, 890-345-566, 76.90 Modul Hubungan Etnik, Shamsul Amri Baharudin, Pusat Penerbitan Universiti(UPENA), 234-234-344, 20.00 and this is for the member (Toggle Plain Text) import java.io.*; import java.util.StringTokenizer; /** The TestScoreReader class reads test scores as tokens from a file and calculates the average of each line of scores. */ public class Member { private FileReader freader; private BufferedReader inputFile; private String line; /** The constructor opens a file to read the grades from. @param filename The file to open. */ public Member(String filename) throws IOException { freader = new FileReader(filename); inputFile = new BufferedReader(freader); } /** The readNextLine method reads the next line from the file. @return true if the line was read, false otherwise. */ public boolean readNextLine() throws IOException { boolean lineRead; // Flag variable // Get the next line. line = inputFile.readLine(); // Determine whether the line was read. if (line != null) lineRead = true; else lineRead = false; return lineRead; } public String tokenTime() throws IOException { String str; String name; String memberID; String noBooks; String amountSpent; StringTokenizer token = new StringTokenizer(line, ","); name = token.nextToken(); memberID = token.nextToken(); noBooks = token.nextToken(); amountSpent = token.nextToken(); str = "Name: " + name + " ID: " + memberID + " No of books bought: " + noBooks + " Amount Spent: " + amountSpent; return str; } /** The close method closes the file. */ public void close() throws IOException { inputFile.close(); } }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.