PDA

View Full Version : Lol, Java Programming FTL


Octocamo
12-10-2006, 10:42 PM
I'm in Java programming for school. I'm going to take AP Java programming in January. It's an okay class. Easy at sometimes, hard at others. Our most recent project was to recreate the game "Pong".

This is how mine came out:

http://img169.imageshack.us/img169/2054/ponggo7.jpg

It's okay. I had to make the AI... "suck" because it was too good. If I can get it uploaded and playable, I will link it here.

Don't criticize my code for being poorly indented or whatever like my teacher does. I know it's not the best optimized, but hey... it works.

/**
* Pong applet - This Pong applet recreates the classic game of Pong in java.
* @author Kevin Lee
* @version 1.0 (December 2006)
*/
import java.awt.image.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import java.applet.*;
import java.awt.color.*;
import javax.swing.JOptionPane;

public class Pong extends JApplet implements Runnable, KeyListener
{

Ball Ball1;
Paddle Paddle1;
Paddle Paddle2;
double AI=0;
boolean ballOffscreen=false;
int reset=1;

int x=250;
int y=250;

// These are the integers used for collision detection
int ballX=250;
int ballY=250;
// Left Paddle's X
int Paddle1X=10;
//Left Paddle's Y
int Paddle1Y=250;
//Right Paddle's X
int Paddle2X=480;
//Right Paddle's Y
int Paddle2Y=250;


// The object we will use to write with instead of the standard screen graphics
Graphics bufferGraphics;
// The image that will contain everything that has been drawn on
// bufferGraphics.
Image offscreen;


int xChange = 1;
int yChange = 1;


public void init()
{
addKeyListener(this);

// This is the ball
Ball1 = new Ball (Color.white,ballX,ballY);

// This is the Paddle on the left
Paddle1 = new Paddle (Color.white,Paddle1X,Paddle1Y);

//This is the Paddle on the right
Paddle2 = new Paddle (Color.white,Paddle2X,Paddle2Y);


Dimension dim = getSize();
// We'll redraw the applet each time the mouse has moved.
// Create an offscreen image to draw on
// Make it the size of the applet, this is just perfect larger
// size could slow it down unnecessary.
offscreen = createImage(dim.width,dim.height);
// by doing this everything that is drawn by bufferGraphics
// will be written on the offscreen image.
bufferGraphics = offscreen.getGraphics();

// This pauses the game until someone presses the spacebar

}

public void paint (Graphics g)
{
drawAnimation();
// draw the offscreen image to the screen like a normal image.
// Since offscreen is the screen width we start at 0,0.
g.drawImage(offscreen,0,0,this);
}
public void drawAnimation()
{
//The following two lines resets the screen
bufferGraphics.setColor(Color.black);
bufferGraphics.fillRect(0,0,500,500);
//This draws the ball.
bufferGraphics.setColor(Color.white);
Ball1.drawBall(bufferGraphics);

//The left side paddle
Paddle1.drawPaddle(bufferGraphics);

//The right side paddle
Paddle2.drawPaddle(bufferGraphics);
//The middle line in the game
bufferGraphics.fillRect(250,0,10,500);
//The method used to check if the ball needs to bounce or not

bufferGraphics.drawString("Java Pong - Kevin Lee",375,495);

collisionDetection();
paddleAI();
}

public void collisionDetection()
{
//Left Paddles hits
//up
if (Ball1.x==20 && Ball1.y>Paddle1.y-20 && Ball1.y<Paddle1.y+10)
{
Ball1.right=true;
Ball1.yMovement=2;
Ball1.xMovement=1;
Ball1.up=false;
}
//right
if (Ball1.x==20 && Ball1.y>Paddle1.PaddleY+10 && Ball1.y<Paddle1.PaddleY+40)
{
Ball1.right=true;
Ball1.yMovement=1;
Ball1.xMovement=2;
}
//down
if (Ball1.x==20 && Ball1.y<Paddle1.PaddleY+100)
{
Ball1.right=true;
Ball1.yMovement=2;
Ball1.xMovement=1;
Ball1.up=true;
}

//Right Paddle hits
//up
if (Ball1.x==460 && Ball1.y>Paddle2.PaddleY-20 && Ball1.y<Paddle2.PaddleY+10)
{
Ball1.right=false;
Ball1.yMovement=2;
Ball1.xMovement=1;
Ball1.up=false;
}
//left
if (Ball1.x==460 && Ball1.y>Paddle2.PaddleY+10 && Ball1.y<Paddle2.PaddleY+40)
{
Ball1.right=false;
Ball1.yMovement=1;
Ball1.xMovement=2;
}
//down
if (Ball1.x==460 && Ball1.y>Paddle2.PaddleY+40 && Ball1.y<Paddle2.PaddleY+100)
{
Ball1.right=false;
Ball1.yMovement=2;
Ball1.xMovement=1;
Ball1.up=true;
}
}
public void paddleAI()
{
AI=Math.random();
if (Paddle1.PaddleY>0)
{
if (Paddle1.PaddleY>Ball1.y)
{

if (AI<.6)
{
Paddle1.PaddleY=Paddle1.PaddleY-2;
}
}
}
if (Paddle1.PaddleY<400)
{
if (Paddle1.PaddleY<Ball1.y)
{
if (AI<.6)
{
Paddle1.PaddleY=Paddle1.PaddleY+2;
}
}
}
}
// This makes the paddles move according to the users presses
public void keyPressed(KeyEvent e)
{
//These variables get the key the user presses
char theChar=e.getKeyChar();
int theCode=e.getKeyCode();

// Right Paddle Move Up
if(theCode==KeyEvent.VK_UP)
{
Paddle2.moveUp();

repaint();

}
// Right Paddle Move Down
if(theCode==KeyEvent.VK_DOWN)
{
Paddle2.moveDown();

repaint();

}


}

public void keyReleased(KeyEvent e)
{
}

public void keyTyped(KeyEvent e)
{
}





/************************************************** *******************************************/
/* BELOW IS FOR ANIMATION. THE ONLY THING THAT YOU NEED TO CHANGE IS DELAY
*/

int frame;
int delay=4; // This is the time of the delay in milliseconds.
Thread animator;

public void update(Graphics g)
{
paint(g);
}

/**
* This method is called when the applet becomes visible on
* the screen. Create a thread and start it.
*/
public void start()
{
animator = new Thread(this);
animator.start();
}

/**
* This method is called by the thread that was created in
* the start method. It does the main animation.
*/
public void run()
{
// Remember the starting time
long tm = System.currentTimeMillis();
while (Thread.currentThread() == animator)
{
// Display the next frame of animation.
repaint();
try
{
tm += delay;
Thread.sleep(Math.max(0, tm - System.currentTimeMillis()));
}
catch (InterruptedException e)
{
break;
}
// Advance the frame
frame++;
}
}

/**
* This method is called when the applet is no longer
* visible. Set the animator variable to null so that the
* thread will exit before displaying the next frame.
*/
public void stop()
{
animator = null;
}
}
/**
* This is the class ball for the Pong applet
*
* @author Kevin Lee
* @date December 2006
*/



class Ball
{
int x=250;
int y=250;
int xChange = 1;
int yChange = 1;
int Player1Score=0;
int Player2Score=0;

//These booleans are what tells the ball which direction to move.
boolean up = true;

boolean right = true;

boolean start = true;
int xMovement=1;
int yMovement=1;
int ballX;
int ballY;
Color ballColor;

public void init()
{

}
public Ball(Color theColor,int x, int y)
{
ballColor = theColor;
ballX= x;
ballY= y;

}
public void resetBall(Graphics g)
{
g.setColor(Color.white);
g.fillOval(x,y,5,5);
}

public void bounceLeft()
{
ballX= ballX-1;
}
public void bounceRight()
{
ballX= ballX+1;
}


public void drawBall(Graphics g)
{

//This draws the ball.
g.setColor(Color.white);
g.fillOval(x,y,15,15);
g.drawString(""+Player1Score,375,15);
g.drawString(""+Player2Score,125,15);

//tells ball to start moving down when it is near the top
if (y>479)
{
up=false;
}
//tells ball to return to the middle once it hits the right wall,
//then gives player 2 a point
if (x>500)
{
x=250;
Player2Score++;
xMovement=1;
yMovement=1;
}
//tells ball to start moving up when it hits the bottom
if (y<1)
{
up=true;
}
//returns ball to middle, gives player 2 a point
if (x<-20)
{
x=250;
Player1Score++;
xMovement=1;
yMovement=1;
}
if (up)
{
y=y+yMovement;
}
else y=y-yMovement;
if (right)
{
x=x+xMovement;
}
else x=x-xMovement;
}

}
/**
* This is the class Paddle for the Java applet
*
* @author Kevin Lee
* @date 11-28-06
*/



class Paddle
{


int PaddleX;
int PaddleY;
Color PaddleColor;
int color = 0;
int upOrDown=0;
int x;
int y;

public void init()
{

}
public Paddle(Color theColor,int x, int y)
{
PaddleColor=theColor;
PaddleX=x;
PaddleY=y;


}

public void drawPaddle (Graphics g)
{
g.setColor(Color.white);
g.fillRect(PaddleX,PaddleY,10,100);

}



public void moveUp()
{
if(PaddleY>=10)
PaddleY= PaddleY-25;

}
public void moveDown()
{
if(PaddleY<400)
PaddleY= PaddleY+25;

}


}




I did have it split up into 3 classes (Pong, Paddle, and Ball), but to upload it for the teacher to grade, I had to put it all into one java file.

So far I've made a paint program, Pong, BMI calculator, and other various mini-applets.

Anyone else have to program in java?

Blackwolf
12-10-2006, 11:08 PM
I actually was the TA for the first JAVA class taught at my college. We had to create Yahtzee. Or rather, the UI for it. (It was really a UI class that used JAVA for the projects.)

I don't know what your teacher is bitching about with the indenting - that's exactly how I have indented things for decades. I'm old. :/

I don't like JAVA because it's sluggish. I prefer to use C++, to be honest.

Burr
12-11-2006, 01:02 AM
I had to use it in college, though we didn't really make any games. It was all about data structures and such. As a teaching language, it's pretty good because Java is a little bit more rigid and doesn't get you into too many bad habits like C++ or Perl can..

Ditto on the sluggish part, but then performance was never really its aim.

Octocamo
12-11-2006, 12:19 PM
I actually was the TA for the first JAVA class taught at my college. We had to create Yahtzee. Or rather, the UI for it. (It was really a UI class that used JAVA for the projects.)

I don't know what your teacher is bitching about with the indenting - that's exactly how I have indented things for decades. I'm old. :/

I don't like JAVA because it's sluggish. I prefer to use C++, to be honest.

Haha.

I don't know man, I think I indent it correctly but when I upload it to his site so he can grade it, I think it messes up the indenting. He takes points off for it too.

Yeah, java sucks. And sometimes what you code doesn't even work right. I had the hardest time on getting the ball to recognize where the hell the paddles were. ;(

Burr, I think that's what we're going to be doing in the AP Java class.

I think for the final project (due at end of January) my friend and I are going to remake Galaga or some arcade game.

Btw, is C++ about the same difficulty as Java or is it much harder?

Blackwolf
12-11-2006, 01:09 PM
C++ doesn't coddle you. You actually have to deal with pointers, and you can actually really fuck up your system if you're not careful with memory management.

A good analogy is that Java is coded in such a way to where you can't really drop something on your toe. Even if you tried. However, C++ will not only allow you to drop something on your toe, it will make sure it's really heavy and going really fast.

GH33DA
12-11-2006, 02:38 PM
Fuck your teacher dude. It's fine. Make sure you post if you upload it.

AcquiredTarget
12-11-2006, 03:23 PM
Bah, there must something about Java that makes instructors demand that kind of indenting. I had two classes (data structures w/ Java and a C++ class) with the same instructor who had an indentation fetish. My VB instructor didn't care about indenting, but he did care about commenting the code. I do the same amount of indenting as you because I find it readable.

Wii6400
12-11-2006, 05:51 PM
Looks good. Would love to see your other programs :)

Octocamo
01-05-2007, 04:02 PM
For my final project I'm making a Metal Slug game with a friend. It's not going to be anything great. In about 30 minutes I've already created the title screen with music and a key listener.

It may be something we will work on after the class is over if it's anything worth doing.

Octocamo
01-16-2007, 06:49 PM
Screens of Work in Progress:

http://img403.imageshack.us/img403/2744/ms3lc9.jpg
http://img158.imageshack.us/img158/2112/ms2ez9.jpg
http://img158.imageshack.us/img158/9142/ms1jz2.jpg

GH33DA
01-17-2007, 12:27 PM
I like everything except that background.

Octocamo
03-18-2007, 03:34 PM
had to make a banking program for a project..code is here:


/**
* This is the bank class which creates an interface for the accounts. The user chooses and option and then they can access information or execute methods to their account
* @author Kevin Lee
* @version 3/15/07
*/
import java.io.*; //input/output
import java.util.Scanner; //imports scanner

public class Bank
{
int choice=1; // used for menu
Scanner scan=new Scanner(System.in); // initializes scanner
Account acct1=null; // accounts, set to null until a new one is created
Account acct2=null;
Account acct3=null;
Account acct4=new Account("Frank", 81437.77); // already made account

//used to distinguish between the accounts in some methods
boolean account1=false;
boolean account2=false;
boolean account3=false;
boolean account4=false;


public Bank()
{
// prints welcome message and then executes the menu
System.out.println("Welcome to the Bank! Please choose an option below by typing the corresponding number: ");
System.out.println("");
menu();// main menu prints
}
// this method allows the user to add a new account, 3 are needed for the program to work
public void newAccount()
{

reset(); // resets the scanner
double money=0; // used to keep track of the money the user enters
String name=""; // used to keep track of the name the user enters

if (acct1==null)//checks if acct1 is usable to create a new account, if so, it does
{
System.out.println(""); System.out.println("Please type a name below: ");
name=scan.nextLine();
System.out.println("Please type your amount of money below: ");
money = scan.nextDouble();
System.out.println("");
acct1 = new Account(name,money);
menu();

}
else if(acct2==null)//checks if acct2 is usable to create a new account, if so, it does
{
System.out.println(""); System.out.println("Please type a name below: ");
name=scan.nextLine();
System.out.println("Please type your amount of money below: ");
money = scan.nextDouble();
System.out.println("");
acct2 = new Account(name,money);
menu();
}
else if(acct3==null)//checks if acct3 is usable to create a new account, if so, it does
{
System.out.println(""); System.out.println("Please type a name below: ");
name=scan.nextLine();
System.out.println("Please type your amount of money below: ");
money = scan.nextDouble();
System.out.println("");
acct3 = new Account(name,money);
menu();
}
else //if 3 accounts are created, no more are accepted
{
System.out.println("Sorry, We're not accepting any more new accounts.");
menu();// prints main menu
}
}
// this method asks the user to choose the individual account and they can deposit, withdraw, or close the account
public void individualAccount()
{
boolean choosen=false;//used for choosing individual account

// lets you choose the account
System.out.println("");
System.out.println("Individual Account");
System.out.println("");
System.out.println("Choose account: ");
System.out.println("");
System.out.println("1.) " + acct1.getName());
System.out.println("2.) " + acct2.getName());
System.out.println("3.) " + acct3.getName());
System.out.println("4.) " + acct4.getName());
String c=readString(); choice=Integer.parseInt(c);
//checks which count you choose
if(choice==1)
{
account1= true;
choosen = true;

}
else if (choice==2)
{
account2=true;
choosen=true;
}
else if (choice==3)
{
account3=true;
choosen=true;

}
else if (choice==4)
{
account4=true;
choosen=true;

}
// allows you to execute methods on the account choosen
if(choosen==true)
{
System.out.println("1. make deposit");
System.out.println("2. make withdrawal");
System.out.println("3. close");
System.out.println("4. exit to menu");
System.out.println("");
c=readString();
choice=Integer.parseInt(c);
if(choice==1)
makeDeposit();
else if (choice==2)
makeWithdrawal();
else if (choice==3)
closeAccount();
else if (choice==4)
menu();
choosen=false;
}
}
public void consolidate()// this method allows the user to consolidate 2 accounts into one
{
// lets you choose the account
boolean choosen=false;
System.out.println(""); System.out.println("Please choose account name below: ");
System.out.println("");
System.out.println("1.) " + acct1.getName());
System.out.println("2.) " + acct2.getName());
System.out.println("3.) " + acct3.getName());
System.out.println("4.) " + acct4.getName());
String c=readString(); choice=Integer.parseInt(c);

if(choice==1)
{
account1= true;
choosen = true;

}
else if (choice==2)
{
account2=true;
choosen=true;
}
else if (choice==3)
{
account3=true;
choosen=true;

}
else if (choice==4)
{
account4=true;
choosen=true;

}
if(choosen==true)
{
System.out.println(""); System.out.println("Please choose the 2nd account name below: ");
System.out.println("");
System.out.println("1.) " + acct1.getName());
System.out.println("2.) " + acct2.getName());
System.out.println("3.) " + acct3.getName());
System.out.println("4.) " + acct4.getName());
c=readString(); choice=Integer.parseInt(c);

if(choice==1)
{
account1= true;
choosen = false;
}
else if (choice==2)
{
account2=true;
choosen = false;
}
else if (choice==3)
{
account3=true;
choosen = false;
}
else if (choice==4)
{
account4=true;
choosen = false;
}

}
if(account1==true&&account2==true)
{
Account.consolidate(acct1,acct2);
menu();// prints main menu
account1=false; account2=false;
}
if(account1==true&&account3==true)
{
Account.consolidate(acct1,acct3);
menu();// prints main menu
account1=false; account3=false;
}
if(account1==true&&account4==true)
{
Account.consolidate(acct1,acct4);
menu();// prints main menu
account1=false; account4=false;
}
if(account2==true&&account3==true)
{
Account.consolidate(acct2,acct3);
menu();// prints main menu
account2=false; account3=false;
}
if(account2==true&&account4==true)
{
Account.consolidate(acct2,acct4);
menu();// prints main menu
account2=false; account4=false;
}
if(account3==true&&account4==true)
{
Account.consolidate(acct3,acct4);
menu();// prints main menu
account3=false; account4=false;
}

}
// this method allows you to choose the account that you choose
public void closeAccount()
{
boolean choosen=false;
// lets you choose the account to close
System.out.println(""); System.out.println("Please choose account name below: ");
System.out.println("");
System.out.println("1.) " + acct1.getName());
System.out.println("2.) " + acct2.getName());
System.out.println("3.) " + acct3.getName());
System.out.println("4.) " + acct4.getName());
String c=readString(); choice=Integer.parseInt(c);

if(choice==1)
{
account1= true;
choosen = true;
}
else if (choice==2)
{
account2=true;
choosen=true;
}
else if (choice==3)
{
account3=true;
choosen=true;

}
else if (choice==4)
{
account4=true;
choosen=true;
}
if(account1==true&&choosen==true)
{
acct1.close();
menu();
choosen=false;account1=false;

}
if(account2==true&&choosen==true)
{
acct2.close();
menu();
choosen=false;account2=false;

}
if(account3==true&&choosen==true)
{
acct3.close();
menu();
choosen=false;account3=false;

}
if(account4==true&&choosen==true)
{
acct4.close();
menu();
choosen=false;account4=false;

}

}
// lets you make a deposit to the account
public void makeDeposit()
{

reset(); //resets scanner
double money=0;

if(account1==true)
{
System.out.println("");
System.out.println("Please type the ammount of money to deposit: ");
money = scan.nextDouble();
acct1.makeDeposit(money);
System.out.println("");
System.out.println(acct1.toString());System.out.pr intln("");
menu();// prints main menu
account1=false;
}
if(account2==true)
{
System.out.println("");
System.out.println("Please type the ammount of money to deposit: ");
money = scan.nextDouble();
acct2.makeDeposit(money);
System.out.println("");
System.out.println(acct2.toString());System.out.pr intln("");
menu();// prints main menu
account2=false;
}
if(account3==true)
{
System.out.println("");
System.out.println("Please type the ammount of money to deposit: ");
money = scan.nextDouble();
acct3.makeDeposit(money);
System.out.println("");
System.out.println(acct3.toString());System.out.pr intln("");
menu();// prints main menu
account3=false;
}
if(account4==true)
{
System.out.println("");
System.out.println("Please type the ammount of money to deposit: ");
money = scan.nextDouble();
acct4.makeDeposit(money);
System.out.println("");
System.out.println(acct4.toString());System.out.pr intln("");
menu();// prints main menu
account4=false;
}
}
// lets you make a withdrawal to the account
public void makeWithdrawal()
{
reset();//resets scanner
double money=0;

if(account1==true)
{
System.out.println("");
System.out.println("Please type the ammount of money to withdraw: ");
money = scan.nextDouble();
acct1.makeWithdrawal(money);
System.out.println("");
System.out.println(acct1.toString());System.out.pr intln("");
menu();// prints main menu
account1=false;
}
if(account2==true)
{
System.out.println("");
System.out.println("Please type the ammount of money to withdraw: ");
money = scan.nextDouble();
acct2.makeWithdrawal(money);
System.out.println("");
System.out.println(acct2.toString());System.out.pr intln("");
menu();// prints main menu
account2=false;
}
if(account3==true)
{
System.out.println("");
System.out.println("Please type the ammount of money to withdraw: ");
money = scan.nextDouble();
acct3.makeWithdrawal(money);
System.out.println("");
System.out.println(acct3.toString());System.out.pr intln("");
menu();// prints main menu
account3=false;
}
if(account4==true)
{
System.out.println("");
System.out.println("Please type the ammount of money to withdraw: ");
money = scan.nextDouble();
acct4.makeWithdrawal(money);
System.out.println("");
System.out.println(acct4.toString());System.out.pr intln("");
menu();// prints main menu
account4=false;
}
}
// this method allows the user to transfer money from one account to another
public void transfer()
{
reset();
boolean choosen=false;
double money=0;

System.out.println(""); System.out.println("Please choose account name below: ");
System.out.println("");
System.out.println("1.) " + acct1.getName());
System.out.println("2.) " + acct2.getName());
System.out.println("3.) " + acct3.getName());
System.out.println("4.) " + acct4.getName());
String c=readString(); choice=Integer.parseInt(c);

if(choice==1)
{
account1= true;
choosen = true;

}
else if (choice==2)
{
account2=true;
choosen=true;
}
else if (choice==3)
{
account3=true;
choosen=true;

}
else if (choice==4)
{
account4=true;
choosen=true;

}
if(choosen==true)
{
System.out.println(""); System.out.println("Please choose the 2nd account name below: ");
System.out.println("");
System.out.println("1.) " + acct1.getName());
System.out.println("2.) " + acct2.getName());
System.out.println("3.) " + acct3.getName());
System.out.println("4.) " + acct4.getName());
c=readString(); choice=Integer.parseInt(c);

if(choice==1)
{
account1= true;
choosen = false;
}
else if (choice==2)
{
account2=true;
choosen = false;
}
else if (choice==3)
{
account3=true;
choosen = false;
}
else if (choice==4)
{
account4=true;
choosen = false;
}


}
System.out.println(""); System.out.println("Please type the amount you wish to transfer: ");
if(account1==true&&account2==true)
{
money = scan.nextDouble();
Account.transfer(acct1,acct2,money);// transfers from acct1 to acct2
menu();// prints main menu
account1=false; account2=false;
}
if(account1==true&&account3==true)
{
money = scan.nextDouble();
Account.transfer(acct1,acct3,money); // transfers from acct1 to acct3
menu();// prints main menu
account1=false; account3=false;
}
if(account1==true&&account4==true)
{
money = scan.nextDouble();
Account.transfer(acct1,acct4,money);//transfers from acct1 to acct4
menu();// prints main menu
account1=false; account4=false;
}
if(account2==true&&account3==true)
{
money = scan.nextDouble();
Account.transfer(acct2,acct3,money);// transfers from acct2 to acct3
menu();// prints main menu
account2=false; account3=false;
}
if(account2==true&&account4==true)
{
money = scan.nextDouble();
Account.transfer(acct2,acct4,money);// tranfers from acct2 to acct4
menu();// prints main menu
account2=false; account4=false;
}
if(account3==true&&account4==true)
{
money = scan.nextDouble();
Account.transfer(acct3,acct4,money);// transfers from acct3 to acct4
menu();// prints main menu
account3=false; account4=false;
}

}
// this method lists all the accounts in the bank
public void listAccounts()
{
System.out.println("");System.out.println(acct1.toString());
System.out.println("");System.out.println(acct2.toString());
System.out.println("");System.out.println(acct3.toString());
System.out.println("");System.out.println(acct4.toString());
menu();// prints main menu

}
// this method prints out information about the bank such as the number of accounts and total money
public void bankInfo()
{
System.out.println("");
System.out.println(Account.bankInfo());// prints number of accounts and total money
System.out.println("");
menu(); // prints main menu

}
// this is the menu for the bank class, it allows the users to choose from a number of options
public void menu()
{

System.out.println("");
System.out.println("1. create new account"); // creates new account
System.out.println("2. select individual account"); // select account and execute methods
System.out.println("3. consolidate accounts"); // consolidate 2 accounts into 1
System.out.println("4. transfer funds"); // transfer funds from 1 account to another
System.out.println("5. list open accounts"); // lists all open accounts at the bank
System.out.println("6. list bank info"); // lists the bank information
System.out.println("7. exit"); // quits the program

while(choice!=0)
{
String c=readString();// reads string
choice=Integer.parseInt(c);// reads choice as an int

if(choice==1)
newAccount();// creates new account
else if (choice==2)
individualAccount();// select individual account
else if (choice==3)
consolidate();// consolidate 2 accounts into 1
else if (choice==4)
transfer();// transfer money from 1 account to another
else if (choice==5)
listAccounts();// list account info
else if (choice==6)
bankInfo();// prints bank info
else if (choice==7)
choice=0;// quit
else if (choice!=0)// if user chooses something that's not there
{
System.out.println("");System.out.println("That is an invalid choice."); System.out.println("");
}
}

}


public String readString()// used for reading in strings
{
// open up standard input
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String theString = null;

try
{
theString = br.readLine();
}
catch (IOException ioe)
{
System.out.println("IO error trying to read the string");
System.exit(1);
}
return theString;
}
private void reset()// resets the scanner
{
scan=new Scanner(System.in);
}


}


