JAVA-实验2-1-类设计与实现综合实验

题目:

实验内容:

请替宠物医院设计并完成一个宠物信息系统,现有完成系统的的类的uml图,及部分类的部分实现代码,和一个测试类P3main.java及main方法的标准输出。总体要求如下:

  1. P3main.java中的main()方法实现对整个宠物信息系统的测试,请不要对main()方法的已有语句做任何修改,但如果你觉得这个方法有些功能还没有测试到的话,可以在该方法新增测试语句。
  2. 系统完成后,对比P3main.java的输出和标准输出,调整你的程序,直至输出和标准输出完全一样。
  3. 仔细阅读类的uml图,明确类之间继承关系,以及类与接口的关系。
  4. 本次作业会考察大家对数组的应用,请不要用java类库中的ArrayList或其它的集合类来完成数据的存储及相关操作。
  5. 完成的类中不能有public的成员变量。

以下是关于你需要实现的类的一些重要说明,请仔细阅读,但说明并不全面,所以你还需要根据功能编写一些额外的方法或域。

  1. Pet类:该类主要用户记录一个宠物的相关信息

A. 一个宠物必须包含name(宠物名),owner’s name(宠物主人的名字)和weight(宠物体重)和其它一些必须的数据。

B. Pet类必须实现Comparable接口,以支持在Vet类中有对庞物排序。排序的依据为首先考虑宠物主人的姓名的字母顺序,再考虑宠物的名字字母顺序,排序的时候均忽略大小写。

C.Pet类必须实现equals方法,如果两个宠物的主人名字和庞物的名字都相等(忽略大小写),则认为这两个宠物相等。

D. 已提供该类的基本架构和相关的文档注释,除了补充完成相关方法之外,还需要根据类的功能增加必要的域和方法。

  1. Cat类:Pet的子类,记录宠物猫的相关信息。

A.该类需要一个实例域来记录该宠物猫是一只仅在户内饲养的猫(inside)还是一只也会去户外活动的猫(outside)。

B.该类需要一个实例域方法能在猫去户外后,改变其状态为一只(outside)的猫。

C. 所有的宠物猫默认情况下仅在户内饲养。但一旦去了一次户外,就是一只outside的猫了。

D.该类需要重写visit()方法,重写后功能为:宠物猫每次来宠物医院,会需要额外20$的洗牙费(每次来都会洗),同时outside猫每次相比一般宠物(Pet)还要多打一针(shot),该针的费用同一般宠物(Pet)打针费用。重写该方法的时候需要注意,Pet类的相关数据也需要正确更新(提示,可先调用父类的visit()方法)。

  1. Dog类:Pet的子类,记录宠物狗的相关信息。

A.该类需要一个实例域来记录该宠物狗是一只小型(small)、中型(medium)还是大型(large)的狗。该实例域在构造方法中赋值,可参看P3main.java中的语句。

B.该类需要重写visit()方法,重写后功能为:宠物狗每次来宠物医院,需要收取的额外费用为:15$的修剪指甲,中型(Medium)狗的打针费用每针比一般宠物(Pet)多收取2.5$,大型(Large)狗的打针费用每针比一般宠物(Pet)多收取5$。重写该方法的时候需要注意,Pet类的相关数据也需要正确更新(提示,可先调用父类的visit()方法)。

  1. Vet类:宠物医院类

A. 该类需要保存但不限于医院的名字以及在该医院就诊的宠物信息集合。

B.该类必须实现已提供的Database.java中的接口,部分代码已经给出,除了完善相关方法之外,也需要根据类的功能增加必要的实例域和相关方法。

     C.delete方法在删除相关数据后,不能改变数组中数据顺序。

UML图:

预期结果(p3out):

visit1: 115.0

--- Vet has 2 clients
Vet Pets R Us client list: 
Rudolph (owner Santa Claus) 350.0 lbs, $115.00 avg cost/visit  
Tweetie Bird (owner Looney Tunes) 1.75 lbs, $0.00 avg cost/visit  
>> average client weight: 175.875
visit0: 105.0
inside visit1: 135.0
outside visit1: 165.0
c is outside cat Sylvester (owner Looney Tunes) 15.5 lbs, $150.00 avg cost/visit  

