I know this is not very good. I am currently in my first programming class (Java). It seems like it works, but I am sure there are things I have not thought of.
The goal is to have a user enter
- the number of credit units each class they need to take is worth, one by one
- the number of credit units to complete in a term
The program should calculate
- the number of remaining credit units
- the number of terms it will take to complete the degree
- the cost
import java.util.ArrayList;
import java.util.Scanner;
public class GraduationPlanner {
public static void main(String[] args) {
int sum = 0;
String creditUnits;
int unitsLeft;
int plannedUnits = 0;
int termsLeft = 0;
double tuitionCost = 0;
final double TUITION_RATE = 2890;
ArrayList<Integer> cu = new ArrayList<Integer>();
Scanner in = new Scanner(System.in);
System.out.println("Please input the Credit Units for each course. Press enter after each course. Press Q when finished: ");
while (in.hasNextInt()) {
int temp = (in.nextInt());
if (temp < 0) {
System.out.print("ERROR! Please enter a number 1-9.: ");
} else {
cu.add(temp);
}
}
System.out.println(cu);
Scanner ex = new Scanner(System.in);
System.out.print("Please enter the number of Credit Units you will complete per term: ");
if (ex.hasNextInt()) {
plannedUnits = ex.nextInt();
}
for (int a : cu)
{
sum += a;
}
unitsLeft = (sum - plannedUnits);
termsLeft = (sum / plannedUnits);
tuitionCost = (termsLeft * TUITION_RATE);
System.out.println("After this term there are " + unitsLeft + " credit units remaining.");
System.out.println("You will complete your degree in " + termsLeft + " terms.");
System.out.print("It will cost you " + tuitionCost + " to complete your degree.");
}
}
New code block
/* Determines the length of time and cost of a degree program
* based on user inputs.
*/
import java.util.ArrayList;
import java.util.Scanner;
public class GraduationPlanner {
public static void main(String[] args) {
final double TUITION_RATE = 2890;
ArrayList<Integer> creditUnits = new ArrayList<Integer>();
Scanner unitsPerCourse = new Scanner(System.in);
GetCreditUnits(unitsPerCourse, creditUnits);
System.out.println(creditUnits);
System.out.print("Please enter the number of Credit Units you will complete per term: ");
Scanner goalUnits = new Scanner(System.in);
int plannedUnits = GetPlannedUnits(goalUnits);
int creditUnitsSum = TotalOfCreditUnits(creditUnits);
double tuitionCost;
int termsLeft = CalculateTermsToComple(creditUnitsSum, plannedUnits);
int unitsLeft;
unitsLeft = CalculateUnitsLeft(creditUnitsSum, plannedUnits);
tuitionCost = CalculateTuition(termsLeft, TUITION_RATE);
System.out.println("After this term there are " + unitsLeft + " credit units remaining.");
System.out.println("You will complete your degree in " + termsLeft + " terms.");
System.out.print("It will cost you " + tuitionCost + " to complete your degree.");
unitsPerCourse.close();
goalUnits.close();
}
public static void GetCreditUnits(Scanner unitsPerCourse, ArrayList<Integer> creditUnits) {
System.out.println("Please input the Credit Units for each course. Press enter after each course. Press Q when finished: ");
while (unitsPerCourse.hasNextInt()) {
int currentUnit = (unitsPerCourse.nextInt());
if (currentUnit < 1 || currentUnit >= 10) {
System.out.print("ERROR! Please enter a number 1-9.: ");
} else {
creditUnits.add(currentUnit);
}
}
}
public static int GetPlannedUnits(Scanner goalUnits) {
int plannedUnits = 0;
if (goalUnits.hasNextInt()) {
plannedUnits = goalUnits.nextInt();
} else {
System.out.print("ERROR! Please enter an integer!: ");
}
return plannedUnits;
}
public static int CalculateTermsToComple(int creditUnitsSum, int plannedUnits) {
int termsLeft = (int) Math.ceil((double) creditUnitsSum / plannedUnits);
return termsLeft;
}
public static int CalculateUnitsLeft(int creditUnitsSum, int plannedUnits) {
int unitsLeft;
unitsLeft = creditUnitsSum - plannedUnits;
return unitsLeft;
}
public static double CalculateTuition(int termsLeft, final double TUITION_RATE) {
double tuitionCost;
tuitionCost = termsLeft * TUITION_RATE;
return tuitionCost;
}
public static int TotalOfCreditUnits(ArrayList<Integer> creditUnits) {
int creditUnitsSum = 0;
for (int unit : creditUnits) {
creditUnitsSum += unit;
}
return creditUnitsSum;
}
}