001: #include <iostream>
002: #include <string>
003: #include <vector>
005: using namespace std;
006: 
007: /**
008:    Describes a product with a description and a price.
009: */
010: class Product {
012: public:  
013: Product(); 014: Product(string d, double p); 015: /** 016: Gets the product description. 017: @return the description 018: */ 019: string get_description() const; 021: /** 022: Gets the product price. 023: @return the unit price 024: */ 025: double get_price() const; 026: 027: private:
028: string description; 029: double price; 030: }; 031: 032: Product::Product() 033: { price = 0; } 036: 037: Product::Product(string d, double p) 038: { description = d; 040: price = p; 041: } 042: 043: string Product::get_description() const 044: { return description; } 047: 048: double Product::get_price() const 049: { return price; } 052: 053: /** 054: Describes a quantity of an article to purchase and its price. 055: */ 056: class Item {
058: public: 059: Item(); 060: Item(Product p, int q); 061: /** 062: Computes the total cost of this item. 063: @return the total price 064: */ 065: double get_total_price() const; 067: /** 068: Prints this item. 069: */ 070: void print() const; 071: private: 072: Product prod; 073: int quantity; 074: }; 075: 076: 077: Item::Item() 078: { quantity = 0; } 081: 082: Item::Item(Product p, int q) 083: { prod = p; 085: quantity = q; 086: } 087: 088: double Item::get_total_price() const 089: { return prod.get_price() * quantity; } 092: 093: void Item::print() const 094: { const int COLUMN_WIDTH = 30; 096: string description = prod.get_description(); 097: 098: cout << description; 100: // pad with spaces to fill column 101: 102: int pad = COLUMN_WIDTH - description.length(); 103: for (int i = 1; i <= pad; i++) cout << " "; 105: 106: cout << prod.get_price() << " " << quantity
108: << " " << get_total_price() << "\n"; 109: } 111: /** 112: Describes a mailing address. 113: */ 114: class Address {
116: public:
117: Address(); 118: Address(string n, string s, 119: string c, string st, string z); 120: /** 121: Prints the address. 122: */ 123: void print() const; 124: private: 125: string name; 126: string street; 127: string city; 128: string state; 129: string zip; 130: }; 131: 132: Address::Address() {} 133: 134: Address::Address(string n, string s, 135: string c, string st, string z) 136: { name = n; 138: street = s; 139: city = c; 140: state = st; 141: zip = z; 142: }
143:
144: void Address::print() const 145: { cout << name << "\n" << street << "\n" 147: << city << ", " << state << " " << zip << "\n";
148: } 150: /** 151: Describes an invoice for a set of purchased products. 152: */ 153: class Invoice { 155: public: 156: Invoice(Address a); 157: /** 158: Adds a charge for a product to this invoice. 159: @param aProduct the product that the customer ordered 160: @param quantity the quantity of the product 161: */ 162: void add(Product p, int quantity); 163: /** 164: Prints the invoice. 165: */ 166: void print() const; 167: private: 168: Address billing_address; 169: vector<Item> items; 170: }; 171: 172: Invoice::Invoice(Address a) 173: { billing_address = a; } 176: 177: void Invoice::add(Product p, int q) 178: { Item it(p, q); 180: items.push_back(it); 181: } 182: 183: void Invoice::print() const 184: { cout << " I N V O I C E\n\n"; 186: billing_address.print(); 187: cout <<
188: "\n\nDescription Price Qty Total\n"; 189: for (int i = 0; i < items.size(); i++) items[i].print(); 191: 192: double amount_due = 0; 193: for (int i = 0; i < items.size(); i++) 194: amount_due = amount_due + items[i].get_total_price(); 195: 196: cout << "\nAMOUNT DUE: $" << amount_due; 197: } 198: 199: int main() 200: { Address sams_address("Sam's Small Appliances", 202: "100 Main Street", "Anytown", "CA", "98765"); 203: 204: Invoice sams_invoice(sams_address); 205: sams_invoice.add(Product("Toaster", 29.95), 3); 206: sams_invoice.add(Product("Hair dryer", 24.95), 1); 207: sams_invoice.add(Product("Car vacuum", 19.99), 2); 208: 209: sams_invoice.print();
210: return 0; 211: }