Well i am working on a drink dispenser machine and got stuck on array part, this program includes selecting a drink, show price, show changes and a option whether to purchase drink again
They are being placed on various method like
public void A()
select drink...
public void B()
calculate change...
public void C()
show change...
public void D()
Option to buy more

As for the array part, assuming each drink has 50 of them(method A will show the number of drink) if i buy drink A AND i choose to purchase more, the number of drinkA should be updated to 49 instead of 50.
I know i got to use array but i couldn't get it right no matter what i do
Here is wat i have done so far

import javax.swing.*;
import java.io.*;

public class SelfServiceMachine {
    private int type , price, payment,change,finish,gg,sum;
    private String choice;
    public void readChoice() {
        boolean error;
int[] myA;
  myA = new int[250];
  myA[0] = 50;
  myA[1] = 50;
  myA[2] = 50;
  myA[3] = 50;
  myA[4] = 50;

       
 do {

            choice = JOptionPane.showInputDialog(null, "Enter your choice of cake"
                    + "\n\n"
                    + "1.Strawberry(90 cents), no. of cakes available:"+myA[0]+"\n"

                    + "2 Chocolate(80 cents),  no. of cakes available: 50\n"
                    + "3.Vanilla(70 cents),    no. of cakes available: 50\n"
                    + "4.Almond(100 cents),    no. of cakes available: 50\n"
                    + "5.Durian(120 cents),    no. of cakes available: 50\n");
                    try{
                        error=true;
                        type=Integer.parseInt(choice);

            if (type < 1 || type > 5) {
                JOptionPane.showMessageDialog(null,
                        "Invalid Input! Please enter in the range from 1 to 5.",
                        "Message", JOptionPane.ERROR_MESSAGE);
                        }
                        }catch (NumberFormatException e){
                            JOptionPane.showMessageDialog(null, "Please enter a numeric number!", "Error", JOptionPane.ERROR_MESSAGE);
            }

    

        } while (type < 1 || type > 5);
  

            if(type==1){
    
                price=90;
            JOptionPane.showMessageDialog(null, "Price is " +price+ "cents", "Message", JOptionPane.INFORMATION_MESSAGE);
            }
 else if(type==2){
     price=80;
     JOptionPane.showMessageDialog(null, "Price is " +price+ "cents", "Message", JOptionPane.INFORMATION_MESSAGE);
 }
 else if(type==3){
     price=70;
     JOptionPane.showMessageDialog(null, "Price is " +price+ "cents", "Message", JOptionPane.INFORMATION_MESSAGE);
 }
  else if(type==4){
     price=100;
     JOptionPane.showMessageDialog(null, "Price is " +price+ "cents", "Message", JOptionPane.INFORMATION_MESSAGE);
 }
  else if(type==5){
     price=120;
     JOptionPane.showMessageDialog(null, "Price is " +price+ "cents", "Message", JOptionPane.INFORMATION_MESSAGE);
 
 }
    }
  public void readPayment(){
do{
    payment=Integer.parseInt(JOptionPane.showInputDialog(null, "Please enter you payment(The price is " +price+ "cents)", "SP Cake Shop",
                            JOptionPane.QUESTION_MESSAGE));
   if (payment < price) {
                        JOptionPane.showMessageDialog(null, "Invalid Input! Please enter a minimum"
                                + "Payment of " +price+ " cents");
}
                }while (payment < price);
    }
  public void computerAndDisplayChange(){
       change = (payment-price);
       JOptionPane.showMessageDialog(null, "Change is " + change + " cents.");
    }
  public void computeAndDisplayCoins(){
                    int change1 = (change / 100);
                    int change2 = ((change % 100) / 50);
                    int change3 = ((change % 100 % 50) / 20);
                    int change4 = ((change % 100 % 50 % 20) / 10);
                     if (payment > price) {
                        JOptionPane.showMessageDialog(null, "Dispensing...\n"
                                + change1 + "x One Dollar\n"
                                + change2 + "x 50c coin\n"
                                + change3 + "x 20c coin\n"
                                + change4 + "x 10c coin\n");
                    }
    }
      public void EndingProgram(){

              finish =Integer.parseInt(JOptionPane.showInputDialog(null, "Do you want more cake?\n"
                            + "(Enter 1 for Yes, other numbers for No)"));
                    if (finish !=1) {
                        JOptionPane.showMessageDialog(null, "Program terminated\nThank You!"
                                + "", "Message", JOptionPane.INFORMATION_MESSAGE);
                    
          }
              if(finish==1){
                  readChoice();
                  readPayment();
                  computerAndDisplayChange();
                  computeAndDisplayCoins();
                  EndingProgram();
              }
                    
          
      }
   
}

Recommended Answers

All 2 Replies

What was your meaning when writing these lines?

myA = new int[250];
myA[0] = 50;
myA[1] = 50;
myA[2] = 50;
myA[3] = 50;
myA[4] = 50;

What was your meaning when writing these lines?

myA = new int[250];
myA[0] = 50;
myA[1] = 50;
myA[2] = 50;
myA[3] = 50;
myA[4] = 50;

sry that was imcomplete work, trying different method to make it reduce it number...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.