Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the address Book Management system Project with Java

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces Java how to achieve the address book management system project, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

I. Preface

We have learned for such a long time, it is very sad that we can not use the knowledge of optics, so we should finish it.

Part of the practice of some projects or questions to consolidate our learning knowledge and stimulate our motivation for continuous learning.

Second, the function of creating communication

Add contact information

Delete contact information

Modify contact information

View all contact information

Exit the address book management system

First, define a People class and provide the corresponding get and set methods

(there is a shortcut key Alt+insert in idea to generate get, set methods, and constructors)

Public class People {private String sid; private String name; private String age; private String address; private String num; public People () {} public People (String sid, String name, String age, String address, String num) {this.sid = sid; this.name = name; this.age = age; this.address = address; this.num=num;} public String getSid () {return sid } public void setSid (String sid) {this.sid = sid;} public String getName () {return name;} public void setName (String name) {this.name = name;} public String getAge () {return age;} public void setAge (String age) {this.age = age;} public String getAddress () {return address } public void setAddress (String address) {this.address = address;} public String getNum () {return num;} public void setNum (String num) {this.num = num;}

Add a menu to the People class and call it in the test class.

Public void menu () {System.out.println ("- Welcome to the address Book Management system -"); System.out.println ("1. Add contacts "); System.out.println (" 2. Delete contact "); System.out.println (" 3. Modify contact "); System.out.println (" 4. View all contacts "); System.out.println (" 5. Exit "); System.out.println (" Please select: ");}

In order to get the information we want directly by printing the object, instead of getting it

View all input information, without rewriting toString: [controller.Student@6e0be858]

If you rewrite the toString method, you can read it. You can also use Alt+Inset shortcuts to quickly generate toString methods.

@ Override public String toString () {/ / override the toString method to make it return "[name:" + name+ ", age:" + age+ ", phone:" + num+ ", relation:" + sid+ ", address:" + address+ "]\ n";}}

The above is all in the People class.

We write the following methods under the PeopleManner class.

In main method:

Public static void main (String [] args) {ArrayList array = new ArrayList (); while (true) {People s = new People (); / / create the object and call the method. s. Menu (); Scanner sc1 = new Scanner (System.in); int d = sc1.nextInt (); switch (d) {case 1: {addPeople (array); System.out.println ("contact added successfully"); break } case 2: {deletePeople (array); break;} case 3: {revisePeople (array); break;} case 4: {checkPeople (array); break } case 5: {System.out.println ("quit successfully, thank you for using"); System.exit (0); break;} default: {System.out.println ("input error, please re-enter") }

In the method of adding contacts addPeople

Public static void addPeople (ArrayList array) {Scanner sc=new Scanner (System.in); System.out.println ("Please enter your name"); String name=sc.nextLine (); System.out.println ("Please enter your phone number"); String num=sc.nextLine (); System.out.println ("Please enter your relationship"); String sid=sc.nextLine () System.out.println ("Please enter age"); String age=sc.nextLine (); System.out.println ("Please enter home address"); String address=sc.nextLine (); People s1=new People (); / / create an object, call the set member and set the member's value s1.setAddress (address); s1.setAge (age); s1.setName (name) S1.setSid (sid); s1.setNum (num); array.add (S1); / / add all set variables to the collection}

In the method checkPeople for viewing all contacts

Public static void checkPeople (ArrayList array) {if (array.isEmpty ()) {/ / isEmpty is used to determine that what is not empty is the collection interface. ArrayList implements the list interface, and the list interface inherits the collection interface System.out.println ("No contact information, please add contacts first and then check");} else {System.out.println (array) } / / View all input information, without rewriting toString: / / [controller.Student@6e0be858]}

In the method of deleting contact information

Public static void deletePeople (ArrayList array) {if (array.isEmpty ()) {System.out.println ("No contact information, please add contact information before operation");} else {Scanner sc=new Scanner (System.in); System.out.println ("Please enter the contact name you want to delete"); String num=sc.nextLine () Int sum=0; for (int iTuno Bandi)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report