Machine code
Author |
Message |
spiderman
|
Posted: Mon Nov 27, 2006 5:34 pm Post subject: Machine code |
|
|
Anyways, no one has been responding to my last post but thats ok. Anyways i Fixed alot of the thing I was asking help for before. Just one little thing I have to fix left and I cant seem to fix it. Its probably easy, something wrong with my if statements in the class but i cant seem to find it. Anyways, could some one tell me how I can get my vend cd button to work. The floppy button works!!!!!! Yea but when I click cd button it thinks its a floppy and outputs a floppy disk. Can some one help me???
By the way, please don't say "Use actionlistner to draw the buttons" or use the function button instead of drawing it urself. I dont wanna use those to things. Ok to get it to work enter a key "t" for tonnie, "l" for "lonnie". When you have eough money hit vend floppy then change. Also the floppy count should go down. The cd count doesnt go down because cant get it to work.
code: |
import java.awt.*;
public class VendingMachine
{
//instance variables
double money; // stores amount of money in machine
int floppys = 20;
int cds = 20; //floppies and cd inventory
String coinName; //string for any named coin entered in machine
int x, y;
int height;
int width;
double floppyprice = 1.00;
double cdprice = 1.75;
int vendCdButtonX = 20;
int vendCdButtonY = 140;
int vendCdButtonHeight = 100;
int vendCdButtonWidth = 100;
int vendFloppyButtonX = 20;
int vendFloppyButtonY = 260;
int vendFloppyButtonHeight = 100;
int vendFloppyButtonWidth = 100;
int vendChangeButtonX = 20;
int vendChangeButtonY = 380;
int vendChangeButtonHeight = 100;
int vendChangeButtonWidth = 100;
Color d;
boolean VendCd;
boolean VendFloppy;
//constructors
public VendingMachine (int numfloppys, int numcds, int initialX, int initialY, int initialHeight, Color initialColor)
{
floppys = numfloppys;
cds = numcds;
x = initialX;
y = initialY;
height = initialHeight;
width = height / 2;
d = Color.RED;
}
//method for inserting coins
public void Tonnie (String getCoins)
{
//coinName = getCoins;
//if toonie
//if (coinName == "t")
money += 2;
}
public void Lonnie (String getCoins)
{
//coinName = getCoins;
//if toonie
//if (coinName == "l")
money += 1;
}
public void Quarter (String getCoins)
{
money += 0.25;
}
public void Dime (String getCoins)
{
money += 0.10;
}
public void Nickel (String getCoins)
{
money += 0.05;
}
public void Penny (String getCoins)
{
money += 0.01;
}
public void vendCDClicked (int xpos2, int ypos2)
{
if (xpos2 > vendCdButtonX
&& xpos2 < vendCdButtonX + vendCdButtonWidth && ypos2 > vendCdButtonY
&& ypos2 < vendCdButtonY + vendCdButtonHeight && money <= cdprice)
{
VendCd = true;
cds -= 1;
}
}
public void vendFloppyClicked (int xpos2, int ypos2)
{
if
(xpos2 > vendFloppyButtonX
&& xpos2 < vendFloppyButtonX + vendFloppyButtonWidth && ypos2 > vendFloppyButtonY
&& ypos2 < vendFloppyButtonY + vendFloppyButtonHeight && money <= floppyprice)
{
VendFloppy = true;
floppys -= 1;
}
}
public void ChangeClicked (int xpos2, int ypos2)
{
if
(VendFloppy = true && xpos2 > vendChangeButtonX
&& xpos2 < vendChangeButtonX + vendChangeButtonWidth && ypos2 > vendChangeButtonY
&& ypos2 < vendChangeButtonY + vendChangeButtonHeight && money >= floppyprice)
{
System.out.println ("Your change is " + (money - floppyprice));
money = 0;
floppys -= 1;
}
if
(VendCd = true && xpos2 > vendChangeButtonX
&& xpos2 < vendChangeButtonX + vendChangeButtonWidth && ypos2 > vendChangeButtonY
&& ypos2 < vendChangeButtonY + vendChangeButtonHeight && money >= cdprice)
{
System.out.println ("Your change is " + (money - cdprice));
money = 0;
cds -= 1;
}
}
public VendingMachine (int initialX, int initialY, int initialHeight, Color initialColor)
{
x = initialX;
y = initialY;
height = initialHeight;
width = height / 2;
d = initialColor;
}
public void setPosition (int newX, int newY)
{
x = newX;
y = newY;
}
public void setHeight (int newHeight)
{
height = newHeight;
width = height / 2;
}
public void draw (Graphics g)
{
g.setColor (d);
g.fillRect (20, 260, 100, 100);
g.setColor (d);
g.fillRect (20, 140, 100, 100);
g.setColor (d);
g.fillRect (10, 10, 20, 80);
g.setColor (d);
g.fillRect (50, 10, 120, 80);
g.setColor (d);
g.fillRect (20, 380, 100, 100);
g.drawString ("VEND CD", 140, 200);
g.drawString ("VEND FLOOPY", 140, 300);
g.drawString ("CHANGE", 140, 420);
g.drawString ("Total Money " + money, 340, 300);
g.drawString ("Floppies Left: " + floppys, 340, 320);
g.drawString ("Cd-Roms Left: " + cds, 340, 340);
}
public String toString ()
{
return "money" + money + floppys + cds;
}
}
|
code: |
import java.awt.*;
import java.io.*;
import java.applet.*;
import java.awt.event.*;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.*;
public class VendingMachineDemo extends Applet implements Runnable, MouseListener, KeyListener
{
int mouseClicks = 0;
boolean mouseInWindow = false;
boolean mouseButtonDown = false;
int xpos;
int ypos;
int Heightpos;
int Widthpos;
int floppys = 20;
int Cds = 20;
int product_price;
String coins;
int coin;
int money;
double total_amount;
double totalmoney;
int rect1xco, rect1yco, rect1width, rect1height;
int rect1xco2, rect1yco2, rect1width2, rect1height2;
int rect1xco3, rect1yco3, rect1width3, rect1height3;
boolean rect1Active;
char key;
boolean doLoop = true;
VendingMachine v;
VendingMachine v2;
VendingMachine v3;
VendingMachine v4;
VendingMachine v5;
Thread t;
public void init ()
{
v = new VendingMachine (20, 140, 100, Color.BLUE);
rect1xco = 20;
rect1yco = 260;
rect1width = 100;
rect1height = 100;
rect1xco2 = 20;
rect1yco2 = 140;
rect1width2 = 100;
rect1height2 = 100;
rect1xco3 = 20;
rect1yco3 = 380;
rect1width3 = 100;
rect1height3 = 100;
addMouseListener (this);
addKeyListener (this);
} // init method
public void start ()
{
t = new Thread (this);
t.start ();
}
public void run ()
{
}
public void keyPressed (KeyEvent e)
{
key = e.getKeyChar ();
if ((key == 't'))
{
v.Tonnie (coins);
repaint ();
}
if ((key == 'l'))
{
v.Lonnie (coins);
repaint ();
}
if ((key == 'q'))
{
v.Quarter (coins);
repaint ();
}
if ((key == 'd'))
{
v.Dime (coins);
repaint ();
}
if ((key == 'n'))
{
v.Nickel (coins);
repaint ();
}
if ((key == 'p'))
{
v.Penny (coins);
repaint ();
}
else
{
repaint ();
}
}
//}
public void keyReleased (KeyEvent e)
{
key = ' ';
repaint ();
}
public void keyTyped (KeyEvent e)
{
}
public void mouseClicked (MouseEvent e)
{
xpos = e.getX ();
ypos = e.getY ();
if (xpos > rect1xco && xpos < rect1xco + rect1width && ypos > rect1yco
&& ypos < rect1yco + rect1height)
{
v.vendCDClicked (xpos, ypos);
}
repaint ();
if (xpos > rect1xco2 && xpos < rect1xco2 + rect1width2 && ypos > rect1yco2
&& ypos < rect1yco2 + rect1height2)
{
v.vendFloppyClicked (xpos, ypos);
}
if (xpos > rect1xco3 && xpos < rect1xco3 + rect1width3 && ypos > rect1yco3
&& ypos < rect1yco3 + rect1height3)
{
v.ChangeClicked (xpos, ypos);
}
repaint ();
}
public void mouseEntered (MouseEvent e)
{
mouseInWindow = true;
repaint ();
}
public void mouseExited (MouseEvent e)
{
mouseInWindow = false;
repaint ();
}
public void mousePressed (MouseEvent e)
{
}
public void mouseReleased (MouseEvent e)
{
mouseButtonDown = false;
repaint ();
}
public void paint (Graphics g)
{
v.draw (g);
g.drawString ("Key: " + key, 450, 110);
}
}
|
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
ericfourfour
|
Posted: Tue Nov 28, 2006 12:40 am Post subject: (No subject) |
|
|
I'm too lazy to run your code so I'm just going to give you a few suggestions.
Is it just me or am I seeing doubles?
Java: | import java.awt.*;
import java.io.*;
import java.applet.*;
import java.awt.event.*;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.*;
|
Your code is not very pleasing to the eyes. This is one reason:
Java: | if
(VendFloppy = true && xpos2 > vendChangeButtonX
&& xpos2 < vendChangeButtonX + vendChangeButtonWidth && ypos2 > vendChangeButtonY
&& ypos2 < vendChangeButtonY + vendChangeButtonHeight && money >= floppyprice) |
This should be in its own method. This code is also repeated a few times (there are subtle differences) and that is more reason to put it in its own method.
It would be cool if your currency was actual objects. So when a toonie, for example, gets put into the machine, an actual toonie object is added to a list. This could give you some experience in inheritance if you want to take it to the next level (look up "extends").
Another cool feature would be if you allowed the user to enter and amount of money and the machine converts to change and then sends it in (look up recursion if you want to go overboard with this idea).
Tying the last to points together. If you gave the user change back and even gave them a limit on their cash (give them something like 5 toonies, 10 loonies, etc.).
Anyway, you're going to have to wait for someone else to debug your program. I'm going to sleep. |
|
|
|
|
|
Neville
|
Posted: Fri Dec 01, 2006 10:38 pm Post subject: (No subject) |
|
|
code: | public void ChangeClicked (int xpos2, int ypos2)
{
if
([b]VendFloppy = true[/b] && xpos2 > vendChangeButtonX
&& xpos2 < vendChangeButtonX + vendChangeButtonWidth && ypos2 > vendChangeButtonY
&& ypos2 < vendChangeButtonY + vendChangeButtonHeight && money >= floppyprice)
{
System.out.println ("Your change is " + (money - floppyprice));
money = 0;
floppys -= 1;
}
if
([b]VendCd = true[/b] && xpos2 > vendChangeButtonX
&& xpos2 < vendChangeButtonX + vendChangeButtonWidth && ypos2 > vendChangeButtonY
&& ypos2 < vendChangeButtonY + vendChangeButtonHeight && money >= cdprice)
{
System.out.println ("Your change is " + (money - cdprice));
money = 0;
cds -= 1;
}
} |
Classic programming error: "VendCD = true" isn't a comparison... it assigns true to VendCD (or VendFloppy) every time the if executes. So there's your main problem. Use either "VendCD==true" for a comparison, or simply "VendCD" since its a boolean to start with.
Your second question was... how can I program better? Because... wow, yeah. You do a lot of useless checks, like 10 ifs in a row. Each of these statements will evaluate! You only need one of them two, so do this in a row of if/else ifs... that would be a nice start. Use less variables too... like a Rectangle object can been drawn over and over again with Graphics2D.draw(Shape), getting rid of the need for 12 instance variables.... etc.[/code] |
|
|
|
|
|
|
|