C
chriske911
it's rather a lengthy question cause off the code I wrote:
package automaat;
/***************************************************************************
**/
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/***************************************************************************
**/
public class Automaten extends Applet implements ActionListener{
boolean RF=true;
int i,xPos=20;
Broodje SmosKaas=new Broodje();
Broodje SmosHesp=new Broodje();
Broodje Martino=new Broodje();
Broodje Pitta=new Broodje();
Button[] knopje;
Button knopje2;
String[] Opschrift = {"SmosKaas","SmosHesp","Martino","Pitta"};
TextField Input;
TextField Fouten;
/***************************************************************************
**/
public void init(){
setLayout(null);
SmosKaas.prijs = 190;
SmosHesp.prijs = 190;
Martino.prijs = 190;
Pitta.prijs = 220;
knopje2 = new Button("Bijvullen");
knopje2.setBounds(300,20,60,30);
add (knopje2);
knopje2.addActionListener(this);
knopje = new Button[4];
for (i=0;i<knopje.length;i++){
knopje = new Button(Opschrift);
knopje.setBounds((xPos+(85*i)),230,80,30);
add (knopje);
knopje.addActionListener(this);
}
Input = new TextField("");
Input.setBounds(160,30,80,40);
Input.setFont(new Font("Serif",Font.BOLD,24));
add (Input);
Fouten = new TextField ("Hier zie je boodschappen van mij aan u ");
Fouten.setBounds(5,270,390,20);
add (Fouten);
}
/***************************************************************************
**/
public void paint (Graphics g){
SmosKaas.DrawStock(g,20,100,60,Color.yellow);
SmosHesp.DrawStock(g,110,100,60,Color.gray );
Martino.DrawStock(g,200,100,60,Color.red );
Pitta.DrawStock(g,290,100,60,Color.black);
}
/***************************************************************************
**/
public class Broodje{
int prijs = 0, stock = 10;
String bericht;
public boolean Bestellen() {
if (stock > 0) {
stock--;
this.bericht = "";
return true;
}
else {
this.bericht = "Deze schuif is leeg";
return false;
}
}
public void BijVullen(int Aantal) {
if (this.stock >= 10) {
this.bericht = "deze schuif zit nog vol met broodjes";
}
else {
if ( (this.stock + Aantal) > 10) {
this.bericht = "Er zijn " + ( (this.stock + Aantal) - 10) +
" broodjes op overschot ";
this.stock = 10;
}
}
}
public void Bijvullen() {
if (this.stock >= 10) {
this.bericht = "deze schuif zit vol ";
}
else {
this.stock++;
this.bericht = "1 broodje erbij";
}
}
public void DrawStock(Graphics g, int X, int Y, int Breedte, Color
Kleur) {
g.setColor(Kleur);
g.fillRect(X, Y, Breedte, (this.stock * 10));
}
public String GetMessage() {
return this.bericht;
}
public void Display(TextField AnyTextField) throws Exception {
try {
AnyTextField.setText(this.bericht);
}
catch (Exception e) {
AnyTextField.setText("");
System.out.println("de volgende fout is opgetreden: " +
e.getMessage());
}
if (AnyTextField.getWidth() < this.bericht.length()) {
throw new Exception("tekstvak is vol te klein ");
}
}
}
/***************************************************************************
**/
public void actionPerformed(ActionEvent ae){
if (ae.getSource() == knopje2) {
if (RF == true) {
RF = false;
knopje2.setLabel("Bestellen ");
Fouten.setText(" U bent in de BroodjesBijVulModus ");
}
else {
RF = true;
knopje2.setLabel("Bijvullen ");
Fouten.setText("U kunt broodjes bestellen");
}
}
if (ae.getSource()==knopje[0]){
Afhandeling(SmosKaas,RF);
}
if (ae.getSource()==knopje[1]){
Afhandeling (SmosHesp,RF);
}
if (ae.getSource()==knopje[2]){
Afhandeling(Martino,RF);
}
if (ae.getSource()==knopje[3]){
Afhandeling(Pitta,RF);
}
repaint();
}
/***************************************************************************
**/
void Afhandeling( Broodje DCN,boolean YN){
int bedrag;
boolean RF = YN;
Broodje broodje = DCN;
try {
bedrag = Integer.parseInt(Input.getText());
}
catch (NumberFormatException nfe) {
System.out.println("Oeps");
bedrag = 0;
}
if (RF==true){
if (bedrag == 0) {
broodje.bericht = "er zit geen geld in de machine ";
Input.setText("" + bedrag);
}
else {
if (bedrag < broodje.prijs) {
broodje.bericht = "u komt " + (broodje.prijs - bedrag) +
" eurocent tekort voor deze aankoop ";
}
else {
if ((broodje.Bestellen())==true){
Input.setText("" + (bedrag - broodje.prijs));
broodje.bericht = "Smakelijk met uw broodje ";
}
}
}
}
else {
broodje.Bijvullen();
}
Fouten.setText(broodje.GetMessage());
//broodje.Display(Fouten);
}
}
/***************************************************************************
**/
this is the second try when the exam was over, the first was a real disaster
I was told to have a strange way of handling things so my question is:
what's wrong with this code or the way I use it ?
p.s.: I've left out most comment to shorten this message
thnx
package automaat;
/***************************************************************************
**/
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/***************************************************************************
**/
public class Automaten extends Applet implements ActionListener{
boolean RF=true;
int i,xPos=20;
Broodje SmosKaas=new Broodje();
Broodje SmosHesp=new Broodje();
Broodje Martino=new Broodje();
Broodje Pitta=new Broodje();
Button[] knopje;
Button knopje2;
String[] Opschrift = {"SmosKaas","SmosHesp","Martino","Pitta"};
TextField Input;
TextField Fouten;
/***************************************************************************
**/
public void init(){
setLayout(null);
SmosKaas.prijs = 190;
SmosHesp.prijs = 190;
Martino.prijs = 190;
Pitta.prijs = 220;
knopje2 = new Button("Bijvullen");
knopje2.setBounds(300,20,60,30);
add (knopje2);
knopje2.addActionListener(this);
knopje = new Button[4];
for (i=0;i<knopje.length;i++){
knopje = new Button(Opschrift);
knopje.setBounds((xPos+(85*i)),230,80,30);
add (knopje);
knopje.addActionListener(this);
}
Input = new TextField("");
Input.setBounds(160,30,80,40);
Input.setFont(new Font("Serif",Font.BOLD,24));
add (Input);
Fouten = new TextField ("Hier zie je boodschappen van mij aan u ");
Fouten.setBounds(5,270,390,20);
add (Fouten);
}
/***************************************************************************
**/
public void paint (Graphics g){
SmosKaas.DrawStock(g,20,100,60,Color.yellow);
SmosHesp.DrawStock(g,110,100,60,Color.gray );
Martino.DrawStock(g,200,100,60,Color.red );
Pitta.DrawStock(g,290,100,60,Color.black);
}
/***************************************************************************
**/
public class Broodje{
int prijs = 0, stock = 10;
String bericht;
public boolean Bestellen() {
if (stock > 0) {
stock--;
this.bericht = "";
return true;
}
else {
this.bericht = "Deze schuif is leeg";
return false;
}
}
public void BijVullen(int Aantal) {
if (this.stock >= 10) {
this.bericht = "deze schuif zit nog vol met broodjes";
}
else {
if ( (this.stock + Aantal) > 10) {
this.bericht = "Er zijn " + ( (this.stock + Aantal) - 10) +
" broodjes op overschot ";
this.stock = 10;
}
}
}
public void Bijvullen() {
if (this.stock >= 10) {
this.bericht = "deze schuif zit vol ";
}
else {
this.stock++;
this.bericht = "1 broodje erbij";
}
}
public void DrawStock(Graphics g, int X, int Y, int Breedte, Color
Kleur) {
g.setColor(Kleur);
g.fillRect(X, Y, Breedte, (this.stock * 10));
}
public String GetMessage() {
return this.bericht;
}
public void Display(TextField AnyTextField) throws Exception {
try {
AnyTextField.setText(this.bericht);
}
catch (Exception e) {
AnyTextField.setText("");
System.out.println("de volgende fout is opgetreden: " +
e.getMessage());
}
if (AnyTextField.getWidth() < this.bericht.length()) {
throw new Exception("tekstvak is vol te klein ");
}
}
}
/***************************************************************************
**/
public void actionPerformed(ActionEvent ae){
if (ae.getSource() == knopje2) {
if (RF == true) {
RF = false;
knopje2.setLabel("Bestellen ");
Fouten.setText(" U bent in de BroodjesBijVulModus ");
}
else {
RF = true;
knopje2.setLabel("Bijvullen ");
Fouten.setText("U kunt broodjes bestellen");
}
}
if (ae.getSource()==knopje[0]){
Afhandeling(SmosKaas,RF);
}
if (ae.getSource()==knopje[1]){
Afhandeling (SmosHesp,RF);
}
if (ae.getSource()==knopje[2]){
Afhandeling(Martino,RF);
}
if (ae.getSource()==knopje[3]){
Afhandeling(Pitta,RF);
}
repaint();
}
/***************************************************************************
**/
void Afhandeling( Broodje DCN,boolean YN){
int bedrag;
boolean RF = YN;
Broodje broodje = DCN;
try {
bedrag = Integer.parseInt(Input.getText());
}
catch (NumberFormatException nfe) {
System.out.println("Oeps");
bedrag = 0;
}
if (RF==true){
if (bedrag == 0) {
broodje.bericht = "er zit geen geld in de machine ";
Input.setText("" + bedrag);
}
else {
if (bedrag < broodje.prijs) {
broodje.bericht = "u komt " + (broodje.prijs - bedrag) +
" eurocent tekort voor deze aankoop ";
}
else {
if ((broodje.Bestellen())==true){
Input.setText("" + (bedrag - broodje.prijs));
broodje.bericht = "Smakelijk met uw broodje ";
}
}
}
}
else {
broodje.Bijvullen();
}
Fouten.setText(broodje.GetMessage());
//broodje.Display(Fouten);
}
}
/***************************************************************************
**/
this is the second try when the exam was over, the first was a real disaster
I was told to have a strange way of handling things so my question is:
what's wrong with this code or the way I use it ?
p.s.: I've left out most comment to shorten this message
thnx