Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Help with a Java program
New Posts  All Forums:Forum Nav:

Help with a Java program

post #1 of 16
Thread Starter 
I am trying to make a bank account program using Java but I am new and stuck. It has to use two classes. So far I have this. It will allow me to input my starting balance and my name. Then it will let me choose either deposit or withdraw but then it skips right to the end rather than go to the if statement. What can I do so that it will go to the loop and define x and preform the correct equation? I'm also new so if my formating is crap, sorry.


Code:
public class BankAccount 
{
    
    public double deposit (double balance)
    {
       balance = balance + x;
       return balance;
    }
    public double withdraw (double balance) 
    {
       balance = balance - x;
       return balance;
    }
    public String name;
    public double balance;
    public double x;
}



Code:
import java.io.*;
import java.util.*;

public class Tester
{
public static void main(String [] args)
{
        
 Scanner kbReader = new Scanner(System.in);
 System.out.println("Enter amount of monet in account");
 double balance = kbReader.nextDouble();
 
 System.out.println("Enter name:");
 String name = kbReader.next();
 
 System.out.println("Withdraw or Deposit");
 String choice = kbReader.next();
if (choice == "Withdraw" )
{  
  
    System.out.println("How much would you like to withdraw?");
     double x = kbReader.nextDouble();
}
 if (choice == "Deposit")
 {
      
     System.out.println("How much would you like to deposit?");
     double x = kbReader.nextDouble();
}

 
 
 System.out.println("The " + name + " account balance is " + balance);
}
}
post #2 of 16
Use .equals() instead of == for strings
Code:
if ("Withdraw".equals(choice))

http://perso.ensta-paristech.fr/~diam/java/online/notes-java/data/expressions/22compareobjects.html
Core I7
(13 items)
 
  
CPUMotherboardGraphicsRAM
I7 920 rev. D0 @ 4.26Ghz EVGA X58 SLI EVGA GTX 285 OCZ XMP 3x2Gb (pc3 12800) 
Hard DriveOptical DriveOSMonitor
Western Digital Caviar Black 640Gb x 2 LG GH22LS30 openSuse 12.1 x64 HP F2105 
PowerCase
CORSAIR 850TX Cooler Master ATCS 840 
  hide details  
Reply
Core I7
(13 items)
 
  
CPUMotherboardGraphicsRAM
I7 920 rev. D0 @ 4.26Ghz EVGA X58 SLI EVGA GTX 285 OCZ XMP 3x2Gb (pc3 12800) 
Hard DriveOptical DriveOSMonitor
Western Digital Caviar Black 640Gb x 2 LG GH22LS30 openSuse 12.1 x64 HP F2105 
PowerCase
CORSAIR 850TX Cooler Master ATCS 840 
  hide details  
Reply
post #3 of 16
Your main method has "monet" instead of "money". Just a heads up.

But yeah, use .equals() to compare strings.
post #4 of 16
Or .equalsIgnoreCase()
Meh
(11 items)
 
  
CPUMotherboardGraphicsRAM
i7 2600k @4.7GHz Asus P8Z68-V GEN3 GTX Titan 16GB Mushkin Redline Ridgeback 
Hard DriveHard DriveCoolingOS
Crucial M4 128GB  2x 500GB WD Green RAID1 + WD 2TB Green  Noctua Nh-DH 14 Win 8 Pro x64 
MonitorPowerCase
Shimian Achieva 2560x1440p IPS Corsair AX850 Lian-Li PC-A05N 
  hide details  
Reply
Meh
(11 items)
 
  
CPUMotherboardGraphicsRAM
i7 2600k @4.7GHz Asus P8Z68-V GEN3 GTX Titan 16GB Mushkin Redline Ridgeback 
Hard DriveHard DriveCoolingOS
Crucial M4 128GB  2x 500GB WD Green RAID1 + WD 2TB Green  Noctua Nh-DH 14 Win 8 Pro x64 
MonitorPowerCase
Shimian Achieva 2560x1440p IPS Corsair AX850 Lian-Li PC-A05N 
  hide details  
Reply
post #5 of 16
Thread Starter 
Thank you so much.
post #6 of 16
Thread Starter 
OK that helped a lot, thank you. However, now now whenever I run it, It prints the original balance that was input rather than the deposited or withdrawn amount. I think it's a failure of communicate between the two classes.
post #7 of 16
All you are doing is printing the variable name and balance as you entered them above.

You never use the class you created. Also you might want to put a constructor in that class

and then a way to create an instance of the class and associate it with the name of the account

examples


added constructor and changed methods to store values instead of returning them
added method to get balance
Code:
public class BankAccount 
{
    private double balance;
    
    public BankAccount(double balance) {
        this.balance = balance;
    }

    public void deposit(double balance) {
       this.balance += balance;
    }
    
    public void withdraw(double balance) {
       this.balance -= balance;
    }
    
    public double getBalance() {
        return this.balance;
    }
}

Code:
import java.util.HashMap;
import java.util.Scanner;

public class Main
{
    public static void main(String [] args){

        HashMap<String,BankAccount> accounts = new HashMap<String,BankAccount>();
        Scanner reader = new Scanner(System.in);
        String name;
        double balance;

        while(true) {

            System.out.println("[Create] account");
            System.out.println("[Withdraw] money from account");
            System.out.println("[Deposit] money in accout");
            System.out.println("[Get] account balance");
            System.out.println("[q] to quit");

            String minput = reader.next();

            if ("q".equalsIgnoreCase(minput)){
                break;
            }
            if ("create".equalsIgnoreCase(minput)){
                while(true) {
                    System.out.print("Enter name [c] to cancel: ");
                    name = reader.next();

                    if ("c".equalsIgnoreCase(name)){
                        break;
                    }

                    if (accounts.containsKey(name)) {
                        System.out.println("Account name is already in use");
                    }
                    else {
                        System.out.println("Enter amount of money in account");
                        balance = reader.nextDouble();
                        accounts.put(name, new BankAccount(balance));
                        break;
                    }
                }
            }

            if ("withdraw".equalsIgnoreCase(minput)){
                while(true) {
                    System.out.println("Enter name of account");
                    System.out.print("to withdraw from or [c] to cancel: ");
                    name = reader.next();

                    if ("c".equalsIgnoreCase(name)){
                        break;
                    }
                    if (accounts.containsKey(name)) {
                        System.out.print("How much would you like to withdraw? ");
                        balance = reader.nextDouble();
                        accounts.get(name).withdraw(balance);
                        break;
                    }
                    else {
                        System.out.println("account name not found");
                    }
                }
            }

            if ("deposit".equalsIgnoreCase(minput)){
                while(true) {
                    System.out.println("Enter name of account");
                    System.out.print("to deposit into or [c] to cancel: ");
                    name = reader.next();

                    if ("c".equalsIgnoreCase(name)){
                        break;
                    }
                    if (accounts.containsKey(name)) {
                        System.out.print("How much would you like to deposit? ");
                        balance = reader.nextDouble();
                        accounts.get(name).deposit(balance);
                        break;
                    }
                    else {
                        System.out.println("Account name not found");
                    }
                }
            }

            if ("get".equalsIgnoreCase(minput)){
                while(true) {
                    System.out.print("Enter name [c] to cancel: ");
                    name = reader.next();

                    if ("c".equalsIgnoreCase(name)){
                        break;
                    }

                    if (accounts.containsKey(name)) {
                        System.out.println("Account balance for " + name + " is $" +
                                accounts.get(name).getBalance());
                        break;
                    }
                    else {
                        System.out.println("No accout with that name");
                    }
                }
            }
        }
    }
}


create an empty HashMap that will use a string as a key and an instance of BankAccout as the value
Code:
HashMap<String,BankAccount> accounts = new HashMap<String,BankAccount>();

add the key,value pairs like so
Code:
accounts.put(name, new BankAccount(balance));

deposit, withdraw, or get the balance by calling the methods on the instance of the class
Code:
accounts.get(name).getBalance());
accounts.get(name).deposit(balance);
accounts.get(name).withdraw(balance);
Core I7
(13 items)
 
  
CPUMotherboardGraphicsRAM
I7 920 rev. D0 @ 4.26Ghz EVGA X58 SLI EVGA GTX 285 OCZ XMP 3x2Gb (pc3 12800) 
Hard DriveOptical DriveOSMonitor
Western Digital Caviar Black 640Gb x 2 LG GH22LS30 openSuse 12.1 x64 HP F2105 
PowerCase
CORSAIR 850TX Cooler Master ATCS 840 
  hide details  
