954,523 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Java student in need help

Hello everyone and thanks in advance for helping me.
I'm a 1st year java student in my first semester of college. Its comming towards the end of the semester and finals are starting up.. well my problem is that my java teacher decided to give us 2 projects to work on both of which im not experienced enough to work on with the time he's given us...

The first project is to fix another programmers code... (How am i suppose to do that i wondered i can barely do my own xD)

It must:
* Be broken into methods
* Have error checking with try and catch
* use a switch statement
* prevent the user from withdraing more money than he has
* use a do while loop or a while() loop
* and must run in a gui environment no systemoutprintln[/LIST]

import javax.swing.JOptionPane;

class MiniLab5
{
	public static void main (String[] args)
	{
	double balance = 0;
	double newBalance = 0;
	double adjustment = 0;

	String response;
	String moreBankingBusiness;

	moreBankingBusiness = JOptionPane.showInputDialog
		( "Do you want to do some banking?" );

	moreBankingBusiness = moreBankingBusiness.toUpperCase();

	while (moreBankingBusiness.equals ("YES"))
	{
	    response = JOptionPane.showInputDialog
		( "What would you like to do? (1=Deposit, 2=Withdraw, 3=Get Balance)");
	    if (response == null)
	    {
			JOptionPane.showMessageDialog
				(null, "You clicked on the Cancel button");
			System.exit (0);
	    } // if
	   else
	      if (response.equals(""))
	     {
			JOptionPane.showMessageDialog
				(null, "You must make an entry in the InputBox");
			System.exit (0);
	    } // else if
	     else
	       if (Integer.parseInt(response) < 1 | Integer.parseInt(response) > 3)
	       {
     			JOptionPane.showMessageDialog
				(null, response + " - is not a valid student type");
			System.exit (0);
	      } // else if

//1 is a Deposit

	     if (Integer.parseInt(response) == 1)
	    {
		     adjustment = Double.parseDouble
		     		(JOptionPane.showInputDialog( "Enter the Deposit Amount" ));

		    newBalance = balance + adjustment;

		    JOptionPane.showMessageDialog
			(null, "*** SMILEY NATIONAL BANK ***\n\n" +
			"Old Balance is: " + balance + "\n" +
			"Adjustment is: +" + adjustment + "\n" +
			"New Balance is: " + newBalance + "\n");
	   } // if

//2 is a Withdrawal
      if (Integer.parseInt(response) == 2)
      {
	    	adjustment = Double.parseDouble
    			(JOptionPane.showInputDialog( "Enter the Withdrawal Amount" ));

    		newBalance = balance - adjustment;

	    	JOptionPane.showMessageDialog
			    (null, "*** SMILEY NATIONAL BANK ***\n\n" +
			     "Old Balance is: " + balance + "\n" +
			     "Adjustment is: -" + adjustment + "\n" +
			    "New Balance is: " + newBalance + "\n");
     } // if

// 3 is a balance inquiry
	if (Integer.parseInt(response) == 3)
	{
			JOptionPane.showMessageDialog
			(null, "*** SMILEY NATIONAL BANK ***\n\n" +
			"Your Current Balance Balance is: " + balance );
	}// if

       balance = newBalance;
	   moreBankingBusiness = JOptionPane.showInputDialog
		  ( "Do you have more banking business?" );
	   moreBankingBusiness = moreBankingBusiness.toUpperCase();
  }	// end of while

	JOptionPane.showMessageDialog(null, "Thanks for banking with us!");
	System.exit (0);

 }				// end of main
}						// end of class


When he gave us the handout for this paper i thought to myself "What..." so if someone could please help me through this it would be a huge load off my shoulders while i study for finals and do the other project.

Unsated
Newbie Poster
21 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
 

Of course we will help you through it. Make a start, and if you get stuck post the complete details of whatever it is that's stopping you progressing.

What? You didn't expect anyone to do it for you, did you? That would be cheating.

JamesCherrill
Posting Genius
Moderator
6,371 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

Alright that sounds great ill work on it and repost where im at later on tonight, and no i didnt expect anyone to do it for me, nor did i want them to. Just a little advice ya know, change this or try this just a little backup. since im already doing 1 where i have to make a rock papers scissors game. i was using this thread as a little slack.

Unsated
Newbie Poster
21 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
 

OK, good luck.

JamesCherrill
Posting Genius
Moderator
6,371 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

TY =)

