Initial commit for 5.2
parent
257357c8d5
commit
eab5febece
@ -0,0 +1,64 @@
|
||||
package _5._2.Bill;
|
||||
|
||||
import _5._2.CarComponents.*;
|
||||
|
||||
public class Bill {
|
||||
private Billtem[] items;
|
||||
|
||||
public Bill(Billtem[] items) {
|
||||
this.items = items;
|
||||
}
|
||||
|
||||
public double getTotalPrice() {
|
||||
double price = 0;
|
||||
for (Billtem i : items) {
|
||||
price += i.getPrice();
|
||||
}
|
||||
|
||||
return price;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
String string = "";
|
||||
for (Billtem i : items) {
|
||||
string += i.toString() + "\n";
|
||||
}
|
||||
double price = this.getTotalPrice();
|
||||
string += "Total: " + price;
|
||||
return string;
|
||||
}
|
||||
|
||||
|
||||
public static class Billtem {
|
||||
private double price;
|
||||
private CarComponent item;
|
||||
|
||||
public Billtem(CarComponent item, double price) {
|
||||
this.item = item;
|
||||
this.price = price;
|
||||
}
|
||||
|
||||
public double getPrice() {
|
||||
return price;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return item.getName() + getComponents() + ": " + price;
|
||||
}
|
||||
|
||||
|
||||
public String getComponents() {
|
||||
CarComponent[] components = item.getComponents();
|
||||
String names = "";
|
||||
|
||||
if (components != null) {
|
||||
names += "(";
|
||||
for (CarComponent i : components) {
|
||||
names += i.getName() + " ";
|
||||
}
|
||||
names += ")";
|
||||
}
|
||||
return names;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,21 @@
|
||||
package _5._2.Bill;
|
||||
|
||||
import _5._2.CarComponents.*;
|
||||
|
||||
public class TestBill {
|
||||
public static void main(String[] args) {
|
||||
Motor m = new Motor("Motor",null);
|
||||
Car c = new Car("Rolls Royce",new CarComponent[] {m});
|
||||
Seat s = new Seat("Seat",null);
|
||||
Wheel w1 = new Wheel("Wheel",null);
|
||||
Wheel w2 = new Wheel("Wheel",null);
|
||||
Wheel w3 = new Wheel("Wheel",null);
|
||||
Wheel w4 = new Wheel("Wheel",null);
|
||||
|
||||
Bill.Billtem[] billtems = new Bill.Billtem[]{new Bill.Billtem(c, 100000.0), new Bill.Billtem(s,2000.0), new Bill.Billtem(w1, 1000.0), new Bill.Billtem(w2, 1000.0), new Bill.Billtem(w3, 1000.0), new Bill.Billtem(w4, 1000.0)};
|
||||
Bill bill = new Bill(billtems);
|
||||
|
||||
String billprice = bill.toString();
|
||||
System.out.println(billprice);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,19 @@
|
||||
package _5._2.CarComponents;
|
||||
|
||||
public class Car implements CarComponent {
|
||||
private String name;
|
||||
private CarComponent[] components;
|
||||
|
||||
public Car(String name, CarComponent[] components) {
|
||||
this.name = name;
|
||||
this.components = components;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public CarComponent[] getComponents() {
|
||||
return components;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,6 @@
|
||||
package _5._2.CarComponents;
|
||||
|
||||
public interface CarComponent {
|
||||
String getName();
|
||||
CarComponent[] getComponents();
|
||||
}
|
||||
@ -0,0 +1,19 @@
|
||||
package _5._2.CarComponents;
|
||||
|
||||
public abstract class CarPart implements CarComponent {
|
||||
private String name;
|
||||
private CarComponent[] components;
|
||||
|
||||
public CarPart(String name,CarComponent[] components) {
|
||||
this.name = name;
|
||||
this.components = components;
|
||||
}
|
||||
//
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public CarComponent[] getComponents() {
|
||||
return components;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,7 @@
|
||||
package _5._2.CarComponents;
|
||||
|
||||
public final class Motor extends CarPart {
|
||||
public Motor(String name,CarComponent[] components) {
|
||||
super(name,components);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,7 @@
|
||||
package _5._2.CarComponents;
|
||||
|
||||
public final class Seat extends CarPart {
|
||||
public Seat(String name,CarComponent[] components) {
|
||||
super(name,components);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,7 @@
|
||||
package _5._2.CarComponents;
|
||||
|
||||
public final class Wheel extends CarPart {
|
||||
public Wheel(String name,CarComponent[] components) {
|
||||
super(name,components);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue