diff --git a/src/main/java/me/alex4386/gachon/sw14462/day06/CheckService.java b/src/main/java/me/alex4386/gachon/sw14462/day06/CheckService.java new file mode 100644 index 0000000..a03c479 --- /dev/null +++ b/src/main/java/me/alex4386/gachon/sw14462/day06/CheckService.java @@ -0,0 +1,20 @@ +package me.alex4386.gachon.sw14462.day06; + +public class CheckService { + public static double acceptCheck(double checkAmount) { + double fee = 0; + if (checkAmount < 10) { + fee = 1; + } else if (checkAmount < 100) { + fee = 0.1 * checkAmount; + } else if (checkAmount < 1000) { + fee = 5; + fee += 0.05 * checkAmount; + } else { + fee = 40; + fee += 0.01 * checkAmount; + } + + return fee; + } +} diff --git a/src/main/java/me/alex4386/gachon/sw14462/day06/GradeConverter.java b/src/main/java/me/alex4386/gachon/sw14462/day06/GradeConverter.java new file mode 100644 index 0000000..da43f36 --- /dev/null +++ b/src/main/java/me/alex4386/gachon/sw14462/day06/GradeConverter.java @@ -0,0 +1,27 @@ +package me.alex4386.gachon.sw14462.day06; + +public class GradeConverter { + public static double letterGradeToGradePoint(char grade) { + double point = 0.0; + switch (grade) { + case 'A': + point = 4.0; + break; + case 'B': + point = 3.0; + break; + case 'C': + point = 2.0; + break; + case 'D': + point = 1.0; + break; + case 'F': + default: + point = 0.0; + break; + } + + return point; + } +} diff --git a/src/main/java/me/alex4386/gachon/sw14462/day06/GradeStatistics.java b/src/main/java/me/alex4386/gachon/sw14462/day06/GradeStatistics.java new file mode 100644 index 0000000..04104be --- /dev/null +++ b/src/main/java/me/alex4386/gachon/sw14462/day06/GradeStatistics.java @@ -0,0 +1,65 @@ +package me.alex4386.gachon.sw14462.day06; + +public class GradeStatistics extends IntegerStatistics { + + int aCount = 0; + int bCount = 0; + int cCount = 0; + int dCount = 0; + int fCount = 0; + + public GradeStatistics() { + total = 0; + count = 0; + } + + @Override + public boolean add(int value) throws IllegalArgumentException { + if (value > 100) { + throw new IllegalArgumentException("Grade must be between 0 and 100"); + } + + boolean isValid = super.add(value); + if (isValid) { + switch (value / 10) { + case 10: + case 9: + aCount++; + break; + case 8: + bCount++; + break; + case 7: + cCount++; + break; + case 6: + dCount++; + break; + default: + fCount++; + break; + } + } + return false; + } + + public int getACount() { + return aCount; + } + + public int getBCount() { + return bCount; + } + + public int getCCount() { + return cCount; + } + + public int getDCount() { + return dCount; + } + + public int getFCount() { + return fCount; + } +} diff --git a/src/main/java/me/alex4386/gachon/sw14462/day06/IntegerStatistics.java b/src/main/java/me/alex4386/gachon/sw14462/day06/IntegerStatistics.java new file mode 100644 index 0000000..a338139 --- /dev/null +++ b/src/main/java/me/alex4386/gachon/sw14462/day06/IntegerStatistics.java @@ -0,0 +1,53 @@ +package me.alex4386.gachon.sw14462.day06; + +public class IntegerStatistics { + long total; + int count; + + int max = Integer.MIN_VALUE; + int min = Integer.MAX_VALUE; + + boolean finalized = false; + + public IntegerStatistics() { + total = 0; + count = 0; + } + + public boolean add(int value) { + if (finalized) return true; + + if (value < 0) { + this.finalized = true; + return true; + } + + if (value < min) min = value; + if (value > max) max = value; + + total += value; + count++; + + return false; + } + + public boolean hasFinalized() { + return this.finalized; + } + + public int getMin() { + return this.min; + } + + public int getMax() { + return this.max; + } + + public int getCount() { + return this.count; + } + + public double getAverage() { + return ((double) total) / count; + } +} diff --git a/src/main/java/me/alex4386/gachon/sw14462/day06/Main.java b/src/main/java/me/alex4386/gachon/sw14462/day06/Main.java new file mode 100644 index 0000000..2637857 --- /dev/null +++ b/src/main/java/me/alex4386/gachon/sw14462/day06/Main.java @@ -0,0 +1,144 @@ +package me.alex4386.gachon.sw14462.day06; + +import me.alex4386.gachon.sw14462.day03.CoinPayment; +import me.alex4386.gachon.sw14462.day03.coins.CoinImpl; +import me.alex4386.gachon.sw14462.day05.Ex3_3; +import me.alex4386.gachon.sw14462.day05.Ex3_6; +import me.alex4386.gachon.sw14462.day05.Ex3_7; + +import java.util.List; +import java.util.Scanner; + +public class Main { + + static Scanner scanner = null; + + public static Scanner getScanner() { + if (scanner == null) { + scanner = new Scanner(System.in); + } + + return scanner; + } + + public static void main(String[] args) { + System.out.println("Practice 3.1"); + System.out.println("Practice 3.1a. Write a cashing program"); + Main.exercise3_1a(); + System.out.println(""); + + System.out.println("Practice 3.1b. Extend the existing get charge coins"); + Main.exercise3_1b(); + System.out.println(""); + + System.out.println("Practice 3.1c. Write a letter grade converter using switch statement"); + Main.exercise3_1c(); + System.out.println(""); + System.out.println(""); + + System.out.println("Practice 3.2"); + System.out.println("Practice 3.2a. Write an integer collection/statistics program"); + Main.exercise3_2a(); + + System.out.println("Practice 3.2b. Extend collection/statistics program for grading"); + Main.exercise3_2b(); + + System.out.println(""); + System.out.println(""); + + System.out.println("Textbook Programming Project ----"); + System.out.println("Exercise 3.3. Write a program that reads a bank account balance and an interest rate and displays the value of the account in ten years."); + System.out.println("ClassPath: "+ Ex3_3.class.getCanonicalName()); + Ex3_3.main(args); + System.out.println(""); + + System.out.println("Exercise 3.6. Write a program that asks the user to enter the size of a triangle."); + System.out.println("ClassPath: "+ Ex3_6.class.getCanonicalName()); + Ex3_6.main(args); + System.out.println(""); + + System.out.println("Exercise 3.7. Write a program that simulates a bouncing ball by computing its height in feet at each second as time passes on a simulated clock."); + System.out.println("ClassPath: "+ Ex3_7.class.getCanonicalName()); + Ex3_7.main(args); + System.out.println(""); + } + + public static void exercise3_1a() { + System.out.print("Enter the amount of check: "); + double checkAmount = getScanner().nextDouble(); + + double charge = CheckService.acceptCheck(checkAmount); + System.out.println("The charge is $"+charge); + } + + public static void exercise3_1b() { + System.out.print("Enter the price (in cents): "); + int price = getScanner().nextInt(); + + if (price < 25 || price % 5 != 0 || price > 100) { + System.err.println("Invalid input. Please enter a number between 25 and 100, divisible by 5."); + } else { + int payCharge = 100 - price; + + try { + CoinPayment chargePayment = new CoinPayment(payCharge); + List paidCoins = chargePayment.pay(); + + System.out.println("Charge coins:"); + for (CoinImpl coin : CoinPayment.coins) { + int count = 0; + for (CoinImpl paidCoin : paidCoins) { + if (paidCoin.getClass().equals(coin.getClass())) { + count++; + } + } + System.out.println(coin.toString() + ": " + count); + } + } catch (IllegalArgumentException e) { + System.err.println(e.getMessage()); + } + } + } + + public static void exercise3_1c() { + System.out.print("Enter the letter based grade: "); + char character = getScanner().next().charAt(0); + + double gradePoint = GradeConverter.letterGradeToGradePoint(character); + System.out.println("The letter grade is "+gradePoint+"."); + } + + public static void exercise3_2a() { + IntegerStatistics stats = new IntegerStatistics(); + + while (!stats.hasFinalized()) { + System.out.print("Enter the integer: "); + int integer = getScanner().nextInt(); + + stats.add(integer); + } + + System.out.println("Max: "+stats.getMax()); + System.out.println("Min: "+stats.getMin()); + System.out.println("Avg: "+stats.getAverage()); + } + + public static void exercise3_2b() { + GradeStatistics stats = new GradeStatistics(); + + while (!stats.hasFinalized()) { + System.out.print("Enter the score: "); + int score = getScanner().nextInt(); + + stats.add(score); + } + + System.out.println("total count: "+stats.getCount()); + System.out.print("A count: "+stats.getACount()+" "); + System.out.print("B count: "+stats.getBCount()+" "); + System.out.print("C count: "+stats.getCCount()+" "); + System.out.print("D count: "+stats.getDCount()+" "); + System.out.print("F count: "+stats.getFCount()+""); + System.out.println(""); + } +}