Object-oriented sortation storage in Java

Source: Internet
Author: User

Student.java

 PackageYzhou.map;/*** Student Class * * *@authorOcean **/ Public classstudent{PrivateString name; PrivateString No; Private Doublescore;  PublicStudent () {} PublicStudent (string name, String No,Doublescore) {        Super();  This. Name =name;  This. No =No;  This. score =score; }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     PublicString Getno () {returnNo; }     Public voidSetno (String no) { This. No =No; }     Public DoubleGetscore () {returnscore; }     Public voidSetScore (Doublescore) {         This. score =score; }            }

Classroom.java

 PackageYzhou.map;Importjava.util.ArrayList;Importjava.util.List;/*** Multiple students in a class *@authorOcean **/ Public classclassroom{PrivateString No; PrivateList<student>stulist; Private DoubleTotal ;  PublicClassroom () {stulist=NewArraylist<student>(); }     PublicClassroom (String no) { This();  This. No =No; }         PublicClassroom (String No, list<student> stulist,DoubleTotal ) {        Super();  This. No =No;  This. stulist =stulist;  This. Total =Total ; }     PublicString Getno () {returnNo; }     Public voidSetno (String no) { This. No =No; }     PublicList<student>getstulist () {returnstulist; }     Public voidSetstulist (list<student>stulist) {         This. stulist =stulist; }     Public Doublegettotal () {returnTotal ; }     Public voidSettotal (DoubleTotal ) {         This. Total =Total ; }        }

Mapdemo03.java

 PackageYzhou.map;Importjava.util.ArrayList;ImportJava.util.HashMap;ImportJava.util.Iterator;Importjava.util.List;ImportJava.util.Map;ImportJava.util.Set;/*** * Object-oriented + exploded storage *@authorOcean **/ Public classmapdemo03{ Public Static voidMain (string[] args) {List<Student> stulist =exam (); Map<string, classroom> map =count (stulist);    View (map); }        /** View the total and average score of each class-"Traverse map *"*/     Public Static voidView (map<string, classroom>map) {Set<String> keys =Map.keyset (); //get Iterator ObjectIterator<string> keysit =Keys.iterator (); //First Judge         while(Keysit.hasnext ()) {//in gettingString No =Keysit.next (); Classroom=Map.get (NO); //View total score calculation average            DoubleTotal =room.gettotal (); DoubleAVG = total/room.getstulist (). Size (); System.out.println (No+ "--->" +total+ "+" +avg); }            }        /** Statistical analysis **/     Public StaticMap<string, classroom> count (list<student>list) {Map<string, classroom> map =NewHashmap<string,classroom>(); //1. Traverse         for(Student stu:list) {//sorting to see if it exists, the number of the classString No =Stu.getno (); DoubleScore =Stu.getscore (); //If it does not exist, create a classClassroom =Map.get (NO); if(NULL==Guest) { the=NewClassroom (NO);            Map.put (no, guest); }                        //exist, put into the studentroom.getstulist (). Add (Stu); Room.settotal (Room.gettotal ()+score); }        returnmap; }                /** Simulated test data to list **/         Public StaticList<student>Exam () {List<Student> list =NewArraylist<student>(); //Store Student scoresList.add (NewStudent ("Yzhou01", "a", 80)); List.add (NewStudent ("yzhou02", "a", 80)); List.add (NewStudent ("yzhou03", "a", 80)); List.add (NewStudent ("yzhou04", "B", 80)); List.add (NewStudent ("yzhou05", "B", 80)); List.add (NewStudent ("yzhou06", "B", 80)); returnlist;    }; }

Object-oriented sortation storage in Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.