diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e0dbdd8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,21 @@ +# Created by https://www.toptal.com/developers/gitignore/api/java +# Edit at https://www.toptal.com/developers/gitignore?templates=java + +### Java ### +# Compiled class file +*.class + +# Log file +*.log + +# BlueJ files +*.ctxt + +# Mobile Tools for Java (J2ME) +.mtj.tmp/ + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* +replay_pid* + +# End of https://www.toptal.com/developers/gitignore/api/java \ No newline at end of file diff --git a/bin/ATM.class b/bin/ATM.class deleted file mode 100644 index 76543bf..0000000 Binary files a/bin/ATM.class and /dev/null differ diff --git a/bin/ATMCaseStudy.class b/bin/ATMCaseStudy.class deleted file mode 100644 index 607c10d..0000000 Binary files a/bin/ATMCaseStudy.class and /dev/null differ diff --git a/bin/Account.class b/bin/Account.class deleted file mode 100644 index 5d2d636..0000000 Binary files a/bin/Account.class and /dev/null differ diff --git a/bin/BalanceInquiry.class b/bin/BalanceInquiry.class deleted file mode 100644 index 48f8e6a..0000000 Binary files a/bin/BalanceInquiry.class and /dev/null differ diff --git a/bin/BankDatabase.class b/bin/BankDatabase.class deleted file mode 100644 index 429aec1..0000000 Binary files a/bin/BankDatabase.class and /dev/null differ diff --git a/bin/CashDispenser.class b/bin/CashDispenser.class deleted file mode 100644 index b8cedba..0000000 Binary files a/bin/CashDispenser.class and /dev/null differ diff --git a/bin/Deposit.class b/bin/Deposit.class deleted file mode 100644 index 57b0c3f..0000000 Binary files a/bin/Deposit.class and /dev/null differ diff --git a/bin/DepositSlot.class b/bin/DepositSlot.class deleted file mode 100644 index a2664ad..0000000 Binary files a/bin/DepositSlot.class and /dev/null differ diff --git a/bin/Keypad.class b/bin/Keypad.class deleted file mode 100644 index 9b066e2..0000000 Binary files a/bin/Keypad.class and /dev/null differ diff --git a/bin/Screen.class b/bin/Screen.class deleted file mode 100644 index a036758..0000000 Binary files a/bin/Screen.class and /dev/null differ diff --git a/bin/Transaction.class b/bin/Transaction.class deleted file mode 100644 index 1330b55..0000000 Binary files a/bin/Transaction.class and /dev/null differ diff --git a/bin/Withdrawal.class b/bin/Withdrawal.class deleted file mode 100644 index 291d645..0000000 Binary files a/bin/Withdrawal.class and /dev/null differ diff --git a/bin/code/database/Account.class b/bin/code/database/Account.class new file mode 100644 index 0000000..7235ba7 Binary files /dev/null and b/bin/code/database/Account.class differ diff --git a/bin/code/database/Transaction.class b/bin/code/database/Transaction.class new file mode 100644 index 0000000..5db28bd Binary files /dev/null and b/bin/code/database/Transaction.class differ diff --git a/bin/test/testEuro.class b/bin/test/testEuro.class new file mode 100644 index 0000000..1a62736 Binary files /dev/null and b/bin/test/testEuro.class differ diff --git a/componenti.txt b/componenti.txt new file mode 100644 index 0000000..420f876 --- /dev/null +++ b/componenti.txt @@ -0,0 +1,2 @@ +Manuel Guiso 4696045 +Kevin Barragan 4662846 diff --git a/lib/hamcrest-core-1.3.jar b/lib/hamcrest-core-1.3.jar new file mode 100644 index 0000000..9d5fe16 Binary files /dev/null and b/lib/hamcrest-core-1.3.jar differ diff --git a/lib/junit-4.13.2.jar b/lib/junit-4.13.2.jar new file mode 100644 index 0000000..6da55d8 Binary files /dev/null and b/lib/junit-4.13.2.jar differ diff --git a/src/ATMCaseStudy.java b/src/code/BusinessLogic/ATMCaseStudy.java similarity index 96% rename from src/ATMCaseStudy.java rename to src/code/BusinessLogic/ATMCaseStudy.java index b9033bd..b972653 100644 --- a/src/ATMCaseStudy.java +++ b/src/code/BusinessLogic/ATMCaseStudy.java @@ -1,6 +1,9 @@ +package code.BusinessLogic; // ATMCaseStudy.java // Driver program for the ATM case study +import code.gui.ATM; + public class ATMCaseStudy { // main method creates and runs the ATM diff --git a/src/CashDispenser.java b/src/code/BusinessLogic/CashDispenser.java similarity index 98% rename from src/CashDispenser.java rename to src/code/BusinessLogic/CashDispenser.java index b249faf..2ab90f1 100644 --- a/src/CashDispenser.java +++ b/src/code/BusinessLogic/CashDispenser.java @@ -1,3 +1,4 @@ +package code.BusinessLogic; // CashDispenser.java // Represents the cash dispenser of the ATM diff --git a/src/DepositSlot.java b/src/code/BusinessLogic/DepositSlot.java similarity index 98% rename from src/DepositSlot.java rename to src/code/BusinessLogic/DepositSlot.java index 64e02c2..5d7dc10 100644 --- a/src/DepositSlot.java +++ b/src/code/BusinessLogic/DepositSlot.java @@ -1,3 +1,4 @@ +package code.BusinessLogic; // DepositSlot.java // Represents the deposit slot of the ATM diff --git a/src/code/BusinessLogic/Euro.java b/src/code/BusinessLogic/Euro.java new file mode 100644 index 0000000..62604c0 --- /dev/null +++ b/src/code/BusinessLogic/Euro.java @@ -0,0 +1,48 @@ +package code.BusinessLogic; +public class Euro { + + + private long valore; + + public Euro(long euro, long cent) { + if (euro >= 0) { + valore = euro*100 + cent; + } else { + valore = euro*100 - cent; + } + } + + public Euro(double d) { + valore = (long)(d*100); + } + + public long getValore() { + return valore; + } + + public Euro somma(Euro e) { + this.valore = this.valore + e.getValore(); + return this; + } + + public Euro sottrai(Euro e) { + this.valore = this.valore - e.getValore(); + return this; + } + + public boolean ugualeA(Euro e){ + if (valore == e.getValore()) + return true; + else return false; + } + + public boolean minoreDi(Euro e){ + if (valore <= e.getValore()) + return true; + else return false; + } + + public String stampa(){ + return (double)valore/100 +" euro"; + } +} diff --git a/src/Account.java b/src/code/database/Account.java similarity index 76% rename from src/Account.java rename to src/code/database/Account.java index c308eca..64b5381 100644 --- a/src/Account.java +++ b/src/code/database/Account.java @@ -1,12 +1,15 @@ +package code.database; // Account.java // Represents a bank account +import code.BusinessLogic.Euro; + public class Account { private int accountNumber; // account number private int pin; // PIN for authentication - private double availableBalance; // funds available for withdrawal - private double totalBalance; // funds available + pending deposits + private Euro availableBalance; // funds available for withdrawal + private Euro totalBalance; // funds available + pending deposits // Account constructor initializes attributes public Account( int theAccountNumber, int thePIN, @@ -14,8 +17,8 @@ public Account( int theAccountNumber, int thePIN, { accountNumber = theAccountNumber; pin = thePIN; - availableBalance = theAvailableBalance; - totalBalance = theTotalBalance; + availableBalance = new Euro(theAvailableBalance); + totalBalance = new Euro(theTotalBalance); } // end Account constructor // determines whether a user-specified PIN matches PIN in Account @@ -28,29 +31,29 @@ public boolean validatePIN( int userPIN ) } // end method validatePIN // returns available balance - public double getAvailableBalance() + public Euro getAvailableBalance() { return availableBalance; } // end getAvailableBalance // returns the total balance - public double getTotalBalance() + public Euro getTotalBalance() { return totalBalance; } // end method getTotalBalance // credits an amount to the account - public void credit( double amount ) + public void credit( Euro amount ) { - totalBalance += amount; // add to total balance + totalBalance.somma(amount); // add to total balance } // end method credit // debits an amount from the account - public void debit( double amount ) + public void debit( Euro amount ) { - availableBalance -= amount; // subtract from available balance - totalBalance -= amount; // subtract from total balance - } // end method debit + availableBalance.sottrai(amount); // subtract from available balance + totalBalance.sottrai(amount); // subtract from total balance + } // end method debit // returns account number public int getAccountNumber() diff --git a/src/BankDatabase.java b/src/code/database/BankDatabase.java similarity index 88% rename from src/BankDatabase.java rename to src/code/database/BankDatabase.java index 3978497..8b6144c 100644 --- a/src/BankDatabase.java +++ b/src/code/database/BankDatabase.java @@ -1,6 +1,9 @@ +package code.database; // BankDatabase.java // Represents the bank account information database +import code.BusinessLogic.Euro; + public class BankDatabase { private Account accounts[]; // array of Accounts @@ -9,8 +12,8 @@ public class BankDatabase public BankDatabase() { accounts = new Account[ 2 ]; // just 2 accounts for testing - accounts[ 0 ] = new Account( 12345, 54321, 1000.0, 1200.0 ); - accounts[ 1 ] = new Account( 98765, 56789, 200.0, 200.0 ); + accounts[ 0 ] = new Account( 34567, 54321, 1000.0, 1200.0 ); + accounts[ 1 ] = new Account( 12345, 56789, 200.0, 200.0 ); } // end no-argument BankDatabase constructor // retrieve Account object containing specified account number @@ -42,25 +45,25 @@ public boolean authenticateUser( int userAccountNumber, int userPIN ) } // end method authenticateUser // return available balance of Account with specified account number - public double getAvailableBalance( int userAccountNumber ) + public Euro getAvailableBalance( int userAccountNumber ) { return getAccount( userAccountNumber ).getAvailableBalance(); } // end method getAvailableBalance // return total balance of Account with specified account number - public double getTotalBalance( int userAccountNumber ) + public Euro getTotalBalance( int userAccountNumber ) { return getAccount( userAccountNumber ).getTotalBalance(); } // end method getTotalBalance // credit an amount to Account with specified account number - public void credit( int userAccountNumber, double amount ) + public void credit( int userAccountNumber, Euro amount ) { getAccount( userAccountNumber ).credit( amount ); } // end method credit // debit an amount from of Account with specified account number - public void debit( int userAccountNumber, double amount ) + public void debit( int userAccountNumber, Euro amount ) { getAccount( userAccountNumber ).debit( amount ); } // end method debit diff --git a/src/Transaction.java b/src/code/database/Transaction.java similarity index 97% rename from src/Transaction.java rename to src/code/database/Transaction.java index 508fea8..e12319d 100644 --- a/src/Transaction.java +++ b/src/code/database/Transaction.java @@ -1,6 +1,9 @@ +package code.database; // Transaction.java // Abstract superclass Transaction represents an ATM transaction +import code.gui.Screen; + public abstract class Transaction { private int accountNumber; // indicates account involved diff --git a/src/ATM.java b/src/code/gui/ATM.java similarity index 97% rename from src/ATM.java rename to src/code/gui/ATM.java index aa3d187..0b50760 100644 --- a/src/ATM.java +++ b/src/code/gui/ATM.java @@ -1,6 +1,12 @@ +package code.gui; // ATM.java // Represents an automated teller machine +import code.BusinessLogic.CashDispenser; +import code.BusinessLogic.DepositSlot; +import code.database.BankDatabase; +import code.database.Transaction; + public class ATM { private boolean userAuthenticated; // whether user is authenticated diff --git a/src/BalanceInquiry.java b/src/code/gui/BalanceInquiry.java similarity index 92% rename from src/BalanceInquiry.java rename to src/code/gui/BalanceInquiry.java index d45fa6a..ff9c9b3 100644 --- a/src/BalanceInquiry.java +++ b/src/code/gui/BalanceInquiry.java @@ -1,6 +1,11 @@ +package code.gui; // BalanceInquiry.java // Represents a balance inquiry ATM transaction +import code.BusinessLogic.Euro; +import code.database.BankDatabase; +import code.database.Transaction; + public class BalanceInquiry extends Transaction { // BalanceInquiry constructor @@ -18,11 +23,11 @@ public void execute() Screen screen = getScreen(); // get the available balance for the account involved - double availableBalance = + Euro availableBalance = bankDatabase.getAvailableBalance( getAccountNumber() ); // get the total balance for the account involved - double totalBalance = + Euro totalBalance = bankDatabase.getTotalBalance( getAccountNumber() ); // display the balance information on the screen diff --git a/src/Deposit.java b/src/code/gui/Deposit.java similarity index 90% rename from src/Deposit.java rename to src/code/gui/Deposit.java index 916ef70..f39e7b0 100644 --- a/src/Deposit.java +++ b/src/code/gui/Deposit.java @@ -1,9 +1,15 @@ +package code.gui; // Deposit.java // Represents a deposit ATM transaction +import code.BusinessLogic.DepositSlot; +import code.BusinessLogic.Euro; +import code.database.BankDatabase; +import code.database.Transaction; + public class Deposit extends Transaction { - private double amount; // amount to deposit + private Euro amount; // amount to deposit private Keypad keypad; // reference to keypad private DepositSlot depositSlot; // reference to deposit slot private final static int CANCELED = 0; // constant for cancel option @@ -30,7 +36,7 @@ public void execute() amount = promptForDepositAmount(); // get deposit amount from user // check whether user entered a deposit amount or canceled - if ( amount != CANCELED ) + if ( !amount.ugualeA(new Euro(CANCELED)) ) { // request deposit envelope containing specified amount screen.displayMessage( @@ -65,7 +71,7 @@ public void execute() } // end method execute // prompt user to enter a deposit amount in cents - private double promptForDepositAmount() + private Euro promptForDepositAmount() { Screen screen = getScreen(); // get reference to screen @@ -76,10 +82,10 @@ private double promptForDepositAmount() // check whether the user canceled or entered a valid amount if ( input == CANCELED ) - return CANCELED; + return new Euro ( CANCELED ); else { - return ( double ) input / 100; // return dollar amount + return new Euro (( double ) input / 100); // return dollar amount } // end else } // end method promptForDepositAmount } // end class Deposit diff --git a/src/Keypad.java b/src/code/gui/Keypad.java similarity index 98% rename from src/Keypad.java rename to src/code/gui/Keypad.java index cd035c7..5cf2c68 100644 --- a/src/Keypad.java +++ b/src/code/gui/Keypad.java @@ -1,3 +1,4 @@ +package code.gui; // Keypad.java // Represents the keypad of the ATM import java.util.Scanner; // program uses Scanner to obtain user input diff --git a/src/Screen.java b/src/code/gui/Screen.java similarity index 90% rename from src/Screen.java rename to src/code/gui/Screen.java index 44d3f30..70c9da1 100644 --- a/src/Screen.java +++ b/src/code/gui/Screen.java @@ -1,6 +1,9 @@ +package code.gui; // Screen.java // Represents the screen of the ATM +import code.BusinessLogic.Euro; + public class Screen { // displays a message without a carriage return @@ -16,9 +19,9 @@ public void displayMessageLine( String message ) } // end method displayMessageLine // display a dollar amount - public void displayDollarAmount( double amount ) + public void displayDollarAmount( Euro amount ) { - System.out.printf( "$%,.2f", amount ); + System.out.printf( "$%,.2f", (float) amount.getValore()/100 ); } // end method displayDollarAmount } // end class Screen diff --git a/src/Withdrawal.java b/src/code/gui/Withdrawal.java similarity index 94% rename from src/Withdrawal.java rename to src/code/gui/Withdrawal.java index 6e0af62..1d06ef3 100644 --- a/src/Withdrawal.java +++ b/src/code/gui/Withdrawal.java @@ -1,6 +1,12 @@ +package code.gui; // Withdrawal.java // Represents a withdrawal ATM transaction +import code.BusinessLogic.CashDispenser; +import code.BusinessLogic.Euro; +import code.database.BankDatabase; +import code.database.Transaction; + public class Withdrawal extends Transaction { private int amount; // amount to withdraw @@ -27,7 +33,7 @@ public Withdrawal( int userAccountNumber, Screen atmScreen, public void execute() { boolean cashDispensed = false; // cash was not dispensed yet - double availableBalance; // amount available for withdrawal + Euro availableBalance; // amount available for withdrawal // get references to bank database and screen BankDatabase bankDatabase = getBankDatabase(); @@ -47,13 +53,13 @@ public void execute() bankDatabase.getAvailableBalance( getAccountNumber() ); // check whether the user has enough money in the account - if ( amount <= availableBalance ) + if ( amount <= availableBalance.getValore() ) { // check whether the cash dispenser has enough money if ( cashDispenser.isSufficientCashAvailable( amount ) ) { // update the account involved to reflect withdrawal - bankDatabase.debit( getAccountNumber(), amount ); + bankDatabase.debit( getAccountNumber(), new Euro(amount) ); cashDispenser.dispenseCash( amount ); // dispense cash cashDispensed = true; // cash was dispensed diff --git a/src/test/testAccount.java b/src/test/testAccount.java new file mode 100644 index 0000000..391e781 --- /dev/null +++ b/src/test/testAccount.java @@ -0,0 +1,52 @@ +package test; + +import org.junit.Before; +import org.junit.Test; +import static org.junit.Assert.*; + +import code.BusinessLogic.Euro; +import code.database.Account; + +public class testAccount { + + private Account account; + + @Before + public void setUp() { + account = new Account(12345, 1111, 200.0, 500.0); + } + + @Test + public void testValidatePIN() { + assertTrue(account.validatePIN(1111)); + assertFalse(account.validatePIN(2222)); + } + + @Test + public void testGetAvailableBalance() { + assertTrue(new Euro(200.0).ugualeA(account.getAvailableBalance())); + } + + @Test + public void testGetTotalBalance() { + assertTrue(new Euro(500.0).ugualeA(account.getTotalBalance())); + } + + @Test + public void testCredit() { + account.credit(new Euro(100.0)); + assertTrue(new Euro(600.0).ugualeA(account.getTotalBalance())); + } + + @Test + public void testDebit() { + account.debit(new Euro(50.0)); + assertTrue(new Euro(150.0).ugualeA(account.getAvailableBalance())); + assertTrue(new Euro(450.0).ugualeA(account.getTotalBalance())); + } + + @Test + public void testGetAccountNumber() { + assertEquals(12345, account.getAccountNumber()); + } +} \ No newline at end of file diff --git a/src/test/testBankDatabase.java b/src/test/testBankDatabase.java new file mode 100644 index 0000000..38e6a48 --- /dev/null +++ b/src/test/testBankDatabase.java @@ -0,0 +1,49 @@ +package test; + +import org.junit.Before; +import org.junit.Test; +import static org.junit.Assert.*; + +import code.BusinessLogic.Euro; +import code.database.BankDatabase; + +public class testBankDatabase { + + private BankDatabase bankDatabase; + + @Before + public void setUp() { + bankDatabase = new BankDatabase(); + } + + @Test + public void testAuthenticateUser() { + assertTrue(bankDatabase.authenticateUser(12345, 54321)); + assertFalse(bankDatabase.authenticateUser(12345, 1111)); + assertFalse(bankDatabase.authenticateUser(11111, 54321)); + } + + @Test + public void testGetAvailableBalance() { + assertTrue(new Euro(1000.0).ugualeA(bankDatabase.getAvailableBalance(12345))); + assertTrue(new Euro(200.0).ugualeA(bankDatabase.getAvailableBalance(98765))); + } + + @Test + public void testGetTotalBalance() { + assertTrue(new Euro(1200.0).ugualeA(bankDatabase.getTotalBalance(12345))); + assertTrue(new Euro(200.0).ugualeA(bankDatabase.getTotalBalance(98765))); + } + + @Test + public void testCredit() { + bankDatabase.credit(12345, new Euro(100.0)); + assertTrue(new Euro(1300.0).ugualeA(bankDatabase.getTotalBalance(12345))); + } + + @Test + public void testDebit() { + bankDatabase.debit(12345, new Euro(50.0)); + assertTrue(new Euro(950.0).ugualeA(bankDatabase.getAvailableBalance(12345))); + } +} diff --git a/src/test/testEuro.java b/src/test/testEuro.java new file mode 100644 index 0000000..72c128d --- /dev/null +++ b/src/test/testEuro.java @@ -0,0 +1,63 @@ +package test; + +import org.junit.Before; +import org.junit.Test; + +import code.BusinessLogic.Euro; + +import static org.junit.Assert.*; + +public class testEuro { + private Euro euro1; + private Euro euro2; + + @Before + public void setup() { + euro1 = new Euro(5, 50); + euro2 = new Euro(4, 50); + } + + @Test + public void testEuroLongConstructor() { + assertEquals(550, euro1.getValore()); + } + + @Test + public void testEuroDoubleConstructor() { + Euro euro = new Euro(5.50); + assertEquals(550, euro.getValore()); + } + + @Test + public void testGetValore() { + assertEquals(550, euro1.getValore()); + } + + @Test + public void testSomma() { + euro1.somma(euro2); + assertEquals(1000, euro1.getValore()); + } + + @Test + public void testSottrai() { + euro1.sottrai(euro2); + assertEquals(100, euro1.getValore()); + } + + @Test + public void testUgualeA() { + Euro euro = new Euro(5, 50); + assertTrue(euro1.ugualeA(euro)); + } + + @Test + public void testMinoreDi() { + assertTrue(euro2.minoreDi(euro1)); + } + + @Test + public void testStampa() { + assertEquals("5.5 euro", euro1.stampa()); + } +} \ No newline at end of file