Java代写:CS603 Gumdrop Machine

代写一个Gumdrop Machine的模拟程序,包含模拟Gumdrop的制造,以及模拟Gumdrop的销售。

Getting Started

This assignment focuses on defining classes and instance methods. You will be writing two classes with names that must exactly match those specified here: GumdropMachine.java and BuyGumdrops.java. In addition, all method names must also exactly match those specified below. When completed, submit both of your Java files via the submit link for this assignment on Blackboard.

Programming Assignment

The GumdropMachine class

Define a class called GumdropMachine for representing a gumdrop vending machine. The class must have the following private instance variables:

  1. id - a String identifier for the machine. Must be in uppercase and contain at least 5 characters.
  2. count - an integer representing the number of gumdrops in the machine.
  3. cost - an integer representing the cost of one gumdrop in cents. For example, if the cost is$1.50, then the value of this instance variable should be 150. The cost of a gumdrop must always be evenly divisible by 5, as only quarters, dimes, and nickels can be entered to the machine.
  4. quarters - an integer representing the total number of quarters in the machine.
  5. dimes - an integer representing the total number of dimes in the machine.
  6. nickels - an integer representing the total number of nickels in the machine.
  7. deposit - an integer representing the total amount of change deposited by the current customer (such as 125 cents, for example).

There should be no other instance variables in this class. If you decide a DecimalFormat object would be helpful, for example, then be sure to declare it either within a method that uses it (i.e., as a local variable within a method) or as a static variable for use by the entire class. Logical variables should be declared within the methods where they are used.

Define the following public methods with the exact method names shown below in theGumdropMachine class. Assume each is an instance method unless otherwise specified.

  1. setId: one parameter of type String. The minimum required length for an id is 5. Set the id of the machine to the value of the parameter in uppercase. If the length is less than the minimum required, then add the appropriate number of zeros to that id to reach the length. For example, if passed “xy” then the id should be set to “XY000”.
  2. setCost: one parameter of type int representing the cost of a gumdrop in cents. No value is returned. If the parameter value is 0 or less, set the cost instance variable to 0. If it is at least 0 and it is divisible by 5, then set the cost instance variable to the value of the parameter. If it is at least 0 but not divisible by 5, then set it to the closest value below it that is divisible by 5. For example, if the parameter value is 156 or 157 or 158 or 159, then the cost should be set to 155. If it is 41 or 42 or 43 or 44, then the cost should be set to 40.
  3. getCost: no parameters, returns the value of the cost instance variable.
  4. Six argument (6-arg) constructor: The parameters must be in the following order: id (String), number of gumdrops (int), cost per gumdrop (int), number of quarters in the machine (int), number of dimes in the machine (int), number of nickels in the machine (int). Use your set methods for setting the values of the corresponding instance variables. For all other instance variables, set them directly to the values passed to the constructor if there is a corresponding parameter. Any other instance variables should be left with their default values.
    NOTE: the constructor should appear in your code after the instance and static variables but before other methods so that it is easy to find.
  5. available: no parameters. This method returns a boolean value of true if there are any gumdrops and a sufficient number of coins in the machine or false otherwise. If the machine is out of gumdrops, a message that includes the phrase “Out of gumdrops” must be printed. Otherwise, if there aren’t at least 10 quarters, 10 dimes, and 10 nickels in the machine, then a message that includes the phrase “Out of change” must be printed. Note that if there aren’t any gumdrops, you don’t need to check the amount of coins.
  6. updateDeposit: three parameters in the following order representing: quarters (int), dimes (int), and nickels (int) deposited by the customer. Update the current value of the deposit instance variable by the amount of change deposited by the customer. For example, if the user deposits 1 quarter, 1 dime, and 1 nickel, then the current value of the deposit instance variable must be increased by 40 cents. No value is returned by this method.
  7. updateCash: three parameters in the following order representing: quarters (int), dimes (int), and nickels (int). Update the corresponding instance variables by these amounts. The values passed to this method can be positive or negative, so that the amount of each can be increased or reduced. This makes it possible to use this method for adding coins to the machine as well as removing coins when providing change back to the customer. No value is returned by this method.
  8. static convertChange: this static method has one parameter of type int representing an amount of money in change (such as 140 cents). It determines the minimum number of quarters, dimes, and nickels that are equivalent to that value. An array of type int is returned, where the first element stores the number of quarters, the second stores the number of dimes, and the third stores the number of nickels. For example, if the value of the parameter is 140, the array returned by this method should have the following contents: {5, 1, 1}, representing 5 quarters, 1 dime, and 1 nickel.
  9. makeChange: no parameters. This method determines the number of quarters, dimes, and nickels due back to the customer if the amount deposited is more than the cost of a gumdrop. It returns a string describing the amount to be refunded, such as: “Change back of 2 quarter(s), 1 dime(s), 1 nickel(s)”
    • In case of an overpayment, this method must
    • Use the static convertChange method described above to calculate the smallest number of quarters, dimes, and nickels due back to the customer.
    • Use the appropriate method to decrease the amount of quarters, dimes, and nickels in the machine by the amounts refunded to the customer.
  10. insert: three parameters in the following order representing: quarters (int), dimes (int), and nickels (int) deposited by the customer. This method returns a boolean value indicating if a gumdrop has been purchased (true) or not (false). You may assume only non-negative numbers are passed to this method. This method must also:
    • Use the appropriate method to update the number of coins in the machine.
    • Check if the amount deposited so far covers the cost of a gumdrop.
    • If the amount deposited doesn’t cover the cost, a message telling the customer of the amount still due (formatted as dollars and cents, with 2 digits after the decimal point) must be printed.For example: “Please insert an additional $0.30”
    • If the total amount deposited is more than the cost of a gumdrop, use the appropriate method to determine the change due back to the customer and print that amount.
    • If the total amount deposited is sufficient to buy a gumdrop (i.e., either the exact amount or an overpayment), then the number of gumdrops should decrease by 1 and message that includes the word “Enjoy” must be printed.
    • Use the appropriate method to update the current amount deposited by the customer, which will either increase in case of an underpayment or be reset to 0 if a gumdrop has been purchased.
  11. Return toString: no parameters, returns a string containing a description of the invoking object, as shown below, with any monetary amounts formatted as dollars and cents:
