This program is to implement a fruit ordering system for an


Program Specifications

This program is to implement a fruit ordering system for an online supermarket. Customers will keep ordering the fruit of their choices and the system will compute the total cost per order until they decide to quit.

Its purpose is to illustrate the use of

  • Java classes
  • the this reference
  • 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 XYZ which contains only the main method (similar to assignment 5)

class OnlineSuperMarket

  • Private instance fields: an array of Fruit objects, a String for market name (Foothill Super Market for example), a String for web address (https://www.foothillmarket.com (Links to an external site.)
  • Links to an external site.
  •  for example).
  • Private constant static field: taxRate (initialized to 0.085)
  • Private constant static field for array size (initialized it to 10)
  • Constructors
  • Default constructor: allocate memory for the Fruit array and its default Fruit objects using the static field array size
  • Non-default constructor: takes two parameters for market name and web address (same name as the instance field names). Do the same array allocation as the default constructor. Must use the this reference.
  • Public instance methods:
  • accessor/mutator for market name and web address
  • init: declare a local String array and manually initialize it with fruit names of your choice. Use a loop to properly initialize the Fruit array instance field for each Fruit object (name, weight, and price where weight and price are randomly generated similar to assignment 4. You must set HI and LO for each Fruit differently as needed.
  • sort (take no parameter): sort the array of Fruit objects in ascending order by fruit Name. Must use one of the elementary sorting algorithms presented in class modules with some modification to fit the assignment. Library sort methods are not allowed
  • 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 method. If user selects Fruit Ordering ask user for fruit Name and weight. Invoke Find method with the input fruitName as parameter. If the Fruit is not found output an error message. If the Fruit is found invoke the order instance method of the found Fruit object. If the order method returns -1 (the requested purchase weight exceeds the Fruit's available weight) output an error message. Otherwise compute the total cost (with tax - use the static field taxRate) to show to the customer (Fruit:  Weight:   Price: Total (plus tax): ) and back to the menu again.
  • find (take a String as fruit Name): search for the fruit Name from Fruit array. The method returns a reference to the found Fruit object in the array or null if the Fruit is not found. Since the array of Fruit objects is sorted you must use binary search to locate the Fruit. Use the binary search algorithm in the class module with some modification to do the work.
  • quit(take no parameter) : display all Fruits (make sure you come up with the correct way to do this or you may encounter point deduction) and output a "Thanks for your visit and please come again." message then terminate the program
  • showFruits: display all fruits (implicitly use the toString method) must use the new for loop syntax

class Fruit

  • Private instance fields: a String for fruit name, a double for weight (in lbs), and a double for unit price
  • Constructors:
  • Default constructor: set fruit name to "?", set weight and unit price to 0.0
  • Non-default constructor: takes three parameters (same name as the instance field names) and properly initialize the instance fields using the this reference syntax.
  • Public instance methods:
  • accessor/mutator
  • order: take a double as fruit weight. If the requested purchase weight is more than what's currently available return -1. Otherwise update the weight instance field (purchase is approved now) then compute and return the cost of Fruit purchase (input weight * unit price)
  • toString: display fruit name, weight, and unit price in nice format (%5.2f or so etc ...)

Implementation Requirements

  • In main method
  • Declare an OnlineSuperMartket object reference and properly initialize it to NULL
  • Use new to allocate an OnlineSuperMartket object (using non-default constructor) and assign it to object reference above
  • Invoke init method
  • Invoke sort method
  • Invoke showFruits method
  • Invoke run

Sample Output

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

                Your most convenient and time savings way to order fruit from

                                   FOOTHILL CS SUPER MARKET

                                   https://www.fh.cs.supermarket.com                            

                                    FRUIT ORDERING

                       Enter a fruit name or XXX to end: Apple

                       Enter weight in lbs: 2

                      You ordered:

                       Fruit: Apple

                       Weight in lbs: 2

                      Price:  $7.60 

                       Total cost (plus tax): $8.25

                      Enter a fruit name or XXX to end: XXX

                      // List of all Fruits

                      Thanks for your visit and please come again!!!

Testing (your output submission must sufficiently show the results of your testing)

  • 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" (i.e. weight==0) and no longer available for ordering

Solution Preview :

Prepared by a verified Expert
Basic Computer Science: This program is to implement a fruit ordering system for an
Reference No:- TGS02340018

Now Priced at $30 (50% Discount)

Recommended (96%)

Rated (4.8/5)