Skip to content

Commit 46f31a8

Browse files
authored
Merge pull request #7 from formidablae/1-what-are-classes-objects-and-methods/1.2-learn-java-methods
Did 1 What are Classes Objects and Methods / 1.2 Learn Java Methods
2 parents 2e5818d + d6e50be commit 46f31a8

File tree

13 files changed

+404
-0
lines changed

13 files changed

+404
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
public class SavingsAccount {
2+
3+
int balance;
4+
5+
public SavingsAccount(int initialBalance){
6+
balance = initialBalance;
7+
}
8+
9+
public static void main(String[] args){
10+
SavingsAccount savings = new SavingsAccount(2000);
11+
12+
//Check balance:
13+
System.out.println("Hello!");
14+
System.out.println("Your balance is "+savings.balance);
15+
16+
//Withdrawing:
17+
int afterWithdraw = savings.balance - 300;
18+
savings.balance = afterWithdraw;
19+
System.out.println("You just withdrew "+300);
20+
21+
//Check balance:
22+
System.out.println("Hello!");
23+
System.out.println("Your balance is "+savings.balance);
24+
25+
//Deposit:
26+
int afterDeposit = savings.balance + 600;
27+
savings.balance = afterDeposit;
28+
System.out.println("You just deposited "+600);
29+
30+
//Check balance:
31+
System.out.println("Hello!");
32+
System.out.println("Your balance is "+savings.balance);
33+
34+
//Deposit:
35+
int afterDeposit2 = savings.balance + 600;
36+
savings.balance = afterDeposit2;
37+
System.out.println("You just deposited "+600);
38+
39+
//Check balance:
40+
System.out.println("Hello!");
41+
System.out.println("Your balance is "+savings.balance);
42+
43+
}
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import java.io.ByteArrayOutputStream;
2+
import java.io.PrintStream;
3+
4+
import org.junit.Test;
5+
import org.junit.Before;
6+
import org.junit.After;
7+
import static org.junit.Assert.assertEquals;
8+
import static org.junit.Assert.assertThat;
9+
import static org.hamcrest.CoreMatchers.instanceOf;
10+
import static org.hamcrest.CoreMatchers.containsString;
11+
12+
public class LETest {
13+
private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
14+
private final ByteArrayOutputStream errContent = new ByteArrayOutputStream();
15+
private final PrintStream originalOut = System.out;
16+
private final PrintStream originalErr = System.err;
17+
18+
@Before
19+
public void setUpStreams() {
20+
System.setOut(new PrintStream(outContent));
21+
System.setErr(new PrintStream(errContent));
22+
}
23+
24+
@After
25+
public void restoreStreams() {
26+
System.setOut(originalOut);
27+
System.setErr(originalErr);
28+
}
29+
30+
@Test
31+
public void someTest() {
32+
String expected = "Hello world!";
33+
Store store = new Store("Lemonade");
34+
store.advertise();
35+
assertThat("Does your code print 'Hello World!'?", outContent.toString(), containsString(expected));
36+
}
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
public class Store {
2+
// instance fields
3+
String productType;
4+
5+
// constructor method
6+
public Store(String product) {
7+
productType = product;
8+
}
9+
10+
// advertise method
11+
public void advertise() {
12+
System.out.println("Come spend some money!");
13+
System.out.println("Selling " + productType + "!");
14+
}
15+
16+
// main method
17+
public static void main(String[] args) {
18+
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
public class Store {
2+
// instance fields
3+
String productType;
4+
5+
// constructor method
6+
public Store(String product) {
7+
productType = product;
8+
}
9+
10+
// advertise method
11+
public void advertise() {
12+
System.out.println("Selling " + productType + "!");
13+
System.out.println("Come spend some money!");
14+
}
15+
16+
// main method
17+
public static void main(String[] args) {
18+
Store lemonadeStand = new Store("Lemonade");
19+
lemonadeStand.advertise();
20+
lemonadeStand.advertise();
21+
lemonadeStand.advertise();
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
public class Store {
2+
// instance fields
3+
String productType;
4+
5+
// constructor method
6+
public Store(String product) {
7+
productType = product;
8+
}
9+
10+
// advertise method
11+
public void advertise() {
12+
String message = "Selling " + productType + "!";
13+
System.out.println(message);
14+
}
15+
16+
// main method
17+
public static void main(String[] args) {
18+
String cookie = "Cookies";
19+
Store cookieShop = new Store(cookie);
20+
21+
cookieShop.advertise();
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
public class Store {
2+
// instance fields
3+
String productType;
4+
5+
// constructor method
6+
public Store(String product) {
7+
productType = product;
8+
}
9+
10+
// advertise method
11+
public void advertise() {
12+
String message = "Selling " + productType + "!";
13+
System.out.println(message);
14+
}
15+
16+
public void greetCustomer(String customer) {
17+
System.out.println("Welcome to the store, " + customer + "!");
18+
}
19+
20+
// main method
21+
public static void main(String[] args) {
22+
Store lemonadeStand = new Store("Lemonade");
23+
lemonadeStand.greetCustomer("Kate");
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import static org.junit.Assert.assertEquals;
2+
import org.junit.Test;
3+
4+
public class LETest {
5+
@Test
6+
public void increasesPriceByOne() {
7+
Store store = new Store("Lemonade", 1);
8+
store.increasePrice(1);
9+
double expected = 2;
10+
double actual = store.price;
11+
assertEquals("We tested increasing the price by 1 for a store with a starting price of 1, and got the wrong price!", expected, actual, 0.05);
12+
}
13+
14+
@Test
15+
public void increasesPriceByTen() {
16+
Store store = new Store("Lemonade", 1);
17+
store.increasePrice(10);
18+
double expected = 11;
19+
double actual = store.price;
20+
assertEquals("We tested increasing the price by 10 for a store with a starting price of 1, and got the wrong price!", expected, actual, 0.05);
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
public class Store {
2+
// instance fields
3+
String productType;
4+
double price;
5+
6+
// constructor method
7+
public Store(String product, double initialPrice) {
8+
productType = product;
9+
price = initialPrice;
10+
}
11+
12+
// increase price method
13+
public void increasePrice(double priceToAdd){
14+
double newPrice = price + priceToAdd;
15+
price = newPrice;
16+
}
17+
18+
// main method
19+
public static void main(String[] args) {
20+
Store lemonadeStand = new Store("Lemonade", 3.75);
21+
lemonadeStand.increasePrice(1.5);
22+
System.out.println(lemonadeStand.price);
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
public class Store {
2+
// instance fields
3+
String productType;
4+
double price;
5+
6+
// constructor method
7+
public Store(String product, double initialPrice) {
8+
productType = product;
9+
price = initialPrice;
10+
}
11+
12+
// increase price method
13+
public void increasePrice(double priceToAdd){
14+
double newPrice = price + priceToAdd;
15+
price = newPrice;
16+
}
17+
18+
// get price with tax method
19+
public double getPriceWithTax(){
20+
double tax = 0.08;
21+
double totalPrice = price + price * tax;
22+
return totalPrice;
23+
}
24+
25+
// main method
26+
public static void main(String[] args) {
27+
Store lemonadeStand = new Store("Lemonade", 3.75);
28+
lemonadeStand.getPriceWithTax()
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
public class Store {
2+
// instance fields
3+
String productType;
4+
double price;
5+
6+
// constructor method
7+
public Store(String product, double initialPrice) {
8+
productType = product;
9+
price = initialPrice;
10+
}
11+
12+
// increase price method
13+
public void increasePrice(double priceToAdd){
14+
double newPrice = price + priceToAdd;
15+
price = newPrice;
16+
}
17+
18+
// get price with tax method
19+
public double getPriceWithTax(){
20+
double tax = 0.08;
21+
double totalPrice = price + price*tax;
22+
return totalPrice;
23+
}
24+
25+
// toString method
26+
public String toString() {
27+
return "This store sells " + productType + " at a price of " + price + ".";
28+
}
29+
30+
// main method
31+
public static void main(String[] args) {
32+
Store lemonadeStand = new Store("Lemonade", 3.75);
33+
Store cookieShop = new Store("Cookies", 5);
34+
System.out.println(lemonadeStand);
35+
System.out.println(cookieShop);
36+
}
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import java.io.ByteArrayOutputStream;
2+
import java.io.PrintStream;
3+
4+
import org.junit.Test;
5+
import org.junit.Before;
6+
import org.junit.After;
7+
import static org.junit.Assert.assertEquals;
8+
import static org.junit.Assert.assertThat;
9+
import static org.hamcrest.CoreMatchers.instanceOf;
10+
import static org.hamcrest.CoreMatchers.containsString;
11+
12+
public class LETest {
13+
private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
14+
private final ByteArrayOutputStream errContent = new ByteArrayOutputStream();
15+
private final PrintStream originalOut = System.out;
16+
private final PrintStream originalErr = System.err;
17+
18+
@Before
19+
public void setUpStreams() {
20+
System.setOut(new PrintStream(outContent));
21+
System.setErr(new PrintStream(errContent));
22+
}
23+
24+
@After
25+
public void restoreStreams() {
26+
System.setOut(originalOut);
27+
System.setErr(originalErr);
28+
}
29+
30+
@Test
31+
public void checkHello() {
32+
String expected = "Hello!";
33+
SavingsAccount savingsTest = new SavingsAccount(2000);
34+
35+
savingsTest.checkBalance();
36+
37+
assertThat("Does your `checkBalance()` method print \"Hello!\"?", outContent.toString(), containsString(expected));
38+
}
39+
40+
@Test
41+
public void checkYourBalanceIs() {
42+
String expected = "Your balance is 2000";
43+
SavingsAccount savingsTest = new SavingsAccount(2000);
44+
45+
savingsTest.checkBalance();
46+
47+
assertThat("Does your `checkBalance()` method print \"Your balance is \" + balance?", outContent.toString(), containsString(expected));
48+
}
49+
}

0 commit comments

Comments
 (0)