ミッション課題

This commit is contained in:
2026-09-18 10:28:27 +09:00
parent 439556d4cc
commit 9780db9a8f
5 changed files with 135 additions and 56 deletions

View File

@@ -1,14 +1,35 @@
public class BasicCheck2 { public class BasicCheck2 {
public static void main(String[] args) { public static void main(String[] args) {
Student student = new Student(); Student student = new Student();
// //Student型のstudentを宣言、インスタンスを代入する
student.studentId = 1; Student student1 = new Student();
student.name = "鈴木一郎"; Student student2 = new Student();
// Student student3 = new Student();
// System.out.print("学籍番号"+student.studentId);
// System.out.print("名前:" + student.name);
student1.studentId = 1;
student1.name = "鈴木一郎";
//studentIDに1,nameに鈴木一郎を代入
student2.studentId = 2;
student2.name ="田中花子";
student3.studentId = 3;
student3.name ="佐藤次郎";
//studentのprintProfileメソッドを呼び出す
student.printProfile(); student.printProfile();
} }
} }
//
//System.out.print("学籍番号"+student.studentId);
//System.out.print("名前:" + student.name);

View File

@@ -8,19 +8,19 @@ public class BasicCheck3 {
Item[] items = new Item[3]; Item[] items = new Item[3];
items[0] = new Item(); items[0] = new Item();
items[0].name = "ノート"; items[0].setName("ノート");
items[0].price = 120; items[0].setPrice(80);
items[0].stock = 50; items[0].setStock(50);
items[1] = new Item(); items[1] = new Item();
items[1].name ="消しゴム"; items[1].setName("ノート");
items[1].price = 80; items[1].setPrice(120);
items[1].stock = 30; items[1].setStock(30);
items[2] = new Item(); items[2] = new Item();
items[2].name ="鉛筆"; items[2].setName("ノート");
items[2].price = 60; items[2].setPrice(60);
items[2].stock = 100; 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); items[i].sell(5);
} }
System.out.println("在庫"); System.out.println("在庫");
for(int i = 0; i < items.length; i ++) { for(int i = 0; i < items.length; i ++) {
System.out.println("商品名" + items[i].name); //items[i]をprintlnに渡すとitemクラスのtoStringが自動で呼ばれる
System.out.println("価格" + items[i].price); System.out.println(items[i]);
System.out.println("在庫" + items[i].stock);
// System.out.println("商品名" + items[i].name+ "価格" + items[i].price +"在庫" + items[i].stock);
// System.out.println("価格" + items[i].price);
// System.out.println("在庫" + items[i].stock);
} }

View File

@@ -9,4 +9,13 @@ public class EmployeeData {
String idDepartment; String idDepartment;
} }
// public EmployeeData() {
// this.idEmployee = "";
// }
public void showinfo() {
System.out.println();
}
} }

View File

@@ -1,43 +1,54 @@
public class Item { public class Item {
//フィールドを宣言 //フィールドを宣言・外から触れないようにprivate
private String name; private String name;
private int price; private int price;
private int stock; private int stock;
//nameを取得するgetter
public String getName() { 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; this.name = name;
} }
//priceを取得するgetter
public int getPrice() { public int getPrice() {
return this.price; return price;
} }
public void getPrice(String name) {
//priceを設定するsetter
public void setPrice(int price) {
this.price = price; this.price = price;
} }
//stockを取得するgetter
public int getStock() { public int getStock() {
return this.stock; return stock;
} }
public void setStock() {
public void setStock(int stock) {
this.stock = stock; this.stock = stock;
} }
// Item(String name, int price, int stock){
// this.name = name;
// this.price = price;
// this.stock = stock;
// }
// Item(String name, int price, int stock){ //売れた数を在庫から引く
// this.name = name;
// this.price = price;
// this.stock = stock;
// }
public void sell(int amount) { public void sell(int amount) {
//メソッドを宣言 this.setStock(this.getStock() - amount);
}
//stockからamountを引く //Itemの情報を文字列として返す
// this.getStock(this.getStock() - amount); @Override
public String toString() {
return getName() + "" + getPrice() + "円(残り" + getStock() + "個)";
} }
} }

View File

@@ -1,22 +1,57 @@
public class Student { public class Student {//クラス新規作成
int studentId; int studentId; //フィールドを宣言 int
String name; String name; //String
static int studentCount = 0; //生徒が何人作られたかを数えるための、クラス全体で共有するフィールド
// public Student(int studentId, String name) { void printProfile() {//メソッドを宣言・以下の内容をsysoで出力
// this.studentId = studentId;
// this.name;
void printProfile() { for(int i = 0; i <= 3; i++) {
System.out.println("学籍番号:" + this.studentId + " 名前:" + this.name); 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) { // public static void main(String[] args) {
// Student s = new Student(1,"鈴木一郎"); // Student s = new Student(1,"鈴木一郎");
// s.printProfile(); // s.printProfile();
// //
}
//
// public Student(int studentId, String name) {
// student.studentId = 1;