Data structure (Java version) practice collection Simple Book Management system

Source: Internet
Author: User
Tags getmessage ming readfile


A practical tutorial on data Structure (Java edition)


Read the first chapter of the collection combined with the code in the book slightly modified to do a little exercise:

Curriculum class:

Package Com.chujianyun.com;public class Table {private string Key;private string Rest;public table () {}public table ( String key, String rest) {super (); This.key = Key;this.rest = rest;} Public String GetKey () {return key;} public void Setkey (String key) {This.key = key;} Public String Getrest () {return rest;} public void Setrest (String rest) {this.rest = rest;} @Overridepublic boolean equals (Object obj) {return key.equals ((Table) obj). key);} @Overridepublic String toString () {return "Table [key=" + key + ", rest=" + rest + "]";}}

File Operation class:

Package Com.chujianyun.com;import Java.io.bufferedreader;import Java.io.bufferedwriter;import java.io.File;import Java.io.fileinputstream;import Java.io.filenotfoundexception;import Java.io.fileoutputstream;import Java.io.filereader;import Java.io.filewriter;import Java.io.ioexception;import Java.io.InputStreamReader;import Java.io.outputstreamwriter;import Java.util.hashset;import Java.util.linkedhashset;import Java.util.Scanner; Import Java.util.set;public class Fileoperator {private static String FilePath = "E:\\table.txt";p ublic static Set<tab  Le> readFile () {set<table> Set = new linkedhashset<table> ();  File File = new file (FilePath);  BufferedReader BF = null;     try {bf = new BufferedReader (new InputStreamReader (new FileInputStream (file), "GBK"));             String str,key,rest; Table tbl =null;while ((Str=bf.readline ())!=null) {key = Str.substring (0,4); rest = str.substring (5); tbl = new Table (Key, rest); Set.add (TBL);}} catch (FileNotFoundException e) {System. out.println ("No Files found!" ");//e.printstacktrace ();} catch (IOException e) {System.out.println ("Open or Access file exception" +e.getmessage ());//e.printstacktrace ();}    Finally{try {bf.close ();} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();}} return set;} public static void WriteFile (Set<table> Set) {File File = new file (FilePath); BufferedWriter bw = null;//table TBL = null;try {bw = new BufferedWriter (new OutputStreamWriter (file) , "GBK")), for (Table tbl:set) {bw.write (Tbl.getkey () + "" +tbl.getrest () + "\ r \ n");} Bw.flush ();} catch (IOException e) {System.out.println ("Open or Access file exception" +e.getmessage ());} Finally{try {bw.close ();} catch (IOException e) {System.out.println ("Write buffer stream close exception" +e.getmessage ());}}} public static void Select () {System.out.println ("\ n-----------------------------------------"); SYSTEM.OUT.PRINTLN ("Input number for function selection:"); System.out.println ("1: Find a course according to the course number;"); System.out.println ("2: Delete Course according to course number:"); System.out.println ("3: Add a course record to the curriculum:"); System.out.println ("4: Show in CurriculumAll course records "); System.out.println ("5: Change course record in Curriculum"); System.out.println ("-1: Exit"); System.out.println ("-----------------------------------------"); System.out.print ("Feature Selection:");} Find public static Table findtable (String key,set<table> TBLs) {for (Table Tbl:tbls) {if (Tbl.getkey ()) based on key and Set. EQ  Uals (key)) {return tbl; }}return null;} public static void Findtableinput (Set<table> tbls,scanner in) {String input = ""; System.out.print ("Please enter the curriculum to find:"); input =in.next (); Table tbl = findtable (input, TBLs), if (tbl==null) {System.out.println ("No curriculum for the course number found");} Else{system.out.println ("The corresponding curriculum is:" +tbl);}} public static void Edittableinput (set<table> tbls,string input) {String key = input.substring (0,4); String rest = input.substring (5); for (Table Tbl:tbls) {if (Tbl.getkey (). Equals (key)) {tbl.setrest (rest); System.out.println ("Modify the course number to [" +key+ "] successfully! ");}}} public static void Edittableinput (Set<table> tbls,scanner in) {String input = "";        System.out.print ("Please enter the curriculum to be modified:");        Input=in.nextline ();Input=in.nextline (); Edittableinput (tbls,input);} public static void Deletetable (String key,set<table> tbls) {Table tblremove = findtable (key, TBLs); if (tblremove!= NULL) {tbls.remove (tblremove); WriteFile (TBLs); System.out.println ("Delete number: [" +key+ "] course success! ");} Else{system.out.println ("Not retrieved, course number: [" +key+ "] corresponding to the course, delete failed! ");} } public static void Deletetableinput (Set<table> tbls,scanner in) {String input = ""; System.out.print ("Please enter the curriculum to be deleted:"), input =in.next ();d eletetable (input, tbls);}         public static void AddTable (Set<table> tbls,scanner in) {String input= "";                  System.out.print ("Please enter the curriculum to be added:");         Input=in.nextline (); Input=in.nextline (); String key = Input.substring (0,4);  String rest = input.substring (5); Tbls.add (new Table (key,rest)); WriteFile (TBLs); System.out.print ("Add Curriculum:" +key+ "Success!) ");} public static void Printtables (Set<table> tbls) {for (Table tbl:tbls) {System.out.println (TBL);}}}

Test class:

Package Com.chujianyun.com;import Java.util.scanner;import Java.util.set;public class Setdemo {public static void main ( String[] args) {set<table> TBLs = Fileoperator.readfile ();/*for (Table tbl:tbls) {System.out.println (TBL);} */scanner in = new Scanner (system.in); Fileoperator.select (); int choice = 0; while ((Choice =in.nextint ())!=-1) {switch (choice) {case-1: system.exit (0); Break;case 1:fileoperator.findtableinput ( Tbls,in); Break;case 2:fileoperator.deletetableinput (tbls,in); Break;case 3:fileoperator.addtable (Tbls,in); Break;case 4: Fileoperator.printtables (TBLs); Break;case 5:fileoperator.edittableinput (TBLs, in); BREAK;DEFAULT:SYSTEM.OUT.PRINTLN ("Input numeric error please reenter");} Fileoperator.select ();}}}

Effect:


-----------------------------------------

Enter the number to select the function:

1: Find the course according to the course number;

2: Delete the course according to the course number:

3: Add a course record to the curriculum:

4: Show all course records in curriculum

5: Revise curriculum record in curriculum

-1: Exit

-----------------------------------------

Function Selection:4

Table [key=c001, rest= Program Design Foundation 4 Jiang Construction]

Table [key=c002, rest= microcomputer principle and Application 5 sheet steel]

Table [key=c003, rest= advanced mathematics 6 Li Ming]

Table [key=c004, rest= Discrete Mathematics 5 Zhao Society]

Table [key=c005, Rest= World Modern History 2 Chen]

Table [key=c006, rest= data Structure (Java Edition) 8 Wang Kai]

-----------------------------------------

Enter the number to select the function:

1: Find the course according to the course number;

2: Delete the course according to the course number:

3: Add a course record to the curriculum:

4: Show all course records in curriculum

5: Revise curriculum record in curriculum

-1: Exit

-----------------------------------------

Function selection:2

Please enter the curriculum you want to delete:C006

Delete number: [C006] Course success!

-----------------------------------------

Enter the number to select the function:

1: Find the course according to the course number;

2: Delete the course according to the course number:

3: Add a course record to the curriculum:

4: Show all course records in curriculum

5: Revise curriculum record in curriculum

-1: Exit

-----------------------------------------

Function selection:1

Please enter the curriculum you want to find:C005

The corresponding curriculum is: Table [key=c005, Rest= World Modern History 2 Chen]

-----------------------------------------

Enter the number to select the function:

1: Find the course according to the course number;

2: Delete the course according to the course number:

3: Add a course record to the curriculum:

4: Show all course records in curriculum

5: Revise curriculum record in curriculum

-1: Exit

-----------------------------------------

Function Selection:3

Please enter the curriculum you want to add:C006 Chinese Modern history 1 Lin

Add a curriculum: C006 success!

-----------------------------------------

Enter the number to select the function:

1: Find the course according to the course number;

2: Delete the course according to the course number:

3: Add a course record to the curriculum:

4: Show all course records in curriculum

5: Revise curriculum record in curriculum

-1: Exit

-----------------------------------------

Function Selection:4

Table [key=c001, rest= Program Design Foundation 4 Jiang Construction]

Table [key=c002, rest= microcomputer principle and Application 5 sheet steel]

Table [key=c003, rest= advanced mathematics 6 Li Ming]

Table [key=c004, rest= Discrete Mathematics 5 Zhao Society]

Table [key=c005, Rest= World Modern History 2 Chen]

Table [key=c006, rest= History of China 1 Lin]

-----------------------------------------

Enter the number to select the function:

1: Find the course according to the course number;

2: Delete the course according to the course number:

3: Add a course record to the curriculum:

4: Show all course records in curriculum

5: Revise curriculum record in curriculum

-1: Exit

-----------------------------------------

Function Selection:5

Please enter the curriculum you want to modify:C006 Java programming 8 Lin

Change the course number to [C006] success!

-----------------------------------------

Enter the number to select the function:

1: Find the course according to the course number;

2: Delete the course according to the course number:

3: Add a course record to the curriculum:

4: Show all course records in curriculum

5: Revise curriculum record in curriculum

-1: Exit

-----------------------------------------

Function Selection:4

Table [key=c001, rest= Program Design Foundation 4 Jiang Construction]

Table [key=c002, rest= microcomputer principle and Application 5 sheet steel]

Table [key=c003, rest= advanced mathematics 6 Li Ming]

Table [key=c004, rest= Discrete Mathematics 5 Zhao Society]

Table [key=c005, Rest= World Modern History 2 Chen]

Table [key=c006, Rest=java programming 8 Lin]

-----------------------------------------

Enter the number to select the function:

1: Find the course according to the course number;

2: Delete the course according to the course number:

3: Add a course record to the curriculum:

4: Show all course records in curriculum

5: Revise curriculum record in curriculum

-1: Exit

-----------------------------------------

Feature Selection:-1




Data structure (Java version) practice collection Simple Book Management system

Related Article

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.