Accept alternative

generic-observer 5
Selebrator 7 years ago
parent cca07f0bd6
commit 85d12e96dd

@ -1,64 +0,0 @@
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;
}
}
}

@ -1,21 +0,0 @@
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);
}
}

@ -1,19 +0,0 @@
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;
}
}

@ -1,6 +0,0 @@
package _5._2.CarComponents;
public interface CarComponent {
String getName();
CarComponent[] getComponents();
}

@ -1,19 +0,0 @@
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;
}
}

@ -1,7 +0,0 @@
package _5._2.CarComponents;
public final class Motor extends CarPart {
public Motor(String name,CarComponent[] components) {
super(name,components);
}
}

@ -1,7 +0,0 @@
package _5._2.CarComponents;
public final class Seat extends CarPart {
public Seat(String name,CarComponent[] components) {
super(name,components);
}
}

@ -1,7 +0,0 @@
package _5._2.CarComponents;
public final class Wheel extends CarPart {
public Wheel(String name,CarComponent[] components) {
super(name,components);
}
}

@ -1,6 +1,6 @@
package _5._2.alternative.bill;
package _5._2.bill;
import _5._2.alternative.car.CarComponent;
import _5._2.car.CarComponent;
import java.util.LinkedList;
import java.util.List;

@ -1,8 +1,8 @@
package _5._2.alternative.bill;
package _5._2.bill;
import _5._2.alternative.car.Motor;
import _5._2.alternative.car.Seat;
import _5._2.alternative.car.Wheel;
import _5._2.car.Motor;
import _5._2.car.Seat;
import _5._2.car.Wheel;
public class BillExample {
public static void main(String[] args) {

@ -1,4 +1,4 @@
package _5._2.alternative.car;
package _5._2.car;
import java.util.HashSet;
import java.util.Set;

@ -1,4 +1,4 @@
package _5._2.alternative.car;
package _5._2.car;
import java.util.Collection;

@ -1,4 +1,4 @@
package _5._2.alternative.car;
package _5._2.car;
public class Motor extends Car.CarPart {
public Motor() {

@ -1,4 +1,4 @@
package _5._2.alternative.car;
package _5._2.car;
public class Seat extends Car.CarPart {
public Seat() {

@ -1,4 +1,4 @@
package _5._2.alternative.car;
package _5._2.car;
public class Wheel extends Car.CarPart {
public Wheel() {
Loading…
Cancel
Save