Results 1 to 8 of 8
  1. #1
    ninozjava is offline Member
    Join Date
    Jun 2011
    Posts
    3
    Rep Power
    0

    Default Bank system giving me problems

    Hi all.. I am new to Java and this forum. I am currently studying for my SCJP and the lecturer has given us a system to.

    It is to create a Bank that will store customers, search through customers, do and store transactions, delete customers, etc...

    I have been getting problems. One of them is I cannot add a customers to my ArrayList. Also, how will i go about serializing my account to maintain persistance through multiple insistences of running bank. Also how do i add transactions of a customer to view later.

    Java Code:
    import java.io.*;
    import java.util.*;
    public class Bank{
    
    	static Customer customer = new Customer();
    	static Console menu = System.console();
    	
    	public static void main(String[] args){
    		mainMenu();
    	}
    	
    	public static void mainMenu(){							//main menu of system with options
    		System.out.println("Welcome to Java Bank\n");	
    		System.out.println("1. New Customer");
    		System.out.println("2. Customer Services");
    		System.out.println("3. Exit\n");
    		
    		String input = menu.readLine("%s", "Select option: ");
    		int menuOption = Integer.parseInt(input);
    		switch(menuOption){
    			case 1: 
    				newCustomer();
    				break;
    			case 2:
    				viewCustomer();
    				break;
    			case 3:
    				System.exit(0);
    			default:
    				System.out.println("Please enter correct option: ");
    				mainMenu();
    		}
    	}
    	
    	public static void newCustomer(){
    		System.out.println("Complete details to create new customer\n");
    		String IDNumber = menu.readLine("%s", "Enter client ID Number: ");		
    		String firstName = menu.readLine("%s", "Enter First Names: ");
    		String lastName = menu.readLine("%s", "Enter Surname: ");
    		String telNumber = menu.readLine("%s", "Enter contact number: ");
    		String emailAddres = menu.readLine("%s", "Enter email address: ");
    		String confirm = menu.readLine("%s", "Are you sure you want to create customer? Y/N ");
    		if(confirm.equalsIgnoreCase("y")){
    			System.out.println("Creating customer...");
    			Customer customer = new Customer(IDNumber, firstName, lastName, telNumber, emailAddres);
    			Customer.customers.add(customer);
    			System.out.println(customer);
    			String proceed = menu.readLine("%s", "To proceed please press enter\n\n");
    				if(proceed.equalsIgnoreCase(""))
    					mainMenu();
    		}
    		else 
    			mainMenu();
    	}
    	
    	public static void viewCustomer(){						//method call from mainMenu() to display options for customer
    		System.out.println("Customer Services\n");
    		searchCustomer();									//to return and work with a selected customer from the searchCustomer method
    		System.out.println("1. Withdraw");
    		System.out.println("2. Deposit");
    		System.out.println("3. Transfer");
    		System.out.println("4. Balance");
    		System.out.println("5. Transaction History");
    		System.out.println("6. Delete Account");
    		System.out.println("7. Return to previous menu\n");	
    		String input = menu.readLine("%s", "Select option: ");
    		int menuOption = Integer.parseInt(input);
    		switch(menuOption){
    				// case 1:
    				// String withAmnt = menu.readLine("%s", "Enter amount to withdraw");
    				// int withdrawAmount = Integer.parseInt(withAmnt);
    				// Customer.currentBalance -= withdrawAmount;
    				// return "Current Balance: " + currentBalance;
    				// viewCustomer();
    				// withdraw();
    			// case 2:
    				// deposit();
    			// case 3:
    				// transfer();
    			// case 4:
    				// balance();
    			//case 5:
    				//transactionHistory();
    			case 6:
    				Customer.customers.remove(customer);
    				mainMenu();
    			case 7:
    				mainMenu();
    			default:
    				System.out.println("\nPlease enter correct option: ");
    				viewCustomer();
    		}
    	}
    	
    
    
    	public static void searchCustomer(){					//method call to search customers with given options		
    		System.out.println("Select option to search with: \n");
    		if(Customer.customers != null){
    			System.out.println("No customers exist");
    			String exit = menu.readLine("%s", "To return to Main Menu please press enter: \n\n");
    			if(exit.equalsIgnoreCase(""))
    				mainMenu();
    		}
    		System.out.println("1. ID Number");
    		System.out.println("2. Name");
    		System.out.println("3. Account Number");
    		System.out.println("4. Return to previous menu\n");
    		String input = menu.readLine("%s", "Select option: ");
    		int inputOption = Integer.parseInt(input);
    		switch(inputOption){
    			case 1:
    				IDCompare idCompare = new IDCompare();
    				Collections.sort(Customer.customers, idCompare);
    				String idnumber = menu.readLine("%s", "Enter ID Number: ");
    				Customer.customers.lastIndexOf(idnumber);
    			case 2:
    				FirstNameCompare fnCompare = new FirstNameCompare();
    				Collections.sort(Customer.customers, fnCompare);
    				String name = menu.readLine("%s", "Enter Name: ");
    				Customer.customers.lastIndexOf(name);
    			case 3:
    				AccountNumberCompare acnumCompare = new AccountNumberCompare();
    				Collections.sort(Customer.customers, acnumCompare);
    				String account = menu.readLine("%s", "Enter Account Number: ");
    				Customer.customers.lastIndexOf(account);
    			case 4:
    				mainMenu();
    			default:
    				System.out.println("Please select correct option: ");
    				searchCustomer();
    		}
    		viewCustomer();
    	}
    }
    			
    class Customer{// implements Comparable<Customer>{
    	static List<Customer> customers = new ArrayList<Customer>();
    	String IDNumber;
    	String firstName;
    	String lastName;
    	String telNumber;
    	String emailAddres;
    	double currentBalance;
    	int accountNumber = 100001;
    	
    	public Customer(){}
    	
    	public Customer(String id, String fname, String lname, String tel){
    		this.IDNumber = id;
    		this.firstName = fname;
    		this.lastName = lname;
    		this.telNumber = tel;
    	}
    		
    	public Customer(String id, String fname, String lname, String tel, String email){
    		this.IDNumber = id;
    		this.firstName = fname;
    		this.lastName = lname;
    		this.telNumber = tel;
    		this.emailAddres = email;
    	}
    	
    	public String getIDNumber(){
    		return IDNumber;
    	}
    	public String getFirstName(){
    		return firstName;
    	}
    	public String getLastName(){
    		return lastName;
    	}
    	public String getTelNumber(){
    		return telNumber;
    	}
    	public String getEMail(){
    		return emailAddres;
    	}
    	public int getAccountNumber(){
    		return accountNumber;
    	}
    	public String toString(){
    		return "Customer Details:\nID Number: " + IDNumber + "\nAccount Number: " + accountNumber + "\nFirst Name: " + firstName + "\nSurname: " + lastName + "\nTelephone Number: " + telNumber + "\nEmail Addres: " + emailAddres;
    	}
    	public int compareTo(Customer customer){
    		return firstName.compareTo(customer.getFirstName());
    	}
    	
    	{ accountNumber++; }
    }
    
    class IDCompare implements Comparator<Customer>{
    	public int compare(Customer customerOne, Customer customerTwo){
    		return customerOne.getIDNumber().compareTo(customerTwo.getIDNumber());
    	}
    }
    
    class FirstNameCompare implements Comparator<Customer>{
    	public int compare(Customer customerOne, Customer customerTwo){
    		return customerOne.getFirstName().compareTo(customerTwo.getFirstName());
    	}
    }
    
    class AccountNumberCompare implements Comparator<Customer>{
    	 public int compare(Customer customerOne, Customer customerTwo){
    		return new Double(customerOne.getAccountNumber()).compareTo(new Double(customerTwo.getAccountNumber()));
    	}
    }
    
    class CustomerCollection{
    	// List<Customer> customers = new ArrayList<Customer>();
    	// customers.add(new Customer(id, fname, lname, tel, email);
    	
    }
    
    class TransactionHistory{
    	List<Transactions> transaction = new ArrayList<Transactions>();
    	public void viewTransactionHistory(){}
    	public void newTransaction(){}
    	
    
    }
    
    class Transactions{
    	
    	double currentBalance;
    	double amountDeposit;
    	double amountWithdraw;
    	public double deposit(){
    		currentBalance += amountDeposit;
    		return currentBalance;
    	}
    	
    	//public double transfer(){}
    	public double withdraw(){
    		currentBalance -= amountWithdraw;
    		return currentBalance;
    	}
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by ninozjava View Post
    I have been getting problems. One of them is I cannot add a customers to my ArrayList. Also, how will i go about serializing my account to maintain persistance through multiple insistences of running bank. Also how do i add transactions of a customer to view later.
    Please don't just give a list of wants and dump your code here. Instead tell us the details of your problems such as what have you done to try to fix it, what errors has this caused, including exact error messages. The brunt of effort for solving these problems must fall on you.

    This link will give you great suggestions on how to ask informative questions that will get the best answers: How to Ask Smart Questions
    Last edited by Fubarable; 06-19-2011 at 03:15 PM.

  3. #3
    ninozjava is offline Member
    Join Date
    Jun 2011
    Posts
    3
    Rep Power
    0

    Default

    My exact problem is this that when I create a customer it does not add to the ArrayList, I do not understand how this is so. I have uses ArrayList<Customer> and refrenced it from Bank.class as Customer.customers.add(customer) but to no avail when searching for a customer I get the "no customer found" error

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    There is way too much use of static methods here. You should really have only one static method(main), and few, if any static variables. For instance, a customer shouldn't contain a list of customers, the customer class should simply represent a single customer. It would make more sense to have a list of customers as an instance variable of the bank. You may also want to encapsulate your variables, you don't want anyone to be able to make changes directly. I hate to say it, but I suggest you scrap the code and start fresh. Customer class represents a single unique customer, and the bank should be a container for a group of customers.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    OK, some problems I see off the bat:

    • Customer should not hold a static List of Customer as that shouldn't be its responsibility. Instead Customer should just encapsulate all the information required for a single Customer, that's it. The List should not be static and should be held by the bank because this just makes more sense. Bank A will have its own list of Customers and Bank B should have the ability to have another completely independent list. Your set up would prevent this from happening.
    • You'll want to give Customer an equals method and a hashCode method
    • The only method/variable that should be static in Bank is the main method. Everything else should be instance (non-static) methods and variables.
    • And one of your main problems is that you're searching your ArrayList using the lastIndexOf method and it's not doing what you think it's doing. Look at the API and you'll see that it will search through the ArrayList looking for any Customer objects that match the parameter passed into the method. You're passing in Strings and these are definitely not the same as Customer objects. Instead you'll need to give whatever class that holds this collection methods that allow it to search through the ArrayList, using for loops finding the Customer whose field that matches that which was passed in. In other words, you'll need to give the code a couple of methods looking something like this:

      PHP Code:
      public Customer findCustomerWithID(String idNumber) {
         // loop through the customer list checking if any of them have an idNumber that matches the idNumber parameter
         // if found, return the found Customer.
         // if not, return null or throw an exception -- your choice.
      }
      
      // you might want your find based on names (or any property that can be duplicated) return a List of 
      // Customer since there may be more than one match. Again, this is up to you.
      public List<Customer> findCustomersWithLastName(String lastName) {
         // create an ArrayList<Customer>, say lastNameList
         // same thing, loop through the customer list with a for loop adding matches to this local lastNameList
         // return the lastNameList. It may be empty if no matches
      }
      
      // etc...
    Last edited by Fubarable; 06-19-2011 at 04:02 PM.

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,380
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by ninozjava View Post
    My exact problem is this that when I create a customer it does not add to the ArrayList, I do not understand how this is so. I have uses ArrayList<Customer> and refrenced it from Bank.class as Customer.customers.add(customer) but to no avail when searching for a customer I get the "no customer found" error
    I can understand that: the customers ArrayList is never equal to null and your search method doesn't search anything but tests whether or not that ArrayList equals null: if it isn't the method prints out that the customer doesn't exist.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Also, make sure you override equals and not overload, overloading can cause some problems. It should look like this
    Java Code:
    @Override public boolean equals(Object o){
      if(o == this)
        return true;
      if(!(o instanceof Customer))
        return false;
      Customer c = (Customer) o;
      //do comparisons between this and c
    }
    This is a pretty solid equals method you should try to implemet every time. This is taken from the book effective java.

  8. #8
    ninozjava is offline Member
    Join Date
    Jun 2011
    Posts
    3
    Rep Power
    0

    Default

    Thanks alot guys for all the help so far...

    I have been working on my code and I did start from scratch. With only one static main method. I will try and implement the searching as Fubarable has explained above as this is one thing I have struggled with, i did realise that lastIndexOf returns an int.

    I have implemented serialization on my Customer List. Will that work through multiple runs?

    Further, my transactions i have included within the viewCustomer() method, and added each transaction to a List. Does that make sense?

    I am not sure if there is anything else I need to look into? I am going to add simple error messages withing if-else statements to test to see if there is enough cash before doing a withdraw or transfer.

    Oh and one more thing - regarding date/time in each transaction. When the customer views transaction history it should show dates. only remembered that now. will have to implement


    PHP Code:
    import java.io.*;
    import java.util.*;
    
    class Bank2{
    	List<Customer> customers = new ArrayList<Customer>();
    	List<Transaction> transactions = new ArrayList<Transaction>();
    	Console menu = System.console();
    	Transaction trans = new Transaction();
    	
    	public void serializeCust(){
    	try {
    			FileOutputStream fs = new FileOutputStream("bank.ser");
    			ObjectOutputStream os = new ObjectOutputStream(fs);
    			os.writeObject(customers);
    			os.close();
    		} 
    		catch (Exception e) { e.printStackTrace(); }
    	}
    	
    	public void deserializeCust(){
    		try {
    			FileInputStream fis = new FileInputStream("bank.ser");
    			ObjectInputStream ois = new ObjectInputStream(fis);
    			customers = (ArrayList<Customer>)ois.readObject();
    			ois.close();
    		} 
    		catch (Exception e) { e.printStackTrace(); }
    	}
    	public static void main(String[] args){
    		Bank2 bank = new Bank2();
    		bank.serializeCust();
    		bank.mainMenu();
    		bank.deserializeCust();
    	}
    	
    	public void mainMenu(){
    		System.out.println("Welcome to Java Bank\n");	
    		System.out.println("1. New Customer");
    		System.out.println("2. Customer Services");
    		System.out.println("3. Exit\n");
    		
    		String input = menu.readLine("%s", "Select option: ");
    		int menuOption = Integer.parseInt(input);
    		switch(menuOption){
    			case 1: 
    				newCustomer();
    				break;
    			case 2:
    				viewCustomer();
    				break;
    			case 3:
    				System.exit(0);
    			default:
    				System.out.println("Please enter correct option: ");
    				mainMenu();
    		}
    	}
    	
    	public void newCustomer(){
    		System.out.println("Complete details to create new customer\n");
    		String IDNumber = menu.readLine("%s", "Enter client ID Number: ");		
    		String firstName = menu.readLine("%s", "Enter First Names: ");
    		String lastName = menu.readLine("%s", "Enter Surname: ");
    		String telNumber = menu.readLine("%s", "Enter contact number: ");
    		String emailAddres = menu.readLine("%s", "Enter email address: ");
    		String confirm = menu.readLine("%s", "Are you sure you want to create customer? Y/N ");
    		if(confirm.equalsIgnoreCase("y")){
    			System.out.println("Creating customer...");
    			Customer customer = new Customer(IDNumber, firstName, lastName, telNumber, emailAddres);
    			customers.add(customer);
    			System.out.println(customer);
    			String proceed = menu.readLine("%s", "To proceed please press enter\n\n");
    			if(proceed.equalsIgnoreCase(""))
    				mainMenu();
    		}
    		else 
    			mainMenu();
    	}
    	
    	public void viewCustomer(){
    		System.out.println("Customer Services\n");
    		searchCustomer();									//to return and work with a selected customer from the 
    
    searchCustomer method
    		System.out.println("1. Withdraw");
    		System.out.println("2. Deposit");
    		System.out.println("3. Transfer");
    		System.out.println("4. Balance");
    		System.out.println("5. Transaction History");
    		System.out.println("6. Delete Account");
    		System.out.println("7. Return to previous menu\n");	
    		String input = menu.readLine("%s", "Select option: ");
    		int menuOption = Integer.parseInt(input);
    		switch(menuOption){
    			case 1:
    				String depAmnt = menu.readLine("%s", "Enter amount to deposit");
    				double depositAmount = Double.parseDouble(depAmnt);
    				trans.currentBalance += depositAmount;
    				System.out.println("Current Balance: " + trans.currentBalance);
    				Transaction depositTransaction = new Transaction(trans.currentBalance, depositAmount);
    				transactions.add(depositTransaction);
    				break;
    			case 2:
    				String withAmnt = menu.readLine("%s", "Enter amount to withdraw");
    				double withdrawAmount = Double.parseDouble(withAmnt);
    				trans.currentBalance += withdrawAmount;
    				System.out.println("Current Balance: " + trans.currentBalance);
    				Transaction withdrawTransaction = new Transaction(trans.currentBalance, withdrawAmount);
    				transactions.add(withdrawTransaction);
    				break;
    			case 4:
    				System.out.println(trans.currentBalance);
    				break;
    			case 5:
    				System.out.println(transactions);
    				break;
    			case 6: 
    				System.out.println("Are you sure you want to delete this customer?\nCustomer Details" + customer);
    				String confirm = menu.readLine("%s", "Please confirm: Y/N");
    				if(confirm.equalsIgnoreCase("y"))
    					customers.remove(customer);
    				else
    					viewCustomer();
    			case 7:
    				mainMenu();
    			default:
    				System.out.println("Please enter correct option: ");
    				viewCustomer();
    		}
    	}
    		
    	public Customer searchCustomer(){
    		System.out.println("Select option to search with: \n");
    		if(customers.size() == 0){
    			System.out.println("No customers exist");
    			String exit = menu.readLine("%s", "To return to Main Menu please press enter: \n\n");
    			if(exit.equalsIgnoreCase(""))
    				mainMenu();
    		}
    		System.out.println("1. ID Number");
    		System.out.println("2. Name");
    		System.out.println("3. Account Number");
    		System.out.println("4. Return to previous menu\n");
    		String input = menu.readLine("%s", "Select option: ");
    		int inputOption = Integer.parseInt(input);
    		switch(inputOption){
    			case 1:
    				IDCompare idCompare = new IDCompare();
    				Collections.sort(customers, idCompare);
    				String idnumber = menu.readLine("%s", "Enter ID Number: ");
    				//Customer tempCustomer = new Customer(idnumber
    				//return customers.lastIndexOf(idnumber);
    				break;
    			case 2:
    				FirstNameCompare fnCompare = new FirstNameCompare();
    				Collections.sort(customers, fnCompare);
    				String name = menu.readLine("%s", "Enter Name: ");
    				//return customers.lastIndexOf(name);
    				break;
    			case 3:
    				AccountNumberCompare acnumCompare = new AccountNumberCompare();
    				Collections.sort(customers, acnumCompare);
    				String account = menu.readLine("%s", "Enter Account Number: ");
    				//return customers.lastIndexOf(account);
    				break;
    			case 4:
    				mainMenu();
    			default:
    				System.out.println("Please select correct option: ");
    				searchCustomer();
    		}
    	}	
    }
    
    class Customer implements Serializable{
    	String IDNumber;
    	String firstName;
    	String lastName;
    	String telNumber;
    	String emailAddres;
    	int accountNumber;// = 100001;
    	double currentBalance;
    	
    	public Customer(String id, String fname, String lname, String tel, String email){
    		this.IDNumber = id;
    		this.firstName = fname;
    		this.lastName = lname;
    		this.telNumber = tel;
    		this.emailAddres = email;
    		accountNumber = (int)(Math.random()* 1000000);
    	}
    	public String toString(){
    		return "Customer Details:\nID Number: " + IDNumber + "\nAccount Number: " + accountNumber + "\nFirst Name: " + firstName + "\nSurname: " + lastName + "\nTelephone 
    
    Number: " + telNumber + "\nEmail Addres: " + emailAddres;
    	}
    		public String getIDNumber(){
    		return IDNumber;
    	}
    	public String getFirstName(){
    		return firstName;
    	}
    	public String getLastName(){
    		return lastName;
    	}
    	public String getTelNumber(){
    		return telNumber;
    	}
    	public String getEMail(){
    		return emailAddres;
    	}
    	public int getAccountNumber(){
    		return accountNumber;
    	}
    }
    
    class IDCompare implements Comparator<Customer>{
    	public int compare(Customer customerOne, Customer customerTwo){
    		return customerOne.getIDNumber().compareTo(customerTwo.getIDNumber());
    	}
    }
    
    class FirstNameCompare implements Comparator<Customer>{
    	public int compare(Customer customerOne, Customer customerTwo){
    		return customerOne.getFirstName().compareTo(customerTwo.getFirstName());
    	}
    }
    
    class AccountNumberCompare implements Comparator<Customer>{
    	 public int compare(Customer customerOne, Customer customerTwo){
    		return new Double(customerOne.getAccountNumber()).compareTo(new Double(customerTwo.getAccountNumber()));
    	}
    }
    
    class Transaction{
    	double currentBalance;
    	double transactionAmount;
    	public Transaction(){}
    	public Transaction(double currentBalnce, double transactionAmount){
    		double transactionFee = 10;
    		this.currentBalance = currentBalance;
    		this.transactionAmount = transactionAmount;
    	}
    }

Similar Threads

  1. Need help with bank system assignment - vectors
    By a3404987 in forum New To Java
    Replies: 1
    Last Post: 03-29-2011, 03:41 AM
  2. Bank Account Project
    By Haal in forum New To Java
    Replies: 0
    Last Post: 03-20-2011, 07:30 PM
  3. Bank Account
    By HPcompaq256 in forum New To Java
    Replies: 11
    Last Post: 02-26-2010, 09:05 PM
  4. Code bank
    By Eranga in forum Suggestions & Feedback
    Replies: 2
    Last Post: 01-01-2008, 05:23 AM
  5. Problems with consumes a lot of system resources
    By Eric in forum Advanced Java
    Replies: 2
    Last Post: 07-03-2007, 06:23 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •