Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Vending- If Else Statement..

807600Oct 15 2007 — edited Oct 17 2007
I'm currently learning Java at my school and it appears I have run into a problem with our new subject. We have just started a program that will add the If Else statement into an older program called vending. The Vending product user various Strings to get information from other methods. Like a vending machine, you put in money get the soda get change. The objective is to add the If Else into the old program to make sure that you are entering enough money to meet the price of the product.
Here is what the book asks us to do
The user should not be able to enter a negative dollar amount.
The user should not be able to enter an ammount over some limit
the user should not be able to et the product until enough money has been inserter.
the user should not be able to insert money if there are no products available

As you can see at the bottom I added an example of the IfElse to see if it will work. But it obviously doesn't

Here is my code and thank you for taking a look at it.
/** -------------------------------------------------------------------
* Program Name : Vending Machine
* This class simulates a vending machine.
* Each machine can sell one type of product
* at a fixed price
* 
* Date Created: 9/26/07
**--------------------------------------------------------------------*/
public class PentiumVendingIV
{
// Instance varibales
private String productType; // type of product
private String productType2; // 2nd type of product
private String productType3; // 3rd product Type
private int productPrice3; // 3rd product Price
private int productPrice2; // 2nd price of product
private int productPrice; // the Product's price
private int numberSold; // total number of units sold
private int balance; // number of money buyer inserted
private int InstructionsMenu; // The instructions
private int totalMoney; // total $ taken by this machine

// constructor

// the public methods:
/**----------------------------------------------------------------------
* Method to se this machines Number Sold - Tried to get it to work but then I have no Idea.
* Looked at VarEx but still not sure.
* ----------------------------------------------------------------------
*/


/**-----------------------------------------------------------------------
* Method to set this machine's type of product
* ----------------------------------------------------------------------
*/

public void setType (String Drinks)
{
productType = Drinks ;
} // end of SetType()

/** ------------------------------------------------------------------------
* Method to set this machine's price of product.
*/

private void productPrice()
{
productPrice = 1;
}

/** --------------------------------------------------------------------------
* Return this machine's type of product
* --------------------------------------------------------------------------
*/
private String getType()
{
return productType;
} // end of getType()

/** ---------------------------------------------------------------------------
* Allow buyer to insert money
* -----------------------------------------------------------------------------
*/
public void insertMoney(int money)
{
balance += money;

} // end of insertMoney()

/** --------------------------------------------------------------------------
* give the buyer the product
* ---------------------------------------------------------------------------
*/
private void giveProduct ()
{
System.out.println ("*------------------------*");
System.out.println (" | You have just bought |");
System.out.println (" | |");
System.out.println (" | |");
System.out.println (" | "+ productType);
System.out.println (" |----------------------|");
numberSold ++; // increase the number sold bt one

} // end of give product

/** ------------------------------------------------------------
* Give buyers change back after giving the product
* -------------------------------------------------------------
*/

private void giveChange()
{
balance -= productPrice; // how much to give back
System.out.println (" Your change is: " + balance);
balance = 0;
} // end of giveChange























/** ------------------------------------------------------------------------
* Method to set this machine's price of product.
*/

private void productPrice2()
{
productPrice = 10;
}

/** --------------------------------------------------------------------------
* Return this machine's type of product
* --------------------------------------------------------------------------
*/
private String getType2()
{
return productType2;
} // end of getType()

/** --------------------------------------------------------------------------
* give the buyer the product
* ---------------------------------------------------------------------------
*/
private void giveProduct2 ()
{
System.out.println ("*------------------------*");
System.out.println (" | You have just bought |");
System.out.println (" | a |");
System.out.println (" | |");
System.out.println (" | "+ productType);
System.out.println (" |----------------------|");
numberSold ++; // increase the number sold bt one

} // end of give product












/** ------------------------------------------------------------------------
* Method to set this machine's price of product.
*/

private void productPrice3()
{
productPrice = 5;
}

/** --------------------------------------------------------------------------
* Return this machine's type of product
* --------------------------------------------------------------------------
*/
private String getType3()
{
return productType3;
} // end of getType()

/** --------------------------------------------------------------------------
* give the buyer the product
* ---------------------------------------------------------------------------
*/
private void giveProduct3 ()
{
System.out.println ("*------------------------*");
System.out.println (" | You have just bought |");
System.out.println (" | a |");
System.out.println (" | |");
System.out.println (" | "+ productType);
System.out.println (" |----------------------|");
numberSold ++; // increase the number sold by one

} // end of give product














public void InstructionsMenu ()
{
System.out.println (" For this soda Machine you will be typing in the soda you want");
System.out.println (" Then you will click the button of the soda you want.");
System.out.println (" Your soda will then appear and you will be given change.");
System.out.println (" To do this you will first enter how much money you have by selecting the Method void insert money .");
System.out.println (" Then you will click setType and type in the soda you want using quotations");
System.out.println (" Finally you will click the button for the drink you have entered");
System.out.println (" The Menu: ");
System.out.println ("Monster - $10");
System.out.println ("Pepsi - $1");
System.out.println ("Rockstar - $5");





}
public void PepsiDrink() // First Drink on the Menu
{

getType();
productPrice();
giveProduct();
giveChange();
}

public void MonsterDrink() // Second drink on the menu.

{
// set the properties to show in the order I want it to w/ one click

getType();
getType2();
productPrice();
productPrice2();
giveProduct();
giveChange();




}

public void RockstarDrink()

{
getType();
getType2();
getType3();
productPrice();
productPrice2();
productPrice3();
giveProduct();
giveChange();
}

/**--------------------------------------------------------------
* If Else Statement
* --------------------------------------------------------------
*/
public void elseIf()

{
int i =0;
if(i!=0)
System.out.println(" i is not zero");
else
System.out.println(" I IS Zero");
}


} // end of VendingMaachine
   
Edited by: Mrdelux24 on Oct 15, 2007 7:14 PM
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 14 2007
Added on Oct 15 2007
9 comments
659 views