Machine: HGLX0 gumdrops: 1000 cost: $1.10
Change in machine: 15 quarter(s), 12 dime(s), 13 nickel(s)
Total cash in machine: $5.60

In addition to the above, you may include any other static or instance methods that would be helpful to you. It is highly recommended that you include a main method in this class for testing purposes. Each time you add one of the required methods, add code to test it in main. This main method will not be graded and does not need to be removed prior to submitting.

The BuyGumdrops class

Define a main method (that will be graded!) to provide the following functionality:

  1. Prompt the user for the following information required to create one GumdropMachine object. The prompts must be in the order shown here:
    • The gumdrop machine id
    • The cost of a gumdrop
    • The number of gumdrops as cents
    • The number of quarters in the machine
    • The number of dimes in the machine
    • The number of nickels in the machine
  2. Instantiate (i.e., use your constructor to make) an object of the GumdropMachine class based on the user’s input.
  3. Print a description of that object (recall that printing a reference variable will automatically invoke the toString() method of the associated class, so no need to invoke it explicitly).
  4. Invoke the available method to check if the machine is available for use. If it isn’t, the available method (not code in your main method) will print an appropriate message (assuming it has been defined correctly). If the machine is available:
    • Prompt the user to enter quarters, dimes, and nickels, respectively.
    • Invoke the insert method, passing it the values entered by the user.
    • Continue to prompt for change and invoke the insert method until the user has entered enough money to purchase a gumdrop.
    • Print a description of the machine again.

Following are sample interactions, with user input shown in boldface. These samples do not cover all test cases. For example, the user may underpay a couple of times and then overpay.
Example 1: Too few dimes in the machine

GUMDROP MACHINE SETUP
Enter the gumdrop machine id: xyz
Enter the cost per gumdrop in cents: 151
Enter the number of gumdrops in the machine: 100
Enter the number of quarters in the machine: 20
Enter the number of dimes in the machine: 9
Enter the number of nickels in the machine: 10

Machine: XYZ00 gumdrops: 100 cost: $1.50
Change in machine: 20 quarter(s), 9 dime(s), 10 nickel(s)
Total cash in machine: $6.40

GUMDROP MACHINE USE
Out of change - please come back later

Example 2: Out of gumdrops (not enough change either, but don’t need to check due to gumdrop issue)

GUMDROP MACHINE SETUP
Enter the gumdrop machine id: asdf
Enter the cost per gumdrop in cents: 133
Enter the number of gumdrops in the machine: 0
Enter the number of quarters in the machine: 10
Enter the number of dimes in the machine: 1
Enter the number of nickels in the machine: 1

Machine: ASDF0 gumdrops: 0 cost: $1.30
Change in machine: 10 quarter(s), 1 dime(s), 1 nickel(s)
Total cash in machine: $2.65

GUMDROP MACHINE USE
Out of gumdrops - please come back later

Example 3: Exact amount entered

GUMDROP MACHINE SETUP
Enter the gumdrop machine id: Abcde
Enter the cost per gumdrop in cents: 140
Enter the number of gumdrops in the machine: 100
Enter the number of quarters in the machine: 20
Enter the number of dimes in the machine: 20
Enter the number of nickels in the machine: 15

Machine: ABCDE gumdrops: 100 cost: $1.40
Change in machine: 20 quarter(s), 20 dime(s), 15 nickel(s)
Total cash in machine: $7.75

GUMDROP MACHINE USE
Purchase a gumdrop for $1.40
Please enter quarters: 5
Please enter dimes: 1
Please enter nickels: 1
Enjoy your gumdrop!

Machine: ABCDE gumdrops: 99 cost: $1.40
Change in machine: 25 quarter(s), 21 dime(s), 16 nickel(s)
Total cash in machine: $9.15

Example 4: Underpaid twice

GUMDROP MACHINE SETUP
Enter the gumdrop machine id: dfgh
Enter the cost per gumdrop in cents: 162
Enter the number of gumdrops in the machine: 100
Enter the number of quarters in the machine: 15
Enter the number of dimes in the machine: 20
Enter the number of nickels in the machine: 25

Machine: DFGH0 gumdrops: 100 cost: $1.60
change in machine: 15 quarter(s), 20 dime(s), 25 nickel(s)
total cash in machine: $7.00

GUMDROP MACHINE USE
Purchase a gumdrop for $1.60
Please enter quarters: 3
Please enter dimes: 2
Please enter nickels: 1
Please insert an additional $0.60

Please enter quarters: 1
Please enter dimes: 2
Please enter nickels: 0
Please insert an additional $0.15

Please enter quarters: 0
Please enter dimes: 1
Please enter nickels: 1
Enjoy your gumdrop!

Machine: DFGH0 gumdrops: 99 cost: $1.60
Change in machine: 19 quarter(s), 25 dime(s), 27 nickel(s)
Total cash in machine: $8.60

Example 5: Overpaid

GUMDROP MACHINE SETUP
Enter the gumdrop machine id: b
Enter the cost per gumdrop in cents: 250
Enter the number of gumdrops in the machine: 100
Enter the number of quarters in the machine: 10
Enter the number of dimes in the machine: 11
Enter the number of nickels in the machine: 12

Machine: B0000 gumdrops: 100 cost: $2.50
change in machine: 10 quarter(s), 11 dime(s), 12 nickel(s)
total cash in machine: $4.20

GUMDROP MACHINE USE
Purchase a gumdrop for $2.50
Please enter quarters: 10
Please enter dimes: 5
Please enter nickels: 8
Change back of 3 quarter(s), 1 dime(s), 1 nickel(s)
Enjoy your gumdrop!

Machine: B0000 gumdrops: 99 cost: $2.50
Change in machine: 17 quarter(s), 15 dime(s), 19 nickel(s)
Total cash in machine: $6.70

Grading

Your programs must compile and run to receive any credit. Points will be allocated as follows:
27 points for correct GumdropMachine class, including:

  • 1 point for exactly seven instance variables, as specified
  • 5 points for set methods
  • 1 point for get method
  • 2 points for six-arg constructor
  • 3 points for available method
  • 4 points for update methods
  • 3 points for static convertChange method
  • 3 points for makeChange method
  • 3 points for insert method
  • 2 points for toString method

6 points for correct BuyGumdrops class
2 points for meeting the style guidelines

Submitting Your Assignment

When you are satisfied that your program meets all requirements, submit your two java files,GumdropMachine.java and BuyGumdrops.java, to the Blackboard page for Assignment 4.