Java Learning 3 Create student properties: study number, name, telephone fully implement the object collection of additions and deletions to search. (Console to do)

Source: Internet
Author: User
Tags stub

First create a student class which includes get,set and constructors

/**
*
*/
Package WORK2;

/**
* @author Administrator
*
*/
Public final class Student {
Private String ID;
private int tell;
private String name;
/**
* Initialize Data
*/
Public Student (String id,int tell,string name) {//constructor assigns an initial value
TODO Auto-generated constructor stub
This.id=id;
This.tell=tell;
This.name=name;
}

/**
*
* @param incoming data set and get method, data security, gave the method to pass in the data itself
*/

public void SetId (String id) {
This.id=id;
}
public void Settell (int tell) {
This.tell=tell;
}
public void SetName (String name) {
This.name=name;
}
Public String getId () {
return ID;
}
public int Geitell () {
return tell;
}
Public String Geiname () {
return name;
}
}

In creating a MyClass class including adding additions and deletions to the search method

Package WORK2;

Import java.util.ArrayList;
Import java.util.List;

public class MyClass {
Set up a list container to load data
Private list<student> stulist;

Public MyClass () {
Stulist=new arraylist<student> ();//Create a Stu Object
}

public boolean ishere (Student stu) {//encapsulation method for judging a number
Boolean is=false;
for (Student s:stulist) {//traverse for each
if (Stu.getid (). Equals (S.getid ()))//The object data passed in the container = traversed object data exists, otherwise does not exist
Is=true;
}
return is;//returns judgment
}

Public boolean Add (Student stu) {
First, determine if there is
Boolean is=this.ishere (Stu);
if (!is) {
Stulist.add (Stu);
}
return!is;
}

public boolean upDate (Student stu) {
First, determine if there is
Boolean is=this.ishere (Stu);
for (Student s:stulist) {
if (Stu.getid (). Equals (S.getid ())) {
Stulist.remove (s);
Stulist.add (Stu);
}
}
return is;
}

public Boolean delete (String ID) {
Boolean issuc=false;
for (Student s:stulist) {
if (Id.equals (S.getid ())) {
Stulist.remove (s);
Issuc=true;
}
}
return ISSUC;
}

Public list<student> query () {
return this.stulist;


}
}

Finally create a mainframe class using the Model View Controller

By calling the method in MyClass to implement the

Package WORK2;

Import java.util.List;
Import Java.util.Scanner;

Import Javax.swing.plaf.synth.SynthStyle;

public class MainFrame {
Public MyClass mc;//Object
Private Scanner sc=new Scanner (system.in);
Public MainFrame () {
TODO Auto-generated constructor stub
Mc=new MyClass ();
}
Public Student GetInfo () {//constructor with parameters
System.out.println ("Please enter the study number");
String Id=sc.next ();
System.out.println ("Please enter the telephone");
int Tell=sc.nextint ();
System.out.println ("Please enter name");
String Name=sc.next ();
Student stu=new Student (ID, tell, name);//data encapsulated into objects,
return Stu;

}
public void Add () {
Student stu=this.getinfo ();//Receive data from the console
Boolean Is=mc.add (Stu);
if (IS) {
System.err.println ("add success");

}
Else
System.out.println ("Add failed");
}

public void Update () {
Student Stu=this.getinfo ();
Boolean is=mc.update (Stu);
if (IS) {
SYSTEM.OUT.PRINTLN ("Modification/success");
}else
SYSTEM.OUT.PRINTLN ("modification failed");
}
public void Delete () {
System.out.println ("Please enter the student's number to delete");
String Id=sc.next ();
Boolean is=mc.delete (ID);
if (IS) {
System.out.println ("delete succeeded");
}else
System.out.println ("Delete failed, student does not exist");

}

public void Quere () {
List<student> Stulist=mc.query ();
for (Student s:stulist) {
System.out.println ("ID:" +s.getid () + "Tell:" +s.geitell () + "Name:" +s.geiname ());

}
}

public void Start () {
Boolean is=true;
while (IS) {
System.out.println ("Enter the option you want to operate:");
System.out.println ("1, increase");
System.out.println ("2. Delete");
System.out.println ("3, modify");
System.out.println ("4, find");
System.out.println ("5, exit");
int Option=sc.nextint ();
Switch (option) {
Case 1:
System.out.println ("Start adding");
Add ();
Break

Case 2:
SYSTEM.OUT.PRINTLN ("Start modification");
Update ();
Break
Case 3:
System.out.println ("Start query");
Quere ();
Break
Case 4:
System.out.println ("start delete");
Delete ();
Break
Case 5:
Is=false;
System.out.println ("Welcome to use, see you next time!! ");
}
}

}
public static void Main (string[] args) {
MainFrame mf=new MainFrame ();
Mf.start ();
}

}

Java Learning 3 Create student properties: study number, name, telephone fully implement the object collection of additions and deletions to search. (Console to do)

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.