\documentclass[a4paper,11pt]{article}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage{xcolor}

\renewcommand\familydefault{\sfdefault}
\usepackage{tgheros}
\usepackage[defaultmono]{droidsansmono}

\usepackage{amsmath,amssymb,amsthm,textcomp}
\usepackage{enumerate}
\usepackage{multicol}
\usepackage{tikz}

\usepackage{geometry}
\geometry{left=25mm,right=25mm,%
bindingoffset=0mm, top=20mm,bottom=20mm}


\linespread{1.3}

\newcommand{\linia}{\rule{\linewidth}{0.5pt}}

% custom theorems if needed
\newtheoremstyle{mytheor}
    {1ex}{1ex}{\normalfont}{0pt}{\scshape}{.}{1ex}
    {{\thmname{#1 }}{\thmnumber{#2}}{\thmnote{ (#3)}}}

\theoremstyle{mytheor}
\newtheorem{defi}{Definition}

% my own titles
\makeatletter
\renewcommand{\maketitle}{
\begin{center}
\vspace{2ex}
{\huge \textsc{\@title}}
\vspace{1ex}
\\
\linia\\
\@author \hfill \@date
\vspace{4ex}
\end{center}
}
\makeatother
%%%

% custom footers and headers
\usepackage{fancyhdr}
\pagestyle{fancy}
\lhead{}
\chead{}
\rhead{}
\lfoot{CSC 1109 Lab 8}
\cfoot{}
\rfoot{Page \thepage}
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0pt}
%

% code listing settings
\usepackage{listings}
\lstset{
    language=java,
    basicstyle=\ttfamily\small,
    aboveskip={1.0\baselineskip},
    belowskip={1.0\baselineskip},
    columns=fixed,
    extendedchars=true,
    breaklines=true,
    tabsize=4,
    prebreak=\raisebox{0ex}[0ex][0ex]{\ensuremath{\hookleftarrow}},
    frame=lines,
    showtabs=false,
    showspaces=false,
    showstringspaces=false,
    keywordstyle=\color[rgb]{0.627,0.126,0.941},
    commentstyle=\color[rgb]{0.133,0.545,0.133},
    stringstyle=\color[rgb]{01,0,0},
    numbers=left,
    numberstyle=\small,
    stepnumber=1,
    numbersep=10pt,
    captionpos=t,
    escapeinside={\%*}{*)}
}

%%%----------%%%----------%%%----------%%%----------%%%

\begin{document}

\title{CSC 1109 Lab 8}

\author{Woon Jun Wei, 2200624}

\date{\today}

\maketitle

\section*{Question 1}
\begin{lstlisting}[label={list:CircleWithException},caption=CircleWithException.java]
import java.util.Scanner;

public class CircleWithException {
    private double radius;

    public CircleWithException(double radius) throws IllegalArgumentException {
        if (radius < 0) {
            throw new IllegalArgumentException("Radius cannot be negative");
        }
        this.radius = radius;
    }

    public double getRadius() {
        return radius;
    }

    public void setRadius(double radius) throws IllegalArgumentException {
        if (radius < 0) {
            throw new IllegalArgumentException("Radius cannot be negative");
        }
        this.radius = radius;
    }

    public double getArea() throws Exception{
        double area = Math.PI * radius * radius;

        if (area > 1000){
            throw new Exception("Area cannot be greater than 1000");
        }
        return area;
    }

    // Exceptions not stated in Lab Instructions
    public double getDiameter() {
        return radius * 2;
    }

    public static void main(String[] args) {
        CircleWithException c1 = new CircleWithException(5);
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a radius: ");
        double radius = sc.nextDouble();
        try {
            CircleWithException c2 = new CircleWithException(radius);
            System.out.println("Radius: " + c2.getRadius());
            System.out.println("Area: " + c2.getArea());
            System.out.println("Diameter: " + c2.getDiameter());
        } catch (IllegalArgumentException e) {
            System.out.println(e.getMessage());
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

}
\end{lstlisting}

\begin{lstlisting}[label={list:CircleWithException},caption=CircleWithException.java Output]
// Negative radius
Enter a radius: -10
Radius cannot be negative

// Area Greater than 1000
Enter a radius: 1000
Radius: 1000.0
Area cannot be greater than 1000

// Valid Circle
Enter a radius: 5
Radius: 5.0
Area: 78.53981633974483
Diameter: 10.0
\end{lstlisting}

\pagebreak

\section*{Question 2}

\begin{lstlisting}[label={list:InsufficientFundsException},caption=InsufficientFundsException.java]
public class InsufficientFundsException extends Exception{
    private double amount;
    public InsufficientFundsException(double amount){
        this.amount = amount;
    }
    public double getAmount(){
        return amount;
    }
}

\end{lstlisting}

\begin{lstlisting}[label={list:CheckingAccount},caption=CheckingAccount.java]
public class CheckingAccount extends InsufficientFundsException{

    private double balance;
    private int number;

    public CheckingAccount(double amount) {
        super(amount);
    }

    public CheckingAccount(){
        this(0);
    }

    public void deposit(double amount){
        if (amount > 0){
            this.balance += amount;
        }
    }

    public void withdraw(double amount) throws InsufficientFundsException{
        if (amount > this.balance){
            throw new InsufficientFundsException(amount);
        }
    }
    public double getBalance(){
        return this.balance;
    }

    public int getNumber(){
        return this.number;
    }


}
\end{lstlisting}

\begin{lstlisting}[label={list:BankDemoTest},caption=BankDemoTest.java]
import java.util.Scanner;
public class BankDemoTest {
    public static void main(String[] args){
        CheckingAccount c = new CheckingAccount();
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter an amount to deposit: ");
        double amount = sc.nextDouble();
        c.deposit(amount);
        System.out.print("Enter an amount to withdraw: ");
        amount = sc.nextDouble();
        try{
            c.withdraw(amount);
        }catch(InsufficientFundsException e){
            if (e.getAmount() > c.getBalance()){
                System.out.println("Sorry, but your account is short by: $" + (e.getAmount() - c.getBalance()));
            }
        }
        if (amount <= c.getBalance())
            System.out.println("The balance after withdrawal is: $" + (c.getBalance() - amount));
    }
}
\end{lstlisting}

\begin{lstlisting}[label={list:BankDemoTestOutput},caption=BankDemoTest.java Output]
// $yy < $xx
Enter an amount to deposit: 100
Enter an amount to withdraw: 20
The balance after withdrawal is: $80.0

// $yy > $xx
Enter an amount to deposit: 100
Enter an amount to withdraw: 254
Sorry, but your account is short by: $154.0
\end{lstlisting}
\end{document}
