added old projects
This commit is contained in:
Vendored
+4
@@ -0,0 +1,4 @@
|
||||
{
|
||||
"java.project.sourcePaths": ["src"],
|
||||
"java.project.outputPath": "bin"
|
||||
}
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,86 @@
|
||||
/*
|
||||
* @author (Student Name)
|
||||
* <p> (File Name)
|
||||
* <p> (Assignment)
|
||||
* <p> (Describe, in general, the code contained.)
|
||||
*/
|
||||
public class Account // TODO: refine this to establish proper relationships among accounts.
|
||||
{
|
||||
protected double _balance;
|
||||
public double getBalance() { return _balance; }
|
||||
|
||||
protected String _name;
|
||||
public String getName() { return _name; }
|
||||
|
||||
protected Account _link;
|
||||
public Account getLink() { return _link; }
|
||||
|
||||
//
|
||||
// TODO: Constructors and other required methods.
|
||||
//
|
||||
|
||||
//
|
||||
// Link this account with the given account;
|
||||
// Cannot link accounts if this equals linkAcct
|
||||
// Otherwise:
|
||||
// Unlink this account, if it is linked
|
||||
// Link the new accounts in BOTH directions
|
||||
//
|
||||
public boolean link(Account linkAcct)
|
||||
{
|
||||
// TODO
|
||||
}
|
||||
|
||||
//
|
||||
// Take $amt from this account, if available.
|
||||
//
|
||||
// Specifically,
|
||||
// If amt is available in this account, withdraw
|
||||
// If amt is not available,
|
||||
// Determine if it is available, in total, from this and the linked account.
|
||||
// If so, take from both.
|
||||
// If not, throw an exception and leave account balances unmodified.
|
||||
//
|
||||
public void withdraw(double amt) throws InsufficientFundsException
|
||||
{
|
||||
// TODO
|
||||
}
|
||||
|
||||
//
|
||||
// Unlink the accounts in BOTH directions
|
||||
//
|
||||
public void unlink()
|
||||
{
|
||||
// TODO
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj)
|
||||
{
|
||||
if (obj == null) return false;
|
||||
|
||||
if (!(obj instanceof Account)) return false;
|
||||
|
||||
Account that = (Account)obj;
|
||||
|
||||
if (Math.abs(_balance - that._balance) > 0.001) return false;
|
||||
|
||||
if (!_name.equals(that._name)) return false;
|
||||
|
||||
if (_link != that._link) return false;
|
||||
|
||||
return this == obj;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
String retS = "";
|
||||
|
||||
retS += "account: " + _name + "\n";
|
||||
retS += "\tBalance: " + _balance + "\n";
|
||||
retS += "\tLink account name: " + _link._name;
|
||||
|
||||
return retS;
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,189 @@
|
||||
import java.util.ArrayList;
|
||||
import java.util.Map;
|
||||
|
||||
public class BankTester extends Tester
|
||||
{
|
||||
public BankTester()
|
||||
{
|
||||
super();
|
||||
}
|
||||
|
||||
public void runAll()
|
||||
{
|
||||
//
|
||||
// Run tests and report
|
||||
//
|
||||
_testErrorMap.put("constructor", testConstructor());
|
||||
_testErrorMap.put("addAccount", testAddAccount());
|
||||
_testErrorMap.put("filtering", testFiltered());
|
||||
|
||||
//
|
||||
// Report results
|
||||
//
|
||||
System.out.println("\nBank Testing Summary: ");
|
||||
int sum = 0;
|
||||
for (Map.Entry<String, Integer> entry : _testErrorMap.entrySet())
|
||||
{
|
||||
System.out.println("\tTest " + entry.getKey() + ": " + entry.getValue() + " errors.");
|
||||
sum += entry.getValue();
|
||||
}
|
||||
|
||||
System.out.println("\tTotal number of errors: " + sum);
|
||||
}
|
||||
|
||||
public int testConstructor()
|
||||
{
|
||||
System.out.println("Test constructor...");
|
||||
|
||||
int errors = 0;
|
||||
|
||||
Bank b = new Bank();
|
||||
if (b.size() != 0)
|
||||
{
|
||||
emitError("Constructor error [bank]; size should be size zero");
|
||||
errors++;
|
||||
}
|
||||
if (b.getFiltered().size() != 0)
|
||||
{
|
||||
emitError("Constructor error [bank]; filtered should be size zero");
|
||||
errors++;
|
||||
}
|
||||
|
||||
return errors;
|
||||
}
|
||||
|
||||
//
|
||||
// Mixes the accounts...
|
||||
//
|
||||
private Bank constructBank(int numChecking, int numSavings, ArrayList<Account> savings, ArrayList<Account> checking, ArrayList<Account> accounts)
|
||||
{
|
||||
Bank bank = new Bank();
|
||||
|
||||
int c = 0;
|
||||
int s = 0;
|
||||
while (c < numChecking && s < numSavings)
|
||||
{
|
||||
Account newAcct = null;
|
||||
if (_rng.nextBoolean())
|
||||
{
|
||||
newAcct = new Checking();
|
||||
checking.add(newAcct);
|
||||
c++;
|
||||
}
|
||||
else
|
||||
{
|
||||
newAcct = new Savings();
|
||||
savings.add(newAcct);
|
||||
s++;
|
||||
}
|
||||
accounts.add(newAcct);
|
||||
bank.addAccount(newAcct);
|
||||
}
|
||||
|
||||
for ( ; c < numChecking; c++)
|
||||
{
|
||||
Account newAcct = new Checking();
|
||||
accounts.add(newAcct);
|
||||
checking.add(newAcct);
|
||||
bank.addAccount(newAcct);
|
||||
}
|
||||
|
||||
for (; s < numSavings; s++)
|
||||
{
|
||||
Account newAcct = new Savings();
|
||||
accounts.add(newAcct);
|
||||
savings.add(newAcct);
|
||||
bank.addAccount(newAcct);
|
||||
}
|
||||
|
||||
return bank;
|
||||
}
|
||||
|
||||
public int testAddAccount()
|
||||
{
|
||||
System.out.println("Add accounts...");
|
||||
|
||||
int errors = 0;
|
||||
|
||||
//
|
||||
// Unique adds
|
||||
//
|
||||
ArrayList<Account> savings = new ArrayList<Account>();
|
||||
ArrayList<Account> checking = new ArrayList<Account>();
|
||||
ArrayList<Account> accounts = new ArrayList<Account>();
|
||||
Bank bank = constructBank(12, 16, savings, checking, accounts);
|
||||
|
||||
if (bank.size() != 28)
|
||||
{
|
||||
emitError("Bank size: Expected " + 28 + " found " + bank.size());
|
||||
errors++;
|
||||
}
|
||||
|
||||
//
|
||||
// Redundant adds
|
||||
//
|
||||
for (Account acct : accounts)
|
||||
{
|
||||
if (bank.addAccount(acct))
|
||||
{
|
||||
emitError("Add account succeeded when should have failed");
|
||||
errors++;
|
||||
}
|
||||
}
|
||||
|
||||
return errors;
|
||||
}
|
||||
|
||||
public int testFiltered()
|
||||
{
|
||||
System.out.println("Test filtering...");
|
||||
int errors = 0;
|
||||
|
||||
//
|
||||
// Get a bank; then populate with values
|
||||
//
|
||||
int numSavings = 10;
|
||||
int numChecking = 10;
|
||||
ArrayList<Account> savings = new ArrayList<Account>();
|
||||
ArrayList<Account> checking = new ArrayList<Account>();
|
||||
ArrayList<Account> accounts = new ArrayList<Account>();
|
||||
Bank bank = constructBank(numChecking, numSavings, savings, checking, accounts);
|
||||
|
||||
for (Account acct : accounts)
|
||||
{
|
||||
try { acct.deposit(1000); } catch (LinkAccountException e) { e.printStackTrace(); errors++; }
|
||||
}
|
||||
|
||||
//
|
||||
// All savings should be satisfied
|
||||
//
|
||||
if (bank.size() != numSavings + numChecking)
|
||||
{
|
||||
emitError("Filtering: Expected " + (numSavings + numChecking) + " found " + bank.size());
|
||||
errors++;
|
||||
}
|
||||
ArrayList<Account> filtered = bank.getFiltered();
|
||||
if (filtered.size() != numSavings)
|
||||
{
|
||||
emitError("Filtering [filtered]: Expected " + numSavings + " found " + filtered.size());
|
||||
errors++;
|
||||
}
|
||||
|
||||
//
|
||||
// Make the checking satisfied by adding dummy accounts
|
||||
//
|
||||
for (int i = 0; i < checking.size(); i++)
|
||||
{
|
||||
checking.get(i).link(savings.get(i));
|
||||
}
|
||||
filtered = bank.getFiltered();
|
||||
if (filtered.size() != accounts.size())
|
||||
{
|
||||
emitError("Filtering [filtered]: Expected " + accounts.size() + " found " + filtered.size());
|
||||
errors++;
|
||||
}
|
||||
|
||||
|
||||
return errors;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
/*
|
||||
* @author (Student Name)
|
||||
* <p> (File Name)
|
||||
* <p> (Assignment)
|
||||
* <p> (Describe, in general, the code contained.)
|
||||
*/
|
||||
public class CappedChecking // TODO: refine this to establish proper relationships among accounts.
|
||||
{
|
||||
//
|
||||
// TODO: Constructors and other required methods.
|
||||
//
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj)
|
||||
{
|
||||
if (obj == null) return false;
|
||||
|
||||
if (!(obj instanceof CappedChecking)) return false;
|
||||
|
||||
return super.equals(obj);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
return "Capped-Checking " + super.toString();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* @author (Student Name)
|
||||
* <p> (File Name)
|
||||
* <p> (Assignment)
|
||||
* <p> (Describe, in general, the code contained.)
|
||||
*/
|
||||
public class Checking // TODO: refine this to establish proper relationships among accounts.
|
||||
{
|
||||
//
|
||||
// TODO: Constructors and other required methods.
|
||||
//
|
||||
|
||||
@Override
|
||||
public boolean link(Account linkAcct)
|
||||
{
|
||||
if (linkAcct == null) return false;
|
||||
|
||||
if (linkAcct instanceof Checking) return false;
|
||||
|
||||
return super.link(linkAcct);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj)
|
||||
{
|
||||
if (obj == null) return false;
|
||||
|
||||
if (!(obj instanceof Checking)) return false;
|
||||
|
||||
return super.equals(obj);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
return "Checking " + super.toString();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
* @author (Student Name)
|
||||
* <p> (File Name)
|
||||
* <p> (Assignment)
|
||||
* <p> (Describe, in general, the code contained.)
|
||||
*/
|
||||
public class InsufficientFundsException extends Exception
|
||||
{
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public InsufficientFundsException() {}
|
||||
|
||||
public InsufficientFundsException(String arg0)
|
||||
{
|
||||
super(arg0);
|
||||
}
|
||||
|
||||
public InsufficientFundsException(Throwable arg0)
|
||||
{
|
||||
super(arg0);
|
||||
}
|
||||
|
||||
public InsufficientFundsException(String arg0, Throwable arg1)
|
||||
{
|
||||
super(arg0, arg1);
|
||||
}
|
||||
|
||||
public InsufficientFundsException(String arg0, Throwable arg1, boolean arg2, boolean arg3)
|
||||
{
|
||||
super(arg0, arg1, arg2, arg3);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
* @author (Student Name)
|
||||
* <p> (File Name)
|
||||
* <p> (Assignment)
|
||||
* <p> (Describe, in general, the code contained.)
|
||||
*/
|
||||
public class LinkAccountException extends Exception
|
||||
{
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public LinkAccountException() {}
|
||||
|
||||
public LinkAccountException(String arg0)
|
||||
{
|
||||
super(arg0);
|
||||
}
|
||||
|
||||
public LinkAccountException(Throwable arg0)
|
||||
{
|
||||
super(arg0);
|
||||
}
|
||||
|
||||
public LinkAccountException(String arg0, Throwable arg1)
|
||||
{
|
||||
super(arg0, arg1);
|
||||
}
|
||||
|
||||
public LinkAccountException(String arg0, Throwable arg1, boolean arg2, boolean arg3)
|
||||
{
|
||||
super(arg0, arg1, arg2, arg3);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
/*
|
||||
* @author (Student Name)
|
||||
* <p> (File Name)
|
||||
* <p> (Assignment)
|
||||
* <p> (Describe, in general, the code contained.)
|
||||
*/
|
||||
public class Main
|
||||
{
|
||||
public static void main(String[] args)
|
||||
{
|
||||
AccountTester acctTester = new AccountTester();
|
||||
BankTester bankTester = new BankTester();
|
||||
|
||||
System.out.println("\nAccount Tests:\n");
|
||||
acctTester.runAll();
|
||||
|
||||
System.out.println("\nBank Tests:\n");
|
||||
bankTester.runAll();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* @author (Student Name)
|
||||
* <p> (File Name)
|
||||
* <p> (Assignment)
|
||||
* <p> (Describe, in general, the code contained.)
|
||||
*/
|
||||
public class Savings // TODO: refine this to establish proper relationships among accounts.
|
||||
{
|
||||
//
|
||||
// TODO: Constructors and other required methods.
|
||||
//
|
||||
|
||||
@Override
|
||||
public boolean link(Account linkAcct)
|
||||
{
|
||||
if (linkAcct == null) return false;
|
||||
|
||||
if (linkAcct instanceof Savings) return false;
|
||||
|
||||
return super.link(linkAcct);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj)
|
||||
{
|
||||
if (obj == null) return false;
|
||||
|
||||
if (!(obj instanceof Savings)) return false;
|
||||
|
||||
return super.equals(obj);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
return "Savings " + super.toString();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
import java.util.HashMap;
|
||||
import java.util.Random;
|
||||
|
||||
public abstract class Tester
|
||||
{
|
||||
protected HashMap<String, Integer> _testErrorMap;
|
||||
protected Random _rng;
|
||||
|
||||
public Tester()
|
||||
{
|
||||
_testErrorMap = new HashMap<String, Integer>();
|
||||
_rng = new Random();
|
||||
_rng.setSeed(0); // consistent testing
|
||||
}
|
||||
|
||||
public abstract void runAll();
|
||||
|
||||
public void emitError(String error)
|
||||
{
|
||||
System.out.println(error);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user