Java and programming newbie needing help
Hi all, I know this is something so basic that most won't even look at it but considering that my son is 14 yr old, new to programming and java, maybe a good soul can help him... and we will appreciate a lot.
First, he is doing the famous bank account program that must have a class and a tester... so far so good but after executing the last call from the tester he needs to test the value of the variable opt and there is no way he can get it... here is his code so far and in the last line of the class BankAccount the variable Opt (in red) is not defined. What should he do to continue his program there?
Sorry for the so basic question.... and thanks in advance
Speed
Code Java:
public class BankAccount1tester {
public static void main(String [] args){
BankAccount1 student1 = new BankAccount1();
BankAccount1.setName();
BankAccount1.setAccountNumber();
BankAccount1.setInitialBalance();
BankAccount1.setOptions();
}
}
Code Java:
import javax.swing.*;
public class BankAccount1 {
public static String setName(){
JFrame BankAccount = new JFrame();
BankAccount.setSize(400,400);
BankAccount.setTitle("Name");
BankAccount.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
String name = JOptionPane.showInputDialog("Enter your name to create an account");
return name;
}
public static int setAccountNumber(){
JFrame BankAccount = new JFrame();
BankAccount.setSize(400,400);
BankAccount.setTitle("AccNum");
BankAccount.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
String AccountNumber = JOptionPane.showInputDialog("Enter the desired account number:");
int AccountNum = Integer.parseInt((AccountNumber));
return AccountNum;
}
public static int setInitialBalance(){
JFrame BankAccount = new JFrame();
BankAccount.setSize(400,400);
BankAccount.setTitle("IntBalance");
BankAccount.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
String InitialBalance = JOptionPane.showInputDialog("Enter your initial balance:");
int InitBalance = Integer.parseInt((InitialBalance));
return InitBalance;
}
public static int setOptions (){
JFrame BankAccount = new JFrame();
BankAccount.setSize(400,400);
BankAccount.setTitle("Options");
BankAccount.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
String Options = JOptionPane.showInputDialog("What would you like to do?: \n" +
"Enter 1 to withdraw \n" +
"Enter 2 to deposit \n" +
"Enter 3 to check current balance \n");
int Opt = Integer.parseInt((Options));
return Opt;
}
if ( Opt == 1){
}
}
}
Re: Java and programming newbie needing help
If you had Opt as a class variable and had a getOptionsMethod that returned the option, then you could use the if statement
if (getOptions() == 1)
....
Put an int called Opt in your constructor.
Also, you need a constructor for BankAccount1 anyway as it looks like you're calling one.
Once you do this.
You have the local variable that was Opt become Opt2 and
for setOptions
Opt = Opt2;
and for getOptions
return Opt;
Re: Java and programming newbie needing help
Still lost... we went one step further but still fighting with variable scope... now we are reading the name, bank account, initial balance and options but when Actions tries to output them we just get null...
What should we do to output those values without losing them somewhere?
Cheers and thanks for the previous answer....
Speed
Code Java:
public class BankAccount1tester {
public static void main(String [] args){
BankAccount1 Bank = new BankAccount1();
BankAccount1.setName();
BankAccount1.setAccountNumber();
BankAccount1.setInitialBalance();
BankAccount1.setOptions();
BankAccount1.Action();
}
}
Code Java:
import javax.swing.*;
public class BankAccount1 {
public static double blank;
public static double InitialBalance;
public static double currentBalance = InitialBalance;
public static double deposit;
public static double withdraw;
public static String name;
public static String accountNumber;
public static String setName(){
JFrame BankAccount = new JFrame();
BankAccount.setSize(400,400);
BankAccount.setTitle("Name");
BankAccount.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
String name = JOptionPane.showInputDialog("Enter your name to create an account");
return name;
}
public static double setAccountNumber(){
JFrame BankAccount = new JFrame();
BankAccount.setSize(400,400);
BankAccount.setTitle("AccNum");
BankAccount.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
String AccountNumber = JOptionPane.showInputDialog("Enter the desired account number:");
double accountNum = Integer.parseInt((AccountNumber));
return accountNum;
}
public static double setInitialBalance(){
JFrame BankAccount = new JFrame();
BankAccount.setSize(400,400);
BankAccount.setTitle("IntBalance");
BankAccount.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
String InitialBalance = JOptionPane.showInputDialog("Enter your initial balance:");
int initBalance = Integer.parseInt((InitialBalance));
return initBalance;
}
public static double setOptions (){
JFrame BankAccount = new JFrame();
BankAccount.setSize(400,400);
BankAccount.setTitle("Options");
BankAccount.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
String Options = JOptionPane.showInputDialog("Hello " + name + ", your account number is " + accountNumber + "\n" +
"What would you like to do?: \n" +
"Enter 1 to withdraw \n" +
"Enter 2 to deposit \n" +
"Enter 3 to check current balance \n");
int Opt = Integer.parseInt((Options));
return Opt;
}
public static double Action(){
JFrame BankAccount = new JFrame();
BankAccount.setSize(400,400);
BankAccount.setTitle("Options");
BankAccount.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
if (setOptions() == 1){
String Withdraw = JOptionPane.showInputDialog("Enter amount to withdraw: ");
double withdraw = Integer.parseInt((Withdraw));
return withdraw;
}
else if (setOptions() == 2){
String Deposit = JOptionPane.showInputDialog("Enter amount to deposit: ");
double deposit = Integer.parseInt((Deposit));
return deposit;
}
else if (setOptions() == 3){
JOptionPane.showMessageDialog(BankAccount, "Your current balance is: \n" + name, "Current Balance", JOptionPane.INFORMATION_MESSAGE);
}
return (Double) blank;
}
}
Re: Java and programming newbie needing help
return (Double) blank;
blank is never initialized.
Have a methods that calculate balance
Also, have a variable, initialized to 0.00 in constructor called balance.
Code java:
public void withdraw()
{
double withdraw2l = Double.parseDouble(JOptionPane.showMessageDialog(null, "Enter amount to withdraw", "Making a widthdrawl", JOptionPane.INFORMATION_MESSAGE));
if (balance -withdraw2l <0)
{
JOptionPane.showMessageDialog(null, "Cannot have negative balance", "Negative withdrawl", JOptionPane.ERROR_MESSAGE);
balance = balance;
}
else
balance = balance - widthdrawl2;
}
public void deposit()
{
double deposit2 = Double.parseDouble(JOptionPane.showMessageDialog(null, "Enter amount to deposit", "Depositing", JOptionPane.INFORMATION_MESSAGE));
balance = balance + deposit2;
}
public double getBalance()
{
return balance;
}
Now for Option 1
if (option ==1)
{
withdraw();
}
else if (option ==2)
{
deposit();
}
else if (option ==3)
{
JOptionPane.showMessageDialog(null, "Your depost is $" +getDeposit() + ".", "Your balance statement", JOptionPane.INFORMATION_MESSAGE);
}
else
{
JOptionPane.showMessageDialog(null, "Enter a 1,2, or 3.", "Invalid option", JOptionPane.ERROR_MESSAGE);
}
and change return type of setOptions to void if you can.
If it must be double, make it return balance or something.
Re: Java and programming newbie needing help
Hello speedmaster, welcome to the forums.
You cannot put the if statement there because it is not in a method.
Take a look at this example:
Code Java:
import javax.swing.*;
public class BankAccount1 {
/**
* JavaProgrammingForums.com
*/
public static int Opt;
public static String setName(){
JFrame BankAccount = new JFrame();
BankAccount.setSize(400,400);
BankAccount.setTitle("Name");
BankAccount.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
String name = JOptionPane.showInputDialog("Enter your name to create an account");
return name;
}
public static int setAccountNumber(){
JFrame BankAccount = new JFrame();
BankAccount.setSize(400,400);
BankAccount.setTitle("AccNum");
BankAccount.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
String AccountNumber = JOptionPane.showInputDialog("Enter the desired account number:");
int AccountNum = Integer.parseInt((AccountNumber));
return AccountNum;
}
public static int setInitialBalance(){
JFrame BankAccount = new JFrame();
BankAccount.setSize(400,400);
BankAccount.setTitle("IntBalance");
BankAccount.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
String InitialBalance = JOptionPane.showInputDialog("Enter your initial balance:");
int InitBalance = Integer.parseInt((InitialBalance));
return InitBalance;
}
public static int setOptions (){
JFrame BankAccount = new JFrame();
BankAccount.setSize(400,400);
BankAccount.setTitle("Options");
BankAccount.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
String Options = JOptionPane.showInputDialog("What would you like to do?: \n" +
"Enter 1 to withdraw \n" +
"Enter 2 to deposit \n" +
"Enter 3 to check current balance \n");
Opt = Integer.parseInt((Options));
return Opt;
}
public static void ifstatement(){
if(Opt == 1){
System.out.println("Opt is 1");
}
}
}
Does it help?