inital commit

This commit is contained in:
gabriel venberg 2021-03-26 22:45:38 -05:00
commit aa37ae1eec
55 changed files with 3927 additions and 0 deletions

113
Lab09/src/Date.java Normal file
View file

@ -0,0 +1,113 @@
/*
* Copyright (C) 2020 Gabriel Venberg
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
*
* @author toric
*/
import java.util.GregorianCalendar;
import java.util.Calendar;
public class Date {
private int day, month, year;
public Date (int startDay, int startMonth, int startYear){
setYear(startYear);
setMonth(startMonth);
setDay(startDay);
}
/**
* returns day
* @return
*/
public int getDay(){
return day;
}
/**
* returns month
* @return
*/
public int getMonth(){
return month;
}
/**
* returns year
* @return
*/
public int getYear(){
return year;
}
/**
* sets the day, throws error if day is impossible under the gregorian calander.
* @param newDay
*/
private void setDay(int newDay){
GregorianCalendar checker = new GregorianCalendar(year, month, day);
//found in javadoc for gregoriancalander. will also take into account leap years.
int daysInMonth = checker.getActualMaximum(Calendar.DAY_OF_MONTH);
if (newDay<=daysInMonth){
day=newDay;
}
else {
throw new IllegalArgumentException("newDay out of range for set month and year");
}
}
/**
* sets month, throws error if month is above 12.
* @param newMonth
*/
private void setMonth(int newMonth){
if (newMonth<=12){
month=newMonth;
}
else {
throw new IllegalArgumentException("newMonth must be <= 12");
}
}
/**
* sets year, returns error if year is before 1582
* @param newYear
*/
private void setYear(int newYear){
if (newYear>=1582){
year=newYear;
}
else {
throw new IllegalArgumentException("newYear must be >= 1582, the year the gregorian calander was adopted.");
}
}
/**
* outputs a string representation of the date in mm/dd/yyyy format.
* @return
*/
public String toString(){
return (month+"/"+day+"/"+year);
}
/**
* tests if date obgect is equal to another date object.
* @param testDate
* @return
*/
public boolean equals(Date testDate){
return testDate.getDay()==day&testDate.getMonth()==month&testDate.getYear()==year;
}
}

41
Lab09/src/DateTest.java Normal file
View file

@ -0,0 +1,41 @@
/*
* Copyright (C) 2020 Gabriel Venberg
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
*
* @author toric
*/
import java.util.Scanner;
public class DateTest {
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
int day, month, year;
System.out.print("input a year >");
year = scan.nextInt();
System.out.print("input a month >");
month = scan.nextInt();
System.out.println("input a day >");
day = scan.nextInt();
Date test = new Date(day, month, year);
System.out.println("The date You entered is "+test.toString());
}
}

120
Lab09/src/Loan.java Normal file
View file

@ -0,0 +1,120 @@
/*
* Copyright (C) 2020 Gabriel Venberg
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
*
* @author toric
*/
public class Loan {
private double interestRate;
private double amount;
private int years;
private Date date;
public Loan(double startInterestRate, double startAmount, int startYears, Date startDate){
setInterestRate(startInterestRate);
setAmount(startAmount);
setYears(startYears);
date=startDate;
}
/**
* returns interestRate
* @return
*/
public double getInterestRate(){
return interestRate;
}
/**
* returns amount
* @return
*/
public double getAmount(){
return amount;
}
/**
* returns years
* @return
*/
public double getYears(){
return years;
}
/**
* returns a string representing date.
* @return
*/
public String getDate(){
return date.toString();
}
/**
* sets interest rate, making sure its not negative.
* @param newInterestRate
*/
private void setInterestRate(double newInterestRate){
if (newInterestRate >=0){
interestRate=newInterestRate;
}
else{
throw new IllegalArgumentException("Interest must be >= 0");
}
}
/**
* sets amount, making sure its not negative.
* @param newAmount
*/
private void setAmount(double newAmount){
if (newAmount >=0){
amount=newAmount;
}
else{
throw new IllegalArgumentException("Amount must be >= 0");
}
}
/**
* sets years, making sure its at least 1.
* @param newYears
*/
private void setYears(int newYears){
if (newYears >0){
years=newYears;
}
else{
throw new IllegalArgumentException("years must be > 0");
}
}
/**
* returns the monthly payment, assuming interest stays static throuought the rest of the loans term.
* @return
*/
public double getMonthlyPayment(){
return (((interestRate/12)*amount)/(1-(1/Math.pow((1+(interestRate/12)),(years*12)))));
}
/**
* returns the total payment made throuhought the life of the loan.
* @return
*/
public double getTotalPayment(){
return getMonthlyPayment()*12*years;
}
/**
* returns the total amount paid over the amount initally borrowed.
* @return
*/
public double getOverpayment(){
return getTotalPayment()-amount;
}
}

