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...

  • Please Walk me through this problem. Write a program that simulates the functionality of a vending...

    Please Walk me through this problem. Write a program that simulates the functionality of a vending machine having the following characteristics: The vending machine offers 5 products The vending machine accepts coins, 1 dollar bills, and 5 dollar bills The change is always given in coins, with maximum possible number of coins in each value: 25, 10, 5 or 1 cent. The selections available for user are numbers from 1 to 5. The user enters the money – simulate the...

  • Create the following program in java please Write a class Store which includes the attributes: store...

    Create the following program in java please Write a class Store which includes the attributes: store name and sales tax rate. Write another class encapsulating a Book Store, which inherits from Store. A Book Store has the following additional attributes: how many books are sold every year and the average price per book. Code the constructor, accessors, mutators, toString and equals method of the super class Store and the subclass Book Store; In the Book Store class, also code a...

  • 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...

  • ----Can someone please help me solve this one using JAVA ----I thank you in advance Create...

    ----Can someone please help me solve this one using JAVA ----I thank you in advance Create an application that reads an HTML file and converts it to plain text. HTML Converter INPUT <h1>Grocery List</h1> <ul>     <li>Eggs</li>     <li>Milk</li>     <li>Butter</li> </ul> OUTPUT Grocery List * Eggs * Milk * Butter

  • COMP Help (Java): Can someone please help me with this problem! Someone answered it but it...

    COMP Help (Java): Can someone please help me with this problem! Someone answered it but it was wrong. 1. Create an ExtArrayList<E> class by extending the ArrayListE> class to include the following methods and estimate the run-time complexity of each method. public ExtArrayList<E> append(ExtArrayList<E> ea) // appends the values in ea to the end of this ExtArrayList and returns the new ExtArrayList //example: this: {1,2,3} parameter : {4,5}, result {1,2,3,4,5} public boolean consecutivePair(E e1, E e2) // checks if this...

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