#new to classes need help

1 messages · Page 1 of 1 (latest)

dark geodeBOT
#

This post has been reserved for your question.

Hey @fresh laurel! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

fresh laurel
#

hi

#

thats my code and idk why i cant run it

#

i have made a method to take input in the bank class

#

still the code cant run when i try to run it

#
import java.util.*;

class methods {
    String name;
    int accnumber;
    Scanner sc = new Scanner(System.in);

    public void takein(){
        System.out.println("please enter your name: ");
        name = sc.nextLine();
        System.out.println("please enter your account number: ");
        accnumber = sc.nextInt();
    }

    public void displayname() {
        System.out.println("your name is " + name);
    }

    public void displayaccnumber() {
        System.out.println("your account number is: " + accnumber);
    }

    public void displayall() {
        System.out.println("your name is" + name);
        System.out.println("your account number is " + accnumber);
    }

    public class bank{
        public static void main(String args[]){
            methods customer1 = new methods();
            customer1.takein();
        }
    }
}
serene lance
#

which line is the error on

fresh laurel
#

no error at all

#

it just doesnt urn

#

run

gentle path
#

Usually a program starts from a method called main

#

you currently have chosen current file to start execution

#

it can't run it since there is no main method inside the file you showed

fresh laurel
#

class methods also wants main ?

gentle path
# fresh laurel there is

oh shit I missed it above. My bad.
and when you are in the file where this is you should be able to run it

fresh laurel
gentle path
#

usually you'll have only 1 class in 1 file so create a new file and move it there

#

(just a tip. the classes names are accepted to be in pascal case)

dark geodeBOT
#

💤 Post marked as dormant

This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
In case your post is not getting any attention, you can try to use /help ping.
Warning: abusing this will result in moderative actions taken against you.

fresh laurel
#

thank you

dark geodeBOT
# fresh laurel thank you

If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.

fresh laurel
#

and apologies my mom called me and there was quite some work that she gave me which is why i've been out all day

fresh laurel
errant urchin
# fresh laurel oh if thats the case how does inheritance work

Independently.
Typically a class will inherit from another, and both classes will be in different files.
The class that inherits from the other is said to extend the other, and it will be declared class Stuff extends Thing { instead of just class Stuff {

dark geodeBOT
#

💤 Post marked as dormant

This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
In case your post is not getting any attention, you can try to use /help ping.
Warning: abusing this will result in moderative actions taken against you.

fresh laurel
errant urchin
fresh laurel
#

i see

#

okay thank you

dark geodeBOT
# fresh laurel okay thank you

If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.

fresh laurel
#

i am tying to make a bank model i'll keep this open

dark geodeBOT
#

💤 Post marked as dormant

This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
In case your post is not getting any attention, you can try to use /help ping.
Warning: abusing this will result in moderative actions taken against you.

fresh laurel
#
import java.util.Random;
import java.util.Scanner;

interface BankInterface {
    void deposit();

    void withdraw();
}

class Methods implements BankInterface {
    String name;
    int accnumber;
    Scanner sc = new Scanner(System.in);
    Random rand = new Random();
    int bb = rand.nextInt(1, 1000000);
    int math;
    int w;

    public void takein() {
        System.out.print("Please enter your name: ");
        name = sc.nextLine();
        System.out.print("Please enter your account number: ");
        accnumber = sc.nextInt();
    }

    public void displayname() {
        System.out.println("Your name is " + name);
    }

    public void displayaccnumber() {
        System.out.println("Your account number is: " + accnumber);
    }

    public void displayall() {
        System.out.println("Your name is " + name);
        System.out.println("Your account number is " + accnumber);
    }

    public void bankbalance() {
        System.out.println("Your bank balance is: ₹" + bb);
    }

    @Override
    public void deposit() {
        int x;
        System.out.print("Enter the amount of money would you like to deposit:");
        x = sc.nextInt();
        math = x + bb;
        System.out.println("Your new bank balance is: " + math);
    }

    @Override
    public void withdraw() {
        int x;
        System.out.print("Enter the amount of money would you like to withdraw: ");
        x = sc.nextInt();
        math = bb - x;
        System.out.println("Your new bank balance is: " + math);
    }
}
#

thats my code for my methods class

#

and i have bank class which extends this class^

#

bank class calls methods that i have implemented in this class

#

the problem here is

#

when a customer enters their name and bank number my code asks them do they wanna view their balance , add money to it or withdraw money

#

the initial balance is randomised

#

and its called "bb" in my code

#

now lets say customer has 20k amount already and adds another 20k by deposit function

#

and goes ahead and deletes 10k out of it

#

the program is written that it should remove 10k out of the initial balance which was 20k

#

so it makes it 10k

#

but logically - a correctly written program should make it 20k + 20k -10k = 30k

#

so i am here to gather some ideas about how can i make it such that it knows if customer deposited some money in bank then bb should change and according to the new balance it should gimme the next calculation

#

also i'd like to add another thing which is a customer shouldnt be able to withdraw more than he already has in the bank

#

so the figure shouldnt go less than 0 and then print an error message which says insufficient balance

#

any ideas ?

fresh laurel
#

i had to remove math from there and yeah update bb itself

fresh laurel
errant urchin
#

First compute what the resulting balance should be in theory

#

Then check whether that balance is lower than zero

#

If it is, then withdrawal is impossible

fresh laurel
#

good thanks imma do that real quick

dark geodeBOT
# fresh laurel good thanks imma do that real quick

If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.

errant urchin
#

If it is zero or more, the operation is valid and you can update the real balance with the candidate value

fresh laurel
#

yep

#

so i use if else

#

1 min brb

errant urchin
#

Note that that was an engineering response.
A layman person would estimate that they can't withdraw more than they have, so they'd check if the requested withdrawal amount is greater than their balance

fresh laurel
#

no thats true but my teacher could try to mess with me by withdrawing more than i have in bank while i show him my code and if he does that i want to have an answer

#

so im doing it beforehand

#
import java.util.Random;
import java.util.Scanner;

interface BankInterface {
    void deposit();

    void withdraw();
}

class Methods implements BankInterface {
    String name;
    int accnumber;
    Scanner sc = new Scanner(System.in);
    Random rand = new Random();
    int bb = rand.nextInt(1, 1000000);
    int math;
    int w;

    public void takein() {
        System.out.print("Please enter your name: ");
        name = sc.nextLine();
        System.out.print("Please enter your account number: ");
        accnumber = sc.nextInt();
    }

    public void displayname() {
        System.out.println("Your name is " + name);
    }

    public void displayaccnumber() {
        System.out.println("Your account number is: " + accnumber);
    }

    public void displayall() {
        System.out.println("Your name is " + name);
        System.out.println("Your account number is " + accnumber);
    }

    public void bankbalance() {
        System.out.println("Your bank balance is: ₹" + bb);
    }

    @Override
    public void deposit() {
        int x;
        System.out.print("Enter the amount of money would you like to deposit:");
        x = sc.nextInt();
        bb = x + bb;
        System.out.println("Your new bank balance is: " + bb);
    }

    @Override
    public void withdraw() {
        int x;
        System.out.print("Enter the amount of money would you like to withdraw: ");
        x = sc.nextInt();
        bb = bb - x;
        if (bb < 0) {
            System.out.println("YOU CANT WITHDRAW MORE THAN YOU HAVE");
            System.exit(0);
        } else if (bb >= 0) {
            System.out.println("Your new bank balance is: " + bb);
        }
    }
}
#

yep i've done it

fresh laurel
#

now gotta make a switchcase in the mainclass to run these methods

dark geodeBOT
#

💤 Post marked as dormant

This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
In case your post is not getting any attention, you can try to use /help ping.
Warning: abusing this will result in moderative actions taken against you.

fresh laurel
#

up

#

hi

#
import java.util.Random;
import java.util.Scanner;

interface BankInterface {
    void deposit();

    void withdraw();
}

class Methods implements BankInterface {
    String name;
    int accnumber;
    Scanner sc = new Scanner(System.in);
    Random rand = new Random();
    int bb = rand.nextInt(1, 1000000);

    public void takein() {
        System.out.print("Please enter your name: ");
        name = sc.nextLine();
        System.out.print("Please enter your account number: ");
        accnumber = sc.nextInt();
    }

    public void bankbalance() {
        System.out.println("Your bank balance is: ₹" + bb);
    }

    @Override
    public void deposit() {
        int x;
        System.out.print("Enter the amount of money would you like to deposit:");
        x = sc.nextInt();
        bb = x + bb;
        System.out.println("Your new bank balance is: ₹" + bb);
    }

    @Override
    public void withdraw() {
        int x;
        System.out.print("Enter the amount of money would you like to withdraw: ");
        x = sc.nextInt();
        bb = bb - x;
        if (bb < 0) {
            System.out.println("YOU CANT WITHDRAW MORE THAN YOU HAVE");
            System.exit(0);
        } else if (bb >= 0) {
            System.out.println("Your new bank balance is: ₹" + bb);
        }
    }

    public void displayall() {
        System.out.println("Your name is " + name);
        System.out.println("Your account number is " + accnumber);
        System.out.println("your bank balance is: " + bb);
    }
}

#
import java.util.Scanner;
public class bank extends Methods {
    public static void main(String[] args) {
        Methods customer1 = new Methods();
        customer1.takein();

        Scanner sc = new Scanner(System.in);
        int choice;

        do {
            System.out.println("1. Display Bank Balance");
            System.out.println("2. Deposit Money");
            System.out.println("3. Withdraw Money");
            System.out.println("4. Display All Information");
            System.out.println("0. Exit");
            System.out.print("Enter your choice: ");
            choice = sc.nextInt();

            switch (choice) {
                case 1:
                    customer1.bankbalance();
                    break;
                case 2:
                    customer1.deposit();
                    break;
                case 3:
                    customer1.withdraw();
                    break;
                case 4:
                    customer1.displayall();
                    break;
                case 0:
                    System.out.println("Exiting...");
                    break;
                default:
                    System.out.println("Invalid choice. Please try again.");
                    break;
            }

        } while (choice != 0);
    }
}
#

i went ahead and made some changes in my code

#

`Please enter your name: Aarpan
Please enter your account number: 108289

  1. Display Bank Balance
  2. Deposit Money
  3. Withdraw Money
  4. Display All Information
  5. Exit
    Enter your choice: 1
    Your bank balance is: ₹383696
  6. Display Bank Balance
  7. Deposit Money
  8. Withdraw Money
  9. Display All Information
  10. Exit
    Enter your choice: 2
    Enter the amount of money would you like to deposit:4
    Your new bank balance is: ₹383700
  11. Display Bank Balance
  12. Deposit Money
  13. Withdraw Money
  14. Display All Information
  15. Exit
    Enter your choice: 3
    Enter the amount of money would you like to withdraw: 700
    Your new bank balance is: ₹383000
  16. Display Bank Balance
  17. Deposit Money
  18. Withdraw Money
  19. Display All Information
  20. Exit
    Enter your choice: 4
    Your name is Aarpan
    Your account number is 108289
    your bank balance is: 383000
  21. Display Bank Balance
  22. Deposit Money
  23. Withdraw Money
  24. Display All Information
  25. Exit
    Enter your choice: 0
    Exiting...
    `
#

thats the output it gives

#

im pretty satisfied with it

#

but

#

i want to underline or somehow highlight the printed output

errant urchin
#

We all want things, but before Windows 11, Windows has not been playing nice, and that make things unreliable

fresh laurel
#

ah thats sad

#

is there any other way you can recommend ? to increase the visibilty of the output

#

because the text really kinda eats it up

errant urchin
#

Lookup terminal escape codes and try something like

#
System.out.println("\u001b[31mHelloWorld");
#

Otherwise you could add newlines and draw boxes with # characters

fresh laurel
#

right

fresh laurel
errant urchin
#

So it works. Lookup all other possible codes

fresh laurel
#

but it happens only when

#

for eg

#
import java.util.Scanner;
public class bank extends Methods {
    public static void main(String[] args) {
        Methods customer1 = new Methods();
        customer1.takein();

        Scanner sc = new Scanner(System.in);
        int choice;

        do {
            System.out.println("1. Display Bank Balance");
            System.out.println("2. Deposit Money");
            System.out.println("3. Withdraw Money");
            System.out.println("4. Display All Information");
            System.out.println("0. Exit");
            System.out.print("Enter your choice: ");
            choice = sc.nextInt();

            switch (choice) {
                case 1:
                    customer1.bankbalance();
                    break;
                case 2:
                    customer1.deposit();
                    break;
                case 3:
                    customer1.withdraw();
                    break;
                case 4:
                    customer1.displayall();
                    break;
                case 0:
                    System.out.println("Exiting...");
                    break;
                default:
                    System.out.println("\u001b[31mHelloWorld");
;
                    break;
            }

        } while (choice != 0);
    }
}
#

look at the last lines

#

System.out.println("Invalid choice. Please try again.");

#

i replaced these lines with the thing u gave me

#

and when i type 20 inside the switch it then makes it red

#

otherwise its white

errant urchin
#

Yes, my friend, using this thing requires a complicated organization. If you want to be a programmer, start making do with that

fresh laurel
#

right

silk bluff
#

Verification

fresh laurel
#

@errant urchin

#
import java.util.Random;
import java.util.Scanner;

interface BankInterface {
    void deposit();

    void withdraw();
}

class Methods implements BankInterface {
    String name;
    int accnumber;
    Scanner sc = new Scanner(System.in);
    Random rand = new Random();
    int bb = rand.nextInt(1, 1000000);

    public void takein() {
        System.out.print("Please enter your name: ");
        name = sc.nextLine();
        System.out.print("Please enter your account number: ");
        accnumber = sc.nextInt();
    }

    public void bankbalance() {
        System.out.println("Your bank balance is: ₹" + bb);
    }

    @Override
    public void deposit() {
        int x;
        System.out.print("Enter the amount of money would you like to deposit:");
        x = sc.nextInt();
        bb = x + bb;
        System.out.println("Your new bank balance is: ₹" + bb);
    }

    @Override
    public void withdraw() {
        int x;
        System.out.print("Enter the amount of money would you like to withdraw: ");
        x = sc.nextInt();
        if (bb < 0) {
            System.out.println("INSUFFICIENT FUNDS. CANNOT WITHDRAW MORE THAN YOUR BALANCE.");
        } else  {
            bb = bb - x;
            System.out.println("Your new bank balance is: ₹" + bb);
        }
    }

    public void displayall() {
        System.out.println("Your name is " + name);
        System.out.println("Your account number is " + accnumber);
        System.out.println("your bank balance is: " + bb);
    }
}

#

i need to use abstract class here

#

can you help me think how

errant urchin
#

Errm. I struggle to imagine a smart reason to use abstract classes. Why do you need to?

silk bluff
fresh laurel
#

actually this is my college projec

#

and i have to use some topics

#

class
interface
switch before main class
methods
abstract class
access modifiers :
constructors this keyword
scanner
array
static blocks
variables (static instance methods)
object
packages

STRING (wrapper cladd loader)

data types (char double float)

comments

use of loop
method overloading n overriding
inheritance
super()
keywords ....polymorphism

#

these

errant urchin
#

I'm afraid your project might be way too simple to go into much subjects

fresh laurel
#

ohhh

#

can i not make this more complex by adding more features ?

errant urchin
#

I'm not super inspired. Maybe with some different kinds of interest plans that have completely different formulas

fresh laurel
#

that can be done yes

#

interest rates

#

also - if not this project

#

you got someth in mind ? which i can work on

#

big enough for all this