CS506 Assignment 1 Solution and Discussion


  • Cyberian's Gold

    Re: CS506 Assignment 1 Solution and Discussion

    Please read the following instructions carefully before solving & submitting assignment:

    Uploading Instructions:
    • You are not allowed to use any IDE tool like NetBeans for this assignment.
    • Use Notepad or Notepad++ for coding and JDK package for Java source code compilation and running.
    • Place all the source code (.java & .class files) in a Zip/RAR file, save with your own Student ID (e.g. bc000000000.zip) and Upload it on VULMS within due date.
    • Your assignment should be in .zip /.rar format. Other file formats will not be accepted.
    • No assignment will be accepted through email.

    Rules for Marking:
    It should be clear that your assignment will not get any credit if:

    o The assignment is submitted after due date.
    o The submitted assignment does not open or file is corrupted.
    o The assignment is fully or partially copied from other student or ditto copy from handouts or Internet; strict disciplinary action will be taken in this case.
    o The assignment is not submitted in .zip /.rar format.

    Note: Do not put any query on MDB regarding this assignment, if you have any query then email at [email protected]
    Lectures Covered: This assignment covers Lectures # 1 to 9

    GOOD LUCK

    Problem Statement:
    You are required to develop a Java program named MyATM, which should simulate behavior of a typical ATM (Automated Teller Machine). In which a user, after entering card details, can deposit amount, withdraw cash and view current balance. The program should be based on a user friendly interface; need to use Java basic GUI component (i.e. JOptionPane) for this purpose. Further, all information must be saved in a backend database using simple text file.

    Detailed Description:
    At start, your program should ask the user to provide card number (e.g. vu-bsxxxxxxx) and pin code (e.g. 1234) as input. After taking input from user the program should fetch user data corresponding to the input from database (i.e. text file) and store in a global variable (e.g. debitCard). Taking input from user is shown in figure no 1.

    Fig. 1: Taking input from User

    If input values are correct (i.e. provided values are same as given in text file) then following ATM services should be displayed via GUI;

    1. Deposit Amount
    2. Withdraw Cash
    3. Check Balance
    4. Exit the Program

    However, in case, if input values are not correct (i.e. found no user against provided card number and pin code) then appropriate message should be shown, like; “Invalid Card No or wrong Pin Code”. Fig. 2: ATM Services GUI

    1. Deposit Amount:

    Each time, user selects this option, s/he will be prompted to enter money for deposit that must be in multiple of Rs. 500/- (hint is given below) and up to a maximum of Rs. 25000/- per transaction. If inputs are correct then the amount must be added in current balance and as well as in database. However, in case of wrong input, appropriate message should be displayed.
    ef11517c-71c8-48c4-a485-1480435fac31-image.png
    Fig. 3: Deposit Amount GUIs

    1. Withdraw Cash:

    User can withdraw any cash but in multiple of Rs. 500/- (hint is given below) and up to a maximum of Rs. 25000/- per transaction by using this option. However, if balance is zero or withdraw amount is greater than the balance, then appropriate message should be displayed. Otherwise, amount must be deducted from current balance and database should be maintained.
    c6313333-e8ec-4fa8-a769-018f97ea70eb-image.png
    Fig. 4: Cash Withdraw GUIs

    1. Check Balance:

    The program should fascinate the user by displaying the current balance via GUI.
    e42491ae-8649-43e6-b89d-121a920bebc1-image.png
    Fig. 5: Check Balance GUI

    1. Exit the Program:
      369c8f49-00d0-4af7-a897-8b4f7d1dd64d-image.png

    Before exiting, the program should display the developer information (i.e. Student Id and name) via GUI.

    Fig. 6: Developer Info GUI
    Required Stuff:
    For this purpose, you have to create a text file and three separate Java classes, details are as follows;

    Text File:

    A simple text file, which should contain single line “vu-bsxxxxxxx,1234,0” in it and must be saved as bsxxxxxxx.txt.

    Here, “bsxxxxxxx” at both (i.e. file name and single line text) must be same as your own student id.
    And “vu-bsxxxxxxx” is card no, “1234” is pin code and “0” is initial balance.

    abd70d78-98cf-48e3-8491-692fc16402fb-image.png
    Fig. 7: Sample Data in Database (.txt file)

    DebitCard.java:
    
    Data Members:
    •	cardNo: String
    •	pin: int
    •	balance: long
    Constructors: 
    •	default, parameterized & copy constructor
    Standard Setters: 
    •	setCardNo(…), setPin(…) & setBalance(…)
    Standard Getters:
    •	getCardNo(), getPin()& getBalance()
    DbHelper.java:
    
    Data Members:
    •	FILE_NAME: String		// static and final variable
    Member Functions: 
    •	loadData() & saveData(…)
    Main Class:
    
    Data Members:
    •	debitCard: DebitCard
    •	dbHelper: DbHelper
    Constructor: 
    •	default
    Member Functions: 
    •	main(…), initLoginGUI(), initServicesGUI(), depositAmoount(), cashWithdraw(), checkBalance() & showDeveloperInfo()
    
    Hint: 
    if (amount % 500 == 0) {
    	// it is a multiple of 500.
    }
    

    Video Tutorials:
     To download & install JDK and create a HellowWorld program in Java, please watch;


     To download JDK setup file for Windows x64 from VU-LMS, please visit;

    Important Things to Implement:
     You have to provide Java classes in separate .java files as guided above. Merging of two or more will result in deduction of marks.
     Each Java class must have proper Data Members and Member Functions along with Constructors, Standard Setters and Getters etc. as discussed above.
     For GUI, you have to use JOptionPane.showInputDialog and JOptionPane.showMessageDialog for taking input from user and showing output to user respectively.
     Need to make sure that exceptions are managed properly throughout the program; especially NullPointerException and NumberFormatException while taking input from user.
     All images and sample data, given in this document, are just for reference purpose only; you have to provide your own implementations. It is not required to be exactly the same.

    Good Luck


  • Cyberian's Gold

    Main.java Code

    import java.util.*;
    import javax.swing.*;
    import javax.swing.JOptionPane;
    
    public class Main
    {
    	DebitCard debitCard;
    	DbHelper dbHelper;
    	
    	public Main()
    	{
    		debitCard =  new DebitCard();
    		dbHelper = new DbHelper();
    	}
    	
    	public static void main(String[] args) 
    	{
    		Main mn = new Main();
    		mn.initLoginGUI();
    	}
    	
    	public void initLoginGUI()
    	{
    		String card_n;
    		int pin;
    		boolean i =false;
    		do
    		{
    			card_n=JOptionPane.showInputDialog(null,"WELCOME TO MY ATM \nPlease enter card No: ", "My ATM", JOptionPane.INFORMATION_MESSAGE);
    			pin=Integer.parseInt(JOptionPane.showInputDialog(null,"Please enter PIN: ", "My ATM", JOptionPane.INFORMATION_MESSAGE));
    			dbHelper.loadData();
    			if(card_n.equals(dbHelper.card_No) && pin==dbHelper.card_pin)
    			{
    				JOptionPane.showMessageDialog(null,"Log in Successfully", "My ATM", JOptionPane.INFORMATION_MESSAGE);
    				initServicesGUI();
    			}
    			else
    			{
    				JOptionPane.showMessageDialog(null,"Invalid Card No or wrong Pin Code", "Error Message ", JOptionPane.ERROR_MESSAGE);
    				i=true;
    			}
    		}
    		while(i);
    	}
    	
    	public void initServicesGUI()
    	{
    		while(true)
    		{
    			int usr_choice=Integer.parseInt(JOptionPane.showInputDialog(null,"Please enter \n1 for 'Deposit Amount'\n2 for 'Withdraw Amount'\n3 for 'Check Balance'\n4 for 'Exit the Program'\n ", "My ATM", JOptionPane.INFORMATION_MESSAGE));
    			try
    			{
    				switch(usr_choice)
    				{
    					case 1 :
    						depositAmoount();
    						break;
    					case 2 :
    						cashWithdraw();
    						break;
    					case 3 :
    						checkBalance();
    						break;
    					case 4 :
    						showDeveloperInfo();
    						System.exit(0);
    					default :
    						JOptionPane.showMessageDialog(null,"Sorry ! Invalid choice ", "Error Message ", JOptionPane.ERROR_MESSAGE);	
    				}
                }
    			catch (NumberFormatException e)
    			{
    				System.out.println("Error occured " + e);
    			}
    		}
    	}
    	
    	public void depositAmoount()
    	{
    		try
            {
    			long amount=Integer.parseInt(JOptionPane.showInputDialog(null,"Please enter money to deposit\n(Amount must be multiple of 500 and upto maximum Rs. 25000/- per transaction.", "Deposit Amount", JOptionPane.INFORMATION_MESSAGE));
    			if(amount%500==0 && amount>0 && amount<=25000)
    			{
    				long up_amount=dbHelper.acc_balance + amount;
    				dbHelper.saveData(up_amount);
    				JOptionPane.showMessageDialog(null,"$"+ amount+ " hase been deposited successfully", "Message", JOptionPane.INFORMATION_MESSAGE);
    				dbHelper.loadData();
    			}
    			else
    			{
    				JOptionPane.showMessageDialog(null,"Amount must be multiple of 500 and upto maximum Rs. 25000/- per transaction.", "Message", JOptionPane.INFORMATION_MESSAGE);
    			}
    		}
    		catch (NumberFormatException e)
            {
                System.out.println("Error occured " + e);
            }
    	}
    	
    	public void cashWithdraw()
    	{
    		long amount=Integer.parseInt(JOptionPane.showInputDialog(null,"Please enter money to Withdraw\n(Amount must be multiple of 500 and upto maximum Rs. 25000/- per transaction.", "Withdraw Cash", JOptionPane.INFORMATION_MESSAGE));
    		try
            {
    			if(amount%500==0 && amount>0 && amount<=25000)
    			{
    				long up_amount=dbHelper.acc_balance - amount;
    				if(up_amount>=0)
    				{
    					dbHelper.saveData(up_amount);
    					JOptionPane.showMessageDialog(null,"$"+ amount+ " hase been withdrawn successfully", "Message", JOptionPane.INFORMATION_MESSAGE);
    					dbHelper.loadData();
    				}
    				else
    				{
    					JOptionPane.showMessageDialog(null,"There is not enough amount to withdraw", "Message", JOptionPane.INFORMATION_MESSAGE);
    				}
    			}
    			else
    			{
    				JOptionPane.showMessageDialog(null,"Amount must be multiple of 500 and upto maximum Rs. 25000/- per transaction.", "Message", JOptionPane.INFORMATION_MESSAGE);
    			}
    		}
    		catch (NumberFormatException e)
            {
                System.out.println("Error occured " + e);
            }
    	}
    	
    	public void checkBalance()
    	{
    		JOptionPane.showMessageDialog(null,"Your current balance is : $" + dbHelper.acc_balance , "Check Balance", JOptionPane.INFORMATION_MESSAGE);
    	}
    	
    	public void showDeveloperInfo()
    	{
    		JOptionPane.showMessageDialog(null,"Developed by : VU ACADEMY (BC090402315)", "Developer Info", JOptionPane.INFORMATION_MESSAGE);
    	}
    	
    
    	
    }
    

    DebitCard.java Code

    import java.util.*;
    import javax.swing.*;
    public class DebitCard 
    {
    	private String cardNo;
    	private int pin;
    	private long balance;
    	
    	public DebitCard()
    	{
    		this.cardNo="";
    		this.pin=0;
    		this.balance=0;
    	}
    	public DebitCard(String c_no, int p, long b)
    	{
    		this.cardNo=c_no;
    		this.pin=p;
    		this.balance=b;
    	}
    	public DebitCard(DebitCard d)
    	{
    		this.cardNo=d.cardNo;
    		this.pin=d.pin;
    		this.balance=d.balance;
    	}
    	
    	public void setcardNo(String c_no)
    	{
    		this.cardNo=c_no;
    	}
    	public void setpin(int p)
    	{
    		this.pin=p;
    	}
    	public void setbalance(long b)
    	{
    		this.balance=b;
    	}
    	
    	public String getcardNo()
    	{
    		return this.cardNo;
    	}
    	public int getpin()
    	{
    		return this.pin;
    	}
    	public long getbalance()
    	{
    		return this.balance;
    	}
    	 
    
    }
    

    DbHelper.java Code

    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.PrintWriter;
    import java.util.*;
    import javax.swing.*;
    import javax.swing.JOptionPane;
    
    public class DbHelper
    {
    	private static String FILE_NAME = "BC090402315.txt";
    	String card_No;
    	int card_pin;
    	long acc_balance;
    	
    	
    	public void loadData()
    	{
    		String tokens[]=null;
              
            try
            {
                FileReader Freader =  new FileReader(FILE_NAME);
                BufferedReader Breader =  new BufferedReader(Freader);
                String line = Breader.readLine();
                    tokens = line.split(",");
                    card_No = tokens[0];
                    card_pin = Integer.parseInt(tokens[1]);
                    acc_balance = Long.parseLong(tokens[2]); 				
                Breader.close();
                Freader.close();
            }
            catch (Exception e)
            {
                System.out.println("Error occured " + e);
            }
    		
    	}
    	
    	public void saveData(long acc_b)
    	{
    		try
            {
                FileWriter fw = new FileWriter(FILE_NAME);
                PrintWriter pw = new PrintWriter(fw);
                pw.println(card_No+","+card_pin+","+acc_b);
                
                pw.flush();
                pw.close();
                fw.close();
    			
            }
            catch (Exception  e)
            {
                System.out.println("Error occured " + e);
            }
        }	
    }
    


Quiz 100% Result Quiz 100% Result
| |