diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6dd29b7 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +bin/ \ 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/componenti.txt b/componenti.txt new file mode 100644 index 0000000..0fd0a0b --- /dev/null +++ b/componenti.txt @@ -0,0 +1,2 @@ +Andrea Chicco S5128817 +Nicolò Catania S5209932 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/Business_logic/ATMCaseStudy.java similarity index 96% rename from src/ATMCaseStudy.java rename to src/code/Business_logic/ATMCaseStudy.java index b9033bd..e9a045d 100644 --- a/src/ATMCaseStudy.java +++ b/src/code/Business_logic/ATMCaseStudy.java @@ -1,6 +1,9 @@ +package code.Business_logic; // 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/BalanceInquiry.java b/src/code/Business_logic/BalanceInquiry.java similarity index 94% rename from src/BalanceInquiry.java rename to src/code/Business_logic/BalanceInquiry.java index d45fa6a..90f3b45 100644 --- a/src/BalanceInquiry.java +++ b/src/code/Business_logic/BalanceInquiry.java @@ -1,6 +1,10 @@ +package code.Business_logic; // BalanceInquiry.java // Represents a balance inquiry ATM transaction +import code.Database.BankDatabase; +import code.GUI.Screen; + public class BalanceInquiry extends Transaction { // BalanceInquiry constructor @@ -18,11 +22,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/Business_logic/Deposit.java similarity index 91% rename from src/Deposit.java rename to src/code/Business_logic/Deposit.java index 916ef70..c74e249 100644 --- a/src/Deposit.java +++ b/src/code/Business_logic/Deposit.java @@ -1,9 +1,14 @@ +package code.Business_logic; +import code.Database.BankDatabase; +import code.GUI.DepositSlot; // Deposit.java // Represents a deposit ATM transaction +import code.GUI.Keypad; +import code.GUI.Screen; 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 +35,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.getValore() != CANCELED ) { // request deposit envelope containing specified amount screen.displayMessage( @@ -65,7 +70,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 @@ -75,12 +80,7 @@ private double promptForDepositAmount() int input = keypad.getInput(); // receive input of deposit amount // check whether the user canceled or entered a valid amount - if ( input == CANCELED ) - return CANCELED; - else - { - return ( double ) input / 100; // return dollar amount - } // end else + return new Euro(input == CANCELED ? CANCELED : (double) input / 100); } // end method promptForDepositAmount } // end class Deposit diff --git a/src/code/Business_logic/Euro.java b/src/code/Business_logic/Euro.java new file mode 100644 index 0000000..3ad9b06 --- /dev/null +++ b/src/code/Business_logic/Euro.java @@ -0,0 +1,48 @@ +package code.Business_logic; + +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/Transaction.java b/src/code/Business_logic/Transaction.java similarity index 96% rename from src/Transaction.java rename to src/code/Business_logic/Transaction.java index 508fea8..5256dca 100644 --- a/src/Transaction.java +++ b/src/code/Business_logic/Transaction.java @@ -1,6 +1,10 @@ +package code.Business_logic; // Transaction.java // Abstract superclass Transaction represents an ATM transaction +import code.Database.BankDatabase; +import code.GUI.Screen; + public abstract class Transaction { private int accountNumber; // indicates account involved diff --git a/src/Withdrawal.java b/src/code/Business_logic/Withdrawal.java similarity index 90% rename from src/Withdrawal.java rename to src/code/Business_logic/Withdrawal.java index 6e0af62..2694455 100644 --- a/src/Withdrawal.java +++ b/src/code/Business_logic/Withdrawal.java @@ -1,6 +1,12 @@ +package code.Business_logic; // Withdrawal.java // Represents a withdrawal ATM transaction +import code.Database.BankDatabase; +import code.GUI.CashDispenser; +import code.GUI.Keypad; +import code.GUI.Screen; + 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 @@ -99,11 +105,11 @@ private int displayMenuOfAmounts() { // display the menu screen.displayMessageLine( "\nWithdrawal Menu:" ); - screen.displayMessageLine( "1 - $20" ); - screen.displayMessageLine( "2 - $40" ); - screen.displayMessageLine( "3 - $60" ); - screen.displayMessageLine( "4 - $100" ); - screen.displayMessageLine( "5 - $200" ); + screen.displayMessageLine( "1 - €20" ); + screen.displayMessageLine( "2 - €40" ); + screen.displayMessageLine( "3 - €60" ); + screen.displayMessageLine( "4 - €100" ); + screen.displayMessageLine( "5 - €200" ); screen.displayMessageLine( "6 - Cancel transaction" ); screen.displayMessage( "\nChoose a withdrawal amount: " ); diff --git a/src/Account.java b/src/code/Database/Account.java similarity index 75% rename from src/Account.java rename to src/code/Database/Account.java index c308eca..2c29e50 100644 --- a/src/Account.java +++ b/src/code/Database/Account.java @@ -1,16 +1,19 @@ +package code.Database; // Account.java // Represents a bank account +import code.Business_logic.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, - double theAvailableBalance, double theTotalBalance ) + Euro theAvailableBalance, Euro theTotalBalance ) { accountNumber = theAccountNumber; pin = thePIN; @@ -21,35 +24,32 @@ public Account( int theAccountNumber, int thePIN, // determines whether a user-specified PIN matches PIN in Account public boolean validatePIN( int userPIN ) { - if ( userPIN == pin ) - return true; - else - return false; + return userPIN == pin; } // 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 + this.totalBalance = this.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 + this.availableBalance = this.availableBalance.sottrai(amount); // subtract from available balance + this.totalBalance = this.totalBalance.sottrai(amount); // subtract from total balance } // end method debit // returns account number diff --git a/src/BankDatabase.java b/src/code/Database/BankDatabase.java similarity index 87% rename from src/BankDatabase.java rename to src/code/Database/BankDatabase.java index 3978497..57fe948 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.Business_logic.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( 12345, 54321, new Euro(1000.0), new Euro(1200.0)); + accounts[ 1 ] = new Account( 98765, 56789, new Euro(200.0), new Euro(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/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..0a49a19 100644 --- a/src/ATM.java +++ b/src/code/GUI/ATM.java @@ -1,6 +1,13 @@ +package code.GUI; // ATM.java // Represents an automated teller machine +import code.Business_logic.BalanceInquiry; +import code.Business_logic.Deposit; +import code.Business_logic.Transaction; +import code.Business_logic.Withdrawal; +import code.Database.BankDatabase; + public class ATM { private boolean userAuthenticated; // whether user is authenticated diff --git a/src/CashDispenser.java b/src/code/GUI/CashDispenser.java similarity index 93% rename from src/CashDispenser.java rename to src/code/GUI/CashDispenser.java index b249faf..e3f3d91 100644 --- a/src/CashDispenser.java +++ b/src/code/GUI/CashDispenser.java @@ -1,3 +1,4 @@ +package code.GUI; // CashDispenser.java // Represents the cash dispenser of the ATM @@ -25,10 +26,7 @@ public boolean isSufficientCashAvailable( int amount ) { int billsRequired = amount / 20; // number of $20 bills required - if ( count >= billsRequired ) - return true; // enough bills available - else - return false; // not enough bills available + return count >= billsRequired; } // end method isSufficientCashAvailable } // end class CashDispenser diff --git a/src/DepositSlot.java b/src/code/GUI/DepositSlot.java similarity index 98% rename from src/DepositSlot.java rename to src/code/GUI/DepositSlot.java index 64e02c2..bde0339 100644 --- a/src/DepositSlot.java +++ b/src/code/GUI/DepositSlot.java @@ -1,3 +1,4 @@ +package code.GUI; // DepositSlot.java // Represents the deposit slot of the ATM 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..f0f716c 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 91% rename from src/Screen.java rename to src/code/GUI/Screen.java index 44d3f30..78d159d 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.Business_logic.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.println(amount.stampa()); } // end method displayDollarAmount } // end class Screen diff --git a/src/test/TestAccount.java b/src/test/TestAccount.java new file mode 100644 index 0000000..4f7a569 --- /dev/null +++ b/src/test/TestAccount.java @@ -0,0 +1,54 @@ +package test; + +import org.junit.Test; +import static org.junit.Assert.*; +import code.Business_logic.Euro; +import code.Database.Account; + +public class TestAccount { + @Test + public void testAccountConstructorAndGetters() { + int accountNumber = 12345; + int pin = 54321; + Euro availableBalance = new Euro(100, 0); + Euro totalBalance = new Euro(200, 0); + Account account = new Account(accountNumber, pin, availableBalance, totalBalance); + + assertEquals(accountNumber, account.getAccountNumber()); + assertTrue(account.validatePIN(pin)); + assertEquals(availableBalance.getValore(), account.getAvailableBalance().getValore()); + assertEquals(totalBalance.getValore(), account.getTotalBalance().getValore()); + } + + @Test + public void testValidatePIN() { + Account account = new Account(12345, 6789, new Euro(100, 0), new Euro(200, 0)); + assertTrue(account.validatePIN(6789)); + assertFalse(account.validatePIN(1234)); + } + + @Test + public void testCredit() { + Euro availableBalance = new Euro(100, 0); + Euro totalBalance = new Euro(200, 0); + Account account = new Account(12345, 6789, availableBalance, totalBalance); + + Euro creditAmount = new Euro(50, 0); + account.credit(creditAmount); + + assertEquals(new Euro(250, 0).getValore(), account.getTotalBalance().getValore()); + } + + @Test + public void testDebit() { + Euro availableBalance = new Euro(500, 0); + Euro totalBalance = new Euro(1000, 0); + Account account = new Account(12345, 6789, availableBalance, totalBalance); + + Euro debitAmount = new Euro(50, 0); + account.debit(debitAmount); + + assertEquals(new Euro(450, 0).getValore(), account.getAvailableBalance().getValore()); + assertEquals(new Euro(950, 0).getValore(), account.getTotalBalance().getValore()); + } +} \ No newline at end of file diff --git a/src/test/TestBankDatabase.java b/src/test/TestBankDatabase.java new file mode 100644 index 0000000..9b70d8a --- /dev/null +++ b/src/test/TestBankDatabase.java @@ -0,0 +1,70 @@ +package test; + +import org.junit.Test; +import static org.junit.Assert.*; + +import org.junit.After; +import org.junit.Before; + +import code.Business_logic.Euro; +import code.Database.Account; +import code.Database.BankDatabase; + +public class TestBankDatabase { + + private Account accounts[]; + private BankDatabase bankDatabase; + + @Before + public void setUp(){ + accounts = new Account[ 2 ]; // just 2 accounts for testing + accounts[ 0 ] = new Account( 12345, 54321, new Euro(1000.0), new Euro(1200.0)); + accounts[ 1 ] = new Account( 98765, 56789, new Euro(200.0), new Euro(200.0)); + bankDatabase = new BankDatabase(); + } + + @After + public void tearDown(){ + accounts = null; + bankDatabase = null; + } + + @Test + public void testBankDatabaseAuthenticateUserSuccess(){ + assertTrue(bankDatabase.authenticateUser(accounts[0].getAccountNumber(), 54321)); + assertTrue(bankDatabase.authenticateUser(accounts[1].getAccountNumber(), 56789)); + } + + @Test + public void testBankDatabaseAuthenticateUserFailure(){ + assertFalse(bankDatabase.authenticateUser(accounts[0].getAccountNumber(), 6666)); + assertFalse(bankDatabase.authenticateUser(accounts[1].getAccountNumber(), 6666)); + } + + @Test + public void testBankDatabaseGetAvailableBalance(){ + assertEquals(accounts[0].getAvailableBalance().getValore(), bankDatabase.getAvailableBalance(accounts[0].getAccountNumber()).getValore()); + assertEquals(accounts[1].getAvailableBalance().getValore(), bankDatabase.getAvailableBalance(accounts[1].getAccountNumber()).getValore()); + } + + @Test + public void testBankDatabaseGetTotalBalance(){ + assertEquals(accounts[0].getTotalBalance().getValore(), bankDatabase.getTotalBalance(accounts[0].getAccountNumber()).getValore()); + assertEquals(accounts[1].getTotalBalance().getValore(), bankDatabase.getTotalBalance(accounts[1].getAccountNumber()).getValore()); + } + + @Test + public void testBankDatabaseCredit(){ + Euro creditAmount = new Euro(50, 0); + bankDatabase.credit(accounts[0].getAccountNumber(), creditAmount); + assertEquals(new Euro(1250, 0).getValore(), bankDatabase.getTotalBalance(accounts[0].getAccountNumber()).getValore()); + } + + @Test + public void testBankDatabaseDebit(){ + Euro debitAmount = new Euro(50, 0); + bankDatabase.debit(accounts[0].getAccountNumber(), debitAmount); + assertEquals(new Euro(950, 0).getValore(), bankDatabase.getAvailableBalance(accounts[0].getAccountNumber()).getValore()); + assertEquals(new Euro(1150, 0).getValore(), bankDatabase.getTotalBalance(accounts[0].getAccountNumber()).getValore()); + } +} \ No newline at end of file diff --git a/src/test/TestEuro.java b/src/test/TestEuro.java new file mode 100644 index 0000000..80eed98 --- /dev/null +++ b/src/test/TestEuro.java @@ -0,0 +1,75 @@ +package test; + +import org.junit.Test; +import static org.junit.Assert.*; +import code.Business_logic.Euro; + +public class TestEuro { + @Test + public void testEuroConstructorAndGetValore() { + Euro euro = new Euro(50, 20); + assertEquals(5020, euro.getValore()); + } + + @Test + public void testEuroConstructorNegativeAndGetValore() { + Euro euro = new Euro(-50, 20); + assertEquals(-5020, euro.getValore()); + } + + @Test + public void testEuroDoubleConstructorAndGetValore() { + Euro euro = new Euro(50.20); + assertEquals(5020, euro.getValore()); + } + + @Test + public void testSomma() { + Euro euro1 = new Euro(50, 20); + Euro euro2 = new Euro(30, 10); + euro1.somma(euro2); + assertEquals(8030, euro1.getValore()); + } + + @Test + public void testSottrai() { + Euro euro1 = new Euro(50, 20); + Euro euro2 = new Euro(30, 10); + euro1.sottrai(euro2); + assertEquals(2010, euro1.getValore()); + } + + @Test + public void testUgualeA() { + Euro euro1 = new Euro(50, 20); + Euro euro2 = new Euro(50, 20); + assertTrue(euro1.ugualeA(euro2)); + } + + @Test + public void testUgualeAFailure() { + Euro euro1 = new Euro(50, 20); + Euro euro2 = new Euro(30, 30); + assertFalse(euro1.ugualeA(euro2)); + } + + @Test + public void testMinoreDi() { + Euro euro1 = new Euro(50, 20); + Euro euro2 = new Euro(60, 20); + assertTrue(euro1.minoreDi(euro2)); + } + + @Test + public void testMinoreDiFailure() { + Euro euro1 = new Euro(50, 20); + Euro euro2 = new Euro(10, 10); + assertFalse(euro1.minoreDi(euro2)); + } + + @Test + public void testStampa() { + Euro euro = new Euro(50, 20); + assertEquals("50.2 euro", euro.stampa()); + } +}