diff --git a/bin/code/Euro.class b/bin/code/Euro.class new file mode 100644 index 0000000..e6672d2 Binary files /dev/null and b/bin/code/Euro.class differ diff --git a/componenti.txt b/componenti.txt new file mode 100644 index 0000000..77290e4 --- /dev/null +++ b/componenti.txt @@ -0,0 +1 @@ +Federica Caleffi \ No newline at end of file diff --git a/src/ATMCaseStudy.java b/src/Business_logic/ATMCaseStudy.java similarity index 97% rename from src/ATMCaseStudy.java rename to src/Business_logic/ATMCaseStudy.java index b9033bd..15b839f 100644 --- a/src/ATMCaseStudy.java +++ b/src/Business_logic/ATMCaseStudy.java @@ -1,6 +1,9 @@ +package Business_logic; // ATMCaseStudy.java // Driver program for the ATM case study +import GUI.ATM; + public class ATMCaseStudy { // main method creates and runs the ATM diff --git a/src/DepositSlot.java b/src/Business_logic/DepositSlot.java similarity index 98% rename from src/DepositSlot.java rename to src/Business_logic/DepositSlot.java index 64e02c2..f2c8e18 100644 --- a/src/DepositSlot.java +++ b/src/Business_logic/DepositSlot.java @@ -1,3 +1,4 @@ +package Business_logic; // DepositSlot.java // Represents the deposit slot of the ATM diff --git a/src/Business_logic/Euro.java b/src/Business_logic/Euro.java new file mode 100644 index 0000000..26da910 --- /dev/null +++ b/src/Business_logic/Euro.java @@ -0,0 +1,48 @@ +package 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"; + } +} \ No newline at end of file diff --git a/src/Transaction.java b/src/Business_logic/Transaction.java similarity index 96% rename from src/Transaction.java rename to src/Business_logic/Transaction.java index 508fea8..89d21b5 100644 --- a/src/Transaction.java +++ b/src/Business_logic/Transaction.java @@ -1,6 +1,10 @@ +package Business_logic; // Transaction.java // Abstract superclass Transaction represents an ATM transaction +import Database.BankDatabase; +import GUI.Screen; + public abstract class Transaction { private int accountNumber; // indicates account involved diff --git a/src/Account.java b/src/Database/Account.java similarity index 79% rename from src/Account.java rename to src/Database/Account.java index c308eca..d77339d 100644 --- a/src/Account.java +++ b/src/Database/Account.java @@ -1,16 +1,19 @@ +package Database; // Account.java // Represents a bank account +import 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; @@ -28,28 +31,28 @@ 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 + availableBalance.sottrai(amount); // subtract from available balance + totalBalance.sottrai(amount); // subtract from total balance } // end method debit // returns account number diff --git a/src/BankDatabase.java b/src/Database/BankDatabase.java similarity index 87% rename from src/BankDatabase.java rename to src/Database/BankDatabase.java index 3978497..b002579 100644 --- a/src/BankDatabase.java +++ b/src/Database/BankDatabase.java @@ -1,6 +1,9 @@ +package Database; // BankDatabase.java // Represents the bank account information database +import 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/GUI/ATM.java similarity index 98% rename from src/ATM.java rename to src/GUI/ATM.java index aa3d187..88fea8a 100644 --- a/src/ATM.java +++ b/src/GUI/ATM.java @@ -1,6 +1,11 @@ +package GUI; // ATM.java // Represents an automated teller machine +import Business_logic.DepositSlot; +import Business_logic.Transaction; +import Database.BankDatabase; + public class ATM { private boolean userAuthenticated; // whether user is authenticated diff --git a/src/BalanceInquiry.java b/src/GUI/BalanceInquiry.java similarity index 93% rename from src/BalanceInquiry.java rename to src/GUI/BalanceInquiry.java index d45fa6a..3c99221 100644 --- a/src/BalanceInquiry.java +++ b/src/GUI/BalanceInquiry.java @@ -1,6 +1,11 @@ +package GUI; // BalanceInquiry.java // Represents a balance inquiry ATM transaction +import Business_logic.Euro; +import Business_logic.Transaction; +import Database.BankDatabase; + 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/CashDispenser.java b/src/GUI/CashDispenser.java similarity index 99% rename from src/CashDispenser.java rename to src/GUI/CashDispenser.java index b249faf..0687b40 100644 --- a/src/CashDispenser.java +++ b/src/GUI/CashDispenser.java @@ -1,3 +1,4 @@ +package GUI; // CashDispenser.java // Represents the cash dispenser of the ATM diff --git a/src/Deposit.java b/src/GUI/Deposit.java similarity index 90% rename from src/Deposit.java rename to src/GUI/Deposit.java index 916ef70..0d1cbb0 100644 --- a/src/Deposit.java +++ b/src/GUI/Deposit.java @@ -1,9 +1,15 @@ +package GUI; // Deposit.java // Represents a deposit ATM transaction +import Business_logic.DepositSlot; +import Business_logic.Euro; +import Business_logic.Transaction; +import Database.BankDatabase; + 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.getValore() != 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(( double ) 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/GUI/Keypad.java similarity index 99% rename from src/Keypad.java rename to src/GUI/Keypad.java index cd035c7..6833a46 100644 --- a/src/Keypad.java +++ b/src/GUI/Keypad.java @@ -1,3 +1,4 @@ +package 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/GUI/Screen.java similarity index 94% rename from src/Screen.java rename to src/GUI/Screen.java index 44d3f30..4f0990e 100644 --- a/src/Screen.java +++ b/src/GUI/Screen.java @@ -1,6 +1,9 @@ +package GUI; // Screen.java // Represents the screen of the ATM +import Business_logic.Euro; + public class Screen { // displays a message without a carriage return @@ -16,7 +19,7 @@ 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 ); } // end method displayDollarAmount diff --git a/src/Withdrawal.java b/src/GUI/Withdrawal.java similarity index 95% rename from src/Withdrawal.java rename to src/GUI/Withdrawal.java index 6e0af62..2208487 100644 --- a/src/Withdrawal.java +++ b/src/GUI/Withdrawal.java @@ -1,6 +1,11 @@ +package GUI; // Withdrawal.java // Represents a withdrawal ATM transaction +import Business_logic.Euro; +import Business_logic.Transaction; +import Database.BankDatabase; + public class Withdrawal extends Transaction { private int amount; // amount to withdraw @@ -27,7 +32,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 +52,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/TestEuro.java b/src/test/TestEuro.java new file mode 100644 index 0000000..5fdbf48 --- /dev/null +++ b/src/test/TestEuro.java @@ -0,0 +1,41 @@ +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; + +class EuroTest { + + @Test + void testSomma() { + Euro euro1 = new Euro(100); + Euro euro2 = new Euro(200); + Euro result = euro1.somma(euro2); + assertEquals(300, result.getValore()); + } + + @Test + void testSottrai() { + Euro euro1 = new Euro(200); + Euro euro2 = new Euro(100); + Euro result = euro1.sottrai(euro2); + assertEquals(100, result.getValore()); + } + + @Test + void testUgualeA() { + Euro euro1 = new Euro(100); + Euro euro2 = new Euro(100); + assertTrue(euro1.ugualeA(euro2)); + } + + @Test + void testMinoreDi() { + Euro euro1 = new Euro(100); + Euro euro2 = new Euro(200); + assertTrue(euro1.minoreDi(euro2)); + } + + @Test + void testStampa() { + Euro euro = new Euro(100); + assertEquals("1.0 euro", euro.stampa()); + } +} \ No newline at end of file