-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAccount.java
151 lines (125 loc) · 3.61 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
/*
Created by Christopher Benda
for CS1050, spring 2011
*/
import java.util.Scanner;
public class Account
{
// add instance variables, constructor, and instance methods here:
String name;
Boolean loggedIn;
int balance, password;
public Account(String nameIn, int passwordIn)
{
name = nameIn;
password = passwordIn;
balance = 0;
loggedIn = false;
}
// deposit method
public void deposit(int depositIn)
{
if(depositIn > 0)
{
balance+=depositIn;
}
else if(depositIn <=0)
{
System.out.println("Amount to deposit must be positive");
}
}
// transferTo method
public void transferTo(Account other, int amount)
{
System.out.println(this.name + " requesting transfer of " + amount + " to " + other.name);
if(loggedIn == true)
{
if(amount <=0 || amount > balance)
{
System.out.println("Could not transfer (amount illegal)");
}
else if(amount > 0 && amount <= balance )
{
other.balance += amount;
balance -= amount;
}
}
else
{
System.out.println("Could not transfer (not logged in)");
}
}
// toString method
public String toString()
{
String out= new String("[" +name + " " + balance + " " +loggedIn + "]");
return out;
}
// login method
public void logIn()
{
String nameIn;
int pass1In, pass2In;
System.out.println("Logging in...");
Scanner keys = new Scanner(System.in);
System.out.print("Enter name: ");
nameIn = keys.nextLine();
if(nameIn.equals(name) )
{
System.out.print("Enter first password: ");
pass1In= keys.nextInt();
System.out.print("enter second password: ");
pass2In = keys.nextInt();
if(pass1In > 1 && pass1In < pass2In)
{
if(password == (pass1In*pass2In))
{
loggedIn= true;
System.out.println("Welcome, "+name);
}
else
{
System.out.println("Incorrect password");
}
}
else
{
System.out.println("Incorrect password");
}
}
else
{
System.out.println("Unknown account name");
}
}
// logout method
public void logOut()
{
loggedIn = false;
}
public static void main(String[] args)
{
Account a = new Account( "Bill", 794377 );
Account b = new Account( "George", 807661 );
a.deposit( -100 ); // test whether a negative amount can be deposited
a.deposit( 1000 ); // test whether deposits can be made even if not
b.deposit( 300 ); // logged in (and prepare for rest of the tests)
// verify that the accounts have the money in them
System.out.println("At start, first account is " + a.toString() );
System.out.println(" second account is " + b.toString() );
a.transferTo( b, 500 ); // test can't transfer from a if not logged in
a.logIn(); // test refusing login if wrong name
a.logIn(); // test refusing login if wrong pass numbers
a.logIn(); // test logging in correctly
a.transferTo( b, -500 ); // test can't transfer negative amount
a.transferTo( b, 1500 ); // test can't transfer more than you have
a.transferTo( b, 500 ); // test a legal transfer
// verify correct transfer
System.out.println("After transfer, first account is " + a.toString() );
System.out.println(" second account is " + b.toString() );
a.logOut();
// verify a logged out
System.out.println("At end, first account is " + a.toString() );
System.out.println(" second account is " + b.toString() );
}
}