ผลต่างระหว่างรุ่นของ "Java-examples"
Jittat (คุย | มีส่วนร่วม) |
|||
แถว 56: | แถว 56: | ||
phone.dat | phone.dat | ||
--------- | --------- | ||
+ | 3 | ||
prepaid#0812345678:1000.00 | prepaid#0812345678:1000.00 | ||
postpaid#0823456789:50 | postpaid#0823456789:50 |
รุ่นแก้ไขเมื่อ 04:59, 29 เมษายน 2553
เนื้อหา
Phone รุ่นแรก (ก่อนเบรค)
Phone.java
<geshi lang="java"> public class Phone { private double credit; Phone(double inCredit) { credit = inCredit; } public double getCredit() { return credit; } public void setCredit(double credit) { this.credit = credit; } public void call(double minute) { credit -= callCost(minute); } protected double callCost(double minute) { return 0; } } </geshi>
PrepaidPhone.java
<geshi lang="java"> public class PrepaidPhone extends Phone {
PrepaidPhone(double inCredit) { super(inCredit); }
protected double callCost(double minute) { if(minute <= 1) return 1; else return 1 + (minute - 1) * 0.5; } } </geshi>
PostpaidPhone.java
<geshi lang="java"> public class PostpaidPhone extends Phone {
PostpaidPhone(double inCredit) { super(inCredit); }
public double callCost(double minute) { return minute * 0.25; } } </geshi>
Phone รุ่นสอง (หลังเบรค)
input/output
phone.dat --------- 3 prepaid#0812345678:1000.00 postpaid#0823456789:50 prepaid#0888888888:20000000 call.dat -------- 0812345678:5 0812345678:2 0823456789:1.5 0888888888:300 0823456789:12.5 0823456789:40.2 output ------ 0812345678:954 0823456789:15.2 0888888888:199992323.5
Codes
Phone.java
<geshi lang="java"> public class Phone { private double credit; private String number; Phone(String num, double inCredit) { credit = inCredit; number = num; } Phone(String raw) { decode(raw); } public void decode(String raw) { // ... }
public void call(double minute) { credit -= callCost(minute); } double callCost(double minute) { return 0; }
public double getCredit() { return credit; } public void setCredit(double credit) { this.credit = credit; } public String getNumber() { return number; } } </geshi>
PrepaidPhone.java
<geshi lang="java"> public class PrepaidPhone extends Phone {
public PrepaidPhone(String raw) { super(raw); }
PrepaidPhone(String num, double inCredit) { super(num, inCredit); }
protected double callCost(double minute) { if(minute <= 1) return 1; else return 1 + (minute - 1) * 0.5; } } </geshi>
PostpaidPhone.java
<geshi lang="java">
public class PostpaidPhone extends Phone {
public PostpaidPhone(String raw) { super(raw); }
PostpaidPhone(String num, double inCredit) { super(num, inCredit); }
public double callCost(double minute) { return minute * 0.25; } } </geshi>
PhoneMain.java
<geshi lang="java"> public class PhoneMain {
static Phone buildPhone(String raw) { return null; }
static Phone findPhone(Phone [] phones, String num) { for(int i=0; i<phones.length; i++) if(phones[i].getNumber() == num) return phones[i]; return null; }
public static void main(String [] argv) { } } </geshi>