Skip to content

Commit 475bd23

Browse files
committed
added exercise7
1 parent f071233 commit 475bd23

19 files changed

Lines changed: 95 additions & 0 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#Tue Nov 05 09:48:36 CST 2013
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
apply plugin: 'java'
2+
apply plugin:'application'
3+
mainClassName = "Main"
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class Account {
2+
String name;
3+
String number;
4+
Integer balance;
5+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Bank {
2+
static Account account(String name, String number, Integer balance) {
3+
Account acct = new Account();
4+
acct.name = name;
5+
acct.number = number;
6+
acct.balance = balance;
7+
return acct;
8+
}
9+
10+
static void deposit(Account acct, Integer amount) {
11+
if(amount <= 0) {
12+
throw new IllegalArgumentException("amount must be positive");
13+
}
14+
acct.balance += amount;
15+
}
16+
17+
static void withdraw(Account acct, Integer amount) {
18+
if(amount > acct.balance) {
19+
throw new RuntimeException("balance not enough");
20+
}
21+
acct.balance -= amount;
22+
}
23+
24+
static String toString(Account acct) {
25+
return String.format("Account(%s, %s, %d)",
26+
acct.name, acct.number, acct.balance);
27+
}
28+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
public class Main {
2+
public static void main(String[] args) {
3+
Account acct = Bank.account("Java", "001", 100);
4+
Bank.deposit(acct, 500);
5+
Bank.withdraw(acct, 200);
6+
System.out.println(Bank.toString(acct));
7+
}
8+
}

0 commit comments

Comments
 (0)