82
Lab09/src/LoanTest.java Normal file
View file

@ -0,0 +1,82 @@
/*
* Copyright (C) 2020 Gabriel Venberg
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
*
* @author toric
*/
import java.util.Scanner;
import java.text.NumberFormat;
import java.text.DecimalFormat;
public class LoanTest {
public static void main(String[] args){
//setup
NumberFormat percentFormat = NumberFormat.getPercentInstance();
NumberFormat moneyFormat = NumberFormat.getCurrencyInstance();
Scanner scan = new Scanner(System.in);
double interestRate, amount;
int years;
int year, month, day;
boolean wantsToContinue = true;
char tempInput;
while (wantsToContinue){
//getting input
System.out.print("intput the annual interest rate as a decimal >");
interestRate = scan.nextDouble();
System.out.print("input the number of years the loan will be held >");
years = scan.nextInt();
System.out.print("input the amount borrowed >");
amount = scan.nextDouble();
System.out.print("input the year >");
year = scan.nextInt();
System.out.print("input the month >");
month = scan.nextInt();
System.out.print("input the day >");
day = scan.nextInt();
Date loanDate = new Date(day, month, year);
Loan testLoan = new Loan(interestRate, amount, years, loanDate);
//some minor calculation in order to keep the line from getting too long.
double overpaymentPercent = testLoan.getOverpayment()/testLoan.getTotalPayment();
System.out.println("SUCSESS!");
System.out.println("Loan created.");
System.out.println("You have sucsessfully signed up for a loan!");
System.out.println("You have an interest rate of "+percentFormat.format(testLoan.getInterestRate()));
System.out.println("You borrowed"+moneyFormat.format(testLoan.getAmount()));
System.out.println("You will be paying us "+moneyFormat.format(testLoan.getMonthlyPayment())+" a month for "+years+" years.");
System.out.println("Even assuming you pay on time every month, by the end of the loan you will have paid us "+moneyFormat.format(testLoan.getTotalPayment())+",");
System.out.println(moneyFormat.format(testLoan.getOverpayment())+" of whitch will be pure profit for us!");
System.out.println("That is, you will have paid "+percentFormat.format(overpaymentPercent)+" more than you initaly borrowed!");
System.out.println("Isnt capitalism great!");
System.out.println("Press y to register for another loan.");
tempInput = scan.next().charAt(0);
switch(tempInput) {
case 'y':
wantsToContinue = true;
break;
default:
wantsToContinue = false;
break;
}
}
}
}

90
Lab09/src/Stock.java Normal file
View file

@ -0,0 +1,90 @@
/*
* Copyright (C) 2020 Gabriel Venberg
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
*Stock class
* @author toric
*/
public class Stock {
private String symbol;
private String name;
private double previousClosingPrice;
private double currentPrice;
/**
* constructor that assigns symbol and name.
* no blank constructor, as symbol and name do not have assignment methods.
* @param startSymbol
* @param startName
*/
public Stock(String startSymbol, String startName){
symbol=startSymbol;
name=startName;
}
/**
* returns symbol
* @return
*/
public String getSymbol(){
return symbol;
}
/**
* returns name
* @return
*/
public String getName(){
return name;
}
/**
* returns previousClosingPrice
* @return
*/
public double getPreviousClosingPrice(){
return previousClosingPrice;
}
/**
* returns currentPrice
* @return
*/
public double getCurrentPrice(){
return currentPrice;
}
/**
* sets previousClosingPrice
* @param newPreviousClosingPrice
*/
public void setPreviousClosingPrice(double newPreviousClosingPrice){
previousClosingPrice=newPreviousClosingPrice;
}
/**
* sets currentPrice
* @param newCurrentPrice
*/
public void setCurrentPrice(double newCurrentPrice){
currentPrice=newCurrentPrice;
}
/**
* returns the percentage change between the previousClosingPrice and the currentPrice.
* @return
*/
public double changePercent(){
return ((currentPrice-previousClosingPrice)/previousClosingPrice)*100;
}
}

33
Lab09/src/StockTest.java Normal file
View file

@ -0,0 +1,33 @@
/*
* Copyright (C) 2020 Gabriel Venberg
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
*
* @author toric
*/
public class StockTest {
public static void main(String[] args){
Stock mmm = new Stock("MMM","3M");
//set monetary values
mmm.setPreviousClosingPrice(165.51);
mmm.setCurrentPrice(150);
//print info.
System.out.println("Yesterdays closing price for "+mmm.getName()+" was "+mmm.getPreviousClosingPrice()+".");
System.out.println("The current price for "+mmm.getName()+" is "+mmm.getCurrentPrice()+".");
System.out.println(mmm.getName()+" has changed by "+mmm.changePercent()+"%.");
}
}