From 9780db9a8fc0b1b9fa8e65507131b01df5715bed Mon Sep 17 00:00:00 2001 From: a-takayama <26-a-takayama-scm@it-college.co.jp> Date: Fri, 18 Sep 2026 10:28:27 +0900 Subject: [PATCH] =?UTF-8?q?=E3=83=9F=E3=83=83=E3=82=B7=E3=83=A7=E3=83=B3?= =?UTF-8?q?=E8=AA=B2=E9=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- em-oop/src/BasicCheck2.java | 33 ++++++++++++++---- em-oop/src/BasicCheck3.java | 29 ++++++++-------- em-oop/src/EmployeeData.java | 9 +++++ em-oop/src/Item.java | 55 ++++++++++++++++++------------ em-oop/src/Student.java | 65 +++++++++++++++++++++++++++--------- 5 files changed, 135 insertions(+), 56 deletions(-) diff --git a/em-oop/src/BasicCheck2.java b/em-oop/src/BasicCheck2.java index ee5fb4e..c1ebe71 100644 --- a/em-oop/src/BasicCheck2.java +++ b/em-oop/src/BasicCheck2.java @@ -1,14 +1,35 @@ public class BasicCheck2 { public static void main(String[] args) { + + Student student = new Student(); -// - student.studentId = 1; - student.name = "鈴木一郎"; -// -// System.out.print("学籍番号"+student.studentId); -// System.out.print("名前:" + student.name); + //Student型のstudentを宣言、インスタンスを代入する + Student student1 = new Student(); + Student student2 = new Student(); + Student student3 = new Student(); + + + student1.studentId = 1; + student1.name = "鈴木一郎"; + //studentIDに1,nameに鈴木一郎を代入 + + student2.studentId = 2; + student2.name ="田中花子"; + + student3.studentId = 3; + student3.name ="佐藤次郎"; + + + + + //studentのprintProfileメソッドを呼び出す student.printProfile(); } } + + +// +//System.out.print("学籍番号"+student.studentId); +//System.out.print("名前:" + student.name); \ No newline at end of file diff --git a/em-oop/src/BasicCheck3.java b/em-oop/src/BasicCheck3.java index 70e0985..6343a74 100644 --- a/em-oop/src/BasicCheck3.java +++ b/em-oop/src/BasicCheck3.java @@ -8,19 +8,19 @@ public class BasicCheck3 { Item[] items = new Item[3]; items[0] = new Item(); - items[0].name = "ノート"; - items[0].price = 120; - items[0].stock = 50; + items[0].setName("ノート"); + items[0].setPrice(80); + items[0].setStock(50); items[1] = new Item(); - items[1].name ="消しゴム"; - items[1].price = 80; - items[1].stock = 30; + items[1].setName("ノート"); + items[1].setPrice(120); + items[1].setStock(30); items[2] = new Item(); - items[2].name ="鉛筆"; - items[2].price = 60; - items[2].stock = 100; + items[2].setName("ノート"); + items[2].setPrice(60); + items[2].setStock(100); @@ -28,16 +28,19 @@ public class BasicCheck3 { - for(int i = 0; i <= 2; i++ ) + for(int i = 0; i < items.length; i++ ) { items[i].sell(5); } System.out.println("在庫"); for(int i = 0; i < items.length; i ++) { - System.out.println("商品名" + items[i].name); - System.out.println("価格" + items[i].price); - System.out.println("在庫" + items[i].stock); + //items[i]をprintlnに渡すとitemクラスのtoStringが自動で呼ばれる + System.out.println(items[i]); + +// System.out.println("商品名" + items[i].name+ "価格" + items[i].price +"在庫" + items[i].stock); +// System.out.println("価格" + items[i].price); +// System.out.println("在庫" + items[i].stock); } diff --git a/em-oop/src/EmployeeData.java b/em-oop/src/EmployeeData.java index bc829e1..3cc0dac 100644 --- a/em-oop/src/EmployeeData.java +++ b/em-oop/src/EmployeeData.java @@ -8,5 +8,14 @@ public class EmployeeData { String password; String idDepartment; } + +// public EmployeeData() { +// this.idEmployee = ""; +// } + + public void showinfo() { + System.out.println(); + + } } diff --git a/em-oop/src/Item.java b/em-oop/src/Item.java index 57a6a61..1087a3e 100644 --- a/em-oop/src/Item.java +++ b/em-oop/src/Item.java @@ -1,43 +1,54 @@ public class Item { - //フィールドを宣言 + //フィールドを宣言・外から触れないようにprivate private String name; private int price; private int stock; - + + //nameを取得するgetter public String getName() { - return this.name; + return name; } - public void setName(String name) { + + //nameを設定するsetter + public void setName(String name) { //item="ノート"はもう使えない。item[0].setName("ノート")にしている this.name = name; } - + + //priceを取得するgetter public int getPrice() { - return this.price; + return price; } - public void getPrice(String name) { + + //priceを設定するsetter + public void setPrice(int price) { this.price = price; } - + + //stockを取得するgetter public int getStock() { - return this.stock; + return stock; } - public void setStock() { + + public void setStock(int stock) { this.stock = stock; } + + // Item(String name, int price, int stock){ + // this.name = name; + // this.price = price; + // this.stock = stock; + // } + + //売れた数を在庫から引く + public void sell(int amount) { + this.setStock(this.getStock() - amount); + } - -// Item(String name, int price, int stock){ -// this.name = name; -// this.price = price; -// this.stock = stock; -// } - - public void sell(int amount) { - //メソッドを宣言 - - //stockからamountを引く -// this.getStock(this.getStock() - amount); + //Itemの情報を文字列として返す + @Override + public String toString() { + return getName() + "" + getPrice() + "円(残り" + getStock() + "個)"; } } diff --git a/em-oop/src/Student.java b/em-oop/src/Student.java index 775b09c..a0d9324 100644 --- a/em-oop/src/Student.java +++ b/em-oop/src/Student.java @@ -1,22 +1,57 @@ -public class Student { - - int studentId; - String name; - -// public Student(int studentId, String name) { -// this.studentId = studentId; -// this.name; - - void printProfile() { - System.out.println("学籍番号:" + this.studentId + " 名前:" + this.name); +public class Student {//クラス新規作成 + + int studentId; //フィールドを宣言 int + String name; //String + static int studentCount = 0; //生徒が何人作られたかを数えるための、クラス全体で共有するフィールド + + void printProfile() {//メソッドを宣言・以下の内容をsysoで出力 + + for(int i = 0; i <= 3; i++) { + 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 Student() { + // TODO 自動生成されたコンストラクター・スタブ + } + + + public int getStudentCount() { + studentCount++; + return studentCount; + } + + +} + + + + + + + + + + + + + // public static void main(String[] args) { // Student s = new Student(1,"鈴木一郎"); // s.printProfile(); + // - } - - - +// +// public Student(int studentId, String name) { +// student.studentId = 1;