Cách sử dụng getter/setter và thừa kế
import java.io.IOException;
import java.util.*;
interface IELECTRONIC_DEVICE {
double sum_total();
}
abstract class TV implements IELECTRONIC_DEVICE {
private String tvID;
private String manifacturer;
private String entryDate;
private double price;
private double num;
public String getTvID() {
return tvID;
}
public void setTvID(String tvID) {
this.tvID = tvID;
}
public String getManifacturer() {
return manifacturer;
}
public void setManifacturer(String manifacturer) {
this.manifacturer = manifacturer;
}
public String getEntryDate() {
return entryDate;
}
public void setEntryDate(String entryDate) {
this.entryDate = entryDate;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public double getNum() {
return num;
}
public void setNum(double num) {
this.num = num;
}
public abstract double sum_total();
public abstract double discount();
public TV(String tvID, String manifacturer, String entryDate, double price, double num) {
this.tvID = tvID;
this.manifacturer = manifacturer;
this.entryDate = entryDate;
this.price = price;
this.num = num;
}
public TV() {
this.tvID = "";
this.manifacturer = "";
this.entryDate = "";
this.price = 0;
this.num = 0;
}
void input() {
Scanner objSc = new Scanner(System.in);
System.out.print("tvID:");
this.setTvID(objSc.nextLine());
System.out.print("Manifacturer:");
this.setManifacturer(objSc.nextLine());
System.out.print("entryDate:");
this.setEntryDate(objSc.nextLine());
System.out.print("Price:");
this.setPrice(objSc.nextDouble());
System.out.print("nume:");
this.setNum(objSc.nextDouble());
}
void output() {
System.out.print("tvID:" + this.getTvID());
System.out.print("Manifacturer:" + this.getManifacturer());
System.out.print("tvID:" + this.getPrice());
}
}
class TV_SAMSUNG extends TV {
String state;
public TV_SAMSUNG() {
this.state = "";
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public double sum_total() {
return this.getPrice() * this.getNum() - this.discount();
}
public double discount() {
double dis;
if (this.getState().equals("new"))
dis = this.getPrice() * this.getNum() * 0.1;
else
dis = this.getPrice() * this.getNum() * 0.6;
return dis;
}
void input() {
super.input();
Scanner objSc = new Scanner(System.in);
System.out.print("state:");
this.setState(objSc.nextLine());
}
void output() {
super.output();
System.out.println("state:" + this.getState());
}
}
class TV_SONY extends TV {
double surchage;
public TV_SONY() {
this.surchage = 0;
}
public double getSurchage() {
return surchage;
}
public void setSurchage(double surchage) {
this.surchage = surchage;
}
@Override
public double sum_total() {
// TODO Auto-generated method stub
return this.getPrice() * this.getNum() + this.getSurchage() - this.discount();
}
@Override
public double discount() {
return this.getPrice() * this.getNum() * 0.05;
}
void output() {
super.output();
System.out.println("surchage:" + this.getSurchage());
}
void input() {
super.input();
Scanner objSc = new Scanner(System.in);
System.out.print("surchage:");
this.setSurchage(objSc.nextDouble());
}
}
Trương Đình Huy