Advertisement
cmiN

Test

Aug 14th, 2011
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.34 KB | None | 0 0
  1. import java.io.*;
  2.  
  3. abstract class Detalii {
  4.     public static int varsta;
  5.     public static char sex;
  6.     public static float nota;
  7. }
  8.  
  9. abstract class Persoana {
  10.     protected int varsta;
  11.     protected char sex;
  12.     Persoana(int varsta, char sex)
  13.     {
  14.         this.varsta = varsta;
  15.         this.sex = sex;
  16.     }
  17.     public abstract void setDetails();
  18. }
  19.  
  20. final class Student
  21. extends Persoana {
  22.     private float nota;
  23.     Student()
  24.     {
  25.         this(0, 'N', 0);
  26.     }
  27.     Student(int varsta, char sex, float nota)
  28.     {
  29.         super(varsta, sex);
  30.         this.nota = nota;
  31.     }
  32.     public void getDetails()
  33.     {
  34.         Detalii.varsta = this.varsta;
  35.         Detalii.sex = this.sex;
  36.         Detalii.nota = this.nota;
  37.     }
  38.     public void setDetails()
  39.     {
  40.         this.varsta = Detalii.varsta;
  41.         this.sex = Detalii.sex;
  42.         this.nota = Detalii.nota;
  43.     }
  44. }
  45.  
  46. public class Test {
  47.     /**
  48.      * Main class of the program
  49.      * Here we read some data
  50.      * Then parse it
  51.      */
  52.     static Student[] studVec;
  53.     static BufferedReader input;
  54.     static int getInt(String param)
  55.     throws IOException
  56.     {
  57.         int nr = -1;
  58.         try {
  59.             nr = Integer.parseInt(input.readLine());
  60.             if (param == "age" && (nr < 18 || nr > 100)) {
  61.                 nr = -1;
  62.             }
  63.         } catch (NumberFormatException exp) {}
  64.         return nr;
  65.     }
  66.     static float getFloat()
  67.     throws IOException
  68.     {
  69.         float nr = -1;
  70.         try {
  71.             nr = Float.parseFloat(input.readLine());
  72.             if (nr > 10 || nr < 1) {
  73.                 nr = -1;
  74.             }
  75.         } catch (NumberFormatException exp) {}
  76.         return nr;
  77.     }
  78.     static char getChar()
  79.     throws IOException
  80.     {
  81.         char nr = 'E';
  82.         try {
  83.             nr = input.readLine().charAt(0);
  84.             if (!(nr == 'M' || nr == 'F')) {
  85.                 nr = 'E';
  86.             }
  87.         } catch (NumberFormatException exp) {}
  88.         return nr;
  89.     }
  90.     static void process(int param)
  91.     throws IOException
  92.     {
  93.         System.out.println("Details for student " + (param + 1) + "...");
  94.         System.out.print("Age: ");
  95.         Detalii.varsta = getInt("age");
  96.         System.out.print("Sex: ");
  97.         Detalii.sex = getChar();
  98.         System.out.print("Grade: ");
  99.         Detalii.nota = getFloat();
  100.         studVec[param].setDetails();
  101.     }
  102.     static void show()
  103.     throws IOException
  104.     {
  105.         for (int i = 0; i < studVec.length; ++i) {
  106.             studVec[i].getDetails();
  107.             System.out.println("Student " + (i + 1) + ": " + Detalii.sex + " " + Detalii.varsta + " " + Detalii.nota);
  108.         }
  109.     }
  110.     public static void main(String[] args)
  111.     {
  112.         int nr = 0;
  113.         input = new BufferedReader(new InputStreamReader(System.in));
  114.         try {
  115.             do {
  116.                 System.out.print("Number: ");
  117.                 nr = getInt("length");
  118.             } while (nr < 0);
  119.             studVec = new Student[nr];
  120.             for (int i = 0; i < nr; ++i) {
  121.                 studVec[i] = new Student();
  122.             }
  123.             for (int i = 0; i < nr / 2; ++i) {
  124.                 process(i);
  125.             }
  126.             show();
  127.         } catch (IOException exp) {
  128.             System.out.println("Error: " + exp.getMessage());
  129.         }
  130.     }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement