Java - paycheck issue - java

import javax.swing.JOptionPane;
public class PayCheckStatic
{
public static void main(String[] args)
{
while (name!=null)
{
String name = JOptionPane.showInputDialog("Please enter the next employees name" );
String wage = JOptionPane.showInputDialog("Please enter their hourly wage?");
String hoursWorked = JOptionPane.showInputDialog ("How many hours did they work this last work?");
double wages = Double.parseDouble(wage);
double hours = Double.parseDouble(hoursWorked);
calculatePay(name,wages,hours);
}
}
private static void calculatePay(String name,double wages,double hours)
{
if (hours > 40)
{
double pay = ((wages * 40)+((hours - 40)*(1.5 * wages)));
JOptionPane.ShowMessageDialog (null,name + "'s pay is £" + pay);
}
else
{
double pay = (wages * hours);
JOptionPane.ShowMessageDialog (null,name + "'s pay is £" + pay);
}
}
}
For some reason my code won't compile and it is coming up with cannot find symbol errors, and I can't work out why. The error is showing 3 times, with 2 of them on the message dialog boxes. Any tips as to how I can fix it?

Your main method starts with:
while(name != null)
but you have not declared name yet. You need to move the String name line before you while loop starts.

As other have pointed out, you need fix your while loop in main because of name not being defined.
However, there is also another error: your calls to JOptionPane.ShowMessageDialog() are incorrect.
The correct method you should be calling is JOptionPane.showMessageDialog() (notice the camel case method name instead of pascal case)

If the code you posted is indeed your full program, then you have an undefined name in your main() function.
You will either have to add a static variable names name in the class, so the static main() can access it or declare it locally in main(). On a second look, you do have a String name declared, but after you are trying to use it. Move the declaration of name before the if() in main()

Related

Simple Eclipse IDE Java Program