--- Vet has 4 clients
Vet Pets R Us client list: 
Rudolph (owner Santa Claus) 350.0 lbs, $115.00 avg cost/visit  
Tweetie Bird (owner Looney Tunes) 1.75 lbs, $0.00 avg cost/visit  
inside cat Tiger (owner Some Body) 8.0 lbs, $105.00 avg cost/visit  
outside cat Sylvester (owner Looney Tunes) 15.5 lbs, $150.00 avg cost/visit  
>> average client weight: 93.8125
med visit3: 197.5
lg visit3: 205.0

--- Vet has 6 clients
Vet Pets R Us client list: 
Rudolph (owner Santa Claus) 350.0 lbs, $115.00 avg cost/visit  
Tweetie Bird (owner Looney Tunes) 1.75 lbs, $0.00 avg cost/visit  
inside cat Tiger (owner Some Body) 8.0 lbs, $105.00 avg cost/visit  
outside cat Sylvester (owner Looney Tunes) 15.5 lbs, $150.00 avg cost/visit  
medium dog Fido (owner Some Body) 32.2 lbs, $197.50 avg cost/visit  
large dog Dino (owner Flintstones) 150.0 lbs, $205.00 avg cost/visit  
>> average client weight: 92.90833333333335

--- Vet has 8 clients
Vet Pets R Us client list: 
Rudolph (owner Santa Claus) 350.0 lbs, $115.00 avg cost/visit  
Tweetie Bird (owner Looney Tunes) 1.75 lbs, $0.00 avg cost/visit  
inside cat Tiger (owner Some Body) 8.0 lbs, $105.00 avg cost/visit  
outside cat Sylvester (owner Looney Tunes) 15.5 lbs, $150.00 avg cost/visit  
medium dog Fido (owner Some Body) 32.2 lbs, $197.50 avg cost/visit  
large dog Dino (owner Flintstones) 150.0 lbs, $205.00 avg cost/visit  
small dog Benji (owner Joe Camp) 10.0 lbs, $0.00 avg cost/visit  
inside cat Tony (owner Kellogg) 20.0 lbs, $0.00 avg cost/visit  
>> average client weight: 73.43125

a few more tests...
find p: inside cat Tony (owner Kellogg) 20.0 lbs, $0.00 avg cost/visit  
Tweetie 5 shots: 235.0
found Tweetie: Tweetie Bird (owner Looney Tunes) 1.75 lbs, $235.00 avg cost/visit  
find Fido: medium dog Fido (owner Some Body) 32.2 lbs, $197.50 avg cost/visit  
adding string to Vet: false
deleting string: null


deleting pets:
Tweetie Bird (owner Looney Tunes) 1.75 lbs, $235.00 avg cost/visit  
medium dog Fido (owner Some Body) 32.2 lbs, $197.50 avg cost/visit  
outside cat Sylvester (owner Looney Tunes) 15.5 lbs, $150.00 avg cost/visit  
--- Vet has 5 clients
Vet Pets R Us client list: 
Rudolph (owner Santa Claus) 350.0 lbs, $115.00 avg cost/visit  
inside cat Tiger (owner Some Body) 8.0 lbs, $105.00 avg cost/visit  
large dog Dino (owner Flintstones) 150.0 lbs, $205.00 avg cost/visit  
small dog Benji (owner Joe Camp) 10.0 lbs, $0.00 avg cost/visit  
inside cat Tony (owner Kellogg) 20.0 lbs, $0.00 avg cost/visit  
>> average client weight: 107.6

after sorting:
--- Vet has 6 clients
Vet Pets R Us client list: 
large dog Dino (owner Flintstones) 150.0 lbs, $205.00 avg cost/visit  
small dog Benji (owner Joe Camp) 10.0 lbs, $0.00 avg cost/visit  
inside cat Tony (owner Kellogg) 20.0 lbs, $0.00 avg cost/visit  
Rudolph (owner Santa Claus) 350.0 lbs, $115.00 avg cost/visit  
Happy (owner Some Body) 13.5 lbs, $0.00 avg cost/visit  
inside cat Tiger (owner Some Body) 8.0 lbs, $105.00 avg cost/visit  
>> average client weight: 91.91666666666667

