Saturday, 1 October 2011

Day 3

7.42am achievement
 we've finally begun. we split up the first bit:
- Kisha takes care of the welcome page
- i do language selection
- Cole and Nayak do the movie selection based on language chosen
 we're planning to accomplish this by today. i'm done with my bit, based on which Cole and Nayak can begin.
woke up at 7.30, and here's what i did:


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class LanguageSelection extends JFrame
{JList langList;
 Container box;
 String languages[]={"Bengali", "English", "Hindi", "Kannada", "Malayalam", "Manipuri", "Marathi", "Tamil", "Telugu"};
 public LanguageSelection()
 {box=getContentPane();
  box.setLayout(new FlowLayout());
  langList=new JList(languages);
  langList.setVisibleRowCount(5);
  langList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  box.add(new JScrollPane(langList));
  setSize(350, 150);
  show();
  langList.addListSelectionListener(
  new ListSelectionListener()
  {
  public void valueChanged(ListSelectionEvent e)
  {String choice=languages[langList.getSelectedIndex()];}
  }
  );
 }
 public static void main(String[] args)
 {new LanguageSelection().addWindowListener(
  new WindowAdapter()
  {
   public void close(WindowEvent e) {System.exit(0);}
  }
  );
 }
}

the fact that it took me 12 minutes to get this done is shameful. but i'm just going to console myself with the fact that this is way, waaay out of syllabus.
*******
11.05am aim
   online booking in movie theatres = user gets to pick his seats. don't ask me how i moved to seats out of the blue. it's just that i find this bit interesting and i'm in my Coding Mood so i'm gonna go ahead and do it.
   we need to display a basic layout and let the user pick. our layout, with seat numbers, will look something like this:

A1 A2 A3 A4 A5 A6 A7 A8 A9 A10  B1 B2 B3 B4 B5 B6 B7 B8 B9 B10 
C1 C2 C3 C4 C5 C6 C7 C8 C9 C10  D1 D2 D3 D4 D5 D6 D7 D8 D9 D10 
E1 E2 E3 E4 E5 E6 E7 E8 E9 E10  F1 F2 F3 F4 F5 F6 F7 F8 F9 F10 
G1 G2 G3 G4 G5 G6 G7 G8 G9 G10  H1 H2 H3 H4 H5 H6 H7 H8 H9 H10 
I1 I2 I3 I4 I5 I6 I7 I8 I9 I10  J1 J2 J3 J4 J5 J6 J7 J8 J9 J10 
K1 K2 K3 K4 K5 K6 K7 K8 K9 K10  L1 L2 L3 L4 L5 L6 L7 L8 L9 L10 
M1 M2 M3 M4 M5 M6 M7 M8 M9 M10  N1 N2 N3 N4 N5 N6 N7 N8 N9 N10 
O1 O2 O3 O4 O5 O6 O7 O8 O9 O10  P1 P2 P3 P4 P5 P6 P7 P8 P9 P10 
Q1 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q9 Q10  R1 R2 R3 R4 R5 R6 R7 R8 R9 R10 
S1 S2 S3 S4 S5 S6 S7 S8 S9 S10  T1 T2 T3 T4 T5 T6 T7 T8 T9 T10 


and (oh yes, there's an AND) i need to store each of the seat numbers so that, in future, when my user picks his seats, i can 'reserve' them.
   for storing i'm going to use a two dimensional array (something which is, again, out of our syllabus) and to generate the seat numbers i will be using a loop. make that two - one for the row and one for the column. there is a theory starting to build...
let's hope it works.
*******
11.57am achievement+aim

WHO'S YOUR DADDY NOW!
import java.io.*;
public class Seats
static String seatAd="", seats[][]=new String[20][10];
 static int seatQty, counter=0, a=0, b=0;
 public static void seatQtyDisplay()throws IOException
 {System.out.println("Enter no. of seats, please. (:");
  seatQty=Integer.parseInt(new DataInputStream(System.in).readLine());
  for(char x='A'; x<='T'; x++)
  {for(char y='1'; y<='9'; y++)
   {seatAd+=x;
    seatAd+=y;
    seats[a][b]=seatAd;
    System.out.print(seatAd+"  ");
    seatAd="";
    if(y=='9') {System.out.print(x+"10\t\t"); seats[a][b]=(x+"10");}
    b++; if(b==9) b=0;
   }
   counter++;
   if(counter==2) {System.out.println(); counter=0;}
   a++;
  }
 }

   that was a very annoying program. also, it's the first time i used looping variables of character datatype (we've always used int).
