forked from lim-at-infinity/CIS18C-Final-Project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUsers.java
153 lines (139 loc) · 3.19 KB
/
Users.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package tester_2;
import java.util.ArrayList;
public class Users
{
private class User
{
private String name;
private int id;
private ArrayList<String> books;
public User(String name, int id)
{
this.name = name;
this.id = id;
books = new ArrayList<>();
}
//Getters
public int getId()
{
return id;
}
public String getName()
{
return name;
}
public ArrayList<String> getCheckedOutBooks()
{
return books;
}
}
private ArrayList<User> users;
//Constructor for Users public class
public Users()
{
users = new ArrayList<User>();
}
public String addUser(String name, int id)
{
try
{
User tmp = new User(name, id);
users.add(tmp);
return "User added!";
}
catch(Exception e)
{
return "Error adding user: " + e;
}
}
// Show users
public String displayUsers()
{
String ans = "";
for(User u : users)
{
ans += u.getName() + ", id: " + u.getId() + ".\n";
}
return ans;
}
// calls binary search, and decides what to return depending on return value
public String searchById(int id)
{
String ans = binarySearch(0, users.size()-1, id);
if(ans == null)
{
return "User not found!";
}
else
{
return ans;
}
}
// binary search performed on the already sorted users arrayList.
private String binarySearch(int start, int end, int target)
{
if(start > end)
{
return null;
}
int mid = start + (end-start)/2;
//System.out.println("start: " + start + ", mid: " + mid +", end: " + end);
if(users.get(mid).getId() == target)
{
return users.get(mid).getName();
}
if(target > users.get(mid).getId())
{
return binarySearch(mid+1, end, target);
}
else return binarySearch(start, mid-1, target);
}
// Sort users by id, call quickSort
public void sortUsers()
{
quickSort(0, users.size()-1);
}
// quicksort
private void quickSort(int lo, int hi)
{
// if the the part of the array has already been sorted
if(lo >= hi)
{
return;
}
int p = partition(lo, hi);
quickSort(lo, p-1);
quickSort(p+1, hi);
}
/*
* To find out where the index of partition will be.
* For simplicity purposes we will always choose pivot to be the last element,
* but the effiency could be improved by choosing a random pivot.
*/
private int partition(int lo, int hi)
{
int i = lo;
for(int j = i; j < hi; j++)
{
if(users.get(j).getId() < users.get(hi).getId())
{
swap(i, j);
i++;
}
}
swap(i, hi);
return i;
}
// to swap elements in the users array list
private void swap(int i, int j)
{
User tmp = users.get(i);
users.set(i, users.get(j));
users.set(j, tmp);
}
}