Merge remote-tracking branch 'origin/master'
commit
e0f8a9360e
@ -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);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,26 @@
|
||||
package _5._3;
|
||||
|
||||
import java.util.LinkedList;
|
||||
|
||||
public class Group<T extends Older> {
|
||||
private LinkedList<T> members = new LinkedList<>();
|
||||
|
||||
public void add(T member) {
|
||||
this.members.add(member);
|
||||
}
|
||||
|
||||
public T getOldest() {
|
||||
if (members.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
T oldestMember = members.get(0);
|
||||
|
||||
for (int i=1; i<members.size(); i++) {
|
||||
if (members.get(i).isOlder(oldestMember)) {
|
||||
oldestMember = members.get(i);
|
||||
}
|
||||
}
|
||||
return oldestMember;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,7 @@
|
||||
package _5._3;
|
||||
|
||||
public interface Older<T> {
|
||||
public boolean isOlder(T other);
|
||||
|
||||
public int getAge();
|
||||
}
|
||||
@ -0,0 +1,23 @@
|
||||
package _5._3;
|
||||
|
||||
public class Person<T extends Older> implements Older<T> {
|
||||
private String name;
|
||||
private int age;
|
||||
|
||||
public Person(String name, int age) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public int getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public boolean isOlder(T other) {
|
||||
return this.age > other.getAge();
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue