lab6
This commit is contained in:
parent
31a256b88a
commit
337d5b0c16
BIN
labs/lab6/Account.class
Normal file
BIN
labs/lab6/Account.class
Normal file
Binary file not shown.
71
labs/lab6/Account.java
Normal file
71
labs/lab6/Account.java
Normal file
@ -0,0 +1,71 @@
|
||||
// Account.java
|
||||
//
|
||||
// A bank account class with methods to deposit to, withdraw from,
|
||||
// change the name on, and get a String representation
|
||||
// of the account.
|
||||
//*******************************************************
|
||||
public class Account
|
||||
{
|
||||
private double balance;
|
||||
private String name;
|
||||
private long acctNum;
|
||||
//----------------------------------------------
|
||||
//Constructor -- initializes balance, owner, and account number
|
||||
//----------------------------------------------
|
||||
public Account(double initBal, String owner, long number)
|
||||
{
|
||||
balance = initBal;
|
||||
name = owner;
|
||||
acctNum = number;
|
||||
}
|
||||
public Account(double initBal, String owner)
|
||||
{
|
||||
balance = initBal;
|
||||
name = owner;
|
||||
}
|
||||
public Account(String owner)
|
||||
{
|
||||
name = owner;
|
||||
}
|
||||
//----------------------------------------------
|
||||
// Checks to see if balance is sufficient for withdrawal.
|
||||
// If so, decrements balance by amount; if not, prints message.
|
||||
//----------------------------------------------
|
||||
public void withdraw(double amount)
|
||||
{
|
||||
if (balance >= amount)
|
||||
balance -= amount;
|
||||
else
|
||||
System.out.println("Insufficient funds");
|
||||
}
|
||||
public void withdraw(int amount, int fee)
|
||||
{
|
||||
if (balance >= amount)
|
||||
balance -= amount + fee;
|
||||
else
|
||||
System.out.println("Insufficient funds");
|
||||
}
|
||||
//----------------------------------------------
|
||||
// Adds deposit amount to balance.
|
||||
//----------------------------------------------
|
||||
public void deposit(double amount)
|
||||
{
|
||||
balance += amount;
|
||||
}
|
||||
//----------------------------------------------
|
||||
// Returns balance.
|
||||
//----------------------------------------------
|
||||
public double getBalance()
|
||||
{
|
||||
return balance;
|
||||
}
|
||||
//----------------------------------------------
|
||||
// Returns a string containing the name, account number, and balance.
|
||||
//----------------------------------------------
|
||||
public String toString()
|
||||
{
|
||||
return "Name:" + name +
|
||||
"\nAccount Number: " + acctNum +
|
||||
"\nBalance: " + balance;
|
||||
}
|
||||
}
|
43
labs/lab6/TestAccount.java
Normal file
43
labs/lab6/TestAccount.java
Normal file
@ -0,0 +1,43 @@
|
||||
//*******************************************************
|
||||
// TestAccount.java
|
||||
//
|
||||
// A simple driver to test the overloaded methods of
|
||||
// the Account class.
|
||||
//*******************************************************
|
||||
import java.util.Scanner;
|
||||
public class TestAccount
|
||||
{
|
||||
public static void main(String[] args)
|
||||
{
|
||||
String name;
|
||||
double balance;
|
||||
long acctNum;
|
||||
Account acct;
|
||||
Scanner scan = new Scanner(System.in);
|
||||
System.out.println("Enter account holder's first name");
|
||||
name = scan.next();
|
||||
acct = new Account(name);
|
||||
System.out.println("Account for " + name + ":");
|
||||
System.out.println(acct);
|
||||
System.out.println("\nEnter initial balance");
|
||||
balance = scan.nextDouble();
|
||||
acct = new Account(balance,name);
|
||||
System.out.println("Account for " + name + ":");
|
||||
System.out.println(acct);
|
||||
System.out.println("\nEnter account number");
|
||||
acctNum = scan.nextLong();
|
||||
acct = new Account(balance,name,acctNum);
|
||||
System.out.println("Account for " + name + ":");
|
||||
System.out.println(acct);
|
||||
System.out.print("\nDepositing 100 into account, balance is now ");
|
||||
acct.deposit(100);
|
||||
System.out.println(acct.getBalance());
|
||||
System.out.print("\nWithdrawing $25, balance is now ");
|
||||
acct.withdraw(25);
|
||||
System.out.println(acct.getBalance());
|
||||
System.out.print("\nWithdrawing $25 with $2 fee, balance is now ");
|
||||
acct.withdraw(25,2);
|
||||
System.out.println(acct.getBalance());
|
||||
System.out.println("\nBye!");
|
||||
}
|
||||
}
|
BIN
labs/lab6/chapter6_lab-6.pdf
Normal file
BIN
labs/lab6/chapter6_lab-6.pdf
Normal file
Binary file not shown.
0
labs/lab6/chapter6_lab-6.pdfZone.Identifier
Normal file
0
labs/lab6/chapter6_lab-6.pdfZone.Identifier
Normal file
Loading…
x
Reference in New Issue
Block a user