Code a complete Java program to the following:
if it is a red light violation, then the fine is 1.75 % of the driver annual salary
if it is a speeding violation, then the fine is 1.2 %of the annual salary plus .5% of the salary for every 1 mile/hour over the speed limit.
What you need to do?
prompt user/officer to enter the full name of the driver
prompt officer to enter his/her full name.
prompt user/officer to enter annual salary of the driver.
prompt the officer to enter the type of violation (1 for red light or 2 for speeding)
if speeding then prompt user to enter how many miles/hour over speed limit.
evaluate with a set of if...else how much fine should be assessed, then print a full report including:
name of officer
name of driver
annual salary
violation type (you need to spell it out not just 1 or 2. (Red Light or Speeding)
amount of fine assessed (print a $ sign and round to two decimals)
Also, you must consider the following:
your program must not accept any code other than 1 or 2 for violation type.
your input must be handled by different methods and not in main()
evaluating the fine must be handled in a method of its own.
report must be handled by a different method.
the whole program must repeat for a new driver.
Extra credits: develop your own solution when a driver is subject to both violation…caught speeding and passing when red
Here is my code:
package FinFines_
import java.util.Scanner;
/*
* Description: This program assesses fines for traffic violations.
*/
public class FinFines_
{
static Scanner get = new Scanner(System.in);
//declarations:
static double mph = 0;
static double salary = 0;
static double fines = 0;
static int violation = 0;
static int answer = 1; //1 to continue or 0 to quit
static String officer = " ";
static String driver = " ";
public static void main(String[] args)
{
//input:
while (answer != 0) //while will allow the program to repeat for a new driver
{
input(officer, driver, salary, violation); //call input method
//calculations:
fines = calculateFines(violation, fines, salary, mph); //call calculateFines method
//Output:
disp(officer, driver, salary, violation,fines); //call disp method
//ask user if they would like to continue
System.out.println("Would you like to write a new ticket? Press 1 for yes or 0 for no: ");
answer = get.nextInt();
}//end while
System.out.println("Goodbye!");
}//end main
//=================================================================
public static void input(String officer, String driver, double salary, int violation) //Input Method
{
System.out.println("Officer, please enter your first and last name: ");
officer = get.nextLine();
System.out.println("Please the driver's first and last name: ");
driver = get.nextLine();
System.out.println("Please enter the driver's salary: ");
salary = get.nextDouble();
System.out.println("Press 1 if the driver ran a red light. Press 2 if the driver was speeding. Press 3 if the driver is subject to both violations. ");
violation = get.nextInt();
System.out.println("How many miles per hour was the driver going over the speed limit?: ");
mph = get.nextDouble();
}//end input
//=================================================================
public static double calculateFines(int violation, double fines, double salary, double mph) //calculates the amount of fines for either violation
{
{
if (violation == 1)
return (salary * 1.75);
else if (violation == 2)
return (salary * 1.2) + (mph * .5 * salary);
else if (violation == 3)
return (salary * 1.75) + ((salary * 1.2) + (mph * .5 * salary));
return 0;
}
}//end calculateFines
//=================================================================
public static void disp(String officer, String driver, double salary, int violation, double fines) //Display Method
{
System.out.println ("Your name is Officer " + officer + "\nThe name of the driver you pulled over is: " + driver);
System.out.println ( String.format( "The driver's annual salary is $%.2f", + salary) );
{
if (violation == 1)
System.out.println("\nThe type of traffic violation the driver received: Red Light");
else if (violation == 2)
System.out.println("\nThe type of traffic violation the driver received: Speeding");
else if (violation == 3)
System.out.println("\nThe type of traffic violation the driver received: Red Light & Speeding");
}
System.out.println ( String.format( "The amount of fines that will be assessed is $%.2f", + fines) );
}//end disp
}//end class FinFines_
Here is my issue:
When I input values for my program, the program acts as if I didn't input any names or numbers. All it says is
"Your name is Officer
The name of the driver you pulled over is:
The driver's annual salary is $0.00
The amount of fines that will be assessed is $0.00"
What can I do to make sure the program holds my inputted values?
When I press 1, to continue, it skips the first line when it asks for the officer's name. It doesn't allow me to input a name for the variable.
What can I do to make sure my program doesn't skip this line?
Is there an issue with my "return 0;" statement? If I don't include that statement then my program can't run, but I worry that me including it is making my program give 0 to all values.
I noticed that in the calculateFines() method, you have multiplied your values by 1.75. This will give 175% of the original. To find 1.75%, you need to multiply by 0.0175. This is also applicable for all other values.
Hope this helps
Though not a good practice to make everything static. Yet to just make your code work change all your methods as following -
public static void input(String officer, String driver, double salary, int violation) //Input Method {
to
public static void input() {
public static double calculateFines(int violation, double fines, double salary, double mph) //calculates the amount of fines for either violation {
to
public static double calculateFines() {
public static void disp(String officer, String driver, double salary, int violation, double fines) //Display Method {
to
public static void disp() {
This would ensure all your static fields are updated as you call these methods using the same parameters within thei definition and now your main() would include
while (answer != 0) {
input();
fines = calculateFines(); //call calculateFines method
disp();
System.out.println("Would you like to write a new ticket? Press 1 for yes or 0 for no: ");
answer = get.nextInt();
}
Note - I've cut short the text and removed comments, please do keep them in your code.
You need to read about the behavior of nextLine(), nextInt(), and nextDouble() regarding new line characters. If any of these leaves a newline character in the input buffer, then the following one will see that character and not consume any further input. This is what you are seeing.
To fix the issue, you need to read the newline character from the Scanner and ignore it. You should look at the Javadocs for Scanner to find an appropriate method to do this.
This is the same issue as #1.
Most likely there is no problem with this return as long as the if statements account for all possible values of violation. In fact, this is a very good solution because it will immediately let you see when violation has an invalid value. You should also learn about enums which allow you to define a finite number of choices for a variable.

How to to use if and else statements properly?

I am having trouble using my if and else if statements for my personal project.
My program seems to work fine in the first lines when asking the user for input, but there is a problem in the code. My compiler is asking me to use a switch method.
I've also encountered a problem where the compiler tells me I can't convert String to double, which is something I have already found using the search.
I know this might be a lot to ask but I would really appreciate your help.
/**
* This application executes number of gallons purchased, car wash if the
* customer desires.
* There will be four options, Regular, Premium, Super,
* or None. A car wash is $1.25 if purchased with $10.00 or more. If it is
* anything equal or below $9.99 then the car wash fee is $3.00.
* Regular per gallon is $2.89
* Premium per gallon is $3.09
* Super per gallon is $3.39
*
*
* #author Christian Guerra
*/
package finalflight;
//The line below is preparing the system to ask the user for inputs
import java.util.Scanner;
public class ExxonCarServices {
public static void main(String[] args) {
String gasType;
String carWash;
String gasPrice;
String numGallons;
double gasRegular = 2.89;
double gasPremium = 3.09;
double gasSuper = 3.39;
double gasNone = 0;
Scanner keyboard = new Scanner(System.in);
System.out.print("Hello which type of gas would you like today? "
+ "Please make the selection Regular, Premium, Super, or None" + " ");
gasType = keyboard.nextLine();
System.out.print("How many gallons would you like?" + " ");
numGallons = keyboard.nextLine();
System.out.print("Would you like to add a professional car wash cleaning today?"
+ " " + "Please select Yes or No" + " ");
carWash = keyboard.nextLine();
if (gasType.equals("Regular")) {
gasRegular = Regular;
} else if (gasType.equals ("Premium")) {
gasPremium = Premium;
} else if (gasType.equals("Super")) {
gasSuper = Super;
} else {
gasNone = 0;
}
if (numGallons * gasPrice <10) {
carWash = 3;
} else {
carWash = 1.25;
}
}
}
The compiler is telling you that this code is correct:
if (gasType.equals("Regular")) {
gasRegular = Regular;
} ...
but starting with Java 7.0 can also be written using a switch statement:
switch (gasType) {
case "Regular":
gasRegular = Regular;
break;
case "Premium":
gasPremium = Premium;
break;
....
}
The error you're getting about "can't convert String to double" is probably due to a missing Double.parseDouble(someString) when assigning a String to a variable of the type Double.
I doubt the compiler is telling you to use a switch-case, it's probably just suggesting it (for more than a few items, a switch-case is almost always the better option for readability's sake).
To convert a String to a Double, simply use Double.parseDouble().
In your case, that would look something like this:
double numGallonsDouble = Double.parseInt(numGallons);

Is this code wrong to call the same constructor more than once [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
The object that is created by the constructor in the following code can be called multiple times. Have I written the code badly by doing so I hope it is written to the proper conventions
import java.util.Scanner;
/**
* this class uses the scanner to take input from the keyboard
* test if the input is a variable of type double if it is store in a
* variable called numOne then take next input test again for type double
* store in variable numTwo, then add the two variables together in the form
* of variable answer and return to the screen. The user then is asked for
* continuation as Y/N then appropriate action is taken
*/
public class Calc {
/**
* this constructer does not throw InputMismatchException
*/
public Calc() {
Scanner in = new Scanner(System.in); // create scanner object
System.out.println("please enter 1st "
+ "number then press enter"
+ "\nthen enter 2nd number then "
+ "press enter again");
/*test for a double returns true */
boolean myBool = in.hasNextDouble();
/*the actual test for a double*/
if(myBool == false) {
System.out.println("wrong format");
/**
* call constructer and instantiate as new object
* by creating a new object i hope to have overwriten
* the original object stored in memory there by getting
* around the problem of hasNextDouble not allowing the
* scanner to advance
*/
Calc c = new Calc();
} else { // 1st else
double numOne = in.nextDouble(); // keyboard input
/*test for a double returns true */
boolean myBool2 = in.hasNextDouble();
/*the actual test for a double*/
if(myBool2 == false) {
System.out.println("wrong format start again");
/**
* call constructer and instantiate as new object
* there by removing need for InputMismatchException
*/
Calc c = new Calc();
} else { // 2nd else
double numTwo = in.nextDouble(); // keyboard input
double answer = numOne + numTwo; // the maths
for(int i = 0; i<35; i++) { // print pattern
System.out.print("*");
}
System.out.println("\n**** "
+ "Your 1st number is "
+ numOne + " ****");
System.out.println("**** " // print operator
+ " " + "+"
+ " ****");
System.out.println("**** "
+ "Your 2nd number is "
+ numTwo + " ****");
System.out.println("**** " // print line
+ " ______ ****");
System.out.println("**** "
+ "The answer is "
+ answer + " ****");
for(int i = 0; i<35; i++) { //print pattern
System.out.print("*");
}
System.out.println("\nDo you have "
+ "more calculations Y or N");
String reply = in.next(); //keyboard input
if(reply.equalsIgnoreCase("Y")) {
Calc c = new Calc();
} else {
System.out.println("OK bye then");
in.close(); //close scanner
} //end else
} // end 1st else
} // end 2nd else
} // end constructor
public static void main(String[] args) {
Calc c = new Calc(); // call constructor
} // end main
} // end class
// just in case i do something wrong thanks in anticipation Simon.
Calling the constructor from within the same constructor, and without any parameters, looks like a sure way to get infinite loop and stack overflow. So, yes, it's wrong.
And since it doesn't look as if you are doing anything with all the instances of Calc that you create within the constructor, it's pointless.
It is usually seen as bad practice to do any work in a constructor that is not directly related to instantiating that object. Consider moving your logic into another method on the Calc. That way you can use a single instance of Calc, and using fields on that instance you might have better luck overwriting the values your comments mention that you want to overwrite.
As Eran mentioned, your current pattern uses recursive calls, which are prone to stack overflows if not done right. I wouldn't go so far as to say recursive calls are always a bad thing, but you should carefully consider whether that's the best pattern for what you're doing.
Although you (probably) won't get an error, this is bad practice in many ways.
First of all, you should not do any of this in a constructor. A constructor is supposed to create an object or throw an Exception, that's it. If the constructor requires any input, read it in advance and pass it as an argument to the constructor. Constructors are not made for user interaction.
The next point is error handling. If the user makes an invalid input, there is absolutely no need to use recursion. Sure, you won't get a StackOverflowError (unless your user fails a few thousand times), but a loop is a better solution.
System.out.print("Enter a double please: ");
while(!scanner.hasNextDouble()) {
System.out.print("Invalid input. Please try again: ");
scanner.next(); //discard invalid input
}
double input = scanner.nextDouble();

User input value code to validate

In my CIS 220 Java Application 2 class, we have just went over the use of objects and classes with constructors and such. The whole goal of this assignment is to utilize a class and its methods to get the user employee data. I feel I've more or less finished the whole thing, but there is one last part. The data must be validated, as explained in this screenshot link http://puu.sh/7vzeI.jpg
I would assume employee ID needs to be 9 characters long as a string. The pay rate and hours worked must be doubles. Sadly I have no idea what sort of code I need in order to validate it in the class. I figured it was a while loop but it didn't seem to work.
Here is the main java method:
public class Project3 {
static Scanner console = new Scanner (System.in);
public static void main(String[] args) {
Employee worker1 = new Employee();
worker1.getEmployeeName();
worker1.getEmployeeNum();
worker1.getPayRate();
worker1.getEmployeeHours();
System.out.println("Employee Name: " + worker1.printEmployeeName());
System.out.println("Employee ID: " + worker1.printEmployeeNum());
System.out.println("Pay Rate: $" + worker1.printPayRate());
System.out.println("Hours Worked: " + worker1.printHoursWorked());
worker1.getNetPay();
}
}
These are the methods in a class titled "Employee" which are used by main:
public String getEmployeeName()
{
System.out.println("Please enter the employee's name: ");
employeeName = console.nextLine();
return employeeName;
}
// Method that prompts the user to enter their hours worked, then returns it as the reference
public double getEmployeeHours()
{
System.out.println("Enter the number of hours worked(a numeric value between 0.0 and 100.0): ");
hoursWorked = console.nextDouble();
return hoursWorked;
}
// Method that prompts the user to enter their employee ID, then returns it as the reference
public String getEmployeeNum ()
{
System.out.println("Please enter the employee's ID: ");
employeeNum = console.nextLine();
return employeeNum;
}
// Method that prompts the user to enter their pay rate, then returns it as the reference
public double getPayRate()
{
System.out.println("Please enter the employee's pay rate (a numeric value): ");
payRate = console.nextDouble();
return payRate;
}
Please forgive me if the format of this question is hideous, as I am still quite new to stack overflow.
I think you may not be clear on the concept of return methods. There is no point in making a return method if you are not going to use the returned value at any point. For example
public String getEmployeeName()
{
System.out.println("Please enter the employee's name: ");
employeeName = console.nextLine();
return employeeName;
}
// in the other class
worker1.getEmployeeName();
You never assign the String "worker1.getEmployeeName()" to anything so making this a return statement is useless. What I would do is that I would remove the getter methods aka "worker1.printEmployeeName()" and do this instead:
String name = worker1.printEmployeeName();
System.out.println("Employee Name: " + name);
Then in the method "printEmployeeName()" I would add my checks to see if the input was valid or not. like so:
(I don't think the name could have an invalid input except for maybe if it was null, so I will use the number instead.)
Okay so to validate the information I would do this:
public String getEmployeeNum ()
{
while(true)
{
System.out.println("Please enter the employee's ID: ");
try{
employeeNum = console.nextInt(); //this should be getting an integer not the line
}catch(Exception e){
employeeNum = 0;
}
if(String.valueOf(employeeNum).length() == 9)
return String.valueOf(employeeNum); // this will break you out of your loop and return the value
else{
System.out.println("Invalid input...");
continue;
}
}
}
Now I'll try an explain the code above
Because we are using "console.nextInt();" in order to check if what the user enters is not a string, a double or anything else that is not a integer I used a try catch statement. If you are unfamiliar with how the try catch works, it basically checks if there is an error with the code in the " try{ } " part and if there is do what is in the "catch(Exception e) { } " block. I could go in to more detail on it but that explains it enough for now. So if there is a problem with the input or we find an "error" with it we set the employeeNum to "0" (You'll see why in the next part).
Then we move on to the if statement "if(String.valueOf(employeeNum).length() == 9)" basically this will turn our input into a String so we can check the length (Remember if there is an error with the input the employeeNum will be "0" with the length of "1")
If the input is valid (9 numbers long) we then return it "return employeeNum;"
If it is not valid (Not long enough or too long) we tell the user that there was an error, and we restart the process.
I hope this helps some or at least gives you an idea on what to do next!
If you want to read and learn some more:
Try Catch : http://docs.oracle.com/javase/tutorial/essential/exceptions/try.html
Edit: (A Hint)
When you check if the values are doubles for the pay rate and hours use something like this:
try {
hoursWorked = console.nextDouble();
}catch(Exception e){
hoursWorked = -1.0;
}
if(hoursWorked > 0.0)
return hoursWorked;
// the end should be the same as the other example :)

Not sure how to create a menu which uses methods from my other classes [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
so far I have created two classes, which I will show below
I have tested both classes and they both seem to work.
I now need to create a menu which will use the two classes I have created and allow users to enter the information and to find information on their account for the following:
Add the customers details
Make a deposit to the business account
Record a meter reading to the business account
Display current balance of the business account
Display full account details
Change the discount value for the business account
Change the cost per unit for all business accounts
How to use the menu system
While I have tried my best to find out how to do it using resources online I am currently lost. Up to this point every time I have tried to create a menu system I just could not get it to work. I would greatly appreciate if someone could help me with the basics on how to go about doing it.
Thanks :)
public class GasAccount
{
private int intAccRefNo;
private String strName;
private String strAddress;
public double dblBalance;
private double dblUnits;
public static double dblUnitsCosts = 0.02;
public GasAccount (int intNewAccRefNo , String strNewName , String strNewAddress)
{
}
public GasAccount (int intNewAccRefNo , String strNewName , String strNewAddress , double dblNewUnits)
{
intAccRefNo = intNewAccRefNo;
strName = strNewName;
strAddress = strNewAddress;
dblUnits = dblNewUnits;
dblBalance = dblUnits * dblUnitsCosts;
}
public int getAccRefNo()
{
return intAccRefNo;
}
public String getName()
{
return strName;
}
public String getAddress()
{
return strAddress;
}
public void deposit(double dblDepositAmount)
{
dblBalance = dblBalance - dblDepositAmount;
}
public double getBalance()
{
return dblBalance;
}
public double getUnitCost()
{
return dblUnitsCosts;
}
public void recordUnits (double dblUnitsUsed)
{
dblBalance = dblBalance + dblUnitsUsed * dblUnitsCosts;
dblUnits = dblUnitsUsed + dblUnits;
}
public double getUnits()
{
return dblUnits;
}
public void updateUnitsCosts(double dblNewUnitsCosts)
{
this.dblUnitsCosts = dblNewUnitsCosts;
}
}
And another which extends it -
public class BusinessAccount extends GasAccount
{
private double dblDiscount;
public BusinessAccount (int intNewAccRefNo, String strNewName, String strNewAddress, double dblNewUnits, double dblNewDiscount)
{
super (intNewAccRefNo , strNewName , strNewAddress, dblNewUnits);
dblDiscount = dblNewDiscount;
}
public void setNewDiscount(double dblNewDiscount)
{
dblDiscount = dblNewDiscount;
}
public double getDiscount()
{
return dblDiscount;
}
#Override
public void recordUnits (double dblUnitsUsed)
{
double dblNewBalance;
dblBalance = dblBalance + dblUnitsUsed * dblUnitsCosts;
dblNewBalance = dblUnitsUsed * dblUnitsCosts * dblDiscount / 100;
dblBalance = dblBalance - dblNewBalance;
}
Here is what my attempted menu looks like up to the fifth option. I am doing something horribly wrong with calling in the methods from the other classes as BuisnessAccount.getMethod always shows up as an error. I am also pretty sure declaring the variables again is completely wrong as then they have no link to my other classes.
If someone could help me solve this it would be greatly appreciated
import java.util.Scanner;
public class Menu
{
public static void main(String [] args)
{
Scanner input = new Scanner(System.in);
int Choice;
{
System.out.println("------------------------------");
System.out.println ( "1. Add the customers details" ) ;
System.out.println ( "2. Make a deposit to the business account" );
System.out.println ( "3. Record a meter reading to the business account" ) ;
System.out.println ( "4. Display current balance of the business account" ) ;
System.out.println ( "5. Display full account details" ) ;
System.out.println ( "6. Change the discount value for the business account" ) ;
System.out.println ( "7. Change the cost per unit for all business accounts ");
System.out.println ( "8. How to use the menu system ");
System.out.println ( "Any other number will exit the program");
System.out.println("------------------------------");
System.out.println ( "\n\nEnter a number from 1 to 8" );
Choice = input.nextInt();
switch (Choice)
{
case 1 :
int intNewAccRefNo;
String strNewName;
String strNewAddress;
Double dblNewUnits;
Double dblNewDiscount;
System.out.println("Please enter the account number?");
intNewAccRefNo = input.nextInt();
System.out.println("Please enter the account name?");
input.nextLine();
strNewName = input.nextLine();
System.out.println("Please enter the account address?");
strNewAddress = input.nextLine();
System.out.println("Please enter the number of initial number of units used?");
dblNewUnits = input.nextDouble();
System.out.println("Please enter the discount?");
dblNewDiscount = input.nextDouble();
case 2:
double dblDeposit;
System.out.println("Please enter the amount you want to deposit?");
dblDeposit = input.nextDouble();
System.out.println ( "The current balance: " + BusinessAccount.getBalance() ) ;
case 3:
double dblUnits;
System.out.println("Enter the number of Units Used");
dblUnits = input.nextDouble();
BusinessAccount.recordUnits(dblUnits);
case 4:
System.out.println("\n Current Balance: £"+ BusinessAccount.getBalance());
case 5:
System.out.println("Account Reference Number: " + BusinessAccount.getAccRefNo());
System.out.println("Address: " + BusinessAccount.getAddress());
System.out.println("Name: " + BusinessAccount.getName());
System.out.println("Balance: " + BusinessAccount.getBalance());
System.out.println("Discount: " + BusinessAccount.getDiscount());
System.out.println("Units: " + BusinessAccount.getUnits());
case 1 :
String strAddress;
System.out.println("Please enter the account address?");
strAddress = input.nextLine();
System.out.println("Address:" + firstAccount.getAddress());
There is no connection between what the user inputs and the method which i am calling, not sure how to fix.
By using BusinessAccount.someMethod(), you're attempting to call said method in a static context, but your methods are not static. To call upon your methods, you either need to make them static or need to create an object which can then call upon them, ie:
BusinessAccount ba= new BusinessAccount (4, "name", "address", 3.4, 43.4);
ba.someMethodFromClass();
In your case, you do NOT want them to be static. Read up more on static methods/variables (see below).
Some documentation that may be helpful:
http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html
http://docs.oracle.com/javase/7/docs/api/javax/swing/JMenu.html
To answer your second question, you need to have a method (in your object's class, not main) which can assign values to your variables. ie:
public void setAddress (String address)
{
strAddress=address;
}
Then, you'd pass the address that you read in from main into your function. By doing that, you're initializing/assigning the value from the user to the value stored in your class, ie:
System.out.println("Please enter the account address?");
String temp = input.nextLine();
ba.setAddress(temp);
System.out.println("Address:" + ba.getAddress());
or better yet,
System.out.println("Please enter the account address?");
ba.setAddress(input.nextLine());
System.out.println("Address:" + ba.getAddress());
Going off of what SteveP said, it would be a good idea to store the instances of the other classes as instance variables in the Menu class.
Also, if you're using a GUI interface, this JMenu class API may come in handy:
http://docs.oracle.com/javase/7/docs/api/javax/swing/JMenu.html
Also look for JMenuBar, JMenuItem

Categories