/*
* This class represents a bank
* @author Kevin Lee
* @date 3-14-07
*/
public class Account
{
private static final int STARTINGACCOUNT=10325356;
private static int numOfAccounts=STARTINGACCOUNT;
private double balance;
private String name;
private int accountNumber;
private static double totalMoney=0;

/**
* The constructor automatically takes in a name and their money and
* set the global name and balance to that automatically.
*/

public Account (String name, double money)
{
this.name=name;
balance=money;
totalMoney+=money;
accountNumber=numOfAccounts;
numOfAccounts++;

}

public void setName(String newName)
{
name=newName;
}

// Returns the name of the account owner
public int getAccountNum ()
{
return accountNumber;
}
public String getName ()
{
return name;
}
// Returns the balance
public double getBalance ()
{
return balance;
}
public static String bankInfo()
{
return ("The bank currently has " + numOfAccounts + " accounts and has $" + totalMoney + " total.");

}
//attempt to make withdrawal, if the amount is available it will return true, otherwise false
public boolean makeWithdrawal(double withdrawalAmount)
{
if (withdrawalAmount<balance)
{
balance-=withdrawalAmount;
totalMoney-=withdrawalAmount;
return true;
}
else
return false;
}

//that will take in a double and add it to balance
public void makeDeposit(double depositAmount)
{
balance+=depositAmount;
totalMoney+=depositAmount;
}

// method will return a useful string of the account info
public String toString()
{
return ("Account name is " +name+ " Their balance is: " + balance + " and their account # is: " + accountNumber);
}
//method will return the number of accounts open to date
public static int totalNumOfAccounts()
{

return numOfAccounts-STARTINGACCOUNT;

}
public static double getTotalMoney()
{

return totalMoney;

}
public void close()
{
name="CLOSED";
totalMoney-=balance;
balance = 0;
numOfAccounts--;

}
public static Account consolidate(Account acct1, Account acct2)
{
if(acct1.name.equals(acct2.name))
{
if(acct1.accountNumber!=acct2.accountNumber)
{
Account returnAccount = new Account(acct1.name,acct1.balance+acct2.balance);
acct1.close();
acct2.close();
System.out.println("");
System.out.println("Account creation sucessful!");
System.out.println(returnAccount.toString());
System.out.println("");
return returnAccount;
}
else
{
System.out.println("");
System.out.println("Account creation failed.");
System.out.println("");
}
}

return null;

}
// The money that is transferred will go from the account you are using to the parameter account.
public void transfer(Account acct, double amount)
{
makeWithdrawal(amount);
acct.makeDeposit(amount);
System.out.println("Money transfer was sucessful.");

}
// The money that is transferred will go from acct1 to acct2.
public static void transfer(Account acct1,Account acct2, double amount)
{
acct1.makeWithdrawal(amount);
acct2.makeDeposit(amount);
System.out.println("Money transfer was sucessful.");
}


}


