Skip to content

Instantly share code, notes, and snippets.

Created July 13, 2015 14:46
Show Gist options
  • Save anonymous/5735c19e95cefc347728 to your computer and use it in GitHub Desktop.
Save anonymous/5735c19e95cefc347728 to your computer and use it in GitHub Desktop.
public class customerDetails {
private static int accountNo;
private static float balance = 0, salary = 0;
private static personalDetails p1;
private boolean overdraftState;
private static int count = 0;
private float overdraft;
//begin constructor
public customerDetails(personalDetails p, int acc, float bal, float sal){
p1 = p;
accountNo = acc;
balance = bal;
salary = ((sal>=0)?sal:0);
count+=1;
}//end constructor
public String toString(){
return p1.toString() + "\nAccount Number: " + accountNo + "\nCurrent Balance: " + balance + "\nSalary: " + salary;
}//end toString
//set and get methods to access and modify variables
public void setAccountNo(int a){
accountNo = a;
}//end setAccountNo
public static int getAccountNo(){
return accountNo;
}//end getAccountNo
public void setBalance(float b){
balance = b;
}//end setBalance
public float getBalance(){
return balance;
}//end getBalance
public void setSalary(float s){
salary = s;
}//end setSalary
public float getSalary(){
return salary;
}//end getSalary
public static int getCount(){
return count;
}//end getCount
public boolean getOverdraftState(){
return overdraftState;
}//end getOverDraftState
public void setOverdraftState(boolean bool){
overdraftState = bool;
}//end setOverdraftState
public float getOverdraft(){
return overdraft;
}//end getOverdraft
public void setOverdraft(float o){
overdraft = o;
}//end setOverdraft
}//end class
public class premiumCustomerDetails extends customerDetails {
boolean overdraftState;
float overdraft;
public premiumCustomerDetails(personalDetails p, int acc, float bal, float sal, boolean oS, float o){
super(p, acc,bal,sal);
overdraftState = oS;
overdraft = o;
}//end constructor
public String toString(){
return super.toString() + "\nOverdraft Amount: " + overdraft;
}//end toString
public boolean getOverdraftState(){
return overdraftState;
}//end getOverDraftState
public void setOverdraftState(boolean bool){
overdraftState = bool;
}//end setOverdraftState
public float getOverdraft(){
return overdraft;
}//end getOverdraft
public void setOverdraft(float o){
overdraft = o;
}//end setOverdraft
}//end class
//import neccessary libraries
import java.awt.event.*;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.LayoutManager;
import javax.swing.*;
//begin class, extend JFrame and Implement listeners
public class mainFile extends JFrame implements ActionListener, ItemListener {
//create static variables
static personalDetails pers[] = new personalDetails[20];
static customerDetails cust[] = new customerDetails[20];
static int cookieHolder = 0;
static int count;
public static int index = 0;
//create JPanel and Frame object to host the program
JFrame frame = new JFrame();
JPanel pnl = new JPanel();
public static void main(String args[]){
//create customers ----------DELETE WHEN FILE READING is working
customerCreation();
//try and set the appearance of the GUI to be based on the system it's run on
try{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}//end try
//catch statements in the event that the program can't be run on the system
catch(UnsupportedLookAndFeelException e){
}//end catch
catch(ClassNotFoundException e){
}//end catch
catch(InstantiationException e){
}//end catch
catch(IllegalAccessException e){
}//end catch
//create the gui
mainFile gui = new mainFile();
}//end main
//create objects for the window
//create items for name
JLabel lblName = new JLabel("Name: ");
JTextField txtName = new JTextField(20);
//create items for address
JLabel lblAddress = new JLabel("Address: ");
JTextField txtAddress = new JTextField(40);
//create items for age
JLabel lblAge = new JLabel("Age: " );
JTextField txtAge = new JTextField(2);
//create items for accountNo.
JLabel lblAccountNo = new JLabel("Account Number: ");
JTextField txtAccountNo = new JTextField(4);
//create items for salary
JLabel lblSalary = new JLabel("Salary: " );
JTextField txtSalary = new JTextField(10);
//create items for balance
JLabel lblBalance = new JLabel("Balance: ");
JTextField txtBalance = new JTextField(10);
//create items forOverdraftAmount
JLabel lblOverdraft = new JLabel ("Overdraft Amount: ");
JTextField txtOverdraft = new JTextField(10);
//Create buttons for moving through the list
JButton btnPrev = new JButton("<");
JButton btnNext = new JButton(">");
JButton btnConfirm = new JButton("Confirm");
JButton btnCancel = new JButton("Cancel");
//checkbox for overdraft
JLabel lblOverdraftState = new JLabel("Overdraft: ");
JCheckBox chkOverdraftState = new JCheckBox();
//Create menu bar
JMenuBar menu = new JMenuBar();
JMenu file = new JMenu("File");
JMenuItem newEntry = new JMenuItem("New");
JMenuItem about = new JMenuItem("About");
JMenuItem exit = new JMenuItem("Exit");
//create grid for layout
Container contentPane = getContentPane();
JPanel grid = new JPanel(new GridLayout(10,2));
public mainFile(){
//set up window
pnl.setSize(450,500);
frame.setTitle("Banking System");
frame.setSize(450,500);
setDefaultCloseOperation(EXIT_ON_CLOSE);
//add items to menu
menu.add(file);
file.add(newEntry);
file.add(about);
file.add(exit);
frame.setJMenuBar(menu);
//add items to grid
//row 1
grid.add(lblAccountNo);
grid.add(txtAccountNo);
//row 2
grid.add(lblName);
grid.add(txtName);
//row 3
grid.add(lblAddress);
grid.add(txtAddress);
//row 4
grid.add(lblAge);
grid.add(txtAge);
//row 5
grid.add(lblBalance);
grid.add(txtBalance);
//row 6
grid.add(lblSalary);
grid.add(txtSalary);
//row 7
grid.add(lblOverdraftState);
grid.add(chkOverdraftState);
//row 8
grid.add(lblOverdraft);
grid.add(txtOverdraft);
//row 9
grid.add(btnPrev);
grid.add(btnNext);
//row 10
grid.add(btnConfirm);
grid.add(btnCancel);
//set textItems to be un-editable
txtAccountNo.setEditable(false);
txtName.setEditable(false);
txtAddress.setEditable(false);
txtAge.setEditable(false);
txtBalance.setEditable(false);
txtSalary.setEditable(false);
txtOverdraft.setEditable(false);
chkOverdraftState.setEnabled(false);
//hide things which are not currently needed
lblOverdraft.setVisible(false);
txtOverdraft.setVisible(false);
btnConfirm.setVisible(false);
btnCancel.setVisible(false);
lblOverdraftState.setVisible(false);
//add action listeners to relevant items
btnNext.addActionListener(this);
btnPrev.addActionListener(this);
btnConfirm.addActionListener(this);
btnCancel.addActionListener(this);
newEntry.addActionListener(this);
about.addActionListener(this);
exit.addActionListener(this);
chkOverdraftState.addItemListener(this);
//set items to be disabled by default
btnPrev.setEnabled(false);
btnNext.setEnabled(true);
Dimension buttonSize = new Dimension (20,50);
//add borders
grid.setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
btnNext.setPreferredSize(buttonSize);
btnPrev.setPreferredSize(buttonSize);
txtName.setText(pers[index].getName());
txtAccountNo.setText(Integer.toString(cust[index].getAccountNo()));
txtAddress.setText(pers[index].getAddress());
txtAge.setText(Integer.toString(pers[index].getAge()));
txtBalance.setText(Float.toString(cust[index].getBalance()));
txtSalary.setText(Float.toString(cust[index].getSalary()));
if(cust[index].getOverdraftState() == true){
chkOverdraftState.setVisible(true);
chkOverdraftState.setEnabled(false);
txtOverdraft.setVisible(true);
txtOverdraft.setText(Float.toString(cust[index].getOverdraft()));
}//end if
else{
chkOverdraftState.setVisible(false);
chkOverdraftState.setEnabled(false);
txtOverdraft.setVisible(false);
txtOverdraft.setEditable(false);
txtOverdraft.setText("");
}//end else
//add in the contentpane
contentPane.add("Center",grid);
//add pnl to frame
frame.getContentPane().add(grid);
//set the frame to be visible
grid.setVisible(true);
frame.setVisible(true);
}//end constructor
//actions
public void actionPerformed(ActionEvent event){
//declare variables to be used here
int accountNumber = 0, age = 0, Count = customerDetails.getCount();
String name = null, address = null;
float balance = 0, salary = 0, overdraftAmount;
boolean overdraftState;
if(event.getSource()==exit){
JOptionPane.showMessageDialog(this, "\nThe program will now close. \nGoodbye","Goodbye",JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}//end if
else if(event.getSource()==about){
JOptionPane.showMessageDialog(this, cust[index].toString());
}//end else if
//NEXT BUTTON
else if(event.getSource()==btnNext){
index+=1;
btnPrev.setEnabled(true);
txtAccountNo.setText(Integer.toString(cust[index].getAccountNo()));
txtName.setText(pers[index].getName());
txtAddress.setText(pers[index].getAddress());
txtAge.setText(Integer.toString(pers[index].getAge()));
txtBalance.setText(Float.toString(cust[index].getBalance()));
txtSalary.setText(Float.toString(cust[index].getSalary()));
if(cust[index].getOverdraftState() == true){
chkOverdraftState.setVisible(true);
chkOverdraftState.setSelected(true);
lblOverdraft.setVisible(true);
lblOverdraftState.setVisible(true);
txtOverdraft.setVisible(true);
txtOverdraft.setText(Float.toString(cust[index].getOverdraft()));
}//end if
else{
chkOverdraftState.setVisible(false);
txtOverdraft.setVisible(false);
chkOverdraftState.setSelected(false);
lblOverdraftState.setVisible(false);
lblOverdraft.setVisible(false);
txtOverdraft.setText("");
}//end else
if(index >= customerDetails.getCount()-1){
btnNext.setEnabled(false);
}//end if
}//end else if source == btnNext
//PREVIOUS BUTTON
else if(event.getSource()==btnPrev){
btnNext.setEnabled(true);
index-=1;
txtAccountNo.setText(Integer.toString(cust[index].getAccountNo()));
txtName.setText(pers[index].getName());
txtAddress.setText(pers[index].getAddress());
txtAge.setText(Integer.toString(pers[index].getAge()));
txtBalance.setText(Float.toString(cust[index].getBalance()));
txtSalary.setText(Float.toString(cust[index].getSalary()));
if(cust[index].getOverdraftState() == true){
chkOverdraftState.setVisible(true);
txtOverdraft.setVisible(true);
lblOverdraft.setVisible(true);
lblOverdraftState.setVisible(true);
txtOverdraft.setText(Float.toString(cust[index].getOverdraft()));
}//end if
else{
chkOverdraftState.setVisible(false);
txtOverdraft.setVisible(false);
lblOverdraft.setVisible(false);
lblOverdraftState.setVisible(false);
}//end else
if(index <= 0){
btnPrev.setEnabled(false);
}//end if
}//end else if source == btnprev
else if(event.getSource()==newEntry){
if(cookieHolder<1){
JOptionPane.showMessageDialog(this,"\nNOTE:\n\tAccount Number is made up of 4 digits\n\tName and Address must have at least 6 characters\n\tAge must be a VALID age\n\tSalary and Balance must be valid numbers\n\tIf customer is a regular customer and not premium,\n\tleave the overdraft section blank");
}//end if
cookieHolder+=1;
//change button and textLine states
txtAccountNo.setEditable(true);
txtName.setEditable(true);
txtAddress.setEditable(true);
txtAge.setEditable(true);
txtBalance.setEditable(true);
txtSalary.setEditable(true);
chkOverdraftState.setVisible(true);
chkOverdraftState.setEnabled(false);
txtOverdraft.setEditable(true);
chkOverdraftState.setEnabled(true);
menu.setEnabled(false);
btnNext.setEnabled(false);
btnPrev.setEnabled(false);
btnConfirm.setVisible(true);
btnCancel.setVisible(true);
//clear the text
txtAccountNo.setText("");
txtName.setText("");
txtAddress.setText("");
txtAge.setText("");
txtBalance.setText("");
txtSalary.setText("");
txtOverdraft.setText("");
}//end else if
else if(event.getSource()==btnConfirm){
boolean errorTrip = false;
if(txtAccountNo.getText().length()<4 || txtAccountNo.getText().length()>4){
JOptionPane.showMessageDialog(this,"Error, your Account Number is not 4 characters in length. Please adjust accordingly","Invalid Length on Account Number",JOptionPane.ERROR_MESSAGE);
errorTrip = true;
}//end if
else if(txtAccountNo.getText().length()==4){
String strTempInt = txtAccountNo.getText();
//check if only digits
if(!strTempInt.matches("\\d+")){
JOptionPane.showMessageDialog(this, "Error, you input for age was NOT a number.\nPlease change your input to a number....", "Format Error on age",JOptionPane.ERROR_MESSAGE);
}//end if
else{
accountNumber = Integer.parseInt(strTempInt);
}//end else
}//end else if to check for validity of age input
if(errorTrip == false){
//else if's to determine validity of name
if(txtName.getText().length()<6 ||txtName.getText().matches("\\d+")){
JOptionPane.showMessageDialog(this,"Error, name cannot be less than 6 characters or made up of numbers.\nAre you sure you included a surname?","Name Length Error",JOptionPane.ERROR_MESSAGE);
errorTrip = true;
}//end if
//check for digits only
else if(txtName.getText().length()>=6 && !txtName.getText().matches("\\d+")){
name = txtName.getText();
}//end else if
}//end if
if(errorTrip == false){
if(txtAddress.getText().length()<10||txtAddress.getText().matches("\\d+")){
JOptionPane.showMessageDialog(this,"Error, address cannot be less than 10 characters or consist solely of numbers...\nPlease enter a valid address to proceed","Error on address",JOptionPane.ERROR_MESSAGE);
errorTrip = true;
}//end else if txtAddress
else if(!(txtAddress.getText().length()<10)){
address = txtAddress.getText();
}//end else if
}//end if errorTrip == false
if(errorTrip == false){
if(txtAge.getText().length()<2){
JOptionPane.showMessageDialog(this, "Error, you entered an incorrect age length. Please adjust your age accordingly...","Number Format Exception on Age",JOptionPane.ERROR_MESSAGE);
errorTrip = true;
}//end if
else if(!(txtAge.getText().matches("\\d+"))){
JOptionPane.showMessageDialog(this, "Error, you entered an incorrect age. Please adjust your age accordingly...","Number Format Exception on Age",JOptionPane.ERROR_MESSAGE);
errorTrip = true;
}//end else if
else{
age = Integer.parseInt(txtAge.getText());
if(age<15||age>120){
JOptionPane.showMessageDialog(this, "Error, you entered an incorrect age. Please adjust your age accordingly...","Number Format Exception on Age",JOptionPane.ERROR_MESSAGE);
errorTrip = true;
}//end if
}//end else
}//end if errorTrip == false
if(errorTrip == false){
if(!txtBalance.getText().matches("\\d+")){
JOptionPane.showMessageDialog(this, "Error, digits only for balance input","Number Format Error on Balance", JOptionPane.ERROR_MESSAGE);
errorTrip = true;
}//end if
else{
int tempBalance = Integer.parseInt(txtBalance.getText());
if(tempBalance < 50.00){
JOptionPane.showMessageDialog(this, "Error, You need at least €50 to open a bank account with us...","Insufficient Funds Error", JOptionPane.ERROR_MESSAGE);
errorTrip = true;
}//end if
else{
balance = tempBalance;
}//end else
}//end else if
}// end if errorTrip == false
if(errorTrip == false){
if(!txtSalary.getText().matches("\\d+")){
JOptionPane.showMessageDialog(this,"Error, salary must be made up of digits only. Please adjuts your input accordingly","Number Format Exception on Salary", JOptionPane.ERROR_MESSAGE);
errorTrip = true;
}//end if Salary is not a digit
else{
int tempSalary;
tempSalary = Integer.parseInt(txtSalary.getText());
if(tempSalary<0){
JOptionPane.showMessageDialog(this,"Error, you cannot have a salary of less than nothing...","Number Format Exception on Salary",JOptionPane.ERROR_MESSAGE);
}//end if
else{
salary = tempSalary;
}//end else
}//end else
}//end if errorTrip == false
if(errorTrip == false){
if(chkOverdraftState.isSelected()){
if(txtOverdraft.getText().matches("")){
JOptionPane.showMessageDialog(this,"Error, you need to enter an overdraft...","Number Format Exception on Overdraft", JOptionPane.ERROR_MESSAGE);
errorTrip = true;
}//end if overdraft is empty
else{
int tempOverdraft = Integer.parseInt(txtOverdraft.getText());
if(tempOverdraft<=0){
JOptionPane.showMessageDialog(this,"Error, you cannot have an overdraft value of 0 or lower. Please adjust your input accordingly...","Number Format Exception on Overdraft" , JOptionPane.ERROR_MESSAGE);
}//end if
else{
overdraftAmount = tempOverdraft;
overdraftState = true;
//create premium object
JOptionPane.showMessageDialog(this, "A premium customer will now be created...\n", "Premium Customer Created!", JOptionPane.INFORMATION_MESSAGE);
JOptionPane.showMessageDialog(this, customerDetails.getCount(),"Number",JOptionPane.INFORMATION_MESSAGE);
pers[customerDetails.getCount()] = new personalDetails(name,address,age);
cust[customerDetails.getCount()] = new premiumCustomerDetails(pers[customerDetails.getCount()],accountNumber, balance, salary, overdraftState, overdraftAmount);
JOptionPane.showMessageDialog(this, customerDetails.getCount(),"Number",JOptionPane.INFORMATION_MESSAGE);
confirmRevert(1);
}//end else
}//end else
}//end if
else if(!chkOverdraftState.isSelected()){
JOptionPane.showMessageDialog(this, "A basic customer will now be created...\n", "Regular Customer Created!",JOptionPane.INFORMATION_MESSAGE);
JOptionPane.showMessageDialog(this, customerDetails.getCount(),"Number",JOptionPane.INFORMATION_MESSAGE);
pers[customerDetails.getCount()] = new personalDetails(name, address, age);
cust[customerDetails.getCount()] = new customerDetails(pers[customerDetails.getCount()],accountNumber, balance, salary);
JOptionPane.showMessageDialog(this, customerDetails.getCount(),"Number",JOptionPane.INFORMATION_MESSAGE);
confirmRevert(1);
}//end else
}//end if errorTrip == false
}//end else if btn confirm.getSource
else if(event.getSource()==btnCancel){
//revert items to previous entry in index
txtAccountNo.setText(Integer.toString(cust[index].getAccountNo()));
txtName.setText(pers[index].getName());
txtAddress.setText(pers[index].getAddress());
txtAge.setText(Integer.toString(pers[index].getAge()));
txtBalance.setText(Float.toString(cust[index].getBalance()));
txtSalary.setText(Float.toString(cust[index].getSalary()));
chkOverdraftState.setSelected(cust[index].getOverdraftState());
if(chkOverdraftState.isSelected()==true){
txtOverdraft.setText(Float.toString(cust[index].getOverdraft()));
}//end if
else{
txtOverdraft.setText("");
}//end else
//set everything to be uneditable
txtAccountNo.setEditable(false);
txtName.setEditable(false);
txtAddress.setEditable(false);
txtAge.setEditable(false);
txtBalance.setEditable(false);
txtSalary.setEditable(false);
chkOverdraftState.setEnabled(false);
if(!chkOverdraftState.isSelected()==true){
lblOverdraftState.setVisible(false);
chkOverdraftState.setVisible(false);
lblOverdraft.setVisible(false);
txtOverdraft.setVisible(false);
}//end if
txtOverdraft.setEditable(false);
btnConfirm.setVisible(false);
btnCancel.setVisible(false);
if(index>=customerDetails.getCount()-1){
btnNext.setEnabled(true);
}//end if
else if(index<=0){
btnPrev.setEnabled(true);
}//end else
}//end else if btn.getSource == btnCancel
}//end actionListener
//ITEM STATE CHANGED ----------------------------------
public void itemStateChanged(ItemEvent event){
if((event.getItemSelectable()==chkOverdraftState)&&(event.getStateChange()==ItemEvent.SELECTED)){
txtOverdraft.setVisible(true);
}//end if
else if((event.getItemSelectable()==chkOverdraftState)&&(event.getStateChange()!=ItemEvent.SELECTED)){
txtOverdraft.setVisible(false);
}
}//end itemStateChange
public void confirmRevert(int loc){
//change button and textLine states
txtAccountNo.setEditable(false);
txtName.setEditable(false);
txtAddress.setEditable(false);
txtAge.setEditable(false);
txtBalance.setEditable(false);
txtSalary.setEditable(false);
chkOverdraftState.setVisible(false);
chkOverdraftState.setEnabled(true);
txtOverdraft.setEditable(false);
chkOverdraftState.setEnabled(false);
menu.setEnabled(true);
btnNext.setEnabled(true);
btnPrev.setEnabled(true);
btnConfirm.setVisible(false);
btnCancel.setVisible(false);
if(loc==0){
//set up the text
txtAccountNo.setText("");
txtName.setText("");
txtAddress.setText("");
txtAge.setText("");
txtBalance.setText("");
txtSalary.setText("");
txtOverdraft.setText("");
}//end if
else if(loc==1){
txtAccountNo.setText("" + cust[customerDetails.getCount()-1].getAccountNo());
txtName.setText("" + pers[customerDetails.getCount()-1].getName());
txtAddress.setText("" + pers[customerDetails.getCount()-1].getAddress());
txtAge.setText(""+ pers[customerDetails.getCount()-1].getAge());
txtBalance.setText("" + cust[customerDetails.getCount()-1].getBalance());
txtSalary.setText("" + cust[customerDetails.getCount()-1].getSalary());
}//end else
}//end confirmReveret
//code to delete - used for debugging
public static void customerCreation(){
pers[0]= new personalDetails("John","Cleese",50);
pers[1] = new personalDetails("Michael", "Palin", 40);
pers[2] = new personalDetails("Eric", "Idle", 30);
pers[3] = new personalDetails("Terry", "Gilliam", 35);
pers[4] = new personalDetails("Terry", "Jones", 50);
pers[5] = new personalDetails("Graham", "Chapman", 25);
cust[0] = new customerDetails(pers[0], 1111, 50000, 20000);
cust[1] = new premiumCustomerDetails(pers[1], 1234, 25000,2000, true, 9000);
cust[2] = new customerDetails(pers[2],9999,75000,45000);
cust[3] = new premiumCustomerDetails(pers[3],4567,80000,40000,true, 20000);
cust[4] = new premiumCustomerDetails(pers[4],2345, 50000, 15000, true, 15000);
cust[5] = new customerDetails(pers[5], 7654, 80000,35000);
}//end customerCreation
}//end class
//class to hold personal details objects
public class personalDetails {
private String name, address;
private int age;
//begin constructor
public personalDetails(String n, String a, int ag){
name = n;
address = a;
age = ((ag>=0)?ag:0);
}//end
public String toString(){
return "\nName: " + name + "\nAddress: " + address + "\nAge: " + age;
}//end toString
public String getName(){
return name;
}//end getName
public void setName(String n){
name = n;
}//end setName
public String getAddress(){
return address;
}//end getAddress
public void setAddress(String a){
address = a;
}//end setAddress
public int getAge(){
return age;
}//end getAge
public void setAge(int a){
age = a;
}//end setAge
}//end personal details
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment