Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

Programming Assignment # 5

Due date:  Wednesday, August 9th, 2023 11:59pm

(NO LATE SUBMISSION IS ACCEPTED - MUST TURN IT IN (complete or incomplete)

ON DUE DATE OR SCORE 0 WILL BE ASSIGNED)

Assignment's score: 100 points

Program Specifications

This program is to implement a fruit ordering system for an online supermarket store. Customers will keep ordering the fruit of their choices while 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 as instance fields of a class

  Elementary Sorting

.  Binary search

Class Design

At minimum you should implement the classes described below. Feel free to come up with more classes as necessary.

class <your choice of class name>: this class contains ONLY the public static main method. You must select a descriptive name for the class based on what the program is set out to do. There will be no constructor or any instance fields should be defined for this class. Below is the logic or algorithm for the main method:

  Declare an OnlineSuperMartket object reference and properly initialize it to null

·  Use new to allocate an OnlineSuperMartket object (using the non-default constructor) and assign it to the object reference above (you may hard-code the store name and the web site URL)

  Invoke showFruits method (should show default Fruit objects now)

  Invoke init method

.  Invoke showFruits method again (should show "meaningful" or "real" Fruits now - unsorted such as "Cherry" "Watermelon" "Orange"   .... "Banana"

  Invoke sort method

  Invoke showFruits method again (Fruits should be sorted such as "Apple"  "Banana" "Cherry" ... "Orange"  .... "Watermelon"

.  Invoke run

class Fruit

  Private instance fields: a String for fruit name, a double for total weights (in lbs), and a double for price per lbs.

.  Public constant static fields:

default value for fruit name ("?")

default values for total weights and price per lbs (0.0).

  Public Constructors:

Default constructor: initialize the instance fields to the static fields' default values.

Non-default constructor: takes three parameters (must have same name as the instance field

names). It will properly initialize the instance fields using the this reference syntax. .  Public instance methods:

accessor/mutator for all instance fields. The mutators must return the this reference.

order: take a double representing the ordered weight (in lbs) of fruit as its only parameter. If the ordered weight is more than the total weight currently available return - 1. Otherwise

update the fruit total weight instance field (order is accepted, subtracting the total weight by the ordered weight) then compute and return the order cost without tax (ordered weight in    lbs * price per lbs).

  equals: see lecture (https://foothillcollege.instructure.com/courses/24171/pages/finally-a-must- implement-the-tostring-and-equals-methods?module_item_id=2169099) on how to do it. Two

Fruit objects are equal if they have same name, same total weights and same price per lbs.

toString: returning a String containing fruit name, total weight in lbs and price per lbs (use %5.2f).

class OnlineSuperMarket

.  Private instance fields: an array of Fruit objects (more accurately: a reference to a Fruit array - I repeat: this is just a reference to an array of Fruits), a String for supper market store name

(Foothill Super Market for example), a String for the store's web address

(http://www.foothillmarket.com      (http://www.xyz.com)  for example).

  Public constant static fields: tax rate (initialized to 0.0975), array size (initialized to 10), store name ("TBD"), web address ("www.unknown.com")

  Public Constructors

Default constructor: allocate memory for the Fruit array then initialize its elements with   default Fruit objects. Also initialize store name and web address using the corresponding static fields.

Non-default constructor: takes two parameters for market name and web address (same

name as the instance field names). Do the same array initialization as specified in the default constructor. Must use the this reference wherever applicable.

  Public instance methods:

accessor (no mutator is asked for) for store name and web address. Note: do not provide accessor/mutator for the Fruit array instance field.

init: this method takes no parameter and is to properly initialize the Fruit array with

"meaningful" Fruit objects as at creation of this OnlineSuperMarket object the Fruit array simply consists of default Fruit objects (see constructors' specs to verify this claim as

needed).

  declare 3 arrays locally to this init method: a String array of fruit names of your choice, a double array of total weights and a double array of prices per lbs. Each array must

contain exactly 10 elements (Example:  int x [ ] = {   1, 2, 3, 4, 5, 6, 7, 8, 9, 10};)

·  Use a loop to assign "meaningful" values to the Fruit objects' instance fields in the array. Use chaining mutators to set fruit name, total weight and price per lbs. If you don't

know how to provide chaining mutator method you may set the Fruit's attributes

individually (using the mutators to set one attribute at a time with some point deduction).

o   sort (take no parameter): sort the array of Fruit objects in ascending order by fruit name.

Must use the elementary sorting algorithms presented in class modules with some

modification to fit the assignment. Library sort methods such as Arrays.sort are not allowed.

run (take no parameter):  showing some advertising banner about online super market store and web address, then using an infinite loop to start asking user to either order fruits or to   quit. If user enters “Q” or "q" (no longer want to order fruit) then invoke quit ( ) method

before breaking out of the loop. If users enter a Fruit name check to see if the Fruit is there in the array using the find ( ) method.  Display an error message if the Fruit is not in the

array then back to the beginning of the loop. Otherwise ask for the total weight in lbs of the   order. Then invoke the order ( ) instance method of the found Fruit object. If the found Fruit object's 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 tax rate) to show to the customer (Fruit:   Weight:    Price:   Total cost (plus  tax): ) and back to the loop again.

showFruits: Use the enhanced for loop on the Fruit array to display all fruits (implicitly

invoke the toString ()  method - code like System.out.println ( fruitList [ i ].toString ( ) )

may get point deduction). If the standard/regular for loop is used some deduction may apply.

  Private instance methods (meant to be used internally within the class itself. Users of this class should not have access to them)

quit (take no parameter) : display all Fruits (do not need a for loop here - Hint: within a Java class instance methods can invoke other instance methods) then output the message “Thanks for your visit. Please come again!” .

find (take a String as fruit Name): search for the fruit from the 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. Do not use any library search method.

Visualization of

OnlineSupermarket object at instantiation (Fruit array contains default Fruit objects)

 

OnlineSupermarket object after invoking init () (Fruit array contains unsorted Fruit objects with meaningful data)

 

OnlineSupermarket object after invoking init () then sort () (Fruit array contains sorted Fruit objects with meaningful data)

 

Implementation Requirements

  Must follow Google style for naming convention for classes, methods, variables, static fields, instance fields, instance methods etc ...

.  Constructors, instance fields, static fields/methods, instance methods must be declared with the correct access specifiers (private or public). Missing access specifier for those will get heavy     points deduction.

  Follow the specs as close as you can. If you need clarification or would like to design and/or

implement the program differently you're welcome to. However it must be communicated prior. Otherwise your submission may be deemed as missing specs resulting in heavy point

deductions.

  It's required to use class_name. as prefix whenever accessing static fields:

class_name.static_field even though class_name. prefix syntax might not be required syntactically due to the static fields being referred to is in the class scope.

Sample Output

// List of Fruits with default values then List of Fruits in sorted order by Fruit names (didn't show in this sample)

Your most convenient and time savings way to order fruit from

FOOTHILL CS SUPER MARKET


FRUIT ORDERING

Enter a fruit name or Q (or q) to end: Apple

Enter weight in lbs:  2

You ordered:

Fruit: Apple

Weight:   2.5 lbs

Price:   $7.60/lbs

Total cost (plus tax):  $20.81

Enter a fruit name or Q (or q) to end: q

// List of all Fruits here

Thanks for your visit and please come again!!!

Testing

It's required that the output submission be sufficiently showing the test cases shown below or point deduction may apply. Post questions in Discussions Forum for clarification as needed.

  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 request a weight that's higher

than what's currently available

  Extreme path:

order an existing Fruit with weight == 0

keep ordering the same Fruit until it's "out-of-stock" (i.e. weight==0) and no longer available for ordering then try to order it one more time.

How to submit your assignment: same as previous assignments

Late submission policy: No late is accepted. Must submit the assignment on the indicated due date. You will be assigned a score 0 for any late submission.