الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

Jfream & JTable

بدأه يارب! في 13 يونيو 2011 · 0 رد · 703 مشاهدة · في JavaSE
مشاركة: واتساب X فيسبوك تيليجرام
#1

السلام عليكم ورحمة الله وبركاته

اللهم صلي على محمد وال محمد

:) اخواني / اخوتي خبراء الحافا

هَل لكم تعليمي بـ كيف أضع { التعامل مع المستخدم } في Jframe ... وَ كيف أضع الـ مصفوفات 2D ذاخل Jtable ?

إذن ان المصفوفات خصوصاً .... تظهر بشكل غير مرتب ابداً عند الطباعه !

هذا هو الـ برنامج بين أيديكم !

ublic class District { // class

    private String DistrictName; // variable with type string for the districtName
    private Region[] regions; // because each district is divided to Regions
    // an array of regions is here :)

    public District() { // zero argument constructor
        this.DistrictName = null;
    }

    public District(String name, Region[] regions) { // constructor wih TWO argument as needed when i creat the functions :)
        this.DistrictName = name;
        this.regions = regions;
    }

    public District(String name) { // constructore with One argument
        this.DistrictName = name;
    }
// set the districtname
    public void setDistrictName(String districtName) {
        this.DistrictName = districtName;
    }
// get the districtname
    public String getDistrictName() {
        return DistrictName;
    }
// get an Array , the array of regions "POINTER TO THE ARRAY !"
    public Region[] getRegions() {
        return regions;
    }
}

public class Region {
// Variables

    private String RegionName;
    private String district;
    private SchoolGroup[] schools; // bcoz each Region has many schools , an array of schools is here :)

    public Region() {// zero argument constructor
    }

    public Region(String s, SchoolGroup[] school) {// constructor with 2 arguments
        this.RegionName = s;
        this.schools = school;

    }

    Region(String RegionName) { // constructor with ONE argument
        this.RegionName = RegionName;
    }
//set the distric

    public void setDistrict(String district) {
        this.district = district;
    }
// set the region name

    public void setName(String name) {
        this.RegionName = name;
    }
// get the region

    public String getRegionName() {
        return RegionName;
    }
// get POINTER to the schools array 

    public SchoolGroup[] getSchoolGroup() {
        return schools;
    }
}

public class SchoolGroup {

// variables ...
    protected int adminEmpNum;
    protected int teachersNum;
    protected int studentsNum;
    protected int numOfSchools;
    protected String schoolType;
// zero argument constructore

    public SchoolGroup() {
        this.adminEmpNum = 0;
        this.numOfSchools = 0;
        this.studentsNum = 0;
        this.teachersNum = 0;
        this.schoolType = null;

    }
// constrcture with argument

    public SchoolGroup(String schoolType, int numOfSchool, int adminEmpNum, int teachersNum, int studentsNum) {
        this.schoolType = schoolType;
        this.numOfSchools = numOfSchool;
        this.teachersNum = teachersNum;
        this.studentsNum = studentsNum;
        this.adminEmpNum = adminEmpNum;
    }
// set all the variables

    public void setInfo(int numOfSchool, int adminEmpNum, int teachersNum, int studentsNum) {
        this.teachersNum = teachersNum;
        this.numOfSchools = numOfSchool;
        this.adminEmpNum = adminEmpNum;
        this.studentsNum = studentsNum;
    }

    // get functions 
    public int getAdminEmpNum() {
        return adminEmpNum;
    }

    public int getTeachersNum() {
        return teachersNum;
    }

    public int getStudentsNum() {

        return studentsNum;

    }

    public int getNumOfSchools() {
        return numOfSchools;
    }

    public String getSchoolType() {
        return schoolType;
    }

    public int getInfo() {

        return 0;
    }
}

mport java.util.*; // this import used for Scanner and exceptions :)
import javax.swing.*;

public class Main { // the main class

