Question

Can someone please create a payment java class that can read bills and coins for a...

Can someone please create a payment java class that can read bills and coins for a vending machine and is not hardcoded and you can get the price from the objects.

Thank you.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.InputMismatchException;
import java.util.Scanner;

public class VendingMachine {

public static void main(String[] args) {

     Scanner scn = new Scanner(System.in);

     // Use an array Object if you have a list of items.
     double[] itemPrices = {1.25, .75, .90, .75, 1.50, .75};

     // This array starts from 0 end ends at 5. SO, we have only 5 item costs only.
     int maxItem = itemPrices.length;

     // Have we been given an item that is Ok yet?
     boolean itemFound = false;

     // We have not been provided an item # yet. This will have to be non-null
     Integer item = null;

     // read an item number until an existing item is found.
     do {
         System.out.print("Enter an item number: ");
         try {
             item = scn.nextInt();
             if ( item >= 1 && item <= maxItem ) {
                 itemFound = true;
             } else {
                 // Inform the user of the problem. Give them another chance.
                 // String.format is very neat stuff. Use it.
                 String message = String.format("Unfortunately, item #%d does not exist.", item);
                 System.out.println(message);
             }

         // note how an exception is raised when we get an invalid input.
         // you must consider what happens when the input is invalid.
         } catch (InputMismatchException e) {
             System.out.println("Bad input. Try again.");
             scn.next();
         }
     } while (!itemFound);


     Double paid = null;
     do {
         System.out.print("Enter the amount to be paid: ");
         try {
             paid = scn.nextDouble();
         } catch (InputMismatchException e) {
             System.out.println("Bad input.Please Try again.!!!!");
             scn.next();
         }
     } while (paid == null);

     double valueOfItem = itemPrices[item-1];

     double changeMoney = paid - valueOfItem;

     double moneyNeeded = 0 - changeMoney;

     if ( moneyNeeded > 0 ) {
         // We can inform the user what the problem was. And again the String.format magic.
         String message = String.format("Please insert another $%.2f.%n", moneyNeeded);
         System.out.println(message);

         // But this time, we will gracefully exit.
         System.exit(0);
     }

     String message = String.format("Thank you for buying item #%d, your changeMoney is $%.2f. Please come again!", item, changeMoney);

     System.out.println(message);

}

}

Add a comment
Know the answer?
Add Answer to:
Can someone please create a payment java class that can read bills and coins for a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 1. Create a new multi-class Java program which implements a vending machine simulator which contains the...

    1. Create a new multi-class Java program which implements a vending machine simulator which contains the following functionality: A) At program startup, the vending machine is loaded with a variety of products in a variety of packaging for example soda/tonic/Coke in bottles, peanuts in bags, juice in cartons, etc. Also included is the cost of each item. The program should be designed to easily load a different set of products easily (for example, from a file). Also at program startup,...

  • Create a java project and create Student class. This class should have the following attributes, name...

    Create a java project and create Student class. This class should have the following attributes, name : String age : int id : String gender : String gpa : double and toString() method that returns the Student's detail. Your project should contain a TestStudent class where you get the student's info from user , create student's objects and save them in an arralist. You should save at least 10 student objects in this arraylist using loop statement. Then iterate through...

  • Write a Java class that implements the concept of Coins, assuming the following attributes (variables): number...

    Write a Java class that implements the concept of Coins, assuming the following attributes (variables): number of quarters, number of dimes, number of nickels, and number of pennies. Include two constructors (non-argument constructor that assigns 1 to each coin, and another constructor that takes necessary arguments to create an object), needed getter and setter methods, method toString (to print coins objects in a meaningful way), and method equals (to compare two coins objects based on number of coins). Also include...

  • Using the IST Linux system create the following Java command line inheritance application Lab4. Create the...

    Using the IST Linux system create the following Java command line inheritance application Lab4. Create the following project Java files: Point.java, Shape.java, Circle.java, Triangle.java, Rectangle.java, and Lab4.java Point.java will contain two coordinates x and y. This will be reused to create point objects to draw all the shapes. Shape.java will be the parent class and will contain a Point type. Circle.java, Triangle.java, Rectangle.java will all be children of Shapes.java class Each class (Circle, Triangle, Rectangle) will contain the private class...

  • Money Lab using arraylists in Java Using the Coin class below create a program with the...

    Money Lab using arraylists in Java Using the Coin class below create a program with the following requirements Create an arraylist that holds the money you have in your wallet. You will add a variety of coins(coin class) and bills (ones, fives, tens *also using the coin class) to your wallet arraylist. Program must include a loop that asks you to purchase items using the coins in your wallet. When purchasing items, the program will remove coins from the arraylist...

  • In this assignment you will create two Java programs: The first program will be a class...

    In this assignment you will create two Java programs: The first program will be a class that holds information (variables) about an object of your choice and provides the relevant behavior (methods) The second program will be the main program which will instantiate several objects using the class above, and will test all the functions (methods) of the class above. You cannot use any of the home assignments as your submitted assignment. Your programs must meet the following qualitative and...

  • Could someone provide me general java script statements that would answer these questions? These can be...

    Could someone provide me general java script statements that would answer these questions? These can be very general, I just need to know what statements to use to perform these functions so I can develop an outline. How to implement concatenated if/else statements. How to implement while loops, do while loops, and for loops. Be able to perform error checking using while loops. Define and implement classes: properties, accessors, mutators, constructors (defaults and with arguments), methods, toString method Create objects...

  • Java programming language! 1. Tossing Coins for a Dollar For this assignment you will create a...

    Java programming language! 1. Tossing Coins for a Dollar For this assignment you will create a game program using the Coin class from Programming Challenge 12. The program should have three instances of the Coin class: one representing a quarter, one representing a dime, and one representing a nickel. When the game begins, your starting balance is $0. During each round of the game, the program will toss the simulated coins. When a coin is tossed, the value of the...

  • JAVA Write an application to test the HuffmanTree class. Your application will need to read a...

    JAVA Write an application to test the HuffmanTree class. Your application will need to read a text file and build a frequency table for the characters occurring in that file. Once that table is built create a Huffman code tree and then a string consisting of 0 and 1 characters that represents the code string for that file. Read that string back in and re-create the contents of the original file. Prompt the user for file name. Read the file,...

  • Creating a Programmer-Defined Class in Java Summary In this lab, you will create a programmer-defined class...

    Creating a Programmer-Defined Class in Java Summary In this lab, you will create a programmer-defined class and then use it in a Java program. The program should create two Rectangle objects and find their area and perimeter. Instructions Make sure the class file named Rectangle.java is open. In the Rectangle class, create two private attributes named lengthand width. Both length and width should be data type double. Write public set methods to set the values for length and width. Write...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT