Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Calculator/.classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="src" path="src"/>
<classpathentry kind="output" path="bin"/>
</classpath>
1 change: 1 addition & 0 deletions Calculator/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/bin/
17 changes: 17 additions & 0 deletions Calculator/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>Calculator</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
11 changes: 11 additions & 0 deletions Calculator/.settings/org.eclipse.jdt.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.8
9 changes: 9 additions & 0 deletions Calculator/src/AnotherCalculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

public class AnotherCalculator {

public static void main(String[] args) {
// TODO Auto-generated method stub

}

}
9 changes: 9 additions & 0 deletions Calculator/src/Calculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

public class Calculator {

public static void main(String[] args) {
// TODO Auto-generated method stub

}

}
6 changes: 6 additions & 0 deletions RosterExample/.classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="src" path="src"/>
<classpathentry kind="output" path="bin"/>
</classpath>
1 change: 1 addition & 0 deletions RosterExample/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/bin/
17 changes: 17 additions & 0 deletions RosterExample/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>RosterExample</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
11 changes: 11 additions & 0 deletions RosterExample/.settings/org.eclipse.jdt.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.8
53 changes: 53 additions & 0 deletions RosterExample/src/Employee.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@

public class Employee {

// Variables

private static int NEXTID = 100; // Static variable that goes with the class

private String name;
private String jobTitle;
private int id;

/*
* TODO:
* Fields to add:
* boolean isActive - true if the employee is active (i.e. not retired or on leave), false otherwise
* LocalDateTime startDate - the date this employee began employment
*/

// Constructor

public Employee(String empName, String empJobTitle) {
name = empName;
jobTitle = empJobTitle;
id = NEXTID;
NEXTID++; // increment
}

// Methods

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getJobTitle() {
return jobTitle;
}

public void setJobTitle(String title) {
this.jobTitle = title;
}
}
78 changes: 78 additions & 0 deletions RosterExample/src/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import java.util.List;
import java.util.Scanner;

public class Main {

public static void main(String[] args) {
Roster myRoster = new Roster();
Scanner reader = new Scanner(System.in);

/*
* User command options:
* add - create a new employee and add them to the roster
* list - print all the employees in the roster
* list [id] - given an employee id, print their info
* quit - exit the program
*
* TODO:
* delete [id] - given an employee id, remove that employee from the roster
* list [-a] - print all active employees
*/
boolean quit = false;

while(!quit) {
System.out.println("Command options: add, list [id], quit");
String input = reader.nextLine(); // Read user's input
String[] inputParts = input.split(" "); // Split user input into multiple strings
String firstCommand = inputParts[0]; // First command typed by user
if (firstCommand.equals("add")) {
// Prompt user for employee info
System.out.println("Please enter employee name");
String name = reader.nextLine();
System.out.println("Please enter employee title");
String title = reader.nextLine();

Employee emp = new Employee(name, title);
myRoster.addEmployee(emp);
} else if (firstCommand.equals("list")) {
if (inputParts.length == 2) { // User has input "list [i]"
int empId = Integer.parseInt(inputParts[1]);
printEmployee(myRoster, empId);
} else if (inputParts.length == 1) { // User has input "list"
printEmployeeList(myRoster);
}
} else if (firstCommand.equals("quit")) {
quit = true;
} else {
System.out.println("Invalid input, please try again");
}
}
reader.close();
}

// Print the entire employee list
private static void printEmployeeList(Roster myRoster) {
List<Employee> employeeList = myRoster.getEmployeeList();
for (int i = 0; i < employeeList.size(); i++) {
Employee currEmp = employeeList.get(i);
System.out.println("Id: " + currEmp.getId()
+ ", Name: " + currEmp.getName()
+ ", Title: " + currEmp.getJobTitle());
}
}

// Given the employee id, find them in the list and print their info
private static void printEmployee(Roster myRoster, int id) {
List<Employee> employeeList = myRoster.getEmployeeList();
for (int i = 0; i < employeeList.size(); i++) {
Employee currEmp = employeeList.get(i);
int currId = currEmp.getId();
if (currId == id) {
System.out.println("Id: " + currEmp.getId()
+ ", Name: " + currEmp.getName()
+ ", Title: " + currEmp.getJobTitle());
}
}
}

}
22 changes: 22 additions & 0 deletions RosterExample/src/Roster.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import java.util.ArrayList;
import java.util.List;

public class Roster {
private List<Employee> employeeList;

public Roster() {
employeeList = new ArrayList<>();
}

public List<Employee> getEmployeeList() {
return employeeList;
}

public void setEmployeeList(List<Employee> employees) {
employeeList = employees;
}

public void addEmployee(Employee toAdd) {
employeeList.add(toAdd);
}
}
93 changes: 93 additions & 0 deletions SQLiteSample/src/Sample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Sample
{
public static void main(String[] args)
{
Connection connection = null;
try
{
// create a database connection
connection = DriverManager.getConnection("jdbc:sqlite:sample.db");
Statement statement = connection.createStatement();
statement.setQueryTimeout(30); // set timeout to 30 sec.

statement.executeUpdate("drop table if exists person");
statement.executeUpdate("create table person (id integer, name string)");
statement.executeUpdate("insert into person values(1, 'leo')");
statement.executeUpdate("insert into person values(2, 'yui')");

ResultSet rs = statement.executeQuery("select * from person");
while(rs.next())
{
// read the result set
System.out.println("name = " + rs.getString("name"));
System.out.println("id = " + rs.getInt("id"));
}
}
catch(SQLException e)
{
// if the error message is "out of memory",
// it probably means no database file is found
System.err.println(e.getMessage());
}
finally
{
try
{
if(connection != null)
connection.close();
}
catch(SQLException e)
{
// connection close failed.
System.err.println(e.getMessage());
}
}
getUser("leo");
}

public static void getUser(String name) {
String sql = "SELECT id, name FROM person where name = ?";

Connection connection = null;
try
{
connection = DriverManager.getConnection("jdbc:sqlite:sample.db");
PreparedStatement pstmt = connection.prepareStatement(sql);
pstmt.setString(1, name);
pstmt.setQueryTimeout(30); // set timeout to 30 sec.

ResultSet rs = pstmt.executeQuery();

while(rs.next()) {
System.out.println(rs.getInt("id") + "\t" +
rs.getString("name"));
}
}
catch(SQLException e)
{
// if the error message is "out of memory",
// it probably means no database file is found
System.err.println(e.getMessage());
}
finally
{
try
{
if(connection != null)
connection.close();
}
catch(SQLException e)
{
// connection close failed.
System.err.println(e.getMessage());
}
}
}
}
Loading