辅导program、讲解Java,c/c++程序设计、Python编程辅导
- 首页 >> Java编程 Complete the provided program by coding the class named Library and a global function named AddGST.
Deliverables
Design and define a class named Library. 15 Marks
Design and define a global function named AddGST 7.5 Marks
Upgrade the main function of the program to use a smart pointer. 7.5 Marks
Update the main function to handle exceptions as required. 5 Marks
The output produced by this program using your solution should match the one provided in this Question. 5 Marks
AddGST Function Requirements
• Your global function named AddGST function receives iterators to the beginning and end of a range of Games objects
• For each Game in the range, this function adds GST to the price and stores the result in the price_w_tax instance
variable.
struct Game {
std::string description;
unsigned year;
double price;
double price_w_tax;
};
Library Class Requirements
Your Library class includes the following instance variables:
• an STL container that holds Game objects
• a boolean that holds the status of the Game price data (before or after adding GST)
• an overloaded += operator that adds a Game object to the Library object and returns a reference to the current
object
o only if the original date of the Game is the cutoff year or greater (CUTOFF_YEAR); otherwise this operator
does nothing and throws an exception,
o update main to handle the exception by printing a message as shown in the sample output.
• a display function that receives two parameters
o the address of one of the following global functions that displays the requested information
➢ byName
➢ byYear
o the address of the heading to be displayed.
This function displays the heading and, for each Game, calls the first parameter to print the label followed by the
price_w_tax. If GST has not been added to the price of each Game in the Library, before printing anything, this
function adds the tax using two STL threads. Each thread processes half of the elements from the container.
Output Requirements
• The output produced by this program using your coding solution should be:
Game is too old: Game of Life
By Name
Monopoly : 25.39
Battleship : 19.20
Candyland : 21.71
Clue : 50.84
Sorry : 19.20
By Year
1903: 25.39
1914: 19.20
1949: 21.71
1950: 50.84
Other Requirements
• Upgrade the main function of the program to use a smart pointer.
Provided Program
#include
#include
#include
constexpr double GST{ 0.13 };
constexpr unsigned CUTOFF_YEAR{ 1900 };
struct Game {
std::string description;
unsigned year;
double price;
double price_w_tax;
};
void byName(const Game& game) {
std::cout << game.description;
}
void byYear(const Game& game) {
std::cout << game.year;
}
int main() {
Game game[]{
{"Monopoly ", 1903, 22.47},
{"Game of Life", 1860, 19.93},
{"Battleship ", 1914, 16.99},
{"Candyland ", 1949, 19.21},
{"Clue ", 1950, 44.99},
{"Sorry ", 2013, 16.99}
};
Library* boardgames = new Library();
for (const auto& bg : game) {
*boardgames += bg;
}
std::cout << std::fixed << std::setprecision(2);
boardgames->display(byName, "\nBy Name");
boardgames->display(byYear, "\nBy Year");
delete boardgames;
}
Deliverables
Design and define a class named Library. 15 Marks
Design and define a global function named AddGST 7.5 Marks
Upgrade the main function of the program to use a smart pointer. 7.5 Marks
Update the main function to handle exceptions as required. 5 Marks
The output produced by this program using your solution should match the one provided in this Question. 5 Marks
AddGST Function Requirements
• Your global function named AddGST function receives iterators to the beginning and end of a range of Games objects
• For each Game in the range, this function adds GST to the price and stores the result in the price_w_tax instance
variable.
struct Game {
std::string description;
unsigned year;
double price;
double price_w_tax;
};
Library Class Requirements
Your Library class includes the following instance variables:
• an STL container that holds Game objects
• a boolean that holds the status of the Game price data (before or after adding GST)
• an overloaded += operator that adds a Game object to the Library object and returns a reference to the current
object
o only if the original date of the Game is the cutoff year or greater (CUTOFF_YEAR); otherwise this operator
does nothing and throws an exception,
o update main to handle the exception by printing a message as shown in the sample output.
• a display function that receives two parameters
o the address of one of the following global functions that displays the requested information
➢ byName
➢ byYear
o the address of the heading to be displayed.
This function displays the heading and, for each Game, calls the first parameter to print the label followed by the
price_w_tax. If GST has not been added to the price of each Game in the Library, before printing anything, this
function adds the tax using two STL threads. Each thread processes half of the elements from the container.
Output Requirements
• The output produced by this program using your coding solution should be:
Game is too old: Game of Life
By Name
Monopoly : 25.39
Battleship : 19.20
Candyland : 21.71
Clue : 50.84
Sorry : 19.20
By Year
1903: 25.39
1914: 19.20
1949: 21.71
1950: 50.84
Other Requirements
• Upgrade the main function of the program to use a smart pointer.
Provided Program
#include
#include
#include
constexpr double GST{ 0.13 };
constexpr unsigned CUTOFF_YEAR{ 1900 };
struct Game {
std::string description;
unsigned year;
double price;
double price_w_tax;
};
void byName(const Game& game) {
std::cout << game.description;
}
void byYear(const Game& game) {
std::cout << game.year;
}
int main() {
Game game[]{
{"Monopoly ", 1903, 22.47},
{"Game of Life", 1860, 19.93},
{"Battleship ", 1914, 16.99},
{"Candyland ", 1949, 19.21},
{"Clue ", 1950, 44.99},
{"Sorry ", 2013, 16.99}
};
Library* boardgames = new Library();
for (const auto& bg : game) {
*boardgames += bg;
}
std::cout << std::fixed << std::setprecision(2);
boardgames->display(byName, "\nBy Name");
boardgames->display(byYear, "\nBy Year");
delete boardgames;
}