I know some of it is sloppy and some of it could be broken down into more methods, but it works... heh.

Octocamo
05-22-2007, 06:59 PM
http://img527.imageshack.us/img527/5055/javacsey6.png

screenshot of current project I'm making. It's Java Counter-Strike for my final project in the class.

A year or 2 ago 2 kids made Doom in Java. Teacher let me use their code and make a mod for it (giving credit of course). So far I've changed a lot of things (all graphics basically) and coding (lol T Win, CT Win), and all the sounds to make it feel like CS. The AI is mad buggy, but it's still fun and it'll be a cool project to show off to the class. After it's finished I'll post a working copy.

It has 3 guns: Pistol, Shotgun, and AK47

Title Screen =

http://img172.imageshack.us/img172/162/csli4.jpg

:thup:

Token White Guy
05-22-2007, 07:28 PM
http://img527.imageshack.us/img527/5055/javacsey6.png

screenshot of current project I'm making. It's Java Counter-Strike for my final project in the class.

A year or 2 ago 2 kids made Doom in Java. Teacher let me use their code and make a mod for it (giving credit of course). So far I've changed a lot of things (all graphics basically) and coding (lol T Win, CT Win), and all the sounds to make it feel like CS. The AI is mad buggy, but it's still fun and it'll be a cool project to show off to the class. After it's finished I'll post a working copy.

