can someone help me add a another class in this code
i got this code in this site, but i need to have at least 2 class
sw8revenge 0 Newbie Poster
import java.util.Scanner;//name of the package(library)that includes the * class
public class ATM{//name of the program
public static Scanner kbd;//Scanner kbd function
public static void main(String[] args) {// the main program with the word void doesn't let anything out
String userAnswer = null;//declaring the string
Scanner kbd = new Scanner(System.in); // declaring the function
double accountbalance = 0, depositamount = 0, withdrawalbalance= 0;//declaring the variables, accountbalance, depositamount, withdrawalbalance, as double
String acctNum,pwd;//declaring acctNum, pwd as string
String acctres;//declaring acctres as string
int counter=0;//declaring counter as int
int Choice;//declaring Choice as int
do//gives the do-while loop the condition to repeat the statement
{
System.out.println("Please enter your account number: ");//gives the system command to print
acctNum = kbd.nextLine();//getting the inputs and allocating
System.out.println("Please enter your account password: ");//gives the system command to print
pwd = kbd.nextLine();//getting the inputs and allocating
acctres=checkID(acctNum,pwd);//Initializing acctres
if(!acctres.equals("error"))// checking for the situation of the acctres variable for error
{
accountbalance = Double.parseDouble(acctres);//Initializing accountbalance
do//gives the do-while loop the condition to repeat the statement
{
Choice = Menu();//Initializing Menu()
System.out.println(Choice);//gives the system command to print
if (Choice == 1)//checking for input 1
{
System.out.println("Your Balance is: " + accountbalance + "$");//gives the system command to print
}
else if (Choice == 2)//checking for input 2
{
System.out.println("How much would you like to deposit to your account? ");//gives the system command to print
depositamount=kbd.nextDouble();//getting the inputs and allocating
double calculatebalance = Deposit(accountbalance, depositamount);//Initializing calculatebalance
System.out.println("Your Balance with your Deposit now is: " + calculatebalance + "$");//gives the system command to print
}
else if (Choice == 3)//checking for input 3
{
System.out.println("How much would you like to withdrawal from your account? ");//gives the system command to print
withdrawalbalance=kbd.nextDouble();//getting the inputs and allocating
double calculatebalance1 = Withdraw(accountbalance, withdrawalbalance);//Initializing calculatebalance1
System.out.println("You Balance now is: " + calculatebalance1 + "$");//gives the system command to print
}
else// checking for the other inputs except the ones which were mentioned
{
System.out.println("Do you want to Log Out? (Y or N) ");//gives the system command to print
userAnswer=kbd.next();//getting the inputs and allocating
userAnswer=userAnswer.toUpperCase();//to ignore the upper case or lower case input and count them as the same
if (userAnswer.equals("Y"))//checking for the situation of userAnswer for being equal Y which means yes
{
System.out.println("You are now logged out!");//gives the system command to print
System.exit(0);// gives command for the if statement
}
}
}while ((Choice <=4)|| (userAnswer.equals("N")));//Checking input N
}
else// checking for other input except the ones that already were mentioned
{
System.out.println("Error");//gives the system command to print
counter++;//Increasing the counter by 1
System.out.println(counter);//gives the system command to print
}
}while(counter<3);//checking for the counter to be less than 3 or not
System.out.println("You have exceeded the required amount of times.");//gives the system command to print
}
public checkID(String acctNum, String pwd)//separating the part from the main program as a new function in the main function
{
String result = "error";//declaring the result as string
// Strings a, b, and c contain the valid account numbers and passwords.
// For each string, the account number is listed first, followed by
// a space, followed by the password for the account, followed by a space,
// followed by the current balance.
String a = "44567-5 mypassword 520.36";//declaring string a which id equal 44567-5 mypassword 520.36 in this function
String b = "12345-6 anotherpassword 48.20";//declaring string b which id equal 12345-6 anotherpassword 48.20 in this function
String c = "54321-0 betterpassword 96.74";//declaring string c which id equal 54321-0 betterpassword 96.74 in this function
String d=a.substring(0,7);//declaring string d in this function
String e=a.substring(8,18);//declaring string e in this function
String eres=a.substring(19);//declaring string eres in this function
String f=b.substring(0,7);//declaring string f in this function
String g=b.substring(8,23);//declaring string g in this function
String gres=b.substring(24);//declaring string gres in this function
String h=c.substring(0,7);//declaring string h in this function
String i=c.substring(8,22);//declaring string i in this function
String ires=c.substring(23);//declaring string ires in this function
if(acctNum.equals(d) && pwd.equals(e))//checking for account number and password
{
result=eres;//gives commands for the if statement
}
else if(acctNum.equals(f) && pwd.equals(g))//checking for account number and password
{
result=gres;//gives commands for the if statement
}
else if(acctNum.equals(h) && pwd.equals(i))//checking for account number and password
{
result=ires;//gives commands for the if statement
}
return result;//to get data out of the function and takes the value with it only once
}
public static double Displaybalance1(double accountbalance)//separating the part from the main program as a new function in the main function
{
return accountbalance;//to get data out of the function and takes the value with it only once
}
public static double Deposit(double accountbalance, double depositamount)//separating the part from the main program as a new function in the main function
{
return accountbalance+ depositamount;//to get data out of the function and takes the value with it only once
}
public static double Withdraw(double accountbalance, double withdrawalbalance)//separating the part from the main program as a new function in the main function
{
double newbalance1 = 0;//declaring the variables, newbalance1, as double
if (withdrawalbalance<=accountbalance)//checking the condition in the parentheses(if withdrawalamount is less than or equal the accontbalance or not)
{
newbalance1 = accountbalance - withdrawalbalance;//gives the system command to print the calculated amount
}
else if (withdrawalbalance>accountbalance)//checking the condition in the parentheses(if withdrawalamount is more than the accontbalance or not)
{
newbalance1 = accountbalance;//declaring the newbalance1 as previous accountbalance without change
System.out.println("error, that is impossible! your account balance still is: " + newbalance1);//gives the system command to print the accountbalance without changing it
}
return newbalance1;//to get data out of the function and takes the value with it only once
}
public static int Menu()//separating the part from the main program as a new function in the main function
{
int Option ;//declaring the variables, Size, as int
do//gives the do-while loop the condition to repeat the statement
{
Scanner kbd1 = new Scanner(System.in);// declaring the function
System.out.println("1. Display Balance ");//gives the system command to print
System.out.println("2. Deposit ");//gives the system command to print
System.out.println("3. Withdraw ");//gives the system command to print
System.out.println("4. Log Out ");//gives the system command to print
System.out.println("Enter your option");//gives the system command to print
Option = kbd1.nextInt();//getting the inputs and allocating
if ((Option<=0)&&(Option > 4))//checking for the statement in parentheses(Smaller or equal 0 or bigger than 4 inputs)
{
System.out.println("Error! Choose again: ");//gives the system command to print
}
}while ((Option<=0)&&(Option > 4));//gives the while loop the condition to stop the repeating statement
return Option;//to get data out of the function and takes the value with it only once
}
}
NormR1 563 Posting Sage Team Colleague
Do you know how to define a class? You must if you have written any java programs.
Can you explain specifically what problem you are having adding a class?
What kind of class do you want? Inner or external? public or ?
sw8revenge 0 Newbie Poster
I am really new to java
about defining a class i know a little...
here's the partial code i need to put into a class
public static String checkID(String acctNum, String pwd)//separating the part from the main program as a new function in the main function
{
String result = "error";//declaring the result as string
// Strings a, b, and c contain the valid account numbers and passwords.
// For each string, the account number is listed first, followed by
// a space, followed by the password for the account, followed by a space,
// followed by the current balance.
String a = "44567-5 mypassword 520.36";//declaring string a which id equal 44567-5 mypassword 520.36 in this function
String b = "12345-6 anotherpassword 48.20";//declaring string b which id equal 12345-6 anotherpassword 48.20 in this function
String c = "54321-0 betterpassword 96.74";//declaring string c which id equal 54321-0 betterpassword 96.74 in this function
String d=a.substring(0,7);//declaring string d in this function
String e=a.substring(8,18);//declaring string e in this function
String eres=a.substring(19);//declaring string eres in this function
String f=b.substring(0,7);//declaring string f in this function
String g=b.substring(8,23);//declaring string g in this function
String gres=b.substring(24);//declaring string gres in this function
String h=c.substring(0,7);//declaring string h in this function
String i=c.substring(8,22);//declaring string i in this function
String ires=c.substring(23);//declaring string ires in this function
if(acctNum.equals(d) && pwd.equals(e))//checking for account number and password
{
result=eres;//gives commands for the if statement
}
a account class
NormR1 563 Posting Sage Team Colleague
What is the problem with putting it in a class? The basics:
class AClass {
// variables and methods here
} // end of class
Your single letter variable names will make it hard to understand what you code is doing if the code ever gets more than 30 lines long. Variables names should be selfdocumenting words that you can read and understand what the variable is used for.
Most of your comments are redundant. They say what the code does but not why. For example:
int ix = 10; // set the int ix to 10
vs
int iy = 20; // set iy to the max search length
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.