Java Programming Tutorial - 18 - Getting user Input and Creating Objects

Published: 10 October 2017
on channel: A J
54,081
589

******************Main Class*********************
import java.util.*;

public class BankProgram {

public static void main(String[] args){

//variables used to create account
String name;
int accountNum;
double amount;
final int MAX = 999999999;

//Scanner for user input, Random for random number.
Scanner keyboard = new Scanner(System.in);
Random rand = new Random();

//random number generated for account number. generates a number from 0 up to MAX.
accountNum = rand.nextInt(MAX);

//get user input to create account
System.out.println("Enter full name: ");
name = keyboard.nextLine();
System.out.println("Enter amount to deposit: ");
amount = keyboard.nextDouble();


//instantiate new object, acct1, with user's values
Account acct1 = new Account(name, accountNum, amount);

//display account information using toString method
System.out.println(acct1.toString());

}
}


**************************Account Class************************
import java.text.*;

public class Account {

//instance variables
private String name;
private int accountNum;
private double balance;

//the Account constructor
public Account(String initName, int initId, double initBalance){

name = initName;
accountNum = initId;
balance = initBalance;
}

//method to deposit a specified amount into the account
public void deposit(double amount){
balance = balance + amount;
}

//method to withdraw a specified amount from the account
public void withdraw(double amount, double fee){
balance = balance - amount - fee;
}

//getter method to return balance
public double getBalance(){
return balance;
}

//toString method that returns the accounts information
public String toString(){
String result = "";
NumberFormat fmt = NumberFormat.getCurrencyInstance();
result = "\nName: " + name + "\nAccount Number: " + accountNum + "\nBalance: " + fmt.format(balance);
return result;

}

}


On this page of the site you can watch the video online Java Programming Tutorial - 18 - Getting user Input and Creating Objects with a duration of hours minute second in good quality, which was uploaded by the user A J 10 October 2017, share the link with friends and acquaintances, this video has already been watched 54,081 times on youtube and it was liked by 589 viewers. Enjoy your viewing!