Skip to content

Commit 7787fbd

Browse files
author
Sky
committed
Add Exception to the System
1 parent 6ca1850 commit 7787fbd

File tree

7 files changed

+436
-88
lines changed

7 files changed

+436
-88
lines changed

src/controller/manager/AssetManager.java

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@ public class AssetManager {
2121
private static Vector<Motorcycle> motorcycles = new Vector<>();
2222
private static Vector<Gold> golds = new Vector<>();
2323
private static Vector<BankAccount> bankAccounts = new Vector<>();
24-
25-
Vector assets = new Vector();
26-
24+
2725
// Add to the Vector to contains All
2826
public static int addLand(Land land){
2927
return lands.add(land) ? 1 : 0;
@@ -56,7 +54,27 @@ public static Vector<Land> getLands()
5654
return new Vector<>(lands);
5755
}
5856

57+
public static Vector<House> getHouses()
58+
{
59+
return new Vector<>(houses);
60+
}
61+
5962
public static Vector<Car> getCars(){
6063
return new Vector<>(cars);
6164
}
65+
66+
public static Vector<Motorcycle> getMotorcycles()
67+
{
68+
return new Vector<>(motorcycles);
69+
}
70+
71+
public static Vector<Gold> getGolds()
72+
{
73+
return new Vector<>(golds);
74+
}
75+
76+
public static Vector<BankAccount> getBankAccounts()
77+
{
78+
return new Vector<>(bankAccounts);
79+
}
6280
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package controller.validator;
2+
3+
public class InvalidNumberException extends Exception{
4+
private static final long serialVersionUID = 1L;
5+
6+
public InvalidNumberException(String val1){
7+
super(val1 + " is not a number.");
8+
}
9+
10+
public InvalidNumberException(String val1, String val2){
11+
super(val1 + " or " + val2 + " is not a number.");
12+
}
13+
14+
public InvalidNumberException(String val1, String val2, String val3){
15+
super(val1 + " or " + val2 + " or " + val3 + " is not a number.");
16+
}
17+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package controller.validator;
2+
3+
public class MinimumNumberException extends Exception {
4+
private static final long serialVersionUID = 1L;
5+
6+
// House, Car, Motorcycle
7+
public MinimumNumberException(double val1, int maximum) {
8+
super("Input value " + val1 + " must be greater than or equals to " + maximum + ".");
9+
}
10+
11+
// Land, BankAccount
12+
public MinimumNumberException(double val1, double val2, int maximum) {
13+
super("Input value " + val1 + " or " + val2 + " must be greater than or equals to " + maximum + ".");
14+
}
15+
16+
// Gold
17+
public MinimumNumberException(double val1, double val2, double val3, int maximum) {
18+
super("Input value " + val1 + " or " + val2 + " or " + val3 + " must be greater than or equals to " + maximum + ".");
19+
}
20+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package controller.validator;
2+
3+
public class Validator {
4+
// House, Car, Motorcycle
5+
public static void validate(String val1, boolean required)throws MinimumNumberException,InvalidNumberException
6+
{
7+
double number = 0;
8+
try {
9+
number = Double.parseDouble(val1);
10+
11+
} catch (NumberFormatException e) {
12+
throw new InvalidNumberException(val1);
13+
}
14+
15+
// Check minimum value
16+
if(number < 0)
17+
{
18+
throw new MinimumNumberException(number, 0);
19+
}
20+
}
21+
22+
// Land, Bank Account
23+
public static void validate(String val1, String val2, boolean required)throws MinimumNumberException,InvalidNumberException
24+
{
25+
double number1 = 0, number2 = 0;
26+
27+
try {
28+
number1 = Double.parseDouble(val1);
29+
number2 = Double.parseDouble(val2);
30+
31+
} catch (NumberFormatException e) {
32+
throw new InvalidNumberException(val1, val2);
33+
}
34+
35+
// Check minimum value
36+
if(number1 < 0 || number2 < 0)
37+
{
38+
throw new MinimumNumberException(number1, number2, 0);
39+
}
40+
}
41+
42+
// Gold
43+
public static void validate(String val1, String val2, String val3, boolean required)throws MinimumNumberException, InvalidNumberException
44+
{
45+
46+
double number1 = 0, number2 = 0, number3 = 0;
47+
48+
try {
49+
number1 = Double.parseDouble(val1);
50+
number2 = Double.parseDouble(val2);
51+
number3 = Double.parseDouble(val3);
52+
53+
} catch (NumberFormatException e) {
54+
throw new InvalidNumberException(val1, val2, val3);
55+
}
56+
57+
// Check minimum value
58+
if(number1 < 0 || number2 < 0 || number3 < 0)
59+
{
60+
throw new MinimumNumberException(number1, number2, number3, 0);
61+
}
62+
}
63+
64+
}

src/model/BankAccount.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,15 @@ public void setInterestRate(double interestRate) {
4747

4848
public double calculateMonetaryValue()
4949
{
50-
return this.balance * (1 + this.interestRate);
50+
double total = 0;
51+
52+
try {
53+
total = this.balance * (1 + this.interestRate);
54+
}catch(ArithmeticException e) {
55+
System.out.println("Please Check Value key in to the system, Error in Calculating your Monetary Value.");
56+
}
57+
58+
return total;
5159
}
5260

5361
public void deposit(double amount)

src/view/AssetManagementSystem.java

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,14 @@
55
import view.AssetView;
66
import view.View;
77

8+
/*
9+
* Author: Quek Yao Jing B031810136
10+
*/
11+
812
public class AssetManagementSystem extends View {
913

1014
public void displayOptions(){
11-
System.out.println("Welcome to Asset Management System");
15+
System.out.println("Welcome to Wealth Management System");
1216
System.out.println("---------------------------------------");
1317
System.out.println("1 \t Manage Asset");
1418
System.out.println("0 \t Exit");
@@ -19,18 +23,11 @@ public void processOption(Scanner scanner, int choice) {
1923
case 1: {
2024
AssetView av = new AssetView();
2125
av.displayOptions();
22-
av.selectOption(scanner, 9); // I use '0' as escape character
26+
av.selectOption(scanner, 9);
2327

2428
displayOptions();
2529
break;
2630

27-
} case 2: {
28-
System.out.println("You've selected: Manage Customers");
29-
break;
30-
31-
} case 3: {
32-
System.out.println("You're selected: Manage Rentals");
33-
break;
3431
}
3532
}
3633
}
@@ -41,7 +38,7 @@ public static void main(String args[]){
4138
AssetManagementSystem assems = new AssetManagementSystem();
4239

4340
assems.displayOptions();
44-
assems.selectOption(scanner, 3); // I use '0' as escape character
41+
assems.selectOption(scanner, 3);
4542
}
4643

4744
}

0 commit comments

Comments
 (0)