ミッション課題
This commit is contained in:
@@ -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);
|
||||
@@ -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);
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -9,4 +9,13 @@ public class EmployeeData {
|
||||
String idDepartment;
|
||||
}
|
||||
|
||||
// public EmployeeData() {
|
||||
// this.idEmployee = "";
|
||||
// }
|
||||
|
||||
public void showinfo() {
|
||||
System.out.println();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,31 +1,38 @@
|
||||
|
||||
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;
|
||||
}
|
||||
public void setStock() {
|
||||
this.stock = stock;
|
||||
return stock;
|
||||
}
|
||||
|
||||
public void setStock(int stock) {
|
||||
this.stock = stock;
|
||||
}
|
||||
|
||||
// Item(String name, int price, int stock){
|
||||
// this.name = name;
|
||||
@@ -33,11 +40,15 @@ public class Item {
|
||||
// this.stock = stock;
|
||||
// }
|
||||
|
||||
//売れた数を在庫から引く
|
||||
public void sell(int amount) {
|
||||
//メソッドを宣言
|
||||
this.setStock(this.getStock() - amount);
|
||||
}
|
||||
|
||||
//stockからamountを引く
|
||||
// this.getStock(this.getStock() - amount);
|
||||
//Itemの情報を文字列として返す
|
||||
@Override
|
||||
public String toString() {
|
||||
return getName() + "" + getPrice() + "円(残り" + getStock() + "個)";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,22 +1,57 @@
|
||||
|
||||
public class Student {
|
||||
public class Student {//クラス新規作成
|
||||
|
||||
int studentId;
|
||||
String name;
|
||||
int studentId; //フィールドを宣言 int
|
||||
String name; //String
|
||||
static int studentCount = 0; //生徒が何人作られたかを数えるための、クラス全体で共有するフィールド
|
||||
|
||||
// public Student(int studentId, String name) {
|
||||
// this.studentId = studentId;
|
||||
// this.name;
|
||||
void printProfile() {//メソッドを宣言・以下の内容をsysoで出力
|
||||
|
||||
void printProfile() {
|
||||
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;
|
||||
|
||||
Reference in New Issue
Block a user