I've defined a variable withdraw
in class Account
. It performs the withdrawl
function just fine that's defined inside the Account
class. However, I wish to access the value of withdraw
variable inside the interest
function present in the Sav_acct
class. It's taking the value of withdraw
as 0. How can I use the value of withdraw
from the withdrawl
function inside the interest
function so I can perform the right mathematical operation?
import java.util.Scanner;
public class Account {
String customer_name;
int account_number;
String type_account;
int balance = 2500;
double deposit;
public static double withdraw;
void deposit() {
Scanner sc = new Scanner(System.in);
System.out.println("how much do you want to deposit?");
int amo = sc.nextInt();
deposit = balance amo;
System.out.println("Your current balance is " balance);
}
void withdrawl() {
Scanner sc = new Scanner(System.in);
System.out.println("how much do you want to withdraw?");
int amo = sc.nextInt();
withdraw = balance - amo;
if (withdraw < 0) {
System.out.println("Your balance is insufficient");
}
else {
System.out.println("Your current balance is " withdraw);
}
}
}
class Curr_acct extends Account {
@SuppressWarnings("empty-statement")
void penalty() {
double Service_charge = withdraw - 100;
double falling = Service_charge;
System.out.println("You've been charged" " 100" " due to low balance");
System.out.println("Your current amount is " falling);
}
}
class Sav_acct extends Account {
void interest() {
double interest;
interest = (1 % 100) * withdraw;
double total_amount = interest withdraw;
System.out.println("Your new balance with interest is " total_amount);
}
}
class publish {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter your name");
String n = sc.nextLine();
System.out.println("Enter your account type: Savings Account or Current Account");
String t = sc.nextLine();
if ("Savings Account".equals(t)) {
System.out.println("Do you want to deposit or withdraw amount?");
String d = sc.nextLine();
if ("deposit".equals(d)) {
Account de = new Account();
de.deposit();
}
else if ("withdraw".equals(d)) {
Account wi = new Account();
wi.withdrawl();
Sav_acct in = new Sav_acct();
in.interest();
}
}
else if ("Current Account".equals(t)) {
System.out.println("Do you want to deposit or withdraw amount?");
String d = sc.nextLine();
if ("deposit".equals(d)) {
Account de = new Account();
de.deposit();
}
else if ("withdraw".equals(d)) {
Account wi = new Account();
wi.withdrawl();
if (withdraw < 2000) {
Curr_acct pe = new Curr_acct();
pe.penalty();
}
else {
}
}
}
else {
System.out.println("please enter the correct account type as per the options provided on the screen");
}
}
}
CodePudding user response:
Don't use public static
in a variable just to access it . Use getter
setter
to access variables of a class. Thats the right way of doing it.
In your case removingpublic static
will do fine;
In Account
class
public static double withdraw;
to
double withdraw;
You are not maintaining any state while creating object for Account
and Sav_acct
class.You are creating two different objects to do 1 job.
You will never get withdraw
value of Account
in Sav_acct
since both are different objects.
You are not maintaining any state while creating objects for the Account
and Sav_acct
classes. You are creating two different objects here.
You will never get the withdraw
value of Account
in Sav_acct
since both are different objects. Plus you are missing the basics of Inheritence
in OOPS
Since Sav_acct
is extending the Account
class it is inheriting all the properties of Account
, so there is no need of creating an object of the Account
class.
Instead, create an object of Sav_acct
and use this obj to perform your operations;
Account wi = new Account();
wi.withdrawl();
Sav_acct in = new Sav_acct();
in.interest();
to
Sav_acct wi = new Sav_acct();
wi.withdrawl();
wi.interest();
You need to know about how Inheritance works in OOPS. Refer here
The main problem
You are declaring objects multiple times whenever you needed.Eg:-
System.out.println("Enter your account type: Savings Account or Current Account");
String t = sc.nextLine();
if ("Savings Account".equals(t)) {
System.out.println("Do you want to deposit or withdraw amount?");
String d = sc.nextLine();
if ("deposit".equals(d)) {
Account de = new Account(); // creating Account object
de.deposit();
}
else if ("withdraw".equals(d)) {
Account wi = new Account(); // creating Account object again
wi.withdrawl();
Sav_acct in = new Sav_acct();
in.interest();
}
}
In the above why create Account de = new Account();
separately for both deposit
and withdraw
just declare one object after getting the account type name and use it to perform respective jobs.
System.out.println("Enter your account type: Savings Account or Current Account");
String t = sc.nextLine();
if ("Savings Account".equals(t)) {
Sav_acct acc = new Sav_acct(); //creating sav_acct object here and use
System.out.println("Do you want to deposit or withdraw amount?");
String d = sc.nextLine();
if ("deposit".equals(d)) {
// Account de = new Account(); no need
acc.deposit();
}
else if ("withdraw".equals(d)) {
//Account wi = new Account(); no need
acc.withdrawl();
//Sav_acct in = new Sav_acct(); no need again
in.interest();
}
}
Redesign your code inside main method otherwise, most of the thing is fine