-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAccount.java
156 lines (122 loc) · 3.76 KB
/
Account.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
154
155
156
package SpaceInvaders;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Font;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import java.util.ArrayList;
public class Account extends JFrame{
public static ArrayList<Account> accounts = new ArrayList<Account>();
public static ArrayList<Score> scores = new ArrayList<Score>();
public static Account account;
private JTextField textField;
private JPasswordField passwordField;
private String username;
private String password;
private int health = 3;
private int score = 0;
private int level = 1;
public Account(){
setBounds(100, 100, 480, 360);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setLayout(null);
setVisible(true);
JLabel newLabel_1 = new JLabel("Username: ");
newLabel_1.setFont(new Font("Terminal", Font.BOLD, 15));
newLabel_1.setBounds(10, 45, 100, 15);
getContentPane().add(newLabel_1);
textField = new JTextField();
textField.setBounds(100, 38, 350, 25);
getContentPane().add(textField);
textField.setColumns(10);
JLabel newLabel_2 = new JLabel("Password: ");
newLabel_2.setFont(new Font("Terminal", Font.BOLD, 15));
newLabel_2.setBounds(10, 90, 100, 15);
getContentPane().add(newLabel_2);
passwordField = new JPasswordField();
passwordField.setBounds(100, 83, 350, 25);
getContentPane().add(passwordField);
JLabel newLabel_3 = new JLabel("");
newLabel_3.setBounds(10, 135, 350, 25);
getContentPane().add(newLabel_3);
JButton newButton = new JButton("Create Account");
newButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
username = textField.getText();
password = new String(passwordField.getPassword());
String string = "";
if(password.length() == 0 && username.length() == 0)
string = "Password and username are empty!";
else if(password.length() == 0)
string = "Password is empty!";
else if(username.length() == 0)
string = "Username is empty!";
else if(hasBlank(password) && hasBlank(username))
string = "Password and username cannot contain spaces!";
else if(hasBlank(password))
string = "Password cannot contain spaces!";
else if(hasBlank(username))
string = "Username cannot contain spaces!";
else if(usernameTaken(username))
string = "This username taken!";
if (string.equals(""))
setVisible(false);
newLabel_3.setText(string);
}
private boolean usernameTaken(String username){
for(int i = 0; i < accounts.size() - 1; i++){
if(username.equals(accounts.get(i).getUsername()))
return true;
}
return false;
}
private boolean hasBlank(String string){
for(int i = 0; i < string.length(); i++){
if(string.charAt(i) == ' ')
return true;
}
return false;
}
});
newButton.setFont(new Font("Terminal", Font.BOLD, 15));
newButton.setBounds(250, 200, 175, 75);
add(newButton);
}
public String getUsername(){
return username;
}
public String getPassword(){
return password;
}
public int getHealth(){
return health;
}
public int getScore(){
return score;
}
public int getLevel(){
return level;
}
public void setHealth(int health){
this.health = health;
}
public void setScore(int score){
this.score = score;
}
public void setLevel(int level){
this.level = level;
}
public void decreaseHealth(){
health--;
}
public void addScore(int add){
score = score + add;
}
public void increaseLevel(){
level++;
}
}