huh?
This commit is contained in:
@@ -0,0 +1,36 @@
|
|||||||
|
package l2a;
|
||||||
|
|
||||||
|
public class Employee {
|
||||||
|
String ID;
|
||||||
|
String name;
|
||||||
|
String department;
|
||||||
|
int salary;
|
||||||
|
int bonus_salary;
|
||||||
|
|
||||||
|
public Employee (String ID, String name, String department, int salary, int bonus_salary) {
|
||||||
|
this.ID = ID;
|
||||||
|
this.name = name;
|
||||||
|
this.department = department;
|
||||||
|
this.salary = (salary);
|
||||||
|
this.bonus_salary = (bonus_salary);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getID() {
|
||||||
|
return ID;
|
||||||
|
}
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
public String getDepartment() {
|
||||||
|
return department;
|
||||||
|
}
|
||||||
|
public int getSalary() {
|
||||||
|
return salary;
|
||||||
|
}
|
||||||
|
public int getBonusSalary() {
|
||||||
|
return bonus_salary;
|
||||||
|
}
|
||||||
|
public String funny() {
|
||||||
|
return ID + " " + name + " " + department + " " + salary + " " + bonus_salary;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,92 @@
|
|||||||
|
package l2a;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Scanner;
|
||||||
|
import java.io.*;
|
||||||
|
|
||||||
|
public class EmployeeFileRead {
|
||||||
|
// thing
|
||||||
|
// function 1:
|
||||||
|
// read from stdin function:
|
||||||
|
// ask for n
|
||||||
|
// for each n ask for info of Employee
|
||||||
|
// how do i dynamic array
|
||||||
|
static List<Employee> employees = new ArrayList<Employee>();
|
||||||
|
|
||||||
|
public static void read() {
|
||||||
|
Scanner input = new Scanner(System.in);
|
||||||
|
System.out.print("Employee count: ");
|
||||||
|
int n = input.nextInt();
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
System.out.print("ID: ");
|
||||||
|
String ID = input.next();
|
||||||
|
System.out.print("Name: ");
|
||||||
|
String name = input.next();
|
||||||
|
System.out.print("Department: ");
|
||||||
|
String department = input.next();
|
||||||
|
System.out.print("Salary: ");
|
||||||
|
int salary = input.nextInt();
|
||||||
|
System.out.print("Bonus: ");
|
||||||
|
int bonus = input.nextInt();
|
||||||
|
Employee employee = new Employee(ID, name, department, salary, bonus);
|
||||||
|
employees.add(employee);
|
||||||
|
}
|
||||||
|
input.close();
|
||||||
|
// for (Employee employee: employees) {
|
||||||
|
// System.out.println(employee.funny());
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void save(String filename) {
|
||||||
|
// save to file
|
||||||
|
try (PrintWriter writer = new PrintWriter(new FileWriter(filename))) {
|
||||||
|
for (Employee employee : employees) {
|
||||||
|
writer.println(employee.funny());
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static List<Employee> get(String filename) {
|
||||||
|
List<Employee> getEmployees = new ArrayList<Employee>();
|
||||||
|
try (BufferedReader file = new BufferedReader(new FileReader(filename));) {
|
||||||
|
String line;
|
||||||
|
while ((line = file.readLine()) != null) {
|
||||||
|
getEmployees.add(line(line));
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return getEmployees;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Employee line(String line) {
|
||||||
|
String[] data = line.split(" ");
|
||||||
|
return new Employee(data[0], data[1], data[2],
|
||||||
|
Integer.parseInt(data[3]),
|
||||||
|
Integer.parseInt(data[4]));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void print(List<Employee> employees) {
|
||||||
|
for (Employee employee : employees) {
|
||||||
|
System.out.println("ID: " +employee.getID());
|
||||||
|
System.out.println("Name: " +employee.getName());
|
||||||
|
System.out.println("Department: " +employee.getDepartment() );
|
||||||
|
double inc = employee.getSalary() + employee.getBonusSalary() * 2.5;
|
||||||
|
System.out.println("Income: " + inc);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
read();
|
||||||
|
String filename = "employees.txt";
|
||||||
|
save(filename);
|
||||||
|
System.out.println("saved to " + filename + "\n");
|
||||||
|
System.out.println("retrieve from " + filename);
|
||||||
|
print(get(filename));
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,39 @@
|
|||||||
|
package l2a;
|
||||||
|
|
||||||
|
|
||||||
|
public class NameCard {
|
||||||
|
String name;
|
||||||
|
String phone;
|
||||||
|
String email;
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
public String getPhone() {
|
||||||
|
return phone;
|
||||||
|
}
|
||||||
|
public String getEmail() {
|
||||||
|
return email;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
public void setPhone(String phone) {
|
||||||
|
this.phone = phone;
|
||||||
|
}
|
||||||
|
public void setEmail(String email) {
|
||||||
|
this.email = email;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String toString() {
|
||||||
|
return "NameCard [name=" + name + ", phone=" + phone + ", email=" + email + "]";
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public NameCard(String name, String phone, String email) {
|
||||||
|
this.name = name;
|
||||||
|
this.phone = phone;
|
||||||
|
this.email = email;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
package l2a;
|
||||||
|
|
||||||
|
public class NameCardTest {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
NameCard namecard = new NameCard("Ninja", "1441414414", "[email protected]");
|
||||||
|
System.out.println(namecard);
|
||||||
|
// equivalent to System.out.println(namecard.toString());
|
||||||
|
// have a toString method in your custom defined class that wants to be to string
|
||||||
|
System.out.println(namecard.getName());
|
||||||
|
System.out.println(namecard.getPhone());
|
||||||
|
System.out.println(namecard.getEmail());
|
||||||
|
// System.err.println(Integer.toString(11));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,70 @@
|
|||||||
|
package l2a;
|
||||||
|
|
||||||
|
public class shapeRead {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Shape[] shapes = new Shape[3];
|
||||||
|
shapes[0] = new Point(1, 2);
|
||||||
|
shapes[1] = new Circle(1, 2, 3);
|
||||||
|
shapes[2] = new Cylinder(1, 2, 3, 4);
|
||||||
|
for (int i = 0; i < shapes.length; i++) {
|
||||||
|
System.out.println(shapes[i].getName());
|
||||||
|
System.out.println(shapes[i].calArea());
|
||||||
|
System.out.println(shapes[i].calVolume());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
abstract class Shape {
|
||||||
|
double calArea() {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
double calVolume() {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
abstract String getName();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
class Point extends Shape {
|
||||||
|
String getName() {
|
||||||
|
return this.getClass().getSimpleName();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected double x;
|
||||||
|
protected double y;
|
||||||
|
|
||||||
|
Point(double x, double y) {
|
||||||
|
this.x = x;
|
||||||
|
this.y = y;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
class Circle extends Point {
|
||||||
|
|
||||||
|
protected double r;
|
||||||
|
Circle(double x, double y, double r) {
|
||||||
|
super(x, y);
|
||||||
|
this.r = r;
|
||||||
|
}
|
||||||
|
double calArea() {
|
||||||
|
return (r*r * Math.PI);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class Cylinder extends Circle {
|
||||||
|
protected double h;
|
||||||
|
Cylinder(double x, double y, double r, double h) {
|
||||||
|
super(x, y, r);
|
||||||
|
this.h = h;
|
||||||
|
}
|
||||||
|
double calVolume() {
|
||||||
|
return (super.calArea() * h);
|
||||||
|
}
|
||||||
|
double calArea() {
|
||||||
|
return 2 * (r*r * Math.PI) + (2 * r * Math.PI) * h;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user