代码如下:

1.Cat

public class Cat extends Pet {
    private boolean inside = true;

    public Cat(String pname, String oname, double wt) {
        super(pname, oname, wt);
        this.inside = true;
    }

    public Cat() {
    }

    void goOutside() {
        this.inside = false;
    }

    public boolean isInside() {
        return inside;
    }

    public void setInside(boolean inside) {
        this.inside = inside;
    }

    @Override
    public double visit(int shots) {
        if (!this.inside) {
            shots++;
        }
        super.visit(shots);
        this.setSum_cost(20);
        return this.getSum_cost();
    }

    @Override
    public String toString() {
        if (this.inside) {
            return super.toString();
        } else {
            return "outside cat " + this.getName() + " (owner " + this.getOwner() + ") " + this.getWeight()
                    + " lbs, $" + getMoney().format(this.avgCost()) + " avg cost/visit  ";
        }
    }

}

2.Dog

public class Dog extends Pet {
    private String type;

    public Dog(String pname, String oname, double wt, String type) {
        super(pname, oname, wt);
        this.type = type;
    }

    public Dog(String type) {
        this.type = type;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    @Override
    public double visit(int shots) {
        super.visit(shots);
        this.setSum_cost(15);
        if (this.type.equals("medium")) {
            this.setSum_cost(2.5 * shots);
        } else if (this.type.equals("large")) {
            this.setSum_cost(5 * shots);
        }
        return this.getSum_cost();
    }

}

3.Pet

import java.text.DecimalFormat;

/**
 * This is a class to define Pet objects. Pets should be compared
 * according to their owner's names, ignoring capitalization. Ties
 * should be broken based on the pet's name, ignoring capitalization.
 * 
 * Your job is to add the necessary data and methods to support the
 * P3main program, as well as the related classes in this system. Some
 * required methods are noted below with comments, but these are not the
 * only things you will need.
 */
public class Pet implements Comparable<Pet> {

   /** Handy for formatting. */
   private static DecimalFormat money = new DecimalFormat("0.00");

   /* The access specifiers for these variables must not be changed! */

   private String name;
   private String owner;
   private double weight;
   private double sum_cost;
   private int time;

   /**
    * Create a Pet object, initializing data members.
    * 
    * @param pname the Pet's name
    * @param oname the owner's name
    * @param wt    the weight of the pet
    */
   public Pet(String pname, String oname, double wt) {
      this.name = pname;
      this.owner = oname;
      this.weight = wt;
      this.sum_cost = 0;
      this.time = 0;
   }

   public Pet() {

   }

   public static DecimalFormat getMoney() {
      return money;
   }

   public static void setMoney(DecimalFormat money) {
      Pet.money = money;
   }

   public String getName() {
      return name;
   }

   public void setName(String name) {
      this.name = name;
   }

   public String getOwner() {
      return owner;
   }

   public void setOwner(String owner) {
      this.owner = owner;
   }

   public double getWeight() {
      return weight;
   }

   public void setWeight(double weight) {
      this.weight = weight;
   }

   public double getSum_cost() {
      return sum_cost;
   }

   public void setSum_cost(double sum_cost) {
      this.sum_cost += sum_cost;
   }

   public int getTime() {
      return time;
   }

   public void setTime(int time) {
      this.time = time;
   }

   @Override
   public String toString() {
      return this.name + " (owner " + this.owner + ") " + this.weight
            + " lbs, $" + money.format(this.avgCost()) + " avg cost/visit  ";
   }

   /**
    * The Pet is visiting the vet, and will be charged accordingly.
    * The base cost for a visit is $85.00, and $30/shot is added.
    * 
    * @param shots the number of shots the pet is getting
    * @return the entire cost for this particular visit
    */
   public double visit(int shots) {
      time++;
      sum_cost += 85 + 30 * shots;
      return 85 + 30 * shots;
   }

   /**
    * Determine the average cost per visit for this pet.
    * 
    * @return that cost, or 0 if no visits have occurred yet
    */
   public double avgCost() {
      if (time == 0)
         return 0;
      return sum_cost / time;
   }

