603 冰品計價系統

第一題

class Material{
double cost;
double price;
Material(double x,double y){
cost=x;
price=y;
}
double getCost(){
return cost;
}
double getPrice(){
return price;
}
}
class Apple extends Material{
Apple(){
super(6,10);
}

}
class Banana extends Material{
Banana(){
super(2,5);
}
}
class Pudding extends Material{
Pudding(){
super(3,5);
}
}
class Strawberry extends Material{
Strawberry(){
super(1,5);
}
}
class Mango extends Material{
Mango(){
super(2,5);
}
}
class JPA06_1 {
    public static void main(String args[]) {
        Apple ab = new Apple();
        Banana bb = new Banana();
        Pudding pt = new Pudding();
        System.out.println("Apple cost:" + ab.getCost());
        System.out.println("Apple price:" + ab.getPrice());
        System.out.println("Banana cost:" + bb.getCost());
        System.out.println("Banana price:" + bb.getPrice());
        System.out.println("Pudding cost:" + pt.getCost());
        System.out.println("Pudding price:" + pt.getPrice());
    }
}

第二題

abstract class product{
abstract double getPrice();
abstract double getCost();
double getProfit(){
return getPrice()-getCost();
}
}
class A extends product{
Material x1,x2;
A(){}
A(Material a,Material b){
x1=a;
x2=b;
}
double getPrice(){
return x1.getPrice()+x2.getPrice();
}
double getCost(){
return x1.getCost()+x2.getCost();
}
}
class B extends product{
Material x1,x2,x3;
B(){}
B(Material a,Material b,Material c){
x1=a;
x2=b;
x3=c;
}
double getPrice(){
return x1.getPrice()+x2.getPrice()+x3.getPrice();
}
double getCost(){
return x1.getCost()+x2.getCost()+x3.getCost();
}

}

class JPA06_2 {
    public static void main(String args[]) {
A t1 = new A(new Apple(), new Banana());
B t2 = new B(new Banana(), new Pudding(), new Strawberry());
B t3 = new B(new Apple(), new Banana(), new Mango());

        System.out.println("t1 price:" + t1.getPrice());
        System.out.println("t1 profit:" + t1.getProfit());
        System.out.println("t2 price:" + t2.getPrice());
        System.out.println("t2 profit:" + t2.getProfit());
        System.out.println("t3 price:" + t3.getPrice());
        System.out.println("t3 profit:" + t3.getProfit());
    }
}

第三題

class C extends A{
C(){}
C(Material a,Material b){super(a,b);}
double getPrice(){
return (x1.getPrice()+x2.getPrice())*1.5;
}
double getCost(){
return x1.getCost()+x2.getCost()+2;
}
}
class D extends B{
D(){}
D(Material a,Material b,Material c){super(a,b,c);}
double getPrice(){
return (x1.getPrice()+x2.getPrice()+x3.getPrice())*1.5;
}
double getCost(){
return x1.getCost()+x2.getCost()+x3.getCost()+2;
}
}
class JPA06_3 {
    public static void main(String args[]) {
        C t1 = new C (new Apple(), new Banana());
D t2 = new D (new Banana(), new Pudding(), new Strawberry());
D t3 = new D (new Apple(), new Banana(), new Mango());

        System.out.println("t1 cost:" + t1.getCost());
    System.out.println("t1 price:" + t1.getPrice());
        System.out.println("t1 profit:" + t1.getProfit());
        System.out.println("t2 cost:" + t2.getCost());
    System.out.println("t2 price:" + t2.getPrice());
        System.out.println("t2 profit:" + t2.getProfit());
        System.out.println("t3 cost:" + t3.getCost());
    System.out.println("t3 price:" + t3.getPrice());
        System.out.println("t3 profit:" + t3.getProfit());
    }
}

第四題

import java.util.*;

class Deliver{
LinkedList<product> list = new LinkedList<product>();

void addProduct(product x){
list.add(x);
}
double getTotalPrice(){
double total=0;
for(product i:list){
total+=i.getPrice();
}
return total;
}
double getTotalProfit(){
double total=0;
for(product i:list){
total+=i.getProfit();
}
return total;
}
double getTotalCost(){
double total=0;
for(product i:list){
total+=i.getCost();
}
return total;
}

}
class JPA06_4 {
    public static void main(String args[]){
        Deliver d1 = new Deliver();
        d1.addProduct(new A(new Apple(), new Banana()));
        d1.addProduct(new B(new Banana(), new Pudding(), new Strawberry()));
        System.out.println("a Price: " + d1.getTotalPrice());
        System.out.println("a Cost: " + d1.getTotalCost());
        System.out.println("a Profit: " + d1.getTotalProfit());
        Deliver d2 = new Deliver();
        d2.addProduct(new B(new Apple(), new Banana(), new Mango()));
        d2.addProduct(new A(new Apple(), new Banana()));
        d2.addProduct(new B(new Banana(), new Pudding(), new Strawberry()));
        d2.addProduct(new B(new Apple(), new Banana(), new Mango()));
        System.out.println("b Price: " + d2.getTotalPrice());
        System.out.println("b Cost: " + d2.getTotalCost());
        System.out.println("b Profit: " + d2.getTotalProfit());
    }
}

第五題

import java.util.*;

class Deliver{
LinkedList<product> list = new LinkedList<product>();

void addProduct(product x){
list.add(x);
}
double getTotalPrice(){
double total=0;
for(product i:list){
total+=i.getPrice();
}
return total;
}
double getTotalProfit(){
double total=0;
for(product i:list){
total+=i.getProfit();
}
return total;
}
double getTotalCost(){
double total=0;
for(product i:list){
total+=i.getCost();
}
return total;
}
void checkOut() throws notenoughException {

if(getTotalPrice()<50){
throw new notenoughException("Not enough order for carry out:"+getTotalPrice());
}

}
}

class notenoughException extends Exception{
notenoughException(String x){
System.out.println(x);
}
}

class JPA06_5 {
    public static void main(String args[]) {
        try{
            Deliver d1 = new Deliver();
            d1.addProduct(new A(new Apple(), new Banana()));
            d1.addProduct(new B(new Banana(), new Pudding(), new Strawberry()));
            d1.addProduct(new B(new Banana(), new Pudding(), new Strawberry()));
            d1.addProduct(new B(new Apple(), new Banana(), new Mango()));
            System.out.println("a Price: " + d1.getTotalPrice());
            System.out.println("a Cost: " + d1.getTotalCost());
            System.out.println("a Profit: " + d1.getTotalProfit());
            System.out.println("");
            d1.checkOut();
            Deliver d2 = new Deliver();
            d2.addProduct(new B(new Apple(), new Banana(), new Mango()));
            d2.addProduct(new A(new Apple(), new Banana()));
            System.out.println("b Price: " + d2.getTotalPrice());
            System.out.println("b Cost: " + d2.getTotalCost());
            System.out.println("b Profit: " + d2.getTotalProfit());
            d2.checkOut();
        }catch(notenoughException e){

        }
    }
}

沒有留言:

張貼留言