Ben Blinebury 6 лет назад
Родитель
Сommit
e2bdcd2678
2 измененных файлов: 238 добавлений и 1 удалений
  1. 130
    0
      CurrencyConverter.java
  2. 108
    1
      CurrencyConverterTest.java

+ 130
- 0
CurrencyConverter.java Просмотреть файл

@@ -1,2 +1,132 @@
1
+import java.util.*;
2
+
1 3
 public class CurrencyConverter {
4
+    
5
+    String curr1;
6
+    String curr2;
7
+    int total;
8
+    
9
+    public static void main(String[] args) {
10
+        
11
+        while(true){
12
+        
13
+            CurrencyConverter conv = new CurrencyConverter();
14
+            Scanner kb = new Scanner(System.in);
15
+        
16
+            System.out.println("You may convert any of the following currencies:\n" + 
17
+            "US Dollar = us\nEuro = eu\nBritish Pound = bp\nIndian Rupee = ir\n" +
18
+            "Australian Dollar = ad\nCanadian Dollar = cd\nSingapore Dollar = sd\n" +
19
+            "Swiss Franc = sf\nMalaysian Ringit = mr\n Japanese Yen = jy\n" +
20
+            "Chinese Yuan = cy");
21
+            System.out.println("Please enter your first currency:");
22
+            String curr1 = kb.nextLine();
23
+            System.out.println("Amount?");
24
+            int total = kb.nextInt();
25
+            kb.nextLine();
26
+            System.out.println("Enter the currency you'd like in return:");
27
+            String curr2 = kb.nextLine();
28
+            System.out.println(conv.convert(curr1, curr2, total));
29
+        
30
+        
31
+        }
32
+    }
33
+    
34
+    public double firstCurr(String curr1) {
35
+       
36
+        double firstCurr = 0.0;
37
+        
38
+        switch(curr1) {
39
+            
40
+            case "us" : 
41
+            firstCurr = 1.00;
42
+            break;
43
+            case "eu" :
44
+            firstCurr = 0.94;
45
+            break;
46
+            case "bp" :
47
+            firstCurr = 0.82;
48
+            break;
49
+            case "ir" :
50
+            firstCurr = 68.32;
51
+            break;
52
+            case "ad" :
53
+            firstCurr = 1.35;
54
+            break;
55
+            case "cd" :
56
+            firstCurr = 1.32;
57
+            break;
58
+            case "sd" :
59
+            firstCurr = 1.43;
60
+            break;
61
+            case "sf" :
62
+            firstCurr = 1.01;
63
+            break;
64
+            case "mr" :
65
+            firstCurr = 4.47;
66
+            break;
67
+            case "jy" :
68
+            firstCurr = 115.84;
69
+            break;
70
+            case "cy" :
71
+            firstCurr = 6.92;
72
+            break;
73
+        }
74
+        
75
+        return firstCurr;
76
+     
77
+    }
78
+    
79
+    public double secCurr(String curr2) {
80
+        double secCurr = 0.0;
81
+        
82
+        
83
+        switch(curr2) {
84
+            
85
+            case "us" : 
86
+            secCurr = 1.00;
87
+            break;
88
+            case "eu" :
89
+            secCurr = 0.94;
90
+            break;
91
+            case "bp" :
92
+            secCurr = 0.82;
93
+            break;
94
+            case "ir" :
95
+            secCurr = 68.32;
96
+            break;
97
+            case "ad" :
98
+            secCurr = 1.35;
99
+            break;
100
+            case "cd" :
101
+            secCurr = 1.32;
102
+            break;
103
+            case "sd" :
104
+            secCurr = 1.43;
105
+            break;
106
+            case "sf" :
107
+            secCurr = 1.01;
108
+            break;
109
+            case "mr" :
110
+            secCurr = 4.47;
111
+            break;
112
+            case "jy" :
113
+            secCurr = 115.84;
114
+            break;
115
+            case "cy" :
116
+            secCurr = 6.92;
117
+            break;
118
+        }
119
+        
120
+        return secCurr;
121
+     
122
+    }
123
+    
124
+    public double convert(String curr1, String curr2, int total) {
125
+       double converted; 
126
+       return converted = (secCurr(curr2) * total)/firstCurr(curr1);
127
+    }
128
+    
129
+    
2 130
 }
131
+
132
+