Unsated
Newbie Poster
21 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
 

This is what i have so far, i know it says for it to run in GUI but i wanted to do some of the other things first. can someone give me a little info on try and catch or show me where it goes? and any tips on how to improve this to make it overall better is greatly appreciated. if i do what was in the first post its 100% but if i add more its extra credit which i would love xD.

import java.util.Scanner;

public class Minilab5 {

public static void main(String[] args)
{
  Scanner in = new Scanner(System.in);
  int userChoice;
  boolean quit = false;
  float balance = 0f;
  do
	{
  	 System.out.println("1. Deposit money");
   	 System.out.println("2. Withdraw money");
   	 System.out.println("3. Check balance");
  	 System.out.print("0. To quit");
   	 userChoice = in.nextInt();

   		switch (userChoice)
            {
               case 1:
               float amount;
                System.out.print("Amount to deposit: ");
                amount = in.nextFloat();

                if (amount <= 0)
                    System.out.println("Cannot deposit negative amounts.");

                  else
                   {
                     balance += amount;
                     System.out.println("$" + amount + " has been deposited.");
                   }
                 break;
                  case 2:
                        System.out.print("Amount to withdraw: ");
                        amount = in.nextFloat();
                        if (amount <= 0 || amount > balance)
                             System.out.println("Insufficient Funds.");
                        else {
                             balance -= amount;
                             System.out.println("$" + amount + " has been withdrawn.");
                        }
                        break;
                  case 3:
                        System.out.println("Your balance: $" + balance);
                        break;
                  case 0:
                        quit = true;
                        break;
                  default:
                        System.out.println("Incorrect choice.");
                        break;
                  }
                  System.out.println();
          }
        while (!quit);
      System.out.println("Have a nice day");
    }
}
Unsated
Newbie Poster
21 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
 

Okay, it's a good start. A couple of important points though.

Firstly, you're running everything in a static method. You should run in an instance. Follow these steps:
1) Take all your local variables out of the main method and make them class variables.
2) Change the line that starts "public static void main" and give it a new, non-static, declaration. Something like "void dispatcher ()" would work.
3) Make a new public static void main that looks like this:

public static void main (String[] args) {
  Minilab5 banker = new Minilab5 ();
  banker.dispatcher ();
}


Keep in mind that dispatcher() will probably evaporate completely away as you GUI'ise your application. You can start GUI'ising by extending javax.swing.JFrame. To make the frame visible you'll have to add the line banker.setVisible (true); before you call the dispatcher.

The next step is to start refactoring out the code in each case into separate methods. So for instance, you'd have a case looking like this:

case 1:
  deposit ();
  break;


Later on you'll add some controls and stop talking through the console. But start with this first and post your next effort.

Paul Norris
Newbie Poster
17 posts since Aug 2011
Reputation Points: 10
Solved Threads: 2
 

Thanks a lot for you help paul, i didn't expect a reply so i'm just checking in now, i'm going to bed.... ill re-post my next effort around 1pm tommorow.

Unsated
Newbie Poster
21 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
 
Thanks a lot for you help paul, i didn't expect a reply so i'm just checking in now, i'm going to bed.... ill re-post my next effort around 1pm tommorow.


hey ive done the RockPaperScissors game for class and when you get to that part let me know

jasperking88
Newbie Poster
3 posts since Oct 2011
Reputation Points: 10
Solved Threads: 1
 

Hi Unsated. Paul's advice looks good to me. Listen to him. J.

JamesCherrill
Posting Genius
Moderator
6,371 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

Sure thing jasper, but thats a whole other monster atm xD.

This is what my teacher wants for that:

* run in a GUI environment

* have the rules for the game displayed so that the user knows how to play the game

* let the user play the game to completion

* use classes to break up the program into manageable modules

* keep track of wins/losses/ties

* have buttons for the user to click to run the game

* use at least one graphic Icon element.

* at least one constructor method

* create a separate Die class (in its own file)

* use programming comments and appropriate use of "white space" to make grading the work easy.

Unsated
Newbie Poster
21 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
 

Very interesting assignement, good luck.

bombay1982
Newbie Poster
14 posts since Mar 2011
Reputation Points: 10
Solved Threads: 1
 

Thanks for all of you help with this topic so far everyone, sorry for not posting an update like i said i was but i had something rather interesting happen to me in the past couple days that have had me distracted. ill hopefully have an update tommorow

Unsated
Newbie Poster
21 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: