4 Commits

6 changed files with 71 additions and 2 deletions

View File

@@ -3,7 +3,13 @@ public class BasicCheck1 {
public static void main(String[] args) {
// 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);
}
}

View File

@@ -0,0 +1,12 @@
public class BasicCheck2 {
public static void main(String[] args) {
// TODO 自動生成されたメソッド・スタブ
Student student = new Student();
student.studentId = 1;
student.name = "鈴木一郎";
student.printProfile();
}
}

View File

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

View File

@@ -1,4 +1,6 @@
public class Book {
String title;
String author;
int price;
}

14
em-oop/src/Item.java Normal file
View File

@@ -0,0 +1,14 @@
public class Item {
String name;
int price;
int stock;
void sell(int amount) {
this.stock -= amount;
}
int totalPrice() {
return this.price * this.stock;
}
}

9
em-oop/src/Student.java Normal file
View File

@@ -0,0 +1,9 @@
public class Student {
int studentId;
String name;
void printProfile() {
System.out.println("学籍番号:" + this.studentId + " 名前:" + this.name);
}
}