   // Pet类必须实现equals方法,如果两个宠物的主人名字和庞物的名字都相等(忽略大小写),则认为这两个宠物相等。
   public boolean equals(Pet p) {
      if (this.compareTo(p) == 0) {
         return true;
      }
      return false;
   }

   // Pet类必须实现Comparable接口,以支持在Vet类中有对庞物排序。
   // 排序的依据为首先考虑宠物主人的姓名的字母顺序,再考虑宠物的名字字母顺序,排序的时候均忽略大小写。
   @Override
   public int compareTo(Pet o) {
      for (int i = 0; i < owner.length(); i++) {
         if (this.owner.toLowerCase().toCharArray()[i] > o.owner.toLowerCase().toCharArray()[i]) {
            return 1;
         }
         if (this.owner.toLowerCase().toCharArray()[i] < o.owner.toLowerCase().toCharArray()[i]) {
            return -1;
         }
      }

      for (int i = 0; i < name.length(); i++) {
         if (this.name.toLowerCase().toCharArray()[i] > o.name.toLowerCase().toCharArray()[i]) {
            return 1;
         }
         if (this.name.toLowerCase().toCharArray()[i] < o.name.toLowerCase().toCharArray()[i]) {
            return -1;
         }
      }
      return 0;
   }
}

4.Vet

import java.util.Arrays;
/**
 * Class to keep track of client (Pet) information for a Veterinary
 * practice. Some methods are sketched for you, but others will need
 * to be added in order to implement the Database interface and
 * support the P3main program and expected output. You'll also need
 * to add the data members.
 */
public class Vet implements Database {

    private int StartSize;
    private String Who;
    private int now_size;
    private Pet[] pets;

    /**
     * Create a veterinary practice.
     * 
     * @param startSize the capacity for how
     *                  many clients they can handle
     * @param who       the name of the vet practice
     */
    public Vet(int startSize, String who) {
        this.StartSize = startSize;
        this.Who = who;
        this.now_size = 0;
        pets = new Pet[STARTSIZE];
    }

    public int getStartSize() {
        return StartSize;
    }

    public void setStartSize(int startSize) {
        StartSize = startSize;
    }

    public String getWho() {
        return Who;
    }

    public void setWho(String who) {
        Who = who;
    }

    public int getNow_size() {
        return now_size;
    }

    public void setNow_size(int now_size) {
        this.now_size = now_size;
    }

    public Pet[] getPets() {
        return pets;
    }

    public void setPets(Pet[] pets) {
        this.pets = pets;
    }

    /**
     * Display the name of the Vet and all the clients, one per line,
     * on the screen. (See sample output for exact format.)
     */
    public void display() {
        System.out.println("Vet " + Who + " client list:");
        this.sort();
        for (int i = 0; i < now_size; i++) {
            System.out.println(pets[i].toString());
        }
    }

    /**
     * Add an item to the database, if there is room.
     * You are limited by the initial capacity.
     * 
     * @param o the object to add (must be a Pet)
     * @return true if added, false otherwise
     */
    public boolean add(Object o) {
        if (now_size < StartSize) {
            if (!(o instanceof Pet)) {
                return false;
            } else {
                pets[now_size] = (Pet) o;
                now_size++;
                return true;
            }
        } else {
            return false;
        }
    }

    /**
     * Delete an item from the database, if it is there,
     * maintaining the current ordering of the list.
     * 
     * @param o the object to delete
     * @return the item if one is deleted, null otherwise
     */
    public Object delete(Object o) {
        if (o instanceof Pet) {
            for (int i = 0; i < this.now_size; i++) {
                if (pets[i] == (Pet) o) {
                    for (int j = i; j < this.now_size - 1; j++) {
                        pets[j] = pets[j + 1];
                    }
                    now_size--;
                    return (Pet) o;
                }
            }
        }
        return null;
    }

    /**
     * Compute the average weight over all clients.
     * 
     * @return the average
     */
    public double averageWeight() {
        double sumw = 0;
        double avw;
        for (int i = 0; i < now_size; i++) {
            sumw += pets[i].getWeight();
        }
        avw = sumw / now_size;
        return avw;
    }