It has 3 guns: Pistol, Shotgun, and AK47

Title Screen =

http://img172.imageshack.us/img172/162/csli4.jpg

:thup:

Nice. Is your teacher going to give you full credit for using an existing code?

Octocamo
05-22-2007, 07:49 PM
Yeah.

Some people are actually stealing code in the class without credit. :/

it's an AP class and we already took the AP test, so the final project was supposed to be more about fun than the actual grade.

Blackwolf
05-22-2007, 08:30 PM
I made a Doom remake as the final in AP Computer Science too. Except it used my school and had pictures of the teachers as targets.

Luckily it was in 1995, and instead of being expelled and sent to a psychiatrist, I had to make a second version with pictures of problem students so the teachers could run around the school and shoot them instead.

Octocamo
05-22-2007, 08:47 PM
Yeah, I remember you talking about that on your show when the kid got expelled from school for his CSS map.

This is their original project: http://myonlinegrades.com/java/files/doom/Doom/Doom.html

I think my adaptation of it improved it a bit. :)

Token White Guy
05-22-2007, 09:16 PM
I always wanted to make Java games, but it's hard to find a good Java app for Mac. If I can make "Hello World" just once I'll be happy.

Decipher
05-23-2007, 04:52 AM
Yeah, I remember you talking about that on your show when the kid got expelled from school for his CSS map.

This is their original project: http://myonlinegrades.com/java/files/doom/Doom/Doom.html

I think my adaptation of it improved it a bit. :)

Wow.. I hope you improved on his sound code. That damn midi kept looping over and over again in firefox long after I'd closed the tab. I had to restart the program to get rid of it.

Token White Guy
05-23-2007, 01:10 PM
Wow.. I hope you improved on his sound code. That damn midi kept looping over and over again in firefox long after I'd closed the tab. I had to restart the program to get rid of it.

It did that to me.

Octocamo
06-16-2007, 10:02 PM
Java does that shit in all browsers. Java FTL.


Play my Counter-Strike Java Game (http://myonlinegrades.com/apjava/uploads/finals/Doom/Doom.html)

hopefully all the graphics and sounds work. Read the splash screens for controls, etc.