+ 108
- 1
CurrencyConverterTest.java Просмотреть файл

@@ -1,2 +1,109 @@
1
+
2
+import org.junit.Assert;
3
+import org.junit.Test;
4
+
1 5
 public class CurrencyConverterTest {
2
-}
6
+    @Test
7
+    public void firstCurrTest(){
8
+        CurrencyConverter conv = new CurrencyConverter();
9
+        double actual = conv.firstCurr("jy");
10
+        double expected = 115.84;
11
+        Assert.assertEquals(expected, actual,.0001);
12
+    }
13
+
14
+    @Test
15
+    public void getCurrency2test(){
16
+        CurrencyConverter conv = new CurrencyConverter();
17
+        double actual = conv.secCurr("sd");
18
+        double expected = 1.43;
19
+        Assert.assertEquals(expected, actual,.0001);
20
+    }
21
+
22
+    @Test
23
+    public void convertTest1(){
24
+        CurrencyConverter conv = new CurrencyConverter();
25
+        double actual = conv.convert("us","jy",1);
26
+        double expected = 115.84;
27
+        Assert.assertEquals(expected, actual,.0001);
28
+    }
29
+
30
+    @Test
31
+    public void convertDollarToEuro(){
32
+        CurrencyConverter conv = new CurrencyConverter();
33
+        double actual = conv.convert("us","eu",1);
34
+        double expected = .94;
35
+        Assert.assertEquals(expected, actual,.0001);
36
+    }
37
+
38
+    @Test
39
+    public void convertEuroToDollar(){
40
+        CurrencyConverter conv = new CurrencyConverter();
41
+        double actual = conv.convert("eu","us",1);
42
+        double expected = 1.063829;
43
+        Assert.assertEquals(expected, actual,.0001);
44
+    }
45
+
46
+    @Test
47
+    public void convertEuroToBritishPound(){
48
+        CurrencyConverter conv = new CurrencyConverter();
49
+        double actual = conv.convert("eu","bp",1);
50
+        double expected = .8723404;
51
+        Assert.assertEquals(expected, actual,.0001);
52
+    }
53
+
54
+    @Test
55
+    public void convertBritishPoundToIndianRupee(){
56
+        CurrencyConverter conv = new CurrencyConverter();
57
+        double actual = conv.convert("bp","ir",1);
58
+        double expected = 83.31707;
59
+        Assert.assertEquals(expected, actual,.0001);
60
+    } 
61
+
62
+    @Test
63
+    public void convertRupeetoCanadian(){
64
+        CurrencyConverter conv = new CurrencyConverter();
65
+        double actual = conv.convert("ir","cd",1);
66
+        double expected = .01932084;
67
+        Assert.assertEquals(expected, actual,.0001);
68
+    }
69
+
70
+    @Test
71
+    public void convertCanadianToSingapore(){
72
+        CurrencyConverter conv = new CurrencyConverter();
73
+        double actual = conv.convert("cd","sd",1);
74
+        double expected = 1.0833333333;
75
+        Assert.assertEquals(expected, actual,.0001);
76
+    } 
77
+
78
+    @Test
79
+    public void convertSingaporeToSwiss(){
80
+        CurrencyConverter conv = new CurrencyConverter();
81
+        double actual = conv.convert("sd","sf",1);
82
+        double expected = .70629370;
83
+        Assert.assertEquals(expected, actual,.0001);
84
+    }
85
+
86
+    @Test
87
+    public void convertSwissToMalaysian(){
88
+        CurrencyConverter conv = new CurrencyConverter();
89
+        double actual = conv.convert("sf","mr",1);
90
+        double expected = 4.4257425;
91
+        Assert.assertEquals(expected, actual,.0001);
92
+    }    
93
+
94
+    @Test
95
+    public void convertMalaysianToYen(){
96
+        CurrencyConverter conv = new CurrencyConverter();
97
+        double actual = conv.convert("mr","jy",1);
98
+        double expected = 25.9149888;
99
+        Assert.assertEquals(expected, actual,.0001);
100
+    }
101
+
102
+    @Test
103
+    public void convertYenToYuan(){
104
+        CurrencyConverter conv = new CurrencyConverter();
105
+        double actual = conv.convert("jy","cy",1);
106
+        double expected = .05973756;
107
+        Assert.assertEquals(expected, actual,.0001);    
108
+    }
109
+}