Finished program

This commit is contained in:
Steven Tracey
2023-01-19 15:14:34 -05:00
parent da35e1df3d
commit d352168fd8
16 changed files with 665 additions and 0 deletions

View File

@@ -0,0 +1,25 @@
package org.example;
import java.util.ArrayList;
import java.util.List;
public class ChargeVerifier {
private static final ArrayList<Integer> VALID_CHARGES = new ArrayList<>(List.of(5658845, 4520125, 7895122, 8777541, 8451277, 1302850, 8080152, 4562555, 5552012, 5050552, 7825877, 1250255, 1005231, 6545231, 3852085, 7576651, 7881200, 4581002));
public static boolean isValidCharge1(int accountNumber) {
return VALID_CHARGES.contains(accountNumber);
}
public static boolean isValidCharge2(int accountNumber) {
for (int chargeNum : VALID_CHARGES) {
if (accountNumber == chargeNum) {
return true;
}
}
return false;
}
public static boolean isValidCharge3(int accountNumber) {
return VALID_CHARGES.stream().anyMatch(i -> accountNumber == i);
}
}

View File

@@ -0,0 +1,164 @@
package org.example;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
helloWorld();
System.out.println("This will appear in console!");
int integerVar = 12345;
char aSingleLetter = 'A';
boolean trueOrFalse = true;
String wordVar = "A bunch of text";
int a = 1 + 2;
int b = a - 1;
int c = b * a;
int d = c / 3;
System.out.println(a + " : " + b + " : " + c + " : " + d);
float mpg = milesPerGallon(65, 1.96F);
System.out.println(mpg);
boolean condition = true;
if (condition) {
System.out.println("condition is true");
}
if (condition) {
System.out.println("condition is true");
} else {
System.out.println("condition is false");
}
int intCondition = 1;
if (intCondition == 0) {
System.out.println("The condition matches this first case");
} else if (intCondition == 1) {
System.out.println("The condition matches a different case");
} else {
System.out.println("The condition did not match any previous case");
}
int time = secondsCompressor(2837);
System.out.println(time);
int whileI = 0;
boolean whileBool = true;
while (whileBool) {
System.out.println("This is looping!");
whileI++;
if (whileI > 5) {
whileBool = false;
}
}
doublePennies(30);
float retailPrice = calculateRetail(5.00F, 100);
System.out.println(retailPrice);
Person me = new Person("Steven", "123 Main Street", 17, 7175558700L);
System.out.println(me);
Person friend = new Person("Bob", "123 Center Street", 18, 7175559900L);
System.out.println(friend);
Person family = new Person("John", "123 Main Street", 15, 7175558600L);
System.out.println(family);
Person[] people = new Person[3];
people[0] = me;
people[1] = friend;
people[2] = family;
for (Person person : people) {
System.out.println(person);
}
//for (int i = 0; i < people.length; i++) {
// System.out.println(people.get(i));
//}
System.out.println(averageAges(people));
}
public static void helloWorld() {
System.out.println("Hello, World!");
}
/**
* returns value formatted to 1 decimal place
*/
public static float milesPerGallon(int miles, float fuelUsed) {
String value = new DecimalFormat("0.0").format(miles / fuelUsed);
return Float.parseFloat(value);
}
/**
* Takes seconds and converts them to minutes hours and days if the value is greater than 1
*/
public static int secondsCompressor(int seconds) {
if (seconds < 60) {
return seconds;
} else if (seconds < 3600) {
return Math.round(seconds / 60F);
} else if (seconds < 86400) {
return Math.round(seconds / 3600F);
} else {
return Math.round(seconds / 86400F);
}
}
/**
* Don't pass anything higher than 64 days, java can't store longs greater than (insert large number) (i couldnt be bothered to look it up)
*/
public static void doublePennies(int days) {
if (days < 0) {
return;
}
if (days > 64) {
return;
}
long pennies = 1L;
for (int i = 0; i < days; i++) {
pennies *= 2;
System.out.println("Day " + (i + 1) + ": " + (pennies / 100D));
}
}
/**
* Receives the wholesale and markup and returns retail
* @param wholeSale wholesale cost in dollar amount
* @param markup markup as a percentage
* @return float dollar amount rounded as X.XX
*/
public static float calculateRetail(float wholeSale, int markup) {
float preRounded = wholeSale * (markup / 100F);
String roundedString = new DecimalFormat("0.00").format(preRounded);
return Float.parseFloat(roundedString);
}
public static float averageAges(Person[] people) {
float ageTotal = 0;
float numOfPeople = 0;
for (Person person : people) {
ageTotal += person.getAge();
numOfPeople++;
}
return ageTotal / numOfPeople;
}
}

View File

@@ -0,0 +1,52 @@
package org.example;
public class Person {
private String name;
private String address;
private int age;
private long phoneNumber;
public Person(String name, String address, int age, long phoneNumber) {
this.name = name;
this.address = address;
this.age = age;
this.phoneNumber = phoneNumber;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public long getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(long phoneNumber) {
this.phoneNumber = phoneNumber;
}
@Override
public String toString() {
return "[ Name: " + name+ ", Address: " + address + ", Age: " + age + ", Phone Number: " + phoneNumber + " ]";
}
}

View File

@@ -0,0 +1,13 @@
package org.example;
public class TestChargeVerifier {
public static void main(String[] args) {
System.out.println(ChargeVerifier.isValidCharge1(1234567)); // False
System.out.println(ChargeVerifier.isValidCharge1(8080152)); // True
System.out.println(ChargeVerifier.isValidCharge2(1234567)); // False
System.out.println(ChargeVerifier.isValidCharge2(8080152)); // True
System.out.println(ChargeVerifier.isValidCharge3(1234567)); // False
System.out.println(ChargeVerifier.isValidCharge3(8080152)); // True
}
}