Project 11: Exception FlashDrive The purpose of this assignment is to work with
ID: 3665774 • Letter: P
Question
Project 11: Exception FlashDrive
The purpose of this assignment is to work with exceptions. As you may recall, I have provided you with a sample class named FlashDrive ( VS2010 or VS2012 or XCode5 or XCode6 or XCode7 ) which has been diagrammed below. I'd like you to enhance this class so that invoking its methods or operators potentially throw exceptions, rather than just printing error messages to cout. Currently, our favorite exception class is std::logic_error. You can create a logic_error by passing a string value to its constructor. Officially, you should also say #include <stdexcept> to begin working with logic_error, but Visual Studio (being a badly behaved child...) let's you get away with it.
Although the sample driver code might not code for all these circumstances, I would like you to throw exceptions whenever:
a negative number is potentially stored in myStorageCapacity (due to calls to operator - or setCapacity)
a negative number is potentially stored in myStorageUsed (due to calls to operator - or setUsed)
having a myStorageUsed that exceeds myStorageCapacity (due to calls to operator + or writeData or setUsed)
So carefully wind your way thru all the operators and methods of the class ensuring that logic_error gets thrown in each of these circumstances.
HINT: Recall that you can create a logic_error by passing a string message. For example,
std::logic_error error( "Bad News" );
While not required with Visual Studio, please #include <stdexcept> when working with this class. Linux fans will require this include; its optional for Windows users but won't hurt anything if you do it. Here is a class diagram for logic_error. As I said, it already exists so please make use of it.
FlashDrive( );
FlashDrive( int capacity, int used, bool pluggedIn );
void plugIn( );
void pullOut( );
void writeData( int amount );
void eraseData( int amount );
void formatDrive( );
int getCapacity( );
void setCapacity( int amount );
int getUsed( );
void setUsed( int amount );
bool isPluggedIn( );
FlashDrive drive1( 10, 0, false );
FlashDrive drive2( 20, 0, false );
drive1.plugIn( );
drive1.formatDrive( );
drive1.writeData( 5 );
drive1.pullOut( );
drive2.plugIn( );
drive2.formatDrive( );
drive2.writeData( 1 );
drive2.pullOut( );
FlashDrive combined = drive1 + drive2;
cout << "this drive's filled to " << combined.getUsed( ) << endl;
FlashDrive other = combined – drive1;
cout << "the other drives's filled to " << other.getUsed( ) << endl;
if (combined > other) {
cout << "looks like combined is bigger..." << endl;
}
else {
cout << "looks like other is bigger..." << endl;
}
if (drive2 > other) {
cout << "looks like drive2 is bigger..." << endl;
}
else {
cout << "looks like other is bigger..." << endl;
}
if (drive2 < drive1) {
cout << "looks like drive2 is smaller..." << endl;
}
else {
cout << "looks like drive1 is smaller..." << endl;
}
cout << "Here is drive1...." << endl;
cout << drive1 << endl;
cout << "Let's set drive1...." << endl;
cin >> drive1;
cout << "Here is drive1. Notice the changes..." << endl;
cout << drive1 << endl;
// Let's try to get some exceptions...
try {
drive1.writeData( -1000 );
cout << "This should not print out..." << endl;
} catch (std::logic_error) {
cout << "Exception correctly caught..." << endl;
}
try {
drive1.setUsed( -1000 );
cout << "This should not print out..." << endl;
} catch (std::logic_error) {
cout << "Exception correctly caught..." << endl;
}
try {
drive1.setCapacity( -1000 );
cout << "This should not print out..." << endl;
} catch (std::logic_error le) {
cout << "Exception correctly caught..." << le.what( ) << endl;
}
try {
FlashDrive f( 10, 20, false );
cout << "This should not print out..." << endl;
} catch (std::logic_error) {
cout << "Exception correctly caught..." << endl;
}
FlashDrive( );
FlashDrive( int capacity, int used, bool pluggedIn );
void plugIn( );
void pullOut( );
void writeData( int amount );
void eraseData( int amount );
void formatDrive( );
int getCapacity( );
void setCapacity( int amount );
int getUsed( );
void setUsed( int amount );
bool isPluggedIn( );
int my_StorageCapacity;int my_StorageUsed;
bool my_IsPluggedIn;
FlashDrive drive1( 10, 0, false );
FlashDrive drive2( 20, 0, false );
drive1.plugIn( );
drive1.formatDrive( );
drive1.writeData( 5 );
drive1.pullOut( );
drive2.plugIn( );
drive2.formatDrive( );
drive2.writeData( 1 );
drive2.pullOut( );
FlashDrive combined = drive1 + drive2;
cout << "this drive's filled to " << combined.getUsed( ) << endl;
FlashDrive other = combined – drive1;
cout << "the other drives's filled to " << other.getUsed( ) << endl;
if (combined > other) {
cout << "looks like combined is bigger..." << endl;
}
else {
cout << "looks like other is bigger..." << endl;
}
if (drive2 > other) {
cout << "looks like drive2 is bigger..." << endl;
}
else {
cout << "looks like other is bigger..." << endl;
}
if (drive2 < drive1) {
cout << "looks like drive2 is smaller..." << endl;
}
else {
cout << "looks like drive1 is smaller..." << endl;
}
cout << "Here is drive1...." << endl;
cout << drive1 << endl;
cout << "Let's set drive1...." << endl;
cin >> drive1;
cout << "Here is drive1. Notice the changes..." << endl;
cout << drive1 << endl;
// Let's try to get some exceptions...
try {
drive1.writeData( -1000 );
cout << "This should not print out..." << endl;
} catch (std::logic_error) {
cout << "Exception correctly caught..." << endl;
}
try {
drive1.setUsed( -1000 );
cout << "This should not print out..." << endl;
} catch (std::logic_error) {
cout << "Exception correctly caught..." << endl;
}
try {
drive1.setCapacity( -1000 );
cout << "This should not print out..." << endl;
} catch (std::logic_error le) {
cout << "Exception correctly caught..." << le.what( ) << endl;
}
try {
FlashDrive f( 10, 20, false );
cout << "This should not print out..." << endl;
} catch (std::logic_error) {
cout << "Exception correctly caught..." << endl;
}
Explanation / Answer
I have implemented FlashDrive.cpp and .h which I think you would be having already.
I have thrown logic_error with appropriate message in the scenarios mentioned by you and wherever I found it neccessary.
But it would have been better if you would have posted your version of flashdrive.cpp and explained some more scenarios so that I could have provided you with a complete solution.
FlashDrive.h
#include<iostream>
using namespace std;
class FlashDrive
{
public:
FlashDrive();
FlashDrive(int capacity, int used, bool pluggedIn);
~FlashDrive();
void plugIn();
void pullOut();
void writeData(int amount);
void eraseData(int amount);
void formatDrive();
int getCapacity();
void setCapacity(int amount);
int getUsed();
void setUsed(int amount);
bool isPluggedIn();
FlashDrive operator+(FlashDrive& drive);
FlashDrive operator-(FlashDrive& drive);
bool operator<(FlashDrive& drive);
bool operator>(FlashDrive& drive);
friend ostream& operator<<(ostream& os, const FlashDrive& drive);
friend istream &operator>>(istream &input, FlashDrive &drive);
private:
int my_StorageCapacity;
int my_StorageUsed;
bool my_IsPluggedIn;
};
FlashDrive.cpp
#include "FlashDrive.h"
FlashDrive::FlashDrive()
{
}
FlashDrive::FlashDrive(int capacity, int used, bool pluggedIn)
{
my_StorageCapacity = capacity;
my_StorageUsed = used;
my_IsPluggedIn = pluggedIn;
}
FlashDrive::~FlashDrive()
{
}
void FlashDrive::plugIn()
{
my_IsPluggedIn = true;
}
void FlashDrive::pullOut()
{
my_IsPluggedIn = false;
}
void FlashDrive::writeData(int amount)
{
if (isPluggedIn())
{
if (amount >= 0)
{
if ((my_StorageCapacity + amount) <= my_StorageCapacity)
{
setUsed(my_StorageUsed + amount);
}
else
{
throw logic_error("drive does not have enough space to store this data");
}
}
else
{
throw logic_error("amount of data to be written on drive cannot be -ve");
}
}
else
{
cout << "First plugin the drive" << endl;
}
}
void FlashDrive::eraseData(int amount)
{
if (isPluggedIn())
{
if (amount >= 0)
{
if (my_StorageUsed - amount > 0)
{
setUsed(my_StorageUsed - amount);
}
else
{
throw logic_error("Data stored in drive is less than the amount of data you are trying to erase");
}
}
else
{
throw logic_error("amount of data to be erased cannot be -ve");
}
}
}
void FlashDrive::formatDrive()
{
if (isPluggedIn())
{
my_StorageUsed = 0;
}
}
int FlashDrive::getCapacity()
{
return my_StorageCapacity;
}
void FlashDrive::setCapacity(int amount)
{
if (isPluggedIn())
{
if (amount >=0)
{
my_StorageCapacity = amount;
}
else
{
throw logic_error("capacity cannot be negative");
}
}
}
int FlashDrive::getUsed()
{
return my_StorageUsed;
}
void FlashDrive::setUsed(int amount)
{
if (isPluggedIn())
{
if (amount > 0)
{
if (amount > my_StorageCapacity)
{
my_StorageUsed = amount;
}
else
{
throw logic_error("Amount of data you are trying to store is greater than capacity of drive");
}
}
else
{
throw logic_error("Usage of drive cannot be negative");
}
}
}
bool FlashDrive::isPluggedIn()
{
return my_IsPluggedIn;
}
FlashDrive FlashDrive::operator+(FlashDrive& drive)
{
FlashDrive fd;
if ((this->my_StorageUsed + drive.my_StorageUsed) > (this->my_StorageCapacity + drive.my_StorageCapacity))
{
throw logic_error("Cannot combine the drives storage is greater than capacity");
}
fd.my_StorageCapacity = this->my_StorageCapacity + drive.my_StorageCapacity;
fd.my_StorageUsed = this->my_StorageUsed + drive.my_StorageUsed;
return fd;
}
FlashDrive FlashDrive::operator-(FlashDrive& drive)
{
FlashDrive fd;
if ((this->my_StorageCapacity - drive.my_StorageCapacity) < 0)
{
throw logic_error("Storage capacity of drive cannot be less than zero");
}
fd.my_StorageCapacity = this->my_StorageCapacity - drive.my_StorageCapacity;
if ((this->my_StorageUsed - drive.my_StorageUsed) > fd.my_StorageCapacity)
{
throw logic_error("Data usage cannot exceed capacity");
}
fd.my_StorageUsed = this->my_StorageUsed - drive.my_StorageUsed;
return fd;
}
bool FlashDrive::operator<(FlashDrive& drive)
{
if (this->my_StorageCapacity < drive.my_StorageCapacity)
{
return true;
}
return false;
}
bool FlashDrive::operator>(FlashDrive& drive)
{
if (this->my_StorageCapacity > drive.my_StorageCapacity)
{
return true;
}
return false;
}
ostream& operator<<(ostream& os, const FlashDrive& drive)
{
os << "capacity " << drive.my_StorageCapacity << " used " << drive.my_StorageUsed << " plugged in " << drive.my_IsPluggedIn << endl;
return os;
}
istream &operator>>(istream &input, FlashDrive &drive)
{
input >> drive.my_StorageCapacity >> drive.my_StorageUsed >> drive.my_IsPluggedIn;
return input;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.