    // an array with type District .. Contain all KSA district except Makkah :)
    public static District DistrictList[] = {null
            , new District("Maddinah")
            , new District("Al Riyadh")
            , new District("Al Bahah")
            , new District("Jizan")
            , new District("Najran")
            , new District("Tabuk")
            , new District("Eastern Province")
            , new District("Asir")
            , new District("Ha'il")
            , new District("Al Qasim")
            , new District("Al Jawf")
            , new District("Northern Border")};
    public static Region regions[]; // array of type regions
//_________________________________________________________________
    public static void main(String[] args) {
        // Define schools information
        // each array for ONE region , each region has three kind of schools
        // each school has number of building ,admin , teachers and students
        SchoolGroup[] JeddahSchool = {new SchoolGroup("PrimarySchool", 100, 500, 1000, 40000),
            new SchoolGroup("IntermediateSchool", 87, 700, 3000, 35000),
            new SchoolGroup("HighSchool", 78, 7000, 5000, 33000)};

        SchoolGroup[] TaifSchool = {new SchoolGroup("PrimarySchool", 50, 100, 400, 13000),
            new SchoolGroup("IntermediateSchool", 45, 200, 2000, 15000),
            new SchoolGroup("HighSchool", 32, 400, 3000, 13000)};

        SchoolGroup[] makkahSchool = {new SchoolGroup("PrimarySchool", 50, 100, 400, 13000),
            new SchoolGroup("IntermediateSchool", 35, 80, 400, 12000),
            new SchoolGroup("HighSchool", 30, 90, 450, 1000)};


        //Define Makkah Regions regions
        Region jeddah = new Region("Jeddah", JeddahSchool);
        Region makkah = new Region("Makkah", makkahSchool);
        Region taif = new Region("Taif", TaifSchool);
        regions = new Region[3];
        regions[0] = jeddah;
        regions[1] = makkah;
        regions[2] = taif;


        //define The main District "Makkah" which is the only district we are
        // going to use it n this program
        District MakkahDistrict = new District("Makkah", regions); // send the regions to Makkah District as we need in the methods
        DistrictList[0] = MakkahDistrict; //initialise the first place n MakkahDistrict with both makkah district and it regions


        Scanner input = new Scanner(System.in);
         int answer = 0;
        String regionAns;
        String DistrictAns;

        while (answer != 11) {// to exite ..
            try
            {
            System.out.println(); // just to make it more arrangeable
            System.out.println();
                 // list to choose from .. containe everything the program can do :)
            System.out.println("1. the number of schools in that region, according to District Name.");
            System.out.println("2. the number of schools in that region, according to Region Name.");
            System.out.println("3. the number of schools in secondary or intermediate or high schools to the district");
            System.out.println("4. the number of schools in secondary or intermediate or high schools to the district, region");
            System.out.println("5. the total number of schools in District.");
            System.out.println("6. the total number of schools in region");
            System.out.println("7. the number of admin employees, teachers and students, according to District Name.");
            System.out.println("8. the number of admin employees or teachers or students in district");
            System.out.println("9. the number of admin employees or teachers or students in region");
            System.out.println("10.the number of admin employees, teachers and students, according to district, region, school level");
            System.out.println("11. Exit");
            System.out.println("What Do you want to do ,enter the number ");
            answer = input.nextInt();
            } catch (InputMismatchException e) { // when the user input a string instead of intger
                System.err.print("" + e + ", plz insert a number from the list above ");
            }

                switch (answer) {
// each case has just a call to da method which can do whatever in the list
                    case 1:
                        NumOfSchoolInRegions();
                        break;
                    case 2:
                        NumberOfOneTypeOfSchoolsInOneRgion();
                        break;
                    case 3:
                        NumberOfSpecificTypeOfSchoolsInDistrcit(); // here
                        break;
                    case 4:
                        NumberOfSchoolsInSpecificLevel();
                        break;
                    case 5:
                        NumberOfSchoolsInDistrict();
                        break;
                    case 6:
                        NumberOfSchoolsInRegion(); 
                        break;
                    case 7:
                        NumberOfEmployeeAndTeacherAndStudentInRegion();
                        break;
                    case 8:
                        NumberOfEmployeeAndTeacherAndStudentInDistrict();
                        break;
                    case 9:
                        NumberOfOneKindOFpplInRegion(); 
                        break;
                    case 10:
                        NumberOfPPLInOneSchoolLevelInRegion(); 
                        break;
                    case 11:
                        System.out.print("Bye Bye");
                    default:

                }

//            } catch (InputMismatchException e) { // when the user input a string instead of intger
//                System.err.print("" + e + ", plz insert a number from the list above ");
//            }


        }
    }

    // Query 1.	Given the district, list the number of schools in that region, according to:
    public static void NumOfSchoolInRegions() {
     District district =getUserDistrict();

        Region[] regions = district.getRegions();

        String[][] toprint = new String[4][4];
        toprint[0][0] = district.getDistrictName();
        toprint[1][0] = "Primary School";
        toprint[2][0] = "Middle School";
        toprint[3][0] = "High School";
        for (int i = 1; i <= regions.length; i++) {
            SchoolGroup[] s = regions[i - 1].getSchoolGroup();
            toprint[0] = regions[i - 1].getRegionName();
            //convert int to string by calling the static method toString()
            // sourse : http://www.javadb.com/convert-an-int-value-to-string

            toprint[1] = Integer.toString(s[0].getNumOfSchools()); 
            toprint[2] = Integer.toString(s[1].getNumOfSchools());
            toprint[3] = Integer.toString(s[2].getNumOfSchools());
        }

         PrintToprintArray(toprint, 4, 4);
    }

    //--------------------------------------------------------------------
    // Query 2.Given the district, region, list the number of schools in that region
    public static void NumberOfOneTypeOfSchoolsInOneRgion() {

        /*
         * Make 2 methods to get the district name and regions from the user and call them in all your other methods
         * to do that you need to make the 2 variables district and regions to be global
         */
        District district =getUserDistrict();

        Region regions = getUserRegion();


        SchoolGroup[] sg = regions.getSchoolGroup();
        // making 2D array
        String[][] toprint = new String[2][4];
        toprint[0][0] = regions.getRegionName();
        toprint[0][1] = "Primary School";
        toprint[0][2] = "Middle School";
        toprint[0][3] = "High School";
        toprint[1][0] = "# of schools";
        toprint[1][1] = Integer.toString(sg[0].getNumOfSchools());
        toprint[1][2] = Integer.toString(sg[1].getNumOfSchools());
        toprint[1][3] = Integer.toString(sg[2].getNumOfSchools());

         PrintToprintArray(toprint, 2, 4);
        /*

         */
//        for (int i = 0; i < 2; i++) {
//            for (int j = 0; j < 4; j++) {
//                System.out.print(toprint[j] + "           |    ");
//            }
//            System.out.println();
//
//        }
    }
      // Query 3.Given the district, list the number of schools in secondary or intermediate or high schools.

    public static void NumberOfSpecificTypeOfSchoolsInDistrcit() {
       District district =getUserDistrict();
        Region[] regions = district.getRegions();
Scanner input = new Scanner (System.in);
        String[][] toprint = new String[2][4];

        System.out.println("Choose School Level ");
        System.out.println("1- Primary School");
        System.out.println("2- Intermediate School");
        System.out.println("3- High School ");
        System.out.print("Your choice:  ");
         int answer = input.nextInt();

        if (answer == 1) {
            toprint[1][0] = "Primary School";
        } 
        else if (answer == 2) {
            toprint[1][0] = "Intermediate School";
        } 
        else if (answer == 3) {
            toprint[1][0] = "High School";
        }


        toprint[0][0] = district.getDistrictName();
        for (int i = 1; i <= regions.length; i++) {
            SchoolGroup[] s = regions[i - 1].getSchoolGroup();
            toprint[0] = regions[i - 1].getRegionName();
            if (answer == 1) {
                toprint[1] = Integer.toString(s[0].getNumOfSchools());
            } else if (answer == 2) {
                toprint[1] = Integer.toString(s[1].getNumOfSchools());
            } else if (answer == 3) {
                toprint[1] = Integer.toString(s[2].getNumOfSchools());
            }

        }
        // print the toprint array
        PrintToprintArray(toprint, 2, 4);
//        for (int i = 0; i < 2; i++) {
//            for (int j = 0; j < 4; j++) {
//                System.out.print(toprint[j] + "           |    ");
//            }
//            System.out.println();
//
//        }
    }

 //Query 4. Given the district, region, list the number of schools in secondary or intermediate or high schools.

    public static void NumberOfSchoolsInSpecificLevel() {
Scanner input = new Scanner (System.in);

     District district =getUserDistrict();

        Region[] regions = district.getRegions();

        for (int i = 0; i < regions.length; i++) {
            System.out.println((i + 1) + "- " + regions.getRegionName());
        }
        // scanner
      int  answer = input.nextInt();
        answer--;
        SchoolGroup[] sg = regions[answer].getSchoolGroup();

             System.out.println("1.Primary School");
             System.out.println("2.Intermediate School");
             System.out.println("3.High School");
             System.out.println("Choose the School Level :");
             answer = input.nextInt();

           System.out.println("the numbere of schools is :"+sg[answer-1].getNumOfSchools());
//           String[][] toprint = new String[2][2];
//           toprint[0][1]= " Number Of Schools ";
//
//
//       for (int i = 0; i < 2; i++) {
//            for (int j = 0; j < 2; j++) {
//                System.out.print(toprint[j] + "           |    ");
//            }
//            System.out.println();
//        }

    }


    //Query 5.Given the district, display the total numberNumberOfSchoolsInDistrict of schools.
    public static void NumberOfSchoolsInDistrict() {

        int totalNumberOfSchoolsInDstrict = 0;
        District district = getUserDistrict();

        for (int i = 0; i < regions.length; i++) {

            SchoolGroup schools[] = regions.getSchoolGroup();

            for (int j = 0; j < schools.length; j++) {
                totalNumberOfSchoolsInDstrict = totalNumberOfSchoolsInDstrict + schools[j].numOfSchools;

            }
        }

        System.out.println("Total Number of Schools " + totalNumberOfSchoolsInDstrict);
    }

    //Query 6.Given the district, region, display the total number of schools

    public static void NumberOfSchoolsInRegion() {
        int totalNumOfSchoolsNRrgion = 0;
       District district =getUserDistrict();

       Region regions = getUserRegion();

        SchoolGroup s[] = regions.getSchoolGroup();

        for (int j = 0; j < s.length; j++) {
            totalNumOfSchoolsNRrgion = totalNumOfSchoolsNRrgion + s[j].numOfSchools;

        }        
        System.out.println("Total Number of Schools " + totalNumOfSchoolsNRrgion);
    }

    //Query 7.Given the district, list the number of admin employees, teachers and students, according to:
    public static void NumberOfEmployeeAndTeacherAndStudentInRegion() {
       District district =getUserDistrict();

        Region[] regions = district.getRegions();

        String[][] toprint = new String[4][4];
        toprint[0][0] = "Primary Schools";
        toprint[1][0] = "Admins";
        toprint[2][0] = "Techers";
        toprint[3][0] = "Students";

        for (int i = 1; i <= regions.length; i++) {
            SchoolGroup[] sg = regions[i - 1].getSchoolGroup();
            toprint[0] = regions[i - 1].getRegionName();
            toprint[1] = Integer.toString(sg[0].getAdminEmpNum()); //convert int to string by calling the static method toString()
            toprint[2] = Integer.toString(sg[0].getTeachersNum());
            toprint[3] = Integer.toString(sg[0].getStudentsNum());
        }

//        for (int i = 0; i < 4; i++) {
//            for (int j = 0; j < 4; j++) {
//                System.out.print(toprint[j] + "           |    ");
//            }
//            System.out.println();
//
//        }
        PrintToprintArray(toprint, 4, 4);

//        String[][] toprint = new String[4][4];
        toprint[0][0] = "Middle Schools";
        toprint[1][0] = "Admins";
        toprint[2][0] = "Techers";
        toprint[3][0] = "Students";

        for (int i = 1; i <= regions.length; i++) {
            SchoolGroup[] sg = regions[i - 1].getSchoolGroup();
            toprint[0] = regions[i - 1].getRegionName();
            toprint[1] = Integer.toString(sg[1].getAdminEmpNum()); //convert int to string by calling the static method toString()
            toprint[2] = Integer.toString(sg[1].getTeachersNum());
            toprint[3] = Integer.toString(sg[1].getStudentsNum());
        }
            System.out.println();
            System.out.println();
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                System.out.print(toprint[j] + "           |    ");
            }
            System.out.println();

        }
        toprint[0][0] = "High Schools";
        toprint[1][0] = "Admins";
        toprint[2][0] = "Techers";
        toprint[3][0] = "Students";

        for (int i = 1; i <= regions.length; i++) {
            SchoolGroup[] sg = regions[i - 1].getSchoolGroup();
            toprint[0] = regions[i - 1].getRegionName();
            toprint[1] = Integer.toString(sg[2].getAdminEmpNum()); //convert int to string by calling the static method toString()
            toprint[2] = Integer.toString(sg[2].getTeachersNum());
            toprint[3] = Integer.toString(sg[2].getStudentsNum());
        }
            System.out.println();
            System.out.println();
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                System.out.print(toprint[j] + "           |    ");
            }
            System.out.println();

        }

    }

    //Query 8.Given the district, list the number of admin employees or teachers or students.
    public static void NumberOfEmployeeAndTeacherAndStudentInDistrict() {
        Scanner input = new Scanner(System.in);
        District district = getUserDistrict();
        System.out.println("Choose Type of people: ");
        System.out.println("1- Admins");
        System.out.println("2- Techers");
        System.out.println("3- Students");
        System.out.print("Your choice:  ");
        int answer = input.nextInt();

        String[][] toprint = new String[2][4];
        toprint[0][0] = district.getDistrictName();
        toprint[0][1] = "# in Primary School";
        toprint[0][2] = "# in Middle School";
        toprint[0][3] = "# in High School";
        if (answer == 1) {
            toprint[1][0] = "Admins";
        } else if (answer == 2) {
            toprint[1][0] = "Teachers";
        } else {
            toprint[1][0] = "Students";
        }

        Region[] regions = district.getRegions();
        int[] temp = {0, 0, 0}; //temporary array to put the sum of peopel in
        for (int i = 1; i <= regions.length; i++) {
            SchoolGroup[] sg = regions[i - 1].getSchoolGroup();
            if (answer == 1) {
                temp[0] += sg[0].getAdminEmpNum();
                temp[1] += sg[1].getAdminEmpNum();
                temp[2] += sg[2].getAdminEmpNum();
            } else if (answer == 2) {
                temp[0] += sg[0].getTeachersNum();
                temp[1] += sg[1].getTeachersNum();
                temp[2] += sg[2].getTeachersNum();
            } else {
                temp[0] += sg[0].getStudentsNum();
                temp[1] += sg[1].getStudentsNum();
                temp[2] += sg[2].getStudentsNum();
            }
        }

        toprint[1][1] = Integer.toString(temp[0]);
        toprint[1][2] = Integer.toString(temp[1]);
        toprint[1][3] = Integer.toString(temp[2]);

        PrintToprintArray(toprint, 2, 4);

//        for (int i = 0; i < 2; i++) {
//            for (int j = 0; j < 4; j++) {
//                System.out.print(toprint[j] + "           |    ");
//            }
//            System.out.println();
//
//        }
    }
//ery  9.Given the district, region, list the number of admin employees or teachers or students

    public static void NumberOfOneKindOFpplInRegion() {
        District district = getUserDistrict();
        Scanner input = new Scanner(System.in);
        System.out.println();

        Region regions = getUserRegion();
        SchoolGroup[] sg = regions.getSchoolGroup();
        boolean x = true;
        int answer = 0;
        try {
            System.out.println("Choose Type of people: ");
            System.out.println("1- Admins");
            System.out.println("2- Techers");
            System.out.println("3- Students");
            System.out.print("Your choice:  ");
            answer = input.nextInt();
            x = false;
        }
 catch (InputMismatchException e) {

            System.err.println("Invalid Input , plz enter one of the numbers in list above");
        }

        String[][] toprint = new String[2][4];
        toprint[0][0] = district.getDistrictName();
        toprint[0][1] = "# in Primary School";
        toprint[0][2] = "# in Middle School";
        toprint[0][3] = "# in High School";
        if (answer == 1) {
            toprint[1][0] = "Admins";
            toprint[1][1] = Integer.toString(sg[0].getAdminEmpNum());
            toprint[1][2] = Integer.toString(sg[1].getAdminEmpNum());
            toprint[1][3] = Integer.toString(sg[2].getAdminEmpNum());

        } else if (answer == 2) {
            toprint[1][0] = "Teachers";
            toprint[1][1] = Integer.toString(sg[0].getTeachersNum());
            toprint[1][2] = Integer.toString(sg[1].getTeachersNum());
            toprint[1][3] = Integer.toString(sg[2].getTeachersNum());
        } else {
            toprint[1][0] = "Students";
            toprint[1][1] = Integer.toString(sg[0].getStudentsNum());
            toprint[1][2] = Integer.toString(sg[1].getStudentsNum());
            toprint[1][3] = Integer.toString(sg[2].getStudentsNum());
        }

        PrintToprintArray(toprint, 2, 4);
//        for (int i = 0; i < 2; i++) {
//            for (int j = 0; j < 4; j++) {
//                System.out.print(toprint[j] + "           |    ");
//            }
//            System.out.println();
//
//        }
    }
//Query 10.Given the district, region, school level, list the number of admin employees, teachers and students

    public static void NumberOfPPLInOneSchoolLevelInRegion() {
        District district = getUserDistrict();
        Scanner input = new Scanner(System.in);
        System.out.println();

        Region regions = getUserRegion();

        SchoolGroup[] sg = regions.getSchoolGroup();

        System.out.println("1. Primary School");
        System.out.println("2.Intermediate School ");
        System.out.println("3.High School");
        System.out.println("Choose the SchoolType : ");
        int userAnswer = input.nextInt();

        SchoolGroup userSG = sg[userAnswer - 1];

        String[][] toprint = new String[2][4];
        toprint[0][1] = "Admin";
        toprint[0][2] = "Teachers";
        toprint[0][3] = " Student";
        toprint[1][0] = "Number";
        if (userAnswer == 1) {
            toprint[0][0] = "Primary School";
        } else if (userAnswer == 2) {
            toprint[0][0] = "Intermediate School";
        } else if (userAnswer == 3) {
            toprint[0][0] = "High School ";
        }
        toprint[1][1] = Integer.toString(userSG.getAdminEmpNum());
        toprint[1][2] = Integer.toString(userSG.getTeachersNum());
        toprint[1][3] = Integer.toString(userSG.getStudentsNum());

        PrintToprintArray(toprint, 2, 4);

//        for (int i = 0; i < 2; i++) {
//
//            for (int j = 0; j < 4; j++) {
//
//                System.out.print(toprint[j] + "   |    ");
//            }
//            System.out.println();
//        }
    }

    // function print the Districts and let the user choose

    public static District getUserDistrict() {
        boolean x = true;
        int answer = 0;
        try {

            for (int i = 0; i < DistrictList.length; i++) {
                System.out.println((i + 1) + "- " + DistrictList.getDistrictName());
            }
            Scanner input = new Scanner(System.in);
            System.out.println("Choose a district: ");
            answer = input.nextInt();

            x = false;
        } catch (NumberFormatException e) {
            System.err.println(" Invalid input , plz choose one of the numbers in list above");
        }
        return DistrictList[answer - 1];
    }

// function print the Regions and let the user choose

    public static Region getUserRegion() {
        //boolean x = true ;
        int answer = 0;
        try {
            Scanner input = new Scanner(System.in);
            for (int i = 0; i < regions.length; i++) {
                System.out.println((i + 1) + "- " + regions.getRegionName());
            }
            System.out.println("Choose Region : ");
            answer = input.nextInt();


            //x = false ;
        } catch (NumberFormatException e) {
            System.err.print("" + e + " , invalide input , "
                    + "plz input of othe list number above ");
        }
        return regions[answer - 1];
    }


 //funtion print the toprint array which takes 2D array and the Limites 

    public static void PrintToprintArray(String[][] toprint, int Ilimit, int Jlimit) {
        for (int i = 0; i < Ilimit; i++) {
            for (int j = 0; j < Jlimit; j++) {
                System.out.print(toprint[j] + "      |    ");
            }
            System.out.println();
        }
    }
}

شكراً جزيلاً مقدماً ...

لا عدمتكم !

مواضيع مشابهة