Sunday, 4 March 2012

Sir Wajed Lecture 3


public class Driver {
 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub 
  /*
  MyTime t1 = new MyTime(2,30,15); 
  MyDate d1 = new MyDate(16,2,2012); 
  Transaction [] a = new Transaction[3]; 
  a[0] = new Transaction('D', 100, d1, t1);
  a[1] = new Transaction('D', 200, new MyDate(20,2,2012), new MyTime(1,30,15));
  a[2] = new Transaction('W', 100, new MyDate(20,2,2012), new MyTime(1,35,15));
  for(int i=0; i<3; i++)
   System.out.println(a[i].toString());
  */
  Account a1 = new Account("AW",
     new MyDate(23,2,2012),
     new MyDate(1,1,1900),
     1000);
  a1.deposit(500, new MyDate(23,2,2012), new MyTime(12,30,15));
  a1.deposit(1500, new MyDate(23,2,2012), new MyTime(12,31,25));
  a1.withdraw(500, new MyDate(23,2,2012), new MyTime(12,30,15));
  System.out.println(a1.getTransactionHistory());
 }
}

///////////////////////////////////////
public class Transaction {
 private static long serial=0;
 //private long SID, DID;
 private char type;
 public double amount;
 public MyDate d;
 public MyTime t;

 public Transaction(char type, double amount, MyDate d, MyTime t){
  this.type = type;
  this.amount = amount;
  this.d = d;
  this.t = t;
 }
 public String toString(){
  if(type =='D')
   return "" + d + " "  + t + " Deposit " + amount;
  else
   return "" + d + " "  + t + " Withdraw " + amount; 
 }


}
///////////////////////////////////////////////
public class MyTime {
 private int h,m,s;
 public MyTime(){
  //perform constructor chaining
  this(0,0,0); 
 }
 public MyTime(int hours, int minutes, int seconds){
  h=hours;
  m=minutes;
  s=seconds; 
 }
 public int getHour(){ return h; }
 public int getMinute(){ return m; }
 public int getSecond(){ return s; }
 public void setHour(int t){ h = t; }
 public void setMinute(int t){ m = t; }
 public void setSecond(int t){ s = t; }
 public String toString(){
  return "" + h + ":" + m + ":" + s; 
 }
}
///////////////////////////////////////////
public class MyDate {
 private int day, month;
 private long year;
 private String [] dayNames = { "", //zero day has no name
         "Monday",
         "Tuesday",
         "Wednesday",
         "Thursday",
         "Friday",
         "Saturday",
         "Sunday",        
         };
 private String [] monthNames = {
          "", //zero month has no name
          "January",
          "February",
          "March",
          "April",
          "May",
          "June",
          "July",
          "August",
          "September",
          "October",
          "November",
          "December"     
         };
 // Paremeterized user defined constructor
 public MyDate(int day, int month, long year){
  if(day > 0 && day <= 31)
   this.day = day;
  if(month > 0 && month <= 12)
   this.month  = month;
  if( year > 0 )
   this.year = year;
 }
 // A user defined copy constructor for cloning
 public MyDate(MyDate d){
  this.day = d.day;
  this.month = d.month;
  this.year = d.year;
 } 

 public boolean equals(MyDate d){
  boolean ans = false;
  if(this.day == d.day && this.month == d.month && this.year == d.year)
   ans = true;
  return ans; 
 }

 public String toString(){
  return "" + day + "-" + month + "-" + year; 
 }
}

//////////////////////////////////////

public class Account {
 private static long serial=1000;
 private long AccountNumber;
 private String AccountTitle;
 private MyDate AccountOpeningDate, DOB;
 private double balance;
 private Transaction [] trans;
 private int tnum;
 public Account(String name, MyDate d, MyDate DOB, double openingBalance){
  AccountNumber = serial;
  serial++;
  balance = openingBalance;
  AccountOpeningDate = d;
  this.DOB = DOB;
  AccountTitle = name;
  trans = new Transaction[10];
  tnum =0;
  trans[tnum++] = new Transaction('D', openingBalance, d, new MyTime(0,0,0));
 }
 public boolean deposit(double amount, MyDate d, MyTime t){
  boolean result = false;
  if(amount > 0){
   balance += amount;
   trans[tnum] = new Transaction('D', amount, d, t);
   tnum +=1;
   result = true;
  }
  else
  {
    System.out.println("Invalid Amount");
  
  } 
  return result;
 }
 public boolean withdraw(double amount, MyDate d, MyTime t){
  boolean result = false;
  if(amount < balance){
   balance -= amount;
   trans[tnum] = new Transaction('W', amount, d, t);
   tnum +=1;
   result = true;
  }
  else
  {
    System.out.println("Invalid Amount");
  
  } 
  return result;
 }
 public double getBalance(){
  return balance;
 
 }
 /**
  * Method getTransactionHistory
  * Returns a string comprising of all transactions so far
  *
  * @return String
  */

 public String getTransactionHistory(){
   String result = "Opening Balance = " + trans[0].amount + "\n" + 
       "Date \t Time \t Type \t Amount \n";
   for(int i=0; i<tnum; i++)
    result += trans[i].toString() + "\n";
   result += "Current Balance = " + balance + "\n";
   return result;
 }


}

No comments:

Post a Comment