Skip to content

Instantly share code, notes, and snippets.

@fahmifan
Created December 10, 2018 14:18
Show Gist options
  • Save fahmifan/95796b943c6a8a380d1bb3480baa1c62 to your computer and use it in GitHub Desktop.
Save fahmifan/95796b943c6a8a380d1bb3480baa1c62 to your computer and use it in GitHub Desktop.
Test 1 OOP 2018
class Test {
public static void main(String[] args) {
Buyer b1 = new Buyer("Miun", "Pasirlayung, Bandung");
Purchase p = new Purchase(b1);
Car t = new Truck("Hino Dutro", 120000.0, 5000.0);
p.addCar(t);
Car s = new SuperCar("Bugati", 800000.0, 300.0);
p.addCar(s);
p.printPurchase();
}
}
class Car {
protected String name;
protected Double price;
public Car(String name , Double price) {
this.name = name;
this.price = price;
}
public String getName() {return name;}
public Double getPrice() {return price;}
public void setName(String name) {
this.name = name ;
}
public void setPrice(Double price) {
this.price = price;
}
}
class SuperCar extends Car {
private Double topSpeed;
public SuperCar(String name, Double price, Double topSpeed) {
super(name, price);
this.topSpeed = topSpeed;
}
public Double getTopSpeed() {return topSpeed;}
public void setTopSpeed(Double topSpeed) {this.topSpeed = topSpeed;}
}
class Truck extends Car {
private Double maxCapacity;
public Truck(String name, Double price, Double maxCapacity) {
// assign constructor parent class, (Car)
super(name, price);
this.maxCapacity = maxCapacity;
}
public Double getMaxCapacity() {return this.maxCapacity;}
public void setMaxCapacity(Double maxCapacity) {this.maxCapacity = maxCapacity;}
}
interface IPurchase {
void printPurchase();
int getTotalPurchase();
void addCar(Car car);
}
class Purchase implements IPurchase {
private Buyer buyer;
private Car[] cars;
private int totalPurchase;
public Purchase(Buyer buyer) {
this.buyer = buyer;
totalPurchase = 0;
cars = new Car[5];
}
@Override
public void addCar(Car car) {
cars[totalPurchase] = car;
totalPurchase++;
}
@Override
public void printPurchase() {
System.out.printf("Mr./Mrs. %s \n%s \n---\n", buyer.getName(), buyer.getAddress());
for(int i = 0; i < totalPurchase; i++) {
// Polymrphism
if(cars[i] instanceof Truck) {
System.out.printf("%d. %s \nPrice: \t$%.2f \nMax capacity: \t%.2f CC \n\n",
i+1, cars[i].getName(), cars[i].getPrice(), ((Truck)cars[i]).getMaxCapacity());
} else {
System.out.printf("%d. %s \nPrice: \t$%.2f \nTop speed: \t%.2f Mph \n\n",
i+1, cars[i].getName(), cars[i].getPrice(), ((SuperCar)cars[i]).getTopSpeed());
}
}
}
@Override
public int getTotalPurchase() {
return totalPurchase;
}
}
class Buyer {
private String name;
private String address;
public Buyer(String name, String address) {
this.name = name;
this.address = address;
}
public String getName() {return this.name;}
public String getAddress() {return this.address;}
public void setName(String name) {this.name = name;}
public void setAddress(String address) {this.address = address;}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment