-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayer.java
More file actions
103 lines (86 loc) · 2.26 KB
/
Player.java
File metadata and controls
103 lines (86 loc) · 2.26 KB
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
import java.text.DecimalFormat;
import java.util.*;
public class Player {
static Scanner scan = new Scanner(System.in);
private Name name;
private Date dob;
private double moneyBalance;
//constructors
public Player() {
makePlayer();
}
public Player(Name name, Date dob, double moneyBalance) {
setName(name);
setDob(dob);
setMoneyBalance(moneyBalance);
}
//end constructors
//setter and getter
public Name getName() {
return name;
}
public void setName(Name name) {
this.name = name;
}
public Date getDob() {
return dob;
}
public void setDob(Date dob) {
this.dob = dob;
}
public double getMoneyBalance() {
return moneyBalance;
}
public void setMoneyBalance(double moneyBalance) {
boolean badInput = false;
while(moneyBalance < 0.00) {
do {
try {
badInput = false;
System.out.println("You cannot set a negative value as your money balance, try again: ");
moneyBalance = scan.nextDouble();
}catch(InputMismatchException e) {
badInput = true;
System.out.println("You entered an invalid value.");
scan.next();
}
}while(badInput);
}
this.moneyBalance = moneyBalance;
}
//end setter and getter
public void makePlayer() {
boolean badInput = false;
//name
System.out.println("-----ENTER NAME-----");
name = new Name();
setName(name);
//date of birth
System.out.println();
System.out.println("-----ENTER DATE OF BIRTH-----" );
dob = new Date();
setDob(dob);
//money balance
System.out.println();
System.out.println("-----ENTER MONEY BALANCE-----");
do {
try {
badInput = false;
System.out.println("Enter your money balance: ");
moneyBalance = scan.nextDouble();
}catch(InputMismatchException e) {
badInput = true;
System.out.println("You entered an invalid value.");
scan.next();
}
}while(badInput);
setMoneyBalance(moneyBalance);
}//end makePlayer
public String toString() {
DecimalFormat df = new DecimalFormat("$#,###.##");
String result = name + "\n";
result += "Date of birth: " + dob + "\n";
result += "Money balance: " + df.format(moneyBalance) + "\n";
return result;
}//end toString
}//end Player Class