goodness, this feels good!
   okay, moving on now. when my user picks his seat, we have to 'reserve' it. to do this, we're thinking of  using a very simple idea: mark it with an x. i shall go about doing this now.
*******
12.59pm tragedy
WHY WON'T THIS EFFIN' WORK?!!
//continued from previous program
static String seatChoice;
public static void seatBook()throws IOException
 {System.out.println("To select seat(s), enter the seat no(s).");
  System.out.println("Note: Seats marked x have been booked already.");
  System.out.println("Ex.:If you wish to pick A5 & A6 - type A5, press Enter. Then type A6 and press Enter.");
  for(int x=0; x<seatQty; x++)
  {seatChoice=new DataInputStream(System.in).readLine();
    for(int a=0; a<20; a++)
   {for(int b=0; b<10; b++)
    {if(seats[a][b].equals(seatChoice)) {seats[a][b]="x"; break;}
    }
    System.out.println();
   }
  }
  System.out.println("Your seat(s) have been reserved.");
  for(int x=0; x<20; x++)
  {for(int y=0; y<10; y++)
   {System.out.print(seats[x][y]+" ");}
   counter++; if(counter==2) {System.out.println(); counter=0;}
  }
 }

   i've been going through this for the past hour. the code seems impeccable. but when i run it, for some very odd reason, the compiler keeps pointing out a NullPointerExcpetion. I HAVE NOT SPECIFIED A FREAKIN' NULL POINTER. also, my seats[a][b].equals(seatChoice) always, ALWAYS returns false. IT MAKES NO SENSE.
FMLFMLFMLFMLFMLFMLFML
FMLFMLFMLFMLFMLFMLFML
FMLFMLFMLFMLFMLFMLFML
FMLFMLFMLFMLFMLFMLFML
FMLFMLFMLFMLFMLFMLFML
FMLFMLFMLFMLFMLFMLFML
FMLFMLFMLFMLFMLFMLFML
*******
1.45pm achievement
<contented sigh>
  so i had two errors:
- NullPointerExcpetion
-seats[a][b].equals(seatChoice) returning false
   the second one was... there's no other word for it but STUPID. stupid, stupid, STUPID. in the first section of the program i showed you (the displaying and storing my seat nos. in the array bit) i had accidentally (don't ask me how) deleted seats[a][b]=seatAd; (i.e, the statement in which my seat no. is stored) and so, obviously, every element in my array was empty and so, OBVIOUSLY my user's choice couldn't possibly be equal to any of them. like i said, STUPID.
   the NullPointerException makes no sense. yes, i have sorted it out, but it still makes no sense. i had to reduce the limit of the column index numbers in my loops by one (i've highlighted the change). why my program works perfectly after doing so will always remain a mystery.

public static void seatBook()throws IOException
 {System.out.println("To select seat(s), enter the seat no(s).");
  System.out.println("Note: Seats marked x have been booked already.");
  System.out.println("Ex.:If you wish to pick A5 & A6 - type A5, press Enter. Then type A6 and press Enter.");
  for(int x=0; x<seatQty; x++)
  {seatChoice=new DataInputStream(System.in).readLine();
   for(int a=0; a<20; a++)
   {for(int b=0; b<9; b++)
    {if(seats[a][b].equals(seatChoice)) {seats[a][b]="x "; break;}
    }
   }
  }
  System.out.println("Your seat(s) have been reserved.");
  for(int x=0; x<20; x++)
  {for(int y=0; y<9; y++)
   {System.out.print(seats[x][y]+" ");}
   counter++; if(counter==2) {System.out.println(); counter=0;}
  }
 }

and i am done for the day.

No comments:

Related Posts Plugin for WordPress, Blogger...