******************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;
}
}
Auf dieser Seite können Sie das Online-Video Java Programming Tutorial - 18 - Getting user Input and Creating Objects mit der Dauer stunde minuten sekunde in guter Qualität ansehen, das der Benutzer A J 10 Oktober 2017 hochgeladen hat, den Link mit Freunden und Bekannten teilen, dieses Video wurde auf Youtube bereits 54,081 Mal angesehen und es wurde von 589 den Zuschauern gefallen. Viel Spaß beim Betrachtenden Zuschauern gefallen!