    /**
     * Sort the clients. (This is complete.)
     */
    public void sort() {
        Arrays.sort(this.pets, 0, this.size());
    }

    @Override
    public int size() {
        return now_size;
    }

    @Override
    public Object find(Object o) {

        int i;
        for (i = 0; i < now_size; i++) {
            if (pets[i].equals((Pet) o)) {
                return pets[i];
            }
        }
        return null;
    }

}

5.Database

/** An interface with common collection operations.
*/
public interface Database {

   // REMEMBER THAT ALL MEMBERS OF AN INTERFACE ARE PUBLIC BY DEFAULT

   /** The default initial size of a database. */
   int STARTSIZE = 20;  // this is public, static, final by default

   /** Find out how many things are actually in the database.
       @return the number
   */
   int size();

   /** Display the items in the database on the screen.
   */
   void display();

   /** Find a particular item in the database.
       @param o the object to search for, based on equals
       @return the object if found, null otherwise
   */
   Object find(Object o);

   /** Add an item to the database, if there is room.
       @param o the object to add
       @return true if added, false otherwise
   */
   boolean add(Object o);

   /** Delete an item from the database, if it is there.
       @param o the object to delete
       @return the item if deleted, null otherwise
   */
   Object delete(Object o);

}

6.P3min

/** 
 *  This program does not test everything your class
 *  system is supposed to do, so you might want to write
 *  some of your own tests.
 */
public class P3main {
   
   /** Main driver method.
    * @param args the arguments (not used)
    */
   public static void main(String[] args) {
      Vet avet = new Vet(50, "Pets R Us");
      Pet p;
   
      p = new Pet("Rudolph", "Santa Claus", 350);
      p.visit(1);
      avet.add(p);
      avet.add(new Pet("Tweetie Bird", "Looney Tunes", 1.75));
      System.out.println("visit1: " + p.visit(1));
      System.out.println();
      show(avet);
   
      p = new Cat("Tiger", "Some Body", 8);
      avet.add(p);
      System.out.println("visit0: " + p.visit(0));
      
      Cat c = new Cat("Sylvester", "Looney Tunes", 15.5);
      System.out.println("inside visit1: " + c.visit(1));
      c.goOutside();
      System.out.println("outside visit1: " + c.visit(1));
      System.out.println("c is " + c);
      avet.add(c);
   
      System.out.println();
      show(avet);
   
      Dog d = new Dog("Fido", "Some Body", 32.2, "medium");
      System.out.println("med visit3: " + d.visit(3));
      avet.add(d);
   
      p = new Dog("Dino", "Flintstones", 150, "large");
      avet.add(p);
      System.out.println("lg visit3: " + p.visit(3));
      System.out.println();
      show(avet);
   
      avet.add(new Dog("Benji", "Joe Camp", 10, "small"));
      p = new Cat("Tony", "Kellogg", 20);
      avet.add(p);
      System.out.println();
      show(avet);
   
      System.out.println("\na few more tests...");
      System.out.println("find p: " + avet.find(p));
      p = (Pet) avet.find(new Pet("Tweetie Bird", "Looney Tunes", 1.5));
      System.out.println("Tweetie 5 shots: " + p.visit(5));
      System.out.println("found Tweetie: " + p);
   
      System.out.println("find Fido: "
         + avet.find(new Dog("Fido", "some body", 32.2, "medium")));
   
      System.out.println("adding string to Vet: " + avet.add("not a pet"));
      System.out.println("deleting string: " + avet.delete("just a string"));
   
      System.out.println();
      System.out.println("\ndeleting pets:");
      System.out.println(avet.delete(p));
      System.out.println(avet.delete(d));
      System.out.println(avet.delete(c));
      show(avet);

      avet.add(new Pet("Happy", "Some Body", 13.5));
      avet.sort();
      System.out.println("\nafter sorting:");
      show(avet);
   }
   
   /** Output the current state of things to the screen.
    *  @param myvet the vet records to display
    */
   public static void show(Vet myvet) {
      System.out.println("--- Vet has " + myvet.size() + " clients");
      myvet.display();
      System.out.println(">> average client weight: " + myvet.averageWeight());
   }
}

运行结果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值