26 Commits

Author SHA1 Message Date
s-sakiyama
db8e199c9d 6段階目(DepartmentDataを作って、ポリモーフィズムの威力を実感しよう) 2026-09-16 15:59:22 +09:00
s-sakiyama
f68bef8367 5段階目(Mainでポリモーフィズムを使おう) 2026-09-16 15:14:05 +09:00
s-sakiyama
9c3605cf87 4段階目(BaseDataを作り、EmployeeDataに継承させよう) 2026-09-16 13:31:36 +09:00
s-sakiyama
e8648e7d8b 3段階目(ポリモーフィズムを体感しよう) 2026-09-16 13:21:23 +09:00
s-sakiyama
551f5a4fe2 2段階目(抽象クラスとインターフェースを使ってみよう) 2026-09-16 13:12:58 +09:00
s-sakiyama
03c7af9751 1段階目(継承とsuperでシンプルな図形クラスを作ろう) 2026-09-16 12:59:09 +09:00
s-sakiyama
856eb9888a Merge remote-tracking branch 'origin/main' into s-sakiyama 2026-09-16 12:39:57 +09:00
s-sakiyama
8fbfa99047 9段階目(何度でも検索できるようにしよう) 2026-09-15 16:06:53 +09:00
s-sakiyama
1c4b834578 8段階目(社員をID検索してみよう) 2026-09-15 15:43:55 +09:00
s-sakiyama
b8aa7238f6 7段階目(IDが一致するか調べるメソッドを作ろう) 2026-09-15 15:06:13 +09:00
s-sakiyama
d88468306f 9段階目(チェックをもう1つの項目にも広げ、コンストラクタと統一しよう) 2026-09-15 14:44:43 +09:00
s-sakiyama
968c5094a4 8段階目(セッターに簡単なチェックを入れてみよう) 2026-09-15 14:24:20 +09:00
s-sakiyama
3b6b23c883 7段階目(staticで社員データの件数を数えよう) 2026-09-15 14:03:03 +09:00
s-sakiyama
f143cf7ec2 6段階目(toStringをオーバーライドしよう) 2026-09-15 13:55:35 +09:00
s-sakiyama
b58fcecaa2 5段階目(EmployeeDataにコンストラクタを作ろう) 2026-09-15 13:34:30 +09:00
s-sakiyama
d98145e598 4段階目(EmployeeDataをカプセル化しよう) 2026-09-15 13:25:46 +09:00
s-sakiyama
3d026db061 3段階目(Studentクラスにコンストラクタとstaticメンバを追加しよう) 2026-09-15 13:08:33 +09:00
s-sakiyama
38620e220f 2段階目(Itemクラスをカプセル化し、toStringをオーバーライドしよう) 2026-09-15 13:02:03 +09:00
s-sakiyama
d4b07b24bf 1段階目(Bookクラスにコンストラクタを作ろう) 2026-09-15 13:01:42 +09:00
s-sakiyama
066af8799d Merge remote-tracking branch 'origin/main' into s-sakiyama 2026-09-15 12:37:00 +09:00
s-sakiyama
27685ac25f 6段階目(複数の社員をまとめて表示してみよう) 2026-09-14 16:50:07 +09:00
s-sakiyama
3e82879f22 4段階目(EmployeeDataクラスの型を作ろう) 2026-09-14 16:37:11 +09:00
s-sakiyama
769fb977ca 3段階目(複数のインスタンスを扱ってみよう) 2026-09-14 16:32:27 +09:00
s-sakiyama
04da447bfe 2段階目(自分でクラスを宣言してみよう) 2026-09-14 16:32:12 +09:00
s-sakiyama
25802b8380 1段階目(用意されたクラスを使ってみよう) 2026-09-14 16:31:51 +09:00
s-sakiyama
be15630c5b 1段階目(用意されたクラスを使ってみよう) 2026-09-14 16:31:32 +09:00
19 changed files with 405 additions and 175 deletions

7
em-oop/src/BaseData.java Normal file
View File

@@ -0,0 +1,7 @@
public abstract class BaseData {
public abstract String toString();
public abstract String toCSV();
public abstract String getPrimaryKeyValue();
}

View File

@@ -3,7 +3,17 @@ public class BasicCheck1 {
public static void main(String[] args) { public static void main(String[] args) {
// TODO 自動生成されたメソッド・スタブ // TODO 自動生成されたメソッド・スタブ
Book book = new Book();
book.title = "吾輩は猫である";
book.author = "夏目漱石";
book.price = 800;
System.out.println(book.title);
System.out.println(book.author);
System.out.println(book.price);
System.out.println();
//9/15段階1
Book book1 = new Book(); Book book1 = new Book();
System.out.println(book1.title); System.out.println(book1.title);
System.out.println(book1.author); System.out.println(book1.author);
@@ -13,5 +23,7 @@ public class BasicCheck1 {
System.out.println(book2.title); System.out.println(book2.title);
System.out.println(book2.author); System.out.println(book2.author);
System.out.println(book2.price); System.out.println(book2.price);
} }
} }

View File

@@ -3,19 +3,13 @@ public class BasicCheck2 {
public static void main(String[] args) { public static void main(String[] args) {
// TODO 自動生成されたメソッド・スタブ // TODO 自動生成されたメソッド・スタブ
Student student1 = new Student(1, "鈴木一郎");
//コンストラクタを使ってインスタンスを作成 Student student2 = new Student(2, "田中花子");
Student student1 = new Student(1,"鈴木一郎"); Student student3 = new Student(3, "佐藤次郎");
Student student2 = new Student(2,"田中花子");
Student student3 = new Student(3,"佐藤次郎");
//メソッド呼び出し
student1.printProfile(); student1.printProfile();
student2.printProfile(); student2.printProfile();
student3.printProfile(); student3.printProfile();
System.out.println("現在の生徒数:" + Student.getStudentCount() + "人");
//クラスから直接呼び出し
System.out.println("現在の生徒数" +Student.getStudentCount() + "人");
} }
} }

View File

@@ -3,32 +3,24 @@ public class BasicCheck3 {
public static void main(String[] args) { public static void main(String[] args) {
// TODO 自動生成されたメソッド・スタブ // TODO 自動生成されたメソッド・スタブ
Item[] items = new Item[3]; Item[] items = new Item[3];
for (int i = 0; i < items.length; i++) {
items[0] = new Item(); items[i] = new Item();
}
items[0].setName("ノート"); items[0].setName("ノート");
items[0].setPrice(120); items[0].setPrice(120);
items[0].setStock(50); items[0].setStock(50);
items[1] = new Item();
items[1].setName("消しゴム"); items[1].setName("消しゴム");
items[1].setPrice(80); items[1].setPrice(80);
items[1].setStock(30); items[1].setStock(30);
items[2] = new Item();
items[2].setName("鉛筆"); items[2].setName("鉛筆");
items[2].setPrice(60); items[2].setPrice(60);
items[2].setStock(100); items[2].setStock(100);
items[0].sell(5); for (Item i : items) {
items[1].sell(5); i.sell(5);
items[2].sell(5); System.out.println(i);
for(int i= 0;i<3;i++) {
System.out.println(items[i]);
} }
} }
} }

View File

@@ -1,20 +1,20 @@
public class Book { public class Book {
public String title; String title;
public String author; String author;
public int price; int price;
//引数なしコンストラクタ //コンストラクタ
public Book() { public Book() {
title = "タイトル未設定"; title = "タイトル未設定";
author = "著者未定"; author = "著者未設定";
price = 0; price = 0;
} }
//3つの引数を持つコンストラクタ
//引数ありコンストラクタ
public Book(String title, String author, int price) { public Book(String title, String author, int price) {
this.title = title; this.title = title;
this.author = author; this.author = author;
this.price = price; this.price = price;
} }
} }

View File

