C++作业代写:lab作业 实现一个online store的fruit ordering system

要求用C++实现一个在线商店的水果预定系统。要求可以随机生成不同水果的价格和数量,在预定的时候计算价格和sales tax, 并减少水果库存直至卖光。主要考察Object Oriented Programming和C++的一些基本用法。

Program Specifications

This program is to implement a fruit ordering system for an online supermarket.

Its purpose is to illustrate the use of

  • The this pointer
  • Arrays of objects
  • Elementary Sorting
  • Binary search
    Class Design

You need to have at least the following classes described below. Feel free to come up with more classes if needed.

class OnlineSuperMarket

  • Private member data: an array of 8 Fruity objects, a string for market name (Foothill Super Market for example), a string for web address (http://www.foothillmarket.com for example).
  • Private static data: tax_rate (initialize to 0.085)
  • Constructors

    • Default constructor: no-op
    • Non-default constructor: takes two parameters for market name and web address. Must use member initialize syntax in the constructor
  • Destructor: Output a message “Online super market <name> at <web address> is closed …”

  • Public member functions:

    • accessor/mutator for market name and web address
    • Init: Use a loop to randomly generating different kinds of Fruits, weight, and price according to the table RADNDOM FRUIT at the end of the specs. Additionally in the loop body invoke Fruit setters (set name, weight, unit price) using function chaining for the current Fruit object element.
    • Sort (take no parameter): sort the array of Fruit objects in ascending order by Fruit Name. Must use the sorting algorithm presented in class lecture
    • Run (take no parameter): showing some advertising banner about online super market and web address, then using an infinite loop to start asking user to either order fruits or to quit. If user selects quit (“XXX”) then invoke the Quit function. If user selects Fruit Ordering ask user for Fruit Name and weight. Invoke Find function with the input FruitName. If the Fruit is not found output an error message. If the Fruit is found invoke Order funtion from the found Fruit object (using the returned index from Find function). If Order function returns -1 (the requested purchase weight exceeds the Fruit available weight) output an error message. Otherwise compute the total cost (with tax - use the public static get_tax_rate function) to show to the customer and back to menu again.
    • Find (take a string as Fruit Name): search for the FruitName from FruitList. The function returns an index to the found Fruit object in the array or -1 if the Fruit is not found. Since the array of Fruit objects is sorted you can use either binary search or use an improved linear search that should stop searching if the element in the array is greater than the target fruit name
    • Quit(take no parameter) : output a “Thanks for your visit and come again.” message then terminate the program
    • ShowAll: display all fruits using Fruit object Display function
  • Public static function: get_tax_rate returns the tax_rate
    class Fruit

  • Private member data: a string for fruit name, float for weight (in lbs), and float for unit price

  • Constructors:

    • Default constructor: set weight and unit price to 0.0
    • Non-default constructor: takes three parameters. Must use member initialize syntax
  • Destructor: output a message “Destroyed Fruit …., Weight = ….”

  • Public member functions:

    • accessor/mutator for all private member data: for all the setters (mutators) implement chaining functions for those functions that will return the Fruit object itself after the setting
    • Order: take a float as fruit weight. If the requested purchase weight is more than what’s currently available return -1. Otherwise update the weight data member (purchase is approved now) then compute and return the cost of Fruit purchase (input weight * unit price)
    • Display: display fruit name, weight, and unit price in nice format with setw and setprecision (2) and fixed
      Implementation Requirements
  • In main program

    • Declare a pointer to an OnlineSuperMartket object and properly initialize it to NULL
    • Dynamically allocate an OnlineSuperMartket object (using non-default constructor) and assigned it to the pointer above
    • Declare an OnlineSuperMartket pointer and properly
    • Invoke Init using the OnlineSuperMartket pointer
    • Invoke Sort
    • Invoke ShowAll using the OnlineSuperMartket pointer (to verify if the array is sorted by Fruit names)
    • Invoke Run
    • De-allocate the memory of the OnlineSuperMartket object

Sample Output

// List of Fruits in sorted order by Fruit names with weight and price

Your most convenient and time savings way to order food and fruit from





Enter a fruit name or XXX to end: Apple

Enter weight in lbs: 2

Total cost: $7.60 (tax included)

Enter a fruit name or XXX to end: XXX

Lab submission: same as before


  • Good path: order the first Fruit in the array, order the last Fruit, order a Fruit in the middle
  • Error path: order a Fruit that is not in the array, order a Fruit with requested weight higher than what’s currently available
  • Extreme path: keep ordering the same Fruit until it’s “out-of-stock” and no longer available for ordering


Random numberFruit nameLO (price)HI (price)Weight
0Banana0.500.85Random between 10.0-50.0
1Strawberry2.103.50Random between 10.0-50.0
2Orange3.003.50Random between 10.0-50.0
3Grapes2.154.50Random between 10.0-50.0
4Watermelon0.200.45Random between 10.0-50.0
5Apple1.503.20Random between 10.0-50.0
6Blueberry3.04.50Random between 10.0-50.0
7Mango0.751.25Random between 10.0-50.0
kamisama wechat