Preference: An Enumerated Type

  • Enumerations (Section 19.3)
  • Types (Section 19.5)
  • Conversions (Section 19.6)


Your assignment is to design, implement, and test a Preference class.

Preference is a class that models one field in an ID3 tag. It is intended to permit the user to specify the quality of an MP3 file (e.g., excellent, good, fair, poor). We need a uniform preference system that will enumerate a fixed set of value choices so that comparisons and subrange queries are possible.

Here is the public interface of the class definition to get you started.

public:
 Preference(int value=0);
 Preference(QString prefstr);
 /**
 If possible, set the host value from the given string and
 @return true. Otherwise return false.
 */
 bool fromString(QString);
 /**
 @return a list of all the acceptable Preference names,
 ordered by value (increasing)
 */
 virtual QStringList getNames() const;


The Assignment

  • Complete the class definition in preference.h.
  • Implement all the functions in preference.cpp so that your class can pass the test case below.
  • Write a main.cpp that calls this test case and tests your Preference class.
  • Generate or write a qmake project file that can build the application.
  • Verify that make dist creates a dist target (a tarball).

A test case that shows how the Preference class must work is provided in Example 25.2. Your Preference class must pass that test.

Example 25.2. ../src/libs/filetagger/testpreference.cpp

#include "testpreference.h"
#include "preference.h"
#include "qstd.h"
using namespace qstd;

void TestPreference::test() {

 Preference verygood("Very Good");
 Preference verygood2("Very Good");
 Preference excellent("Excellent");
 Preference fair("Fair");
 Preference good("Good");

 Preference none("None");
 Preference poor("Poor");
 Preference badtaste("Bad Taste");
 Preference undefined("undefined");
 ASSERT_EQUALS(verygood, verygood2);
 ASSERT_NOTEQUALS(verygood, fair);

 ASSERT_EQUALS(undefined, 0);
 ASSERT_TRUE(none > undefined);
 ASSERT_TRUE(poor < none);
 ASSERT_TRUE(badtaste < poor);
 ASSERT_TRUE(verygood > good);
 ASSERT_TRUE(good > fair);
 ASSERT_TRUE(fair > none);
 ASSERT_TRUE(fair < verygood);
 ASSERT_TRUE(verygood < excellent);

 ASSERT_EQUALS(verygood.toString(), "Very Good");

 qDebug() << verygood.toString();
 Preference q("notsogood");
 ASSERT_EQUALS(0, (int)q);
 qDebug() << q.getNames().join(", ");

 /* Optional - Case Ignore conversions? */

 // Does this print "fair is fair" or "fair is 4"?
 cout << "Fair is " << fair;

 Preference verygoodlc("very good");
 ASSERT_EQUALS(verygood, verygoodlc);

}

The header file is provided to you in Example 25.3 for completeness.

Example 25.3. ../src/libs/filetagger/testpreference.h

#include 
#include <assertequals.h>

class TestPreference : public TestCase {
 TestPreference() : TestCase("TestPreference") {}

protected:
 virtual void test() ;
};

When the test case is run, the output should look like this.


Very Good
Undefined, Bad Taste, Poor, None, Fair, Good, Very Good, Excellent

./testpreference.cpp:44: assertequalsfailed:
 expr var="verygood" val="6" , expr var="verygoodlc" val="0"
 

Fair is Fair




Part I: Introduction to C++ and Qt 4

C++ Introduction

Classes

Introduction to Qt

Lists

Functions

Inheritance and Polymorphism

Part II: Higher-Level Programming

Libraries

Introduction to Design Patterns

QObject

Generics and Containers

Qt GUI Widgets

Concurrency

Validation and Regular Expressions

Parsing XML

Meta Objects, Properties, and Reflective Programming

More Design Patterns

Models and Views

Qt SQL Classes

Part III: C++ Language Reference

Types and Expressions

Scope and Storage Class

Statements and Control Structures

Memory Access

Chapter Summary

Inheritance in Detail

Miscellaneous Topics

Part IV: Programming Assignments

MP3 Jukebox Assignments

Part V: Appendices

MP3 Jukebox Assignments

Bibliography

MP3 Jukebox Assignments



An Introduction to Design Patterns in C++ with Qt 4
An Introduction to Design Patterns in C++ with Qt 4
ISBN: 0131879057
EAN: 2147483647
Year: 2004
Pages: 268

Flylib.com © 2008-2020.
If you may any questions please contact us: flylib@qtcs.net