From 2861b39bd2a68d2148af40cabed18083cd74b6ce Mon Sep 17 00:00:00 2001 From: Johao Date: Tue, 18 Jun 2024 17:03:36 -0500 Subject: [PATCH 1/4] solucion OCP 2 --- .idea/.gitignore | 8 ++ .idea/misc.xml | 5 + .idea/modules.xml | 8 ++ .idea/vcs.xml | 6 ++ BankApllication.iml | 11 +++ BankApplication.java | 101 ++++++++------------ BankCommand.java | 5 + BankingOperation.java | 40 +++++++- BankingOperation_q.java | 5 + CreateNewBankAccountCommand.java | 14 +++ CreateNewBankAccountOperation.java | 144 +++++++++++++++++++++++++++++ DepositOperation.java | 53 +++++++++++ DepositUserMoneyCommand.java | 14 +++ WithdrawUserMoneyCommand.java | 14 +++ 14 files changed, 365 insertions(+), 63 deletions(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 BankApllication.iml create mode 100644 BankCommand.java create mode 100644 BankingOperation_q.java create mode 100644 CreateNewBankAccountCommand.java create mode 100644 CreateNewBankAccountOperation.java create mode 100644 DepositOperation.java create mode 100644 DepositUserMoneyCommand.java create mode 100644 WithdrawUserMoneyCommand.java diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..ef89d98 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..dfadd91 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/BankApllication.iml b/BankApllication.iml new file mode 100644 index 0000000..97ab6d1 --- /dev/null +++ b/BankApllication.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/BankApplication.java b/BankApplication.java index a919ca4..5cf5f8f 100644 --- a/BankApplication.java +++ b/BankApplication.java @@ -12,70 +12,47 @@ public class BankApplication { public static void main(String[] args) { @SuppressWarnings("resource") - Scanner input=new Scanner(System.in); - BankingOperation bankingoperation=new BankingOperation(); - UserInterest userinterest=new UserInterest(); - UserTransactionHistory usertransactionhistory=new UserTransactionHistory(); + Scanner input = new Scanner(System.in); + BankingOperation bankingOperation = new BankingOperation(); + UserInterest userInterest = new UserInterest(); + UserTransactionHistory userTransactionHistory = new UserTransactionHistory(); + + Map commands = new HashMap<>(); + commands.put("1", new CreateNewBankAccountCommand(bankingOperation)); + commands.put("2", new DepositUserMoneyCommand(bankingOperation)); + commands.put("3", new WithdrawUserMoneyCommand(bankingOperation)); + // Agregar otros comandos de manera similar... + System.out.println("Welcome to the Nitheesh's online banking service's :"); - while(true) - { - try - { - System.out.println("Enter the option for Banking operation"); - System.out.println("1.Create new Bank Account"); - System.out.println("2.Deposit Your Amount"); - System.out.println("3.Withraw Your Amount"); - System.out.println("4.To calculate Interest Amount for your Loan"); - System.out.println("5.To view Your Account Details"); - System.out.println("6.To view you're Transaction History"); - System.out.println("7.Exit the Banking operation"); - String Userchoice=input.next(); - switch(Userchoice) - { - case "1": - bankingoperation.CreateNewBankAccount(); - break; - case "2": - bankingoperation.DepositeUserMoney(); - break; - case "3": - bankingoperation.WithdrawUserMoney(); - break; - case "4": - System.out.println("Please choose the Below option's :\n1.Simple Interest\n2.Compound Interest\n3.Main menu\n4.Exit\n"); - String InterestOption=input.next(); - switch(InterestOption) - { - case "1": - userinterest.calculatesimpleinterest(); - break; - case "2": - userinterest.calculatecompoundinterest(); - break; - case "3": - break; - case "4": - System.exit(0); + + while (true) { + try { + System.out.println("Enter the option for Banking operation"); + System.out.println("1.Create new Bank Account"); + System.out.println("2.Deposit Your Amount"); + System.out.println("3.Withdraw Your Amount"); + System.out.println("4.To calculate Interest Amount for your Loan"); + System.out.println("5.To view Your Account Details"); + System.out.println("6.To view your Transaction History"); + System.out.println("7.Exit the Banking operation"); + + String userChoice = input.next(); + + if (userChoice.equals("7")) { + System.out.println("************ Thank you for using our service! Have a GREAT DAY **************"); + break; } - break; - case "5": - bankingoperation.DisplayUserAccountDetails(); - break; - case "6": - usertransactionhistory.usertransactionhistory(); - break; - case "7": - System.out.println("************ Thank you for using our service! Have a GREAT DAY **************"); - System.exit(0); - default: - throw new InValidInputException("Please Enter the correct choice.\n"); + + BankCommand command = commands.get(userChoice); + + if (command != null) { + command.execute(); + } else { + throw new InValidInputException("Please Enter the correct choice.\n"); + } + } catch (InValidInputException e) { + System.out.println("You entered an invalid choice.\n" + e); + } } - - } - catch(InValidInputException e) - { - System.out.println("You're enter the invalid choice.\n"+e); } - } - } } \ No newline at end of file diff --git a/BankCommand.java b/BankCommand.java new file mode 100644 index 0000000..26e31b5 --- /dev/null +++ b/BankCommand.java @@ -0,0 +1,5 @@ +package bankapplication; + +public interface BankCommand { + void execute(); +} diff --git a/BankingOperation.java b/BankingOperation.java index 6d53fc1..d2974b1 100644 --- a/BankingOperation.java +++ b/BankingOperation.java @@ -276,4 +276,42 @@ public void DisplayUserAccountDetails() //To display the userdetails System.out.println("Invalid Account Number\n"); } } -} \ No newline at end of file + + + + public class BankApplication { + private Map operations = new HashMap<>(); + private Scanner input; + + public BankApplication(Scanner input) { + this.input = input; + initializeOperations(); + } + + private void initializeOperations() { + operations.put("CreateAccount", new CreateNewBankAccountOperation(input)); + operations.put("Deposit", new DepositOperation(input)); + operations.put("Withdraw", new WithdrawOperation(input)); + operations.put("DisplayDetails", new DisplayAccountDetailsOperation(input)); + } + + public void executeOperation(String operationType) { + BankingOperation operation = operations.get(operationType); + if (operation != null) { + operation.execute(); + } else { + throw new UnsupportedOperationException("Operación no soportada: " + operationType); + } + } + + public static void main(String[] args) { + Scanner input = new Scanner(System.in); + BankApplication bankApplication = new BankApplication(input); + + // Ejemplo de cómo ejecutar una operación + System.out.println("Seleccione una operación: CreateAccount, Deposit, Withdraw, DisplayDetails"); + String operationType = input.next(); + bankApplication.executeOperation(operationType); + } + } + diff --git a/BankingOperation_q.java b/BankingOperation_q.java new file mode 100644 index 0000000..5d31dac --- /dev/null +++ b/BankingOperation_q.java @@ -0,0 +1,5 @@ +package bankapplication; + +public interface BankingOperation_q { + void execute(); +} \ No newline at end of file diff --git a/CreateNewBankAccountCommand.java b/CreateNewBankAccountCommand.java new file mode 100644 index 0000000..a9c4caf --- /dev/null +++ b/CreateNewBankAccountCommand.java @@ -0,0 +1,14 @@ +package bankapplication; + +public class CreateNewBankAccountCommand implements BankCommand { + private BankingOperation bankingOperation; + + public CreateNewBankAccountCommand(BankingOperation bankingOperation) { + this.bankingOperation = bankingOperation; + } + + @Override + public void execute() { + bankingOperation.CreateNewBankAccount(); + } +} \ No newline at end of file diff --git a/CreateNewBankAccountOperation.java b/CreateNewBankAccountOperation.java new file mode 100644 index 0000000..95f88b0 --- /dev/null +++ b/CreateNewBankAccountOperation.java @@ -0,0 +1,144 @@ +package bankapplication; + +import java.util.regex.Pattern; + +import static java.lang.System.exit; + +public class CreateNewBankAccountOperation implements BankingOperation_q { + + void CreateNewBankAccount() // To creating new user bank account + { + UserDetails userdetails=new UserDetails(); + System.out.println("---------------Enter Your Detail's------------------"); + flag=true; // Validating First Name + while(flag) + { + try + { + System.out.println("Enter your first Name :"); + String firstname=input.next(); + if(Pattern.matches("[A-Za-z]*",firstname)) + { + userdetails.setFirstname(firstname); + flag=false; + } + else + { + flag=true; + throw new InvalidFirstNameException("Please provide the correct First Name.\n"); + } + } + catch(InvalidFirstNameException e) + { + System.out.println("You're Entering the Invaild First Name.\n"+e); + } + } + flag=true; // Validating Last Name + while(flag) + { + try + { + System.out.println("Enter your Last Name :"); + String lastname=input.next(); + if(Pattern.matches("[A-Za-z]*",lastname)) + { + userdetails.setLastname(lastname); + flag=false; + } + else + { + flag=true; + throw new InvalidLastNameException("Please provide the correct Last Name.\n"); + } + } + catch(InvalidLastNameException e) + { + System.out.println("You're Entering the Invaild Last Name.\n"+e); + } + } + flag=true; // Validating Aadhar Number + while(flag) + { + try + { + String AadharRegex = "^[2-9]{1}[0-9]{3}[0-9]{4}[0-9]{4}$"; + System.out.println("Enter your Aadhar Number :"); + String Aadharnumber=input.next(); + if(Aadharnumber.matches(AadharRegex)) + { + userdetails.setAadharnumber(Aadharnumber); + flag=false; + } + else + { + flag=true; + throw new InvalidAadharException("Please provide the correct Aadhar Number.\n"); + } + } + catch(InvalidAadharException e) + { + System.out.println("You're Entering the Invaild Aadhar Number.\n"+e); + } + } + flag=true; // Validating Age + while(flag) + { + System.out.println("Enter your Age :"); + int age=input.nextInt(); + try + { + if(age>=18) + { + userdetails.setAge(age); + flag=false; + } + else if((age>=1) && (age<=17)) + { + System.out.println("You're in under 18, so not eligible to create Bank Account. please Contact You're nearest branch.\n************ Thank you for using our service! Have a GREAT DAY **************"); + exit(0); + } + else + { + flag=true; + throw new InvalidAgeException("Please provide the correct age.\n"); + } + } + catch(InvalidAgeException e) + { + System.out.println("You're Entering the Invaild age.\n"+e); + } + } + flag=true; // Validating Mobile Number + while(flag) + { + try + { + String MobileNumberregex = "[6-9]{1}[0-9]{9}"; //(0/91)? + System.out.println("Enter your Mobile Number :"); + String mobilenumber=input.next(); + if(mobilenumber.length()==10 && mobilenumber.matches(MobileNumberregex)) + { + userdetails.setMobilenumber(mobilenumber); + flag=false; + } + else + { + flag=true; + throw new InvalidMobileNumberException("Please provide the correct Mobile Number.\n"); + } + } + catch(InvalidMobileNumberException e) + { + System.out.println("You're Entering the Invaild MoblieNumber.\n"+e); + } + } + //System.out.println("For new account opening, Please deposite the Minimum Balance Amount 1000 in your account:"); + userdetails.setBalanceamount(0); + userdetails.setAccountnumber(); + UserDetailSet.add(userdetails); + System.out.println(".....................Your's New Bank account was successfully created......................"); + System.out.println("Your Account Number is : "+userdetails.getAccountnumber()); + System.out.println("Your Account Balance is : "+userdetails.getBalanceamount()); + System.out.println("Your Account Balance is very low, So please depoite money for further transaction's.\n"); + } +} diff --git a/DepositOperation.java b/DepositOperation.java new file mode 100644 index 0000000..4a08cff --- /dev/null +++ b/DepositOperation.java @@ -0,0 +1,53 @@ +package bankapplication; + +import java.util.Iterator; +import java.util.regex.Pattern; + +public class DepositOperation implements BankingOperation_q { + +{ + int Depositeflag = 0; + //Pattern pattern=Pattern.compile("[a-zA-Z]*"); + System.out.println("Enter Account number: "); + long accountNumber = input.nextLong(); + String ConvertAccountNumber=String.valueOf(accountNumber); + if(!(Pattern.matches("[a-zA-Z]",ConvertAccountNumber))) + { + Iterator iterator = UserDetailSet.iterator(); + UserDetails userdetails; + while(iterator.hasNext()) + { + userdetails = (UserDetails) iterator.next(); + if(userdetails.getAccountnumber()== accountNumber) + { + Depositeflag = 1; + System.out.println("Enter Deposit amount: "); + int depositAmount = input.nextInt(); + userdetails.setBalanceamount(userdetails.getBalanceamount()+depositAmount); + System.out.println("Successfully money deposited to your account number "+userdetails.getAccountnumber()+".\n"); + History history = new History(); + history.CreditAmount = depositAmount; + history.TotalAmount = userdetails.getBalanceamount(); + if(userdetails.TransactionHistory.size()>=5) + { + userdetails.TransactionHistory.remove(); + userdetails.TransactionHistory.add(history); + } + else + { + userdetails.TransactionHistory.add(history); + } + break; + } + } + if(Depositeflag == 0) + { + System.out.println("Account Number Not Created.\n"); + } + } + else + { + System.out.println("You're enter the invalid Account Number.\n"); + } +} +} diff --git a/DepositUserMoneyCommand.java b/DepositUserMoneyCommand.java new file mode 100644 index 0000000..c509958 --- /dev/null +++ b/DepositUserMoneyCommand.java @@ -0,0 +1,14 @@ +package bankapplication; + +public class DepositUserMoneyCommand implements BankCommand { + private BankingOperation bankingOperation; + + public DepositUserMoneyCommand(BankingOperation bankingOperation) { + this.bankingOperation = bankingOperation; + } + + @Override + public void execute() { + bankingOperation.DepositeUserMoney(); + } +} diff --git a/WithdrawUserMoneyCommand.java b/WithdrawUserMoneyCommand.java new file mode 100644 index 0000000..0f24610 --- /dev/null +++ b/WithdrawUserMoneyCommand.java @@ -0,0 +1,14 @@ +package bankapplication; + +public class WithdrawUserMoneyCommand implements BankCommand { + private BankingOperation bankingOperation; + + public WithdrawUserMoneyCommand(BankingOperation bankingOperation) { + this.bankingOperation = bankingOperation; + } + + @Override + public void execute() { + bankingOperation.WithdrawUserMoney(); + } +} \ No newline at end of file From a19786fa2ee357030740ee17d4686596f4a1d9ed Mon Sep 17 00:00:00 2001 From: Johao Date: Tue, 18 Jun 2024 17:58:58 -0500 Subject: [PATCH 2/4] DIP 1 --- .idea/misc.xml | 3 +- BankApplication.java | 101 +++++---- BankCommand.java | 5 - BankingOperation.java | 318 +++-------------------------- BankingOperation_q.java | 5 - CreateNewBankAccountCommand.java | 14 -- CreateNewBankAccountOperation.java | 144 ------------- DepositOperation.java | 53 ----- DepositUserMoneyCommand.java | 14 -- InputProvider.java | 10 + UserDetailsService.java | 8 + WithdrawUserMoneyCommand.java | 14 -- 12 files changed, 107 insertions(+), 582 deletions(-) delete mode 100644 BankCommand.java delete mode 100644 BankingOperation_q.java delete mode 100644 CreateNewBankAccountCommand.java delete mode 100644 CreateNewBankAccountOperation.java delete mode 100644 DepositOperation.java delete mode 100644 DepositUserMoneyCommand.java create mode 100644 InputProvider.java create mode 100644 UserDetailsService.java delete mode 100644 WithdrawUserMoneyCommand.java diff --git a/.idea/misc.xml b/.idea/misc.xml index ef89d98..639900d 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,5 +1,6 @@ + - + \ No newline at end of file diff --git a/BankApplication.java b/BankApplication.java index 5cf5f8f..a919ca4 100644 --- a/BankApplication.java +++ b/BankApplication.java @@ -12,47 +12,70 @@ public class BankApplication { public static void main(String[] args) { @SuppressWarnings("resource") - Scanner input = new Scanner(System.in); - BankingOperation bankingOperation = new BankingOperation(); - UserInterest userInterest = new UserInterest(); - UserTransactionHistory userTransactionHistory = new UserTransactionHistory(); - - Map commands = new HashMap<>(); - commands.put("1", new CreateNewBankAccountCommand(bankingOperation)); - commands.put("2", new DepositUserMoneyCommand(bankingOperation)); - commands.put("3", new WithdrawUserMoneyCommand(bankingOperation)); - // Agregar otros comandos de manera similar... - + Scanner input=new Scanner(System.in); + BankingOperation bankingoperation=new BankingOperation(); + UserInterest userinterest=new UserInterest(); + UserTransactionHistory usertransactionhistory=new UserTransactionHistory(); System.out.println("Welcome to the Nitheesh's online banking service's :"); - - while (true) { - try { - System.out.println("Enter the option for Banking operation"); - System.out.println("1.Create new Bank Account"); - System.out.println("2.Deposit Your Amount"); - System.out.println("3.Withdraw Your Amount"); - System.out.println("4.To calculate Interest Amount for your Loan"); - System.out.println("5.To view Your Account Details"); - System.out.println("6.To view your Transaction History"); - System.out.println("7.Exit the Banking operation"); - - String userChoice = input.next(); - - if (userChoice.equals("7")) { - System.out.println("************ Thank you for using our service! Have a GREAT DAY **************"); - break; + while(true) + { + try + { + System.out.println("Enter the option for Banking operation"); + System.out.println("1.Create new Bank Account"); + System.out.println("2.Deposit Your Amount"); + System.out.println("3.Withraw Your Amount"); + System.out.println("4.To calculate Interest Amount for your Loan"); + System.out.println("5.To view Your Account Details"); + System.out.println("6.To view you're Transaction History"); + System.out.println("7.Exit the Banking operation"); + String Userchoice=input.next(); + switch(Userchoice) + { + case "1": + bankingoperation.CreateNewBankAccount(); + break; + case "2": + bankingoperation.DepositeUserMoney(); + break; + case "3": + bankingoperation.WithdrawUserMoney(); + break; + case "4": + System.out.println("Please choose the Below option's :\n1.Simple Interest\n2.Compound Interest\n3.Main menu\n4.Exit\n"); + String InterestOption=input.next(); + switch(InterestOption) + { + case "1": + userinterest.calculatesimpleinterest(); + break; + case "2": + userinterest.calculatecompoundinterest(); + break; + case "3": + break; + case "4": + System.exit(0); } - - BankCommand command = commands.get(userChoice); - - if (command != null) { - command.execute(); - } else { - throw new InValidInputException("Please Enter the correct choice.\n"); - } - } catch (InValidInputException e) { - System.out.println("You entered an invalid choice.\n" + e); - } + break; + case "5": + bankingoperation.DisplayUserAccountDetails(); + break; + case "6": + usertransactionhistory.usertransactionhistory(); + break; + case "7": + System.out.println("************ Thank you for using our service! Have a GREAT DAY **************"); + System.exit(0); + default: + throw new InValidInputException("Please Enter the correct choice.\n"); } + + } + catch(InValidInputException e) + { + System.out.println("You're enter the invalid choice.\n"+e); } + } + } } \ No newline at end of file diff --git a/BankCommand.java b/BankCommand.java deleted file mode 100644 index 26e31b5..0000000 --- a/BankCommand.java +++ /dev/null @@ -1,5 +0,0 @@ -package bankapplication; - -public interface BankCommand { - void execute(); -} diff --git a/BankingOperation.java b/BankingOperation.java index d2974b1..1cb9bf9 100644 --- a/BankingOperation.java +++ b/BankingOperation.java @@ -14,304 +14,36 @@ import java.util.Scanner; import java.util.regex.Pattern; -class BankingOperation implements Transaction -{ - boolean flag; - Scanner input=new Scanner(System.in); - static HashSetUserDetailSet=new HashSet<>(); - void CreateNewBankAccount() // To creating new user bank account - { - UserDetails userdetails=new UserDetails(); - System.out.println("---------------Enter Your Detail's------------------"); - flag=true; // Validating First Name - while(flag) - { - try - { - System.out.println("Enter your first Name :"); - String firstname=input.next(); - if(Pattern.matches("[A-Za-z]*",firstname)) - { - userdetails.setFirstname(firstname); - flag=false; - } - else - { - flag=true; - throw new InvalidFirstNameException("Please provide the correct First Name.\n"); - } - } - catch(InvalidFirstNameException e) - { - System.out.println("You're Entering the Invaild First Name.\n"+e); - } - } - flag=true; // Validating Last Name - while(flag) - { - try - { - System.out.println("Enter your Last Name :"); - String lastname=input.next(); - if(Pattern.matches("[A-Za-z]*",lastname)) - { - userdetails.setLastname(lastname); - flag=false; - } - else - { - flag=true; - throw new InvalidLastNameException("Please provide the correct Last Name.\n"); - } - } - catch(InvalidLastNameException e) - { - System.out.println("You're Entering the Invaild Last Name.\n"+e); - } - } - flag=true; // Validating Aadhar Number - while(flag) - { - try - { - String AadharRegex = "^[2-9]{1}[0-9]{3}[0-9]{4}[0-9]{4}$"; - System.out.println("Enter your Aadhar Number :"); - String Aadharnumber=input.next(); - if(Aadharnumber.matches(AadharRegex)) - { - userdetails.setAadharnumber(Aadharnumber); - flag=false; - } - else - { - flag=true; - throw new InvalidAadharException("Please provide the correct Aadhar Number.\n"); - } - } - catch(InvalidAadharException e) - { - System.out.println("You're Entering the Invaild Aadhar Number.\n"+e); - } - } - flag=true; // Validating Age - while(flag) - { - System.out.println("Enter your Age :"); - int age=input.nextInt(); - try - { - if(age>=18) - { - userdetails.setAge(age); - flag=false; - } - else if((age>=1) && (age<=17)) - { - System.out.println("You're in under 18, so not eligible to create Bank Account. please Contact You're nearest branch.\n************ Thank you for using our service! Have a GREAT DAY **************"); - exit(0); - } - else - { - flag=true; - throw new InvalidAgeException("Please provide the correct age.\n"); - } - } - catch(InvalidAgeException e) - { - System.out.println("You're Entering the Invaild age.\n"+e); - } - } - flag=true; // Validating Mobile Number - while(flag) - { - try - { - String MobileNumberregex = "[6-9]{1}[0-9]{9}"; //(0/91)? - System.out.println("Enter your Mobile Number :"); - String mobilenumber=input.next(); - if(mobilenumber.length()==10 && mobilenumber.matches(MobileNumberregex)) - { - userdetails.setMobilenumber(mobilenumber); - flag=false; - } - else - { - flag=true; - throw new InvalidMobileNumberException("Please provide the correct Mobile Number.\n"); - } - } - catch(InvalidMobileNumberException e) - { - System.out.println("You're Entering the Invaild MoblieNumber.\n"+e); - } - } - //System.out.println("For new account opening, Please deposite the Minimum Balance Amount 1000 in your account:"); - userdetails.setBalanceamount(0); - userdetails.setAccountnumber(); - UserDetailSet.add(userdetails); - System.out.println(".....................Your's New Bank account was successfully created......................"); - System.out.println("Your Account Number is : "+userdetails.getAccountnumber()); - System.out.println("Your Account Balance is : "+userdetails.getBalanceamount()); - System.out.println("Your Account Balance is very low, So please depoite money for further transaction's.\n"); +public class BankingOperation implements Transaction { + private boolean flag; + private InputProvider input; + private UserDetailsService userDetailsService; + + public BankingOperation(InputProvider input, UserDetailsService userDetailsService) { + this.input = input; + this.userDetailsService = userDetailsService; } - @Override - public void DepositeUserMoney() //To deposite money - { - int Depositeflag = 0; - //Pattern pattern=Pattern.compile("[a-zA-Z]*"); - System.out.println("Enter Account number: "); - long accountNumber = input.nextLong(); - String ConvertAccountNumber=String.valueOf(accountNumber); - if(!(Pattern.matches("[a-zA-Z]",ConvertAccountNumber))) - { - Iterator iterator = UserDetailSet.iterator(); - UserDetails userdetails; - while(iterator.hasNext()) - { - userdetails = (UserDetails) iterator.next(); - if(userdetails.getAccountnumber()== accountNumber) - { - Depositeflag = 1; - System.out.println("Enter Deposit amount: "); - int depositAmount = input.nextInt(); - userdetails.setBalanceamount(userdetails.getBalanceamount()+depositAmount); - System.out.println("Successfully money deposited to your account number "+userdetails.getAccountnumber()+".\n"); - History history = new History(); - history.CreditAmount = depositAmount; - history.TotalAmount = userdetails.getBalanceamount(); - if(userdetails.TransactionHistory.size()>=5) - { - userdetails.TransactionHistory.remove(); - userdetails.TransactionHistory.add(history); - } - else - { - userdetails.TransactionHistory.add(history); - } - break; - } - } - if(Depositeflag == 0) - { - System.out.println("Account Number Not Created.\n"); - } - } - else - { - System.out.println("You're enter the invalid Account Number.\n"); - } + + public void createNewBankAccount() { + UserDetails userDetails = new UserDetails(); + System.out.println("---------------Enter Your Details------------------"); + + // Código de validación y creación de cuenta omitido por brevedad + // Utiliza input para obtener datos y userDetailsService para guardar detalles de usuario } + @Override - public void WithdrawUserMoney() //To withdraw money - { - int Withdrawflag = 0; - System.out.println("Enter Account number: "); - int accountNumber = input.nextInt(); - Iterator iterator = UserDetailSet.iterator(); - UserDetails userdetails; - while(iterator.hasNext()) - { - userdetails = (UserDetails) iterator.next(); - if(userdetails.getAccountnumber()== accountNumber) - { - Withdrawflag = 1; - System.out.println("Enter withdraw amount: "); - int withdrawAmount = input.nextInt(); - if(userdetails.getBalanceamount()-withdrawAmount >= 600) - { - userdetails.setBalanceamount(userdetails.getBalanceamount()-withdrawAmount); - System.out.println("----------------Successfully withdrawn, Please collect your Cash----------------\n"); - History History = new History(); - History.DebitAmount = withdrawAmount; - History.TotalAmount = userdetails.getBalanceamount(); - if(userdetails.TransactionHistory.size()>=5) - { - userdetails.TransactionHistory.remove(); - userdetails.TransactionHistory.add(History); - } - else - { - userdetails.TransactionHistory.add(History); - } - } - else - { - System.out.println("Insufficient Balance\n"); - } - break; - } - } - if(Withdrawflag == 0) - { - System.out.println("Invalid Account Number\n"); - } + public void depositUserMoney() { + // Implementación de depósito utilizando userDetailsService } + @Override - public void DisplayUserAccountDetails() //To display the userdetails - { - int Displayflag=0; - System.out.println("Enter your Account number to display your bank details :"); - long AccountNumber=input.nextLong(); - Iterator iterator=UserDetailSet.iterator(); - UserDetails userdetails; - while(iterator.hasNext()) - { - userdetails=(UserDetails)iterator.next(); - if(userdetails.getAccountnumber()==AccountNumber) - { - Displayflag=1; - System.out.println("----------------Account Details-----------------"); - System.out.println("Account Number : "+userdetails.getAccountnumber()); - System.out.println("First Name : "+userdetails.getFirstname()); - System.out.println("Last Name : "+userdetails.getLastname()); - System.out.println("Age : "+userdetails.getAge()); - System.out.println("Phone Number: "+userdetails.getMobilenumber()); - //System.out.println("Aadhar Number: "+userdetails.getAadhar_number()); - System.out.println("Account Balance :"+userdetails.getBalanceamount()+"\n"); - break; - } - } - if(Displayflag==0) - { - System.out.println("Invalid Account Number\n"); - } + public void withdrawUserMoney() { + // Implementación de retiro utilizando userDetailsService } - - - public class BankApplication { - private Map operations = new HashMap<>(); - private Scanner input; - - public BankApplication(Scanner input) { - this.input = input; - initializeOperations(); - } - - private void initializeOperations() { - operations.put("CreateAccount", new CreateNewBankAccountOperation(input)); - operations.put("Deposit", new DepositOperation(input)); - operations.put("Withdraw", new WithdrawOperation(input)); - operations.put("DisplayDetails", new DisplayAccountDetailsOperation(input)); - } - - public void executeOperation(String operationType) { - BankingOperation operation = operations.get(operationType); - if (operation != null) { - operation.execute(); - } else { - throw new UnsupportedOperationException("Operación no soportada: " + operationType); - } - } - - public static void main(String[] args) { - Scanner input = new Scanner(System.in); - BankApplication bankApplication = new BankApplication(input); - - // Ejemplo de cómo ejecutar una operación - System.out.println("Seleccione una operación: CreateAccount, Deposit, Withdraw, DisplayDetails"); - String operationType = input.next(); - bankApplication.executeOperation(operationType); - } + @Override + public void displayUserAccountDetails() { + // Implementación de mostrar detalles de cuenta utilizando userDetailsService } - +} \ No newline at end of file diff --git a/BankingOperation_q.java b/BankingOperation_q.java deleted file mode 100644 index 5d31dac..0000000 --- a/BankingOperation_q.java +++ /dev/null @@ -1,5 +0,0 @@ -package bankapplication; - -public interface BankingOperation_q { - void execute(); -} \ No newline at end of file diff --git a/CreateNewBankAccountCommand.java b/CreateNewBankAccountCommand.java deleted file mode 100644 index a9c4caf..0000000 --- a/CreateNewBankAccountCommand.java +++ /dev/null @@ -1,14 +0,0 @@ -package bankapplication; - -public class CreateNewBankAccountCommand implements BankCommand { - private BankingOperation bankingOperation; - - public CreateNewBankAccountCommand(BankingOperation bankingOperation) { - this.bankingOperation = bankingOperation; - } - - @Override - public void execute() { - bankingOperation.CreateNewBankAccount(); - } -} \ No newline at end of file diff --git a/CreateNewBankAccountOperation.java b/CreateNewBankAccountOperation.java deleted file mode 100644 index 95f88b0..0000000 --- a/CreateNewBankAccountOperation.java +++ /dev/null @@ -1,144 +0,0 @@ -package bankapplication; - -import java.util.regex.Pattern; - -import static java.lang.System.exit; - -public class CreateNewBankAccountOperation implements BankingOperation_q { - - void CreateNewBankAccount() // To creating new user bank account - { - UserDetails userdetails=new UserDetails(); - System.out.println("---------------Enter Your Detail's------------------"); - flag=true; // Validating First Name - while(flag) - { - try - { - System.out.println("Enter your first Name :"); - String firstname=input.next(); - if(Pattern.matches("[A-Za-z]*",firstname)) - { - userdetails.setFirstname(firstname); - flag=false; - } - else - { - flag=true; - throw new InvalidFirstNameException("Please provide the correct First Name.\n"); - } - } - catch(InvalidFirstNameException e) - { - System.out.println("You're Entering the Invaild First Name.\n"+e); - } - } - flag=true; // Validating Last Name - while(flag) - { - try - { - System.out.println("Enter your Last Name :"); - String lastname=input.next(); - if(Pattern.matches("[A-Za-z]*",lastname)) - { - userdetails.setLastname(lastname); - flag=false; - } - else - { - flag=true; - throw new InvalidLastNameException("Please provide the correct Last Name.\n"); - } - } - catch(InvalidLastNameException e) - { - System.out.println("You're Entering the Invaild Last Name.\n"+e); - } - } - flag=true; // Validating Aadhar Number - while(flag) - { - try - { - String AadharRegex = "^[2-9]{1}[0-9]{3}[0-9]{4}[0-9]{4}$"; - System.out.println("Enter your Aadhar Number :"); - String Aadharnumber=input.next(); - if(Aadharnumber.matches(AadharRegex)) - { - userdetails.setAadharnumber(Aadharnumber); - flag=false; - } - else - { - flag=true; - throw new InvalidAadharException("Please provide the correct Aadhar Number.\n"); - } - } - catch(InvalidAadharException e) - { - System.out.println("You're Entering the Invaild Aadhar Number.\n"+e); - } - } - flag=true; // Validating Age - while(flag) - { - System.out.println("Enter your Age :"); - int age=input.nextInt(); - try - { - if(age>=18) - { - userdetails.setAge(age); - flag=false; - } - else if((age>=1) && (age<=17)) - { - System.out.println("You're in under 18, so not eligible to create Bank Account. please Contact You're nearest branch.\n************ Thank you for using our service! Have a GREAT DAY **************"); - exit(0); - } - else - { - flag=true; - throw new InvalidAgeException("Please provide the correct age.\n"); - } - } - catch(InvalidAgeException e) - { - System.out.println("You're Entering the Invaild age.\n"+e); - } - } - flag=true; // Validating Mobile Number - while(flag) - { - try - { - String MobileNumberregex = "[6-9]{1}[0-9]{9}"; //(0/91)? - System.out.println("Enter your Mobile Number :"); - String mobilenumber=input.next(); - if(mobilenumber.length()==10 && mobilenumber.matches(MobileNumberregex)) - { - userdetails.setMobilenumber(mobilenumber); - flag=false; - } - else - { - flag=true; - throw new InvalidMobileNumberException("Please provide the correct Mobile Number.\n"); - } - } - catch(InvalidMobileNumberException e) - { - System.out.println("You're Entering the Invaild MoblieNumber.\n"+e); - } - } - //System.out.println("For new account opening, Please deposite the Minimum Balance Amount 1000 in your account:"); - userdetails.setBalanceamount(0); - userdetails.setAccountnumber(); - UserDetailSet.add(userdetails); - System.out.println(".....................Your's New Bank account was successfully created......................"); - System.out.println("Your Account Number is : "+userdetails.getAccountnumber()); - System.out.println("Your Account Balance is : "+userdetails.getBalanceamount()); - System.out.println("Your Account Balance is very low, So please depoite money for further transaction's.\n"); - } -} diff --git a/DepositOperation.java b/DepositOperation.java deleted file mode 100644 index 4a08cff..0000000 --- a/DepositOperation.java +++ /dev/null @@ -1,53 +0,0 @@ -package bankapplication; - -import java.util.Iterator; -import java.util.regex.Pattern; - -public class DepositOperation implements BankingOperation_q { - -{ - int Depositeflag = 0; - //Pattern pattern=Pattern.compile("[a-zA-Z]*"); - System.out.println("Enter Account number: "); - long accountNumber = input.nextLong(); - String ConvertAccountNumber=String.valueOf(accountNumber); - if(!(Pattern.matches("[a-zA-Z]",ConvertAccountNumber))) - { - Iterator iterator = UserDetailSet.iterator(); - UserDetails userdetails; - while(iterator.hasNext()) - { - userdetails = (UserDetails) iterator.next(); - if(userdetails.getAccountnumber()== accountNumber) - { - Depositeflag = 1; - System.out.println("Enter Deposit amount: "); - int depositAmount = input.nextInt(); - userdetails.setBalanceamount(userdetails.getBalanceamount()+depositAmount); - System.out.println("Successfully money deposited to your account number "+userdetails.getAccountnumber()+".\n"); - History history = new History(); - history.CreditAmount = depositAmount; - history.TotalAmount = userdetails.getBalanceamount(); - if(userdetails.TransactionHistory.size()>=5) - { - userdetails.TransactionHistory.remove(); - userdetails.TransactionHistory.add(history); - } - else - { - userdetails.TransactionHistory.add(history); - } - break; - } - } - if(Depositeflag == 0) - { - System.out.println("Account Number Not Created.\n"); - } - } - else - { - System.out.println("You're enter the invalid Account Number.\n"); - } -} -} diff --git a/DepositUserMoneyCommand.java b/DepositUserMoneyCommand.java deleted file mode 100644 index c509958..0000000 --- a/DepositUserMoneyCommand.java +++ /dev/null @@ -1,14 +0,0 @@ -package bankapplication; - -public class DepositUserMoneyCommand implements BankCommand { - private BankingOperation bankingOperation; - - public DepositUserMoneyCommand(BankingOperation bankingOperation) { - this.bankingOperation = bankingOperation; - } - - @Override - public void execute() { - bankingOperation.DepositeUserMoney(); - } -} diff --git a/InputProvider.java b/InputProvider.java new file mode 100644 index 0000000..d9c1f9a --- /dev/null +++ b/InputProvider.java @@ -0,0 +1,10 @@ +package bankapplication; + +public interface InputProvider { + String next(); + int nextInt(); + long nextLong(); + void close(); +} + +// Interfaz para operaciones con usuarios \ No newline at end of file diff --git a/UserDetailsService.java b/UserDetailsService.java new file mode 100644 index 0000000..35edc65 --- /dev/null +++ b/UserDetailsService.java @@ -0,0 +1,8 @@ +package bankapplication; + +public interface UserDetailsService { + void addUser(UserDetails userDetails); + UserDetails getUser(long accountNumber); +} + +// Clase UserDetails para almacenar detalles del usuario \ No newline at end of file diff --git a/WithdrawUserMoneyCommand.java b/WithdrawUserMoneyCommand.java deleted file mode 100644 index 0f24610..0000000 --- a/WithdrawUserMoneyCommand.java +++ /dev/null @@ -1,14 +0,0 @@ -package bankapplication; - -public class WithdrawUserMoneyCommand implements BankCommand { - private BankingOperation bankingOperation; - - public WithdrawUserMoneyCommand(BankingOperation bankingOperation) { - this.bankingOperation = bankingOperation; - } - - @Override - public void execute() { - bankingOperation.WithdrawUserMoney(); - } -} \ No newline at end of file From 866ddbb7d9006eacb248e25b24e174a1fd6506a5 Mon Sep 17 00:00:00 2001 From: Johao Date: Tue, 18 Jun 2024 19:31:25 -0500 Subject: [PATCH 3/4] DIP 2 --- BankApplication.java | 71 +++---------------------------------- BankingOperation.java | 9 ++--- InputReader.java | 5 +++ InvalidAadharException.java | 19 ---------- ScannerInputReader.java | 14 ++++++++ 5 files changed, 27 insertions(+), 91 deletions(-) create mode 100644 InputReader.java delete mode 100644 InvalidAadharException.java create mode 100644 ScannerInputReader.java diff --git a/BankApplication.java b/BankApplication.java index a919ca4..6e0b622 100644 --- a/BankApplication.java +++ b/BankApplication.java @@ -11,71 +11,10 @@ public class BankApplication { public static void main(String[] args) { - @SuppressWarnings("resource") - Scanner input=new Scanner(System.in); - BankingOperation bankingoperation=new BankingOperation(); - UserInterest userinterest=new UserInterest(); - UserTransactionHistory usertransactionhistory=new UserTransactionHistory(); - System.out.println("Welcome to the Nitheesh's online banking service's :"); - while(true) - { - try - { - System.out.println("Enter the option for Banking operation"); - System.out.println("1.Create new Bank Account"); - System.out.println("2.Deposit Your Amount"); - System.out.println("3.Withraw Your Amount"); - System.out.println("4.To calculate Interest Amount for your Loan"); - System.out.println("5.To view Your Account Details"); - System.out.println("6.To view you're Transaction History"); - System.out.println("7.Exit the Banking operation"); - String Userchoice=input.next(); - switch(Userchoice) - { - case "1": - bankingoperation.CreateNewBankAccount(); - break; - case "2": - bankingoperation.DepositeUserMoney(); - break; - case "3": - bankingoperation.WithdrawUserMoney(); - break; - case "4": - System.out.println("Please choose the Below option's :\n1.Simple Interest\n2.Compound Interest\n3.Main menu\n4.Exit\n"); - String InterestOption=input.next(); - switch(InterestOption) - { - case "1": - userinterest.calculatesimpleinterest(); - break; - case "2": - userinterest.calculatecompoundinterest(); - break; - case "3": - break; - case "4": - System.exit(0); - } - break; - case "5": - bankingoperation.DisplayUserAccountDetails(); - break; - case "6": - usertransactionhistory.usertransactionhistory(); - break; - case "7": - System.out.println("************ Thank you for using our service! Have a GREAT DAY **************"); - System.exit(0); - default: - throw new InValidInputException("Please Enter the correct choice.\n"); - } - - } - catch(InValidInputException e) - { - System.out.println("You're enter the invalid choice.\n"+e); + InputReader inputReader = new ScannerInputReader(); + BankingOperation bankingOperation = new BankingOperation(inputReader); + + System.out.println("Welcome to the banking application."); + bankingOperation.performOperation(); } - } - } } \ No newline at end of file diff --git a/BankingOperation.java b/BankingOperation.java index 1cb9bf9..12f9f19 100644 --- a/BankingOperation.java +++ b/BankingOperation.java @@ -15,13 +15,10 @@ import java.util.regex.Pattern; public class BankingOperation implements Transaction { - private boolean flag; - private InputProvider input; - private UserDetailsService userDetailsService; + private final InputReader inputReader; - public BankingOperation(InputProvider input, UserDetailsService userDetailsService) { - this.input = input; - this.userDetailsService = userDetailsService; + public BankingOperation(InputReader inputReader) { + this.inputReader = inputReader; } public void createNewBankAccount() { diff --git a/InputReader.java b/InputReader.java new file mode 100644 index 0000000..b2a89a5 --- /dev/null +++ b/InputReader.java @@ -0,0 +1,5 @@ +package bankapplication; + +public interface InputReader { + String readInput(); +} \ No newline at end of file diff --git a/InvalidAadharException.java b/InvalidAadharException.java deleted file mode 100644 index 8f971f0..0000000 --- a/InvalidAadharException.java +++ /dev/null @@ -1,19 +0,0 @@ -/* -@Banking application - With the help of this application we can create new bank -account and other banking operation's. -@Author - Nitheesh G. -@Created at - 02-16-2021 -@Updated at - 02-16-2021 -@Reviewed by - Anto -*/ -package bankapplication; - -public class InvalidAadharException extends Exception { - - private static final long serialVersionUID = 1L; - - public InvalidAadharException(String s) - { - super(s); - } -} diff --git a/ScannerInputReader.java b/ScannerInputReader.java new file mode 100644 index 0000000..50ac1e6 --- /dev/null +++ b/ScannerInputReader.java @@ -0,0 +1,14 @@ +package bankapplication; + +public class ScannerInputReader implements InputReader { + private Scanner scanner; + + public ScannerInputReader() { + this.scanner = new Scanner(System.in); + } + + @Override + public String readInput() { + return scanner.nextLine(); + } +} \ No newline at end of file From 54c75aab6afa0503bffce170e75e8670292a3e0f Mon Sep 17 00:00:00 2001 From: Johao Date: Tue, 18 Jun 2024 22:15:14 -0500 Subject: [PATCH 4/4] DIP3 --- UserDetails.java | 88 +++++++++++++++++++------------------ UserDetailsRepository.java | 5 +++ UserTransactionHistory.java | 43 +++++++----------- 3 files changed, 68 insertions(+), 68 deletions(-) create mode 100644 UserDetailsRepository.java diff --git a/UserDetails.java b/UserDetails.java index 1884c7b..7235354 100644 --- a/UserDetails.java +++ b/UserDetails.java @@ -11,74 +11,78 @@ import java.util.Queue; public class UserDetails { - static long uniquenumber=1010106001; + private static long uniquenumber = 1010106001; private String firstname; private String lastname; - private int age,balanceamount; + private int age; + private int balanceamount; private String mobilenumber; private String aadharnumber; - static long accountnumber; - QueueTransactionHistory; + private long accountnumber; + Queue TransactionHistory; - UserDetails() - { + public UserDetails() { this.TransactionHistory = new LinkedList<>(); } - public void setAccountnumber() - { - UserDetails.accountnumber=uniquenumber; + + public void setAccountnumber() { + this.accountnumber = uniquenumber; uniquenumber++; } - public long getAccountnumber() - { + + public long getAccountnumber() { return accountnumber; } - public void setBalanceamount(int balanceamount) - { - this.balanceamount=balanceamount; + + public void setBalanceamount(int balanceamount) { + this.balanceamount = balanceamount; } - public int getBalanceamount() - { + + public int getBalanceamount() { return balanceamount; } - public String getFirstname() - { + + public String getFirstname() { return firstname; } - public void setFirstname(String firstname) - { - this.firstname=firstname; + + public void setFirstname(String firstname) { + this.firstname = firstname; } - public String getLastname() - { + + public String getLastname() { return lastname; } - public void setLastname(String lastname) - { - this.lastname=lastname; + + public void setLastname(String lastname) { + this.lastname = lastname; } - public int getAge() - { + + public int getAge() { return age; } - public void setAge(int age) - { - this.age=age; + + public void setAge(int age) { + this.age = age; } - public String getMobilenumber() - { + + public String getMobilenumber() { return mobilenumber; } - public void setMobilenumber(String mobilenumber) - { - this.mobilenumber=mobilenumber; + + public void setMobilenumber(String mobilenumber) { + this.mobilenumber = mobilenumber; } - public String getAadharnumber() - { + + public String getAadharnumber() { return aadharnumber; } - public void setAadharnumber(String aadharnumber) - { - this.aadharnumber=aadharnumber; + + public void setAadharnumber(String aadharnumber) { + this.aadharnumber = aadharnumber; + } + + public Queue getTransactionHistory() { + return TransactionHistory; } -} \ No newline at end of file +} diff --git a/UserDetailsRepository.java b/UserDetailsRepository.java new file mode 100644 index 0000000..1c5d57f --- /dev/null +++ b/UserDetailsRepository.java @@ -0,0 +1,5 @@ +package bankapplication; + +public interface UserDetailsRepository { + UserDetails getUserDetailsByAccountNumber(int accountNumber); +} \ No newline at end of file diff --git a/UserTransactionHistory.java b/UserTransactionHistory.java index 359db8e..15e01af 100644 --- a/UserTransactionHistory.java +++ b/UserTransactionHistory.java @@ -12,33 +12,24 @@ import java.util.Iterator; import java.util.Scanner; public class UserTransactionHistory { - void usertransactionhistory() - { - @SuppressWarnings("resource") - Scanner input=new Scanner(System.in); - HashSetTransactionDetailSet=BankingOperation.UserDetailSet; - int flag = 0; - System.out.println("Enter Account number: "); - int accntNumber = input.nextInt(); - Iterator iterator = TransactionDetailSet.iterator(); - UserDetails userdetails; - while(iterator.hasNext()) - { - userdetails = (UserDetails) iterator.next(); - if(userdetails.getAccountnumber()== accntNumber) - { - flag = 1; - System.out.println("Credit | Debit | Total"); - for(History s : userdetails.TransactionHistory) - { - System.out.println(s.CreditAmount + " | " + s.DebitAmount + " | " + s.TotalAmount+"\n"); - } - break; + private UserDetailsRepository userRepository; + + public UserTransactionHistory(UserDetailsRepository userRepository) { + this.userRepository = userRepository; + } + + public void getUserTransactionHistory(int accountNumber) { + UserDetails userDetails = userRepository.getUserDetailsByAccountNumber(accountNumber); + + if (userDetails != null) { + System.out.println("Credit | Debit | Total"); + for (History transaction : userDetails.getTransactionHistory()) { + System.out.println(transaction.getCreditAmount() + " | " + + transaction.getDebitAmount() + " | " + + transaction.getTotalAmount()); } - } - if(flag == 0) - { - System.out.println("BankAccount Not Available\n"); + } else { + System.out.println("BankAccount Not Available"); } } } \ No newline at end of file