Reply
Core I7
(13 items)
 
  
CPUMotherboardGraphicsRAM
I7 920 rev. D0 @ 4.26Ghz EVGA X58 SLI EVGA GTX 285 OCZ XMP 3x2Gb (pc3 12800) 
Hard DriveOptical DriveOSMonitor
Western Digital Caviar Black 640Gb x 2 LG GH22LS30 openSuse 12.1 x64 HP F2105 
PowerCase
CORSAIR 850TX Cooler Master ATCS 840 
  hide details  
Reply
post #8 of 16
tom.slick is correct about how you use the class. Before you were simply using it like a variable. The point of a class to be able to create an object, as he showed. However you don't need to use a hashmap, that is more complicated for you to understand. Simply declare one or more bankAccounts as follows:

BankAccount account1 = new BankAccount();
BankAccount account2 = new BankAccount();

and reference like
account1.getBalance();

To tom.slick - if he didn't know to correctly use a class, you shouldn't be describing a more complicated way of declaring an object in list or a hashmap. keep it simply for him. While it might be a better, cleaner way to do it, that is not what he is learning at the moment. People have to learn to walk before they can run. And if I was the instructor and I was teaching someone how to declare a class, and they came back with using a hashmap I would be suspicious of how they came up with that unless they excelled the entire time in the class.
Work in Progress
(15 items)
 
  
CPUMotherboardGraphicsRAM
AMD FX-8350 BE Vishera GIGABYTE 990FXA SAPPHIRE FleX-2 7950 G.SKILL Ripjaws X Series 
Hard DriveOptical DriveCoolingOS
OCZ Vertex 4 HP 20X DVD±R DVD Burner CORSAIR H100i Windows 8 
MonitorMonitorKeyboardPower
22in Widescreen Samsung 24in Widescreen Asus Microsoft Corsair 650TX 
CaseMouse
Corsair Vengeance C70 in Gunmetal Black Logitech G700 
  hide details  
Reply
Work in Progress
(15 items)
 
  
CPUMotherboardGraphicsRAM
AMD FX-8350 BE Vishera GIGABYTE 990FXA SAPPHIRE FleX-2 7950 G.SKILL Ripjaws X Series 
Hard DriveOptical DriveCoolingOS
OCZ Vertex 4 HP 20X DVD±R DVD Burner CORSAIR H100i Windows 8 
MonitorMonitorKeyboardPower
22in Widescreen Samsung 24in Widescreen Asus Microsoft Corsair 650TX 
CaseMouse
Corsair Vengeance C70 in Gunmetal Black Logitech G700 
  hide details  
Reply
post #9 of 16
Thread Starter 
Thank you for simplifying it for me. Should I replace my methods with those?
post #10 of 16
Quote:
Originally Posted by Ike View Post

To tom.slick - if he didn't know to correctly use a class, you shouldn't be describing a more complicated way of declaring an object in list or a hashmap. keep it simply for him. While it might be a better, cleaner way to do it, that is not what he is learning at the moment. People have to learn to walk before they can run. And if I was the instructor and I was teaching someone how to declare a class, and they came back with using a hashmap I would be suspicious of how they came up with that unless they excelled the entire time in the class.

Your right I should have kept it simple. I sometimes forget the steps in between and just solve the problem.
Core I7
(13 items)
 
  
CPUMotherboardGraphicsRAM
I7 920 rev. D0 @ 4.26Ghz EVGA X58 SLI EVGA GTX 285 OCZ XMP 3x2Gb (pc3 12800) 
Hard DriveOptical DriveOSMonitor
Western Digital Caviar Black 640Gb x 2 LG GH22LS30 openSuse 12.1 x64 HP F2105 
PowerCase
CORSAIR 850TX Cooler Master ATCS 840 
  hide details  
Reply
Core I7
(13 items)
 
  
CPUMotherboardGraphicsRAM
I7 920 rev. D0 @ 4.26Ghz EVGA X58 SLI EVGA GTX 285 OCZ XMP 3x2Gb (pc3 12800) 
Hard DriveOptical DriveOSMonitor
Western Digital Caviar Black 640Gb x 2 LG GH22LS30 openSuse 12.1 x64 HP F2105 
PowerCase
CORSAIR 850TX Cooler Master ATCS 840 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Help with a Java program