Question
Write in java as simple as possible coding..
) Object-Oriented Design 7 Write a program that simulates a vending machine. Products can be purchased by inserting coins wit
1 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.Scanner;

class vendingMachine
   {
         
       String Snickers, Peanuts ,ButterCups; //list the items
       int Snickers_count, Peanuts_count ,ButterCups_count;
         
       double Snickers_price, Peanuts_price ,ButterCups_price;
         
       public vendingMachine()
       {
           Snickers_count=3;
           Peanuts_count=3 ; // count of each item
           ButterCups_count=3;
          
          
           Snickers_price=25.25; // price of each item
           Peanuts_price=30.54;
           ButterCups_price=50.65;
          
           System.out.println("List of Items in vending machine and prices");
          
           System.out.println("Snickers "+Snickers_price);
           System.out.println("Peanuts "+Peanuts_price);
           System.out.println("ButterCups " +ButterCups_price);
          
             
       }
      
       String start(String str,double coins)
       {
          
           String line="";
          
           if(str.equals("Snickers")) // check for which item required
           {
               if(coins<Snickers_price) // compare price and coins
               {
                  
                   line="Insufficient Amount, please collect your money";
                  
               }
               else if(Snickers_count>0)
               {
                  
                   line="please collect remaining change : "+(coins-Snickers_price); // return change
                  
                   Snickers_count--;
                  
               }
               else
                  
                   line="Sorry Item already sold out ";
              
              
              
           }
           else if(str.equals("Peanuts")) // check for which item required
           {
               if(coins<Peanuts_price) // compare price and coins
               {
                  
                   line="Insufficient Amount, please collect your money";
                  
               }
               else if(Peanuts_count>0)
               {
                  
                   line="please collect remaining change : "+(coins-Peanuts_price);// return change
                  
                   Peanuts_count--;
                  
               }
               else
                  
                   line="Sorry Item already sold out ";
              
              
              
           }
           else if(str.equals("ButterCups"))
           {
               if(coins<ButterCups_price) // compare price and coins
               {
                  
                   line="Insufficient Amount, please collect your money";
                  
               }
               else if(ButterCups_count>0)
               {
                  
                   line="please collect remaining change : "+(coins-ButterCups_price); // return change
                  
                   ButterCups_count--;
                  
               }
               else
                  
                   line="Sorry Item already sold out ";
              
              
              
           }
          
           return line;
          
          
       }
      
      
      
      
   }

//driver class
public class Test {

  
  
  
   public static void main(String[] args) {
      
      
      
       vendingMachine vm =new vendingMachine();
      
       while(true)
       {
       Scanner sc= new Scanner(System.in);
       System.out.println("Enter the Item name ");
       String str=sc.nextLine();
      
       System.out.println("Insert the coins ");
      
       double coins=sc.nextDouble();
      
      
       System.out.println(vm.start(str,coins));
      
       System.out.println();
       }
      
      

   }

}

Sample Run

Add a comment
Know the answer?
Add Answer to:
) Object-Oriented Design 7 Write a program that simulates a vending machine. Products can be purc...
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
  • Write a C+OOP program that simulates a vending machine. The program should define at least two...

    Write a C+OOP program that simulates a vending machine. The program should define at least two classes. Less than two classes will result in 20 points deduction. (30 points) One must be general vending machine (50 points) The other can be either vending machine of soda, or vending machine of snack. It must be a subclass of above general machine These vending machines should have following components and functions e Coin slot o Allow adding coins o Display total coin...

  • LABWORK Design and Implement a Vending Machine Simulator that has functions : Show Products : D...

    LABWORK Design and Implement a Vending Machine Simulator that has functions : Show Products : Display Name , Description and Price of the Products Insert Coin : Allows user to Add a USD coin into the machine allowed types are : Nickel(0.05) , Dime(0.1) , Quarter(0.25) and Dollar (1) Buy: If the sufficient amount of coins are thrown into the machine , machine should dispense the cold drink , and remove it from its product inventory. If insufficient amount is...

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

  • Write a program in C++ that simulates a soft drink machine. The program will need several...

    Write a program in C++ that simulates a soft drink machine. The program will need several classes: DrinkItem, DrinkMachine and Receipt. For each of the classes you must create the constructors and member functions required below. You can, optionally, add additional private member functions that can be used for doing implementation work within the class. DrinkItem class The DrinkItem class will contains the following private data members: name: Drink name (type of drink – read in from a file). Type...

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