Work
Author |
Message |
spiderman
|
Posted: Sat Nov 25, 2006 5:10 pm Post subject: Work |
|
|
Can someone tell me how to get this program to run.
code: |
import java.awt.*;
import java.applet.*;
import java.awt.event.*; // import statement
public class VendMachine extends Applet implements ActionListener
{
double total_amount = 0.0; // Display total momey amount
double product_price; // Display Selection Product Price
int i = 1; // Declare Case Switch prarameter
VendingMachine c;
public void init ()
{
Panel VendMachine = new Panel (); // Creat new panel
add (VendMachine); // Layout pVendMachine
VendingMachine.Cd.addActionListener (this); //Register Production Selection Button
VendingMachine.Floopy.addActionListener (this);
VendingMachine.Dollar.addActionListener (this); //Register Money Button
VendingMachine.Quarter.addActionListener (this);
VendingMachine.Dime.addActionListener (this);
VendingMachine.Nickel.addActionListener (this);
VendingMachine.Penny.addActionListener (this);
VendingMachine.Cancel.addActionListener (this); //Register Cancel Button
VendingMachine.Change.addActionListener (this); //Register Accept Button
} // end of init method
public void actionPerformed (ActionEvent Vent)
{
if (Vent.getSource () == VendingMachine.Dollar) //Action for Dollar button
{
total_amount = 1.00 + total_amount; //Display total amount
VendingMachine.Amount.setText ("Your Money Input: $" + total_amount);
if (product_price != 0) //Condition for display accept message
VendingMachine.Message.setText ("Please Push Change Button");
}
if (Vent.getSource () == VendingMachine.Quarter) //Action for Quarter button
{
total_amount = 0.25 + total_amount;
VendingMachine.Amount.setText ("Your Money Input: $" + total_amount);
if (total_amount >= product_price && product_price != 0) //Condition for display message
VendingMachine.Message.setText ("Please Push Change Button");
}
if (Vent.getSource () == VendingMachine.Dime) //Action for Dime button
{
total_amount = 0.10 + total_amount;
VendingMachine.Amount.setText ("Your Money Input: $" + total_amount);
if (total_amount >= product_price && product_price != 0)
VendingMachine.Message.setText ("Please Push Change Button");
}
if (Vent.getSource () == VendingMachine.Nickel) //Action for Nicke button
{
total_amount = .05 + total_amount;
VendingMachine.Amount.setText ("Your Money Input: $" + total_amount);
if (total_amount >= product_price && product_price != 0)
VendingMachine.Message.setText ("Please Push Change Button");
}
if (Vent.getSource () == VendingMachine.Penny) //Action for Nicke button
{
total_amount = .01 + total_amount;
VendingMachine.Amount.setText ("Your Money Input: $" + total_amount);
if (total_amount >= product_price && product_price != 0)
VendingMachine.Message.setText ("Please Push Change Button");
}
// Product Selection Action
if (Vent.getSource () == VendingMachine.Cd && VendingMachine.Cd_count > 0) //Action for Pepsi button (condition for stock counting)
{
VendingMachine.Message.setText ("Cd, please deposit $1.75");
VendingMachine.Amount.setText ("Your Money Input: $" + total_amount);
product_price = 1.75;
i = 1; // For Switch case parameter(stock counting)
if (total_amount >= product_price) // Condition for display accept button
VendingMachine.Message.setText ("Cd, please Push Accept Button");
}
if (Vent.getSource () == VendingMachine.Floopy) //Action for Coke button
{
VendingMachine.Message.setText ("Floopy, please deposit $1.00");
VendingMachine.Amount.setText ("Your Money Input: $" + total_amount);
product_price = 1.00;
i = 2;
if (total_amount >= product_price) // Condition for display accept button
VendingMachine.Message.setText ("Floopy, please Push Change Button");
}
// Accept and Cancel Function Action
if (Vent.getSource () == VendingMachine.Cancel)
{
total_amount = 0;
VendingMachine.Message.setText ("Your order have been cancel.");
}
if (Vent.getSource () == VendingMachine.Change)
{
total_amount = total_amount - product_price; //Total input amount - product_price
if ((product_price == 0.5 && total_amount == -0.5) || product_price == 0.8 && total_amount == -.8)
//Condition for insufficient fund
{
VendingMachine.Message.setText ("Please deposit $" + product_price + " in total.");
total_amount = 0;
VendingMachine.Amount.setText ("Not input money yet");
}
else if (product_price > 0 && (total_amount < 0 && total_amount >= -.45)
|| (total_amount < 0 && total_amount >= -.75))
{
VendingMachine.Message.setText ("Please deposit $" + product_price + " in total.");
total_amount = total_amount + product_price;
VendingMachine.Amount.setText ("Not enough money");
}
else if (product_price > 0 && total_amount == 0) //For no change function
{
VendingMachine.Amount.setText ("No Change");
total_amount = 0;
product_price = 0;
VendingMachine.Cd_count--;
VendingMachine.Cdstock.setText ("Cd Stock = " + VendingMachine.Cd_count); //Display stock quan.
VendingMachine.Floopystock.setText ("Floopy Stock = " + VendingMachine.Floopy_count);
}
else if (product_price > 0 && total_amount > 0) //For change receive function
{
VendingMachine.Amount.setText ("Your Change: $" + total_amount);
total_amount = 0;
product_price = 0;
VendingMachine.Floopy_count--;
VendingMachine.Cdstock.setText ("Cd Stock = " + VendingMachine.Cd_count);
VendingMachine.Floopystock.setText ("Floopy Stock = " + VendingMachine.Floopy_count);
}
} //end of Accept action
} //end of actionPerform
}
|
code: |
import java.awt.*;
public class VendingMachine extends Panel //Applet implements ActionListener
{
public static int Cd_count = 20; // Stock with 5 quantities of products each
public static int Floopy_count = 20;
public static Label Amount; // Declare lMessage Label
public static Label Cdstock;
public static Label Floopystock;
public void Stock ()
{
setLayout (new GridLayout (4, 1));
setBackground (Color.yellow);
Cdstock = new Label ("Cd Stock = " + Cd_count); // Stock Quantities Label
Floopystock = new Label ("Floopy Stock = " + Floopy_count);
Amount = new Label ("*****Total Amount****");
Amount.setAlignment (Label.CENTER);
Amount.setBackground (Color.blue);
Amount.setForeground (Color.white);
add (Amount);
add (Cdstock);
add (Floopystock);
} // end of Pstock method
public static Button Cd; // Declare product selection button
public static Button Floopy;
// Creat labels for three product's names
public static Label lCd = new Label ("Cd"),
lFloopy = new Label ("Floopy");
public static Label Price1 = new Label ("$1.50"), // Creat labels for product's price
Price2 = new Label ("$1.00");
public static Label Name = new Label ("Daniel's Vending Machine"); // Creat new label
public static Label Message = new Label ("****Display Message****");
public void Product ()
{
setLayout (new BorderLayout ());
Panel pHolding = new Panel (); // Creat a panel to hold product selections
pHolding.setLayout (new GridLayout (3, 3)); // Ensure panel's layout 3 columns and 3 rows
pHolding.setBackground (Color.red); // Background = red
pHolding.setForeground (Color.blue); // Foreground = blue
lCd.setAlignment (Label.CENTER); // All Label Alignment - Center
lFloopy.setAlignment (Label.CENTER);
Price1.setAlignment (Label.CENTER);
Price2.setAlignment (Label.CENTER);
Name.setAlignment (Label.CENTER);
Message.setAlignment (Label.CENTER);
Message.setBackground (Color.pink);
pHolding.add (lCd); // Add 1st product's name, price & button
pHolding.add (Price1);
Cd = new Button ("A");
pHolding.add (Cd);
pHolding.add (lFloopy); // Add 2nd product's name, price & button
pHolding.add (Price2);
Floopy = new Button ("B");
pHolding.add (Floopy);
add ("North", Name);
add ("Center", pHolding);
add ("South", Message);
} // Pselection
public static Button Dollar; // Declare money function button
public static Button Quarter;
public static Button Dime;
public static Button Nickel;
public static Button Penny;
public void Money ()
{
setLayout (new GridLayout (4, 1)); // Ensure 1 column & 4 row
Dollar = new Button ("Dollar");
Quarter = new Button ("Quarter");
Dime = new Button ("Dime");
Nickel = new Button ("Nickel");
Penny = new Button ("Penny");
add (Dollar); // add 4 Buttons
add (Quarter);
add (Dime);
add (Nickel);
add (Penny);
} // end of Pmoney method
public static Button Cancel; // Declare Cancel and Accept button
public static Button Change;
public void Function ()
{
Change = new Button ("Change"); // Creat new button
Cancel = new Button ("Cancel"); // Creat new button
add (Change); // Add bAccept on pControl
add (Cancel); // Add bCancel on pControl
setForeground (Color.black); // Foregrond Color = black
} // Pmoney method
} // Pmoney method
|
|
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
wtd
|
Posted: Sat Nov 25, 2006 5:48 pm Post subject: Re: Work |
|
|
You will have to ask more specific questions, or we won't be able to help you.
As it is, the only thing I can say about this code is that it doesn't use proper naming conventions, or conventions about spacing. |
|
|
|
|
 |
|
|