@@ -1,22 +1,19 @@
public class Circle extends Shape { public class Circle extends Shape {
//Shapeクラスを継承
//フィールド作成
private double radius; private double radius;
//コンストラクタ作成
public Circle(double radius) { public Circle(double radius) {
super("円"); super("円");
this.radius = radius; this.radius = radius;
} }
//メソッド作成 @Override
public double area() { public double area() {
return Math.PI * radius * radius; return Math.PI * radius * radius;
} }
@Override
public void showInfo() { public void showInfo() {
super.showInfo(); super.showInfo();
System.out.println("(半径:" + this.radius + ")"); System.out.println("(半径:" + this.radius + ")");

View File

@@ -2,14 +2,7 @@
public class Day3Check1 { public class Day3Check1 {
public static void main(String[] args) { public static void main(String[] args) {
Circle circle = new Circle(2);
//インスタンス作成 circle.showInfo();
Circle c = new Circle(2);
//メソッド呼び出し
c.showInfo();
} }
} }

View File

@@ -2,10 +2,13 @@
public class Day3Check2 { public class Day3Check2 {
public static void main(String[] args) { public static void main(String[] args) {
// TODO 自動生成されたメソッド・スタブ Rectangle rectangle = new Rectangle(3, 4);
Rectangle r = new Rectangle(3, 4); rectangle.showInfo();
r.showInfo(); Greeting jpGreeting = new JapaneseGreeting();
Greeting gre = new JapaneseGreeting(); System.out.println(jpGreeting.hello());
System.out.println(gre.hello()); LoudGreeting jpShout = new JapaneseGreeting();
System.out.println(jpShout.shout());
} }
} }

View File

@@ -1,17 +1,11 @@
public class Day3Check3 { public class Day3Check3 {
public static void main(String[] args) { public static void main(String[] args) {
// TODO 自動生成されたメソッド・スタブ
//Shape型の変数shapeにradius引数2のCircle代入
Shape shape1 = new Circle(2); Shape shape1 = new Circle(2);
shape1.showInfo(); shape1.showInfo();
//Shape型の変数2に引数(widthが3、heightが4)を代入する
Shape shape2 = new Rectangle(3, 4); Shape shape2 = new Rectangle(3, 4);
shape2.showInfo(); shape2.showInfo();
} }
} }

View File

@@ -0,0 +1,39 @@
public class DepartmentData extends BaseData {
private String idDepartment;
private String nmDepartment;
@Override
public String toString() {
return getIdDepartment() + "\t" + getNmDepartment();
}
@Override
public String toCSV() {
return getIdDepartment() + "," + getNmDepartment();
}
@Override
public String getPrimaryKeyValue() {
// TODO 自動生成されたメソッド・スタブ
return getIdDepartment();
}
public String getIdDepartment() {
return idDepartment;
}
public void setIdDepartment(String idDepartment) {
this.idDepartment = idDepartment;
}
public String getNmDepartment() {
return nmDepartment;
}
public void setNmDepartment(String nmDepartment) {
this.nmDepartment = nmDepartment;
}
}

View File

@@ -1,17 +1,112 @@
public class EmployeeData { public class EmployeeData extends BaseData {
String idEmployee; private String idEmployee;
String nmEmployee; private String nmEmployee;
String knEmployee; private String knEmployee;
String mailAddress; private String mailAddress;
String password; private String password;
String idDepartment; private String idDepartment;
private static int empCount = 0;
void showInfo(){ static final String COMPANY_NAME = "Rhizome";
System.out.println(idEmployee + "\t" + nmEmployee +
"\t" +knEmployee + "\t" + mailAddress + //コンストラクタ
"\t" +password + "\t" + idDepartment ); public EmployeeData() {
empCount++;
}
//引数ありコンストラクタ
public EmployeeData(String idEmployee, String nmEmployee, String knEmployee,
String mailAddress, String password, String idDepartment) {
this.setIdEmployee(idEmployee);
this.setNmEmployee(nmEmployee);
this.setKnEmployee(knEmployee);
this.setMailAddress(mailAddress);
this.setPassword(password);
this.setIdDepartment(idDepartment);
empCount++;
}
@Override
public String toString() {
return this.idEmployee + "\t" + this.nmEmployee
+ "\t" + this.knEmployee + "\t" + this.mailAddress
+ "\t" + this.password + "\t" + this.idDepartment;
}
public String getIdEmployee() {
return idEmployee;
}
public void setIdEmployee(String idEmployee) {
if (!check(idEmployee, "社員ID"))
return;
this.idEmployee = idEmployee;
}
public String getNmEmployee() {
return nmEmployee;
}
public void setNmEmployee(String nmEmployee) {
if (!check(nmEmployee, "社員名"))
return;
this.nmEmployee = nmEmployee;
}
public String getKnEmployee() {
return knEmployee;
}
public void setKnEmployee(String knEmployee) {
this.knEmployee = knEmployee;
}
public String getMailAddress() {
return mailAddress;
}
public void setMailAddress(String mailAddress) {
this.mailAddress = mailAddress;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getIdDepartment() {
return idDepartment;
}
public void setIdDepartment(String idDepartment) {
this.idDepartment = idDepartment;
}
public static int getEmpCount() {
return empCount;
}
private boolean check(String str, String stringName) {
if (str == null || str.strip().length() == 0) {
System.out.println(stringName + "が空のため設定できませんでした");
return false;
}
return true;
}
@Override
public String toCSV() {
return getIdEmployee() + "," + getNmEmployee() + "," + getKnEmployee() + ","
+ getMailAddress() + "," + getPassword() + "," + getIdDepartment();
}
@Override
public String getPrimaryKeyValue() {
return this.idEmployee;
} }
} }

View File

@@ -1,5 +1,4 @@
public interface Greeting { public interface Greeting {
String hello(); String hello();
} }

View File

@@ -1,10 +1,22 @@
public class Item { public class Item {
// private String name;
String name;
private int price; private int price;
private int stock; private int stock;
void sell(int amount) {
this.setStock(this.getStock() - amount);
}
@Override
public String toString() {
return this.getName() + " " + this.getPrice() + "円(残り" + this.getStock() + "個)";
}
int totalPrice() {
return this.price * this.stock;
}
public String getName() { public String getName() {
return name; return name;
} }
@@ -28,12 +40,4 @@ public class Item {
public void setStock(int stock) { public void setStock(int stock) {
this.stock = stock; this.stock = stock;
} }
}
void sell(int amount) {
this.setStock(this.getStock() - amount);
}
@Override
public String toString() {
return getName() + " " + getPrice() + "円 (残り" + getStock() + "個)";
}
}

View File

@@ -1,10 +1,14 @@
//
public class JapaneseGreeting implements Greeting{ public class JapaneseGreeting implements Greeting, LoudGreeting {
@Override @Override
public String hello() { public String hello() {
// TODO 自動生成されたメソッド・スタブ
return "こんにちは"; return "こんにちは";
} }
@Override
public String shout() {
return "やっほー!";
}
} }

View File

@@ -0,0 +1,4 @@
public interface LoudGreeting {
String shout();
}

View File

@@ -1,57 +1,145 @@
import java.util.Scanner;
public class Main { public class Main {
public static void main(String[] args) { public static void main(String[] args) {
// TODO 自動生成されたメソッド・スタブ System.out.println("会社名:" + EmployeeData.COMPANY_NAME);
//型、変数宣言しインスタンス化 BaseData employee = new EmployeeData("10000", "山田龍也", "ヤマダタツヤ",
EmployeeData employee = new EmployeeData(); "yamada@hoge.jp", "aaa", "10");
System.out.println(employee.toCSV());
EmployeeData[] employees = new EmployeeData[5]; System.out.println(employee.getPrimaryKeyValue());
employees[0] = new EmployeeData();
employees[1] = new EmployeeData(); System.out.println();
employees[2] = new EmployeeData();
employees[3] = new EmployeeData(); BaseData[] employees = new BaseData[5];
employees[4] = new EmployeeData();
//setterがBaseData.javaにはないためコンストラクタで代入
//フィールドに代入 // for (int i = 0; i < employees.length; i++) {
employees[0].idEmployee = "10000"; // employees[i] = new EmployeeData();
employees[0].nmEmployee = "山田龍也"; // }
employees[0].knEmployee = "ヤマダタツヤ"; //
employees[0].mailAddress = "yamada@hoge.jp"; // employees[0].setIdEmployee("10000");
employees[0].password = "aaa"; // employees[0].setNmEmployee("山田龍也");
employees[0].idDepartment = "10"; // employees[0].setKnEmployee("ヤマダタツヤ");
// employees[0].setMailAddress("yamada@hoge.jp");
employees[1].idEmployee = "10001"; // employees[0].setPassword("aaa");
employees[1].nmEmployee = "高橋準"; // employees[0].setIdDepartment("10");
employees[1].knEmployee = "タカハシジュン"; //
employees[1].mailAddress = "jtaka@hoge.jp"; // employees[1].setIdEmployee("10001");
employees[1].password = "bbb"; // employees[1].setNmEmployee("高橋準\t");
employees[1].idDepartment = "10"; // employees[1].setKnEmployee("タカハシジュン");
// employees[1].setMailAddress("jtaka@hoge.jp");
employees[2].idEmployee = "10002"; // employees[1].setPassword("bbb");
employees[2].nmEmployee = "麻枝史明"; // employees[1].setIdDepartment("10");
employees[2].knEmployee = "マエダフミアキ"; //
employees[2].mailAddress = "fumi@hoge.jp"; // employees[2].setIdEmployee("10002");
employees[2].password = "ccc"; // employees[2].setNmEmployee("麻枝史明");
employees[2].idDepartment = "11"; // employees[2].setKnEmployee("マエダフミアキ");
// employees[2].setMailAddress("fmak@hoge.jp");
employees[3].idEmployee = "10003"; // employees[2].setPassword("ccc");
employees[3].nmEmployee = "丸戸章介"; // employees[2].setIdDepartment("11");
employees[3].knEmployee = "マルトショウスケ"; //
employees[3].mailAddress = "marusyo@hoge.jp"; // employees[3].setIdEmployee("10003");
employees[3].password = "ddd"; // employees[3].setNmEmployee("丸戸章介");
employees[3].idDepartment = "12"; // employees[3].setKnEmployee("マルトショウスケ");
// employees[3].setMailAddress("marusyo@hoge.jp");
employees[4].idEmployee = "10004"; // employees[3].setPassword("ddd");
employees[4].nmEmployee = "三宅武"; // employees[3].setIdDepartment("12");
employees[4].knEmployee = "ミヤケタケシ"; //
employees[4].mailAddress = "miya@hoge.jp"; // employees[4].setIdEmployee("10004");
employees[4].password = "eee"; // employees[4].setNmEmployee("三宅武\t");
employees[4].idDepartment = "13"; // employees[4].setKnEmployee("ミヤケタケシ");
// employees[4].setMailAddress("miya@hoge.jp");
for(int i= 0; i < 5 ; i++) { // employees[4].setPassword("eee");
employees[i].showInfo(); // employees[4].setIdDepartment("13");
employees[0] = new EmployeeData("10000", "山田龍也", "ヤマダタツヤ",
"yamada@hoge.jp", "aaa", "10");
employees[1] = new EmployeeData("10001", "高橋準", "タカハシジュン",
"jtaka@hoge.jp", "bbb", "10");
employees[2] = new EmployeeData("10002", "麻枝史明", "マエダフミアキ",
"fmak@hoge.jp", "ccc", "11");
employees[3] = new EmployeeData("10003", "丸戸章介", "マルトショウスケ",
"marusyo@hoge.jp", "ddd", "12");
employees[4] = new EmployeeData("10004", "三宅武", "ミヤケタケシ",
"miya@hoge.jp", "eee", "13");
for (BaseData e : employees) {
System.out.println(e);
}
System.out.println("現在の社員のデータ件数:" + EmployeeData.getEmpCount() + "件");
System.out.println();
//社員データ+部署データ
BaseData[] allData = new BaseData[11];
for (int i = 0; i < employees.length; i++) {
allData[i] = employees[i];
}
DepartmentData[] dept = new DepartmentData[6];
for (int i = 0; i < dept.length; i++) {
dept[i] = new DepartmentData();
}
dept[0].setIdDepartment("10");
dept[0].setNmDepartment("役員");
allData[5] = dept[0];
dept[1].setIdDepartment("11");
dept[1].setNmDepartment("人事・総務部");
allData[6] = dept[1];
dept[2].setIdDepartment("12");
dept[2].setNmDepartment("経理部");
allData[7] = dept[2];
dept[3].setIdDepartment("13");
dept[3].setNmDepartment("システム開発事業部");
allData[8] = dept[3];
dept[4].setIdDepartment("14");
dept[4].setNmDepartment("教育事業部");
allData[9] = dept[4];
dept[5].setIdDepartment("15");
dept[5].setNmDepartment("営業企画部");
allData[10] = dept[5];
for (BaseData data : allData) {
System.out.println(data);
}
System.out.println();
//nullおよび前後の空白を取り除いた結果が空文字チェック
// BaseData emp = new EmployeeData("10000", "デフォルト", "ヤマダタツヤ", "yamada@hoge.jp", "aaa", "10");
// System.out.println(emp);
// (EmployeeData)emp.setNmEmployee(null);
// System.out.println(emp);
// emp.setNmEmployee("   ");
// System.out.println(emp);
BaseData emptyEmployee = new EmployeeData("", "", "", "", "", "");
System.out.println(emptyEmployee);
System.out.println();
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("検索したい社員IDを入力してください(終了する場合は0)");
System.out.print(">>>>");
String findId = scanner.nextLine();
if (findId.equals("0")) {
System.out.println("システムを終了します");
return;
}
boolean matchFlag = false;
for (BaseData e : employees) {
if (e.getPrimaryKeyValue().equals(findId)) {
System.out.println(e);
matchFlag = true;
break;
}
}
if (!matchFlag) {
System.out.println("該当する社員が見つかりませんでした");
}
System.out.println();
} }
} }

18
em-oop/src/Rectangle.java Normal file
View File

@@ -0,0 +1,18 @@
public class Rectangle extends Shape {
private double width;
private double height;
public Rectangle(double width, double height) {
super("四角形");
this.width = width;
this.height = height;
}
@Override
public double area() {
return width * height;
}
}

View File

@@ -1,22 +1,14 @@
public class Shape { public abstract class Shape {
//フィールド作成
private String name; private String name;
//コンストラクタ作成
public Shape(String name) { public Shape(String name) {
this.name = name;
} }
//メソッド作成 public abstract double area();
double area() {
return 0.0;
}
//メソッド作成 public void showInfo() {
void showInfo() {
//出力
System.out.println(this.name + "の面積は" + this.area() + "です"); System.out.println(this.name + "の面積は" + this.area() + "です");
} }
} }

View File

@@ -1,25 +1,20 @@
public class Student { public class Student {
//変数を作る int studentId;
int studentId; String name;
String name; static int studentCount = 0;
//フィールド追加 void printProfile() {
static int studentCount = 0;
//コンストラクタ追加
public Student(int studentId,String name) {
this.studentId = studentId;
this.name = name;
studentCount++;
}
//メソッド追加
public static int getStudentCount() {
return studentCount;
}
//メソッド
public void printProfile() {
System.out.println("学籍番号:" + this.studentId + " 名前:" + this.name); System.out.println("学籍番号:" + this.studentId + " 名前:" + this.name);
}
}
public Student(int studentId, String name) {
this.studentId = studentId;
this.name = name;
studentCount++;
}
public static int getStudentCount() {
return studentCount;
}
} }