اخواني فزعتكم اريد احد يشرح لي الكود خطوه بخطوه. الله يرحم والديكم
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package infsys3816exam1;
interface Convertible{
public Object convertTo(Object o);
@Override
public String toString();
} // end of interface Convertible
abstract class Currency{
private double Value;
private String Symbol;
public Currency(String s, double v){
Symbol = s;
Value = v;
}
public String getSymbol(){
return Symbol;
}
protected double getValue(){
return Value;
}
protected void setValue(double v){
Value=v;
}
@Override
public abstract String toString();
} // end of class Currency
class Dollar extends Currency implements Convertible{
public Dollar(){
this(0.0);
}
public Dollar(double d){
super("$", d);
}
@Override
public String toString(){
String FormattedValue = String.format("%,9.2f", getValue());
return(getSymbol() + FormattedValue);
}
@Override
public Object convertTo(Object o) {
Currency C=null;
if (o instanceof Convertible){
if (o instanceof Euro){
C=(Euro) o;
C.setValue(getValue()/1.5);
}
else{ // assume o is Dollar
C = (Dollar) o;
C.setValue(getValue());
}
}
return C;
}
} // end of class Dollar
class Euro extends Currency implements Convertible{
public Euro(){
this(0.0);
}
public Euro(double d){
super("\u20ac", d);
}
@Override
public String toString(){
String FormattedValue = String.format("%,9.2f", getValue());
return(getSymbol() + FormattedValue);
}
@Override
public Object convertTo(Object o) {
Currency C=null;
if (o instanceof Convertible){
if (o instanceof Dollar){
C=(Dollar) o;
C.setValue(getValue()*1.5);
}
else{ // assume o is Euro
C = (Euro) o;
C.setValue(getValue());
}
}
return C;
}
}// end of class Euro
public class ConversionDriver {
/**
* @param args the command line arguments
*/
public static void main(String[] args){
Convertible L = new Dollar(10000);
Object C = L.convertTo(new Euro()); // convert Dollar to Euro
System.out.println(L + " ==> " + (C!=null? C : "Error: Object is not a valid currency"));
System.out.println();
L = new Euro(10000);
C = L.convertTo(new Dollar()); // convert Euro to Dollar
System.out.println(L + " ==> " + (C!=null? C : "Error: Object is not a valid currency"));
System.out.println();
L=new Euro(1000);
C = L.convertTo(new String());// convert Euro to String, will return null object
System.out.println(L + " ==> " + (C!=null? C : "Error: Object is not a valid currency") );
}
}// end of class CurrencyDriver
