查找会员积分
Posted wangsihui
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了查找会员积分相关的知识,希望对你有一定的参考价值。
1 package homework20191126;
2
3 public class User {
4
5 private String id;
6 private int num;
7
8 @Override
9 public int hashCode() {
10 final int prime = 31;
11 int result = 1;
12 result = prime * result + ((id == null) ? 0 : id.hashCode());
13 return result;
14 }
15 @Override
16 public boolean equals(Object obj) {
17 if (this == obj)
18 return true;
19 if (obj == null)
20 return false;
21 if (getClass() != obj.getClass())
22 return false;
23 User other = (User) obj;
24 if (id == null) {
25 if (other.id != null)
26 return false;
27 } else if (!id.equals(other.id))
28 return false;
29 return true;
30 }
31
32 @Override
33 public String toString() {
34 return "User [id=" + id + ", num=" + num + "]";
35 }
36
37 public User() {
38
39 }
40
41 public User(String id, int num) {
42 this.id = id;
43 this.num = num;
44 }
45
46 public String getId() {
47 return id;
48 }
49 public void setId(String id) {
50 this.id = id;
51 }
52 public int getNum() {
53 return num;
54 }
55 public void setNum(int num) {
56 this.num = num;
57 }
58 }
1 package homework20191126; 2 3 import java.util.ArrayList; 4 5 public class Usermanager { 6 7 private ArrayList list = new ArrayList(); 8 9 public void add(User user) { 10 list.add(user); 11 } 12 13 public void showAll() { 14 System.out.println("***会员列表***"); 15 System.out.println("编号 积分"); 16 User user; 17 for(Object obj : list) { 18 user = (User)obj; 19 System.out.println(user.getId()+" "+user.getNum()); 20 } 21 } 22 23 public User find(String id) { 24 User user = new User(); 25 user.setId(id); 26 27 int index = list.indexOf(user); 28 if(index == -1) { 29 return null; 30 }else { 31 return (User)list.get(index); 32 } 33 } 34 }
1 package homework20191126;
2
3 import java.util.Scanner;
4
5 public class Testuser1 {
6
7 public static void main(String[] args) {
8 Scanner input = new Scanner(System.in);
9
10 String id;
11 int num;
12 User user;
13
14 Usermanager m = new Usermanager();
15
16 for(int i = 0; i < 4;i++) {
17 System.out.print("请输入会员编号:");
18 id = input.next();
19 System.out.print("请输入会员积分:");
20 num = input.nextInt();
21
22 user = new User(id, num);
23 m.add(user);
24 }
25
26 m.showAll();
27
28 System.out.println("请输入要查找的会员编号:");
29 id = input.next();
30
31 User user2 = m.find(id);
32
33 if(user2 == null) {
34 System.out.println("没有找到");
35 }else {
36 System.out.println("该会员积分为:"+user2.getNum());
37 }
38 }
39 }
以上是关于查找会员积分的主要内容,如果未能解决你的问题,请参考以下文章