需要各种方法来提出这个输出

时间:2016-04-05 15:54:01

标签: java

我应该想出这个输出。

enter image description here

但我得到了这个......

enter image description here

这是我的代码:

import java.lang.*;
import java.util.*;
import java.io.*;

public class Sample{

     private String name;
     private Hashtable customers = new Hashtable();
     private Hashtable movies  = new Hashtable();

     public Sample(String aName){
         name = aName;
     }

     public String getName(){
         return name;

     }

     public void setName(String aName){
         name = aName;
 }
  public void addCustomer (Customer customer) {
    customers.put(customer.getName(), customer);
     }

     public Customer getCustomer (String customerName) {
        return (Customer)customers.get(customerName);
   }

     public void addMovie (Movie movie) {
        movies.put(movie.getName(), movie);
     }

     public Movie getMovie (String movieName) {
        return (Movie)movies.get(movieName);
   }

     public void error (String message) {
     System.out.println ("ERROR: " + message);
   }

     public Enumeration getMovies() {
     return movies.elements();
   }
     public Enumeration getCustomers() {
     return customers.elements();
   }
      public void showAll() {
     System.out.println ("name: "+ this.getName());
     Enumeration kk = this.getCustomers();
     while (kk.hasMoreElements()) {
       Customer one = (Customer) kk.nextElement();


       System.out.println (one.show());
     }
     Enumeration ff = this.getMovies();
     while (ff.hasMoreElements()) {
       Movie one = (Movie) ff.nextElement();
       System.out.println (one.show());
     }
   } 
public void test()  {
         Customer k1 =  new Customer ("Jonah")   ; this.addCustomer (k1);
         Customer k2 =  new Customer ("Hellen") ; this.addCustomer (k2);
         Customer k3 =  new Customer ("Agnes")  ; this.addCustomer (k3) ;
         Movie f1 = new Movie ("StarWars"); this.addMovie (f1) ;
         Movie f2 = new Movie ("Shrek"); this.addMovie (f2) ;
         System.out.println("-**-**- test part 1 -**-**-") ;
         this.showAll();

         System.out.println("-**-**- test part 2 -**-**-") ;
         System.out.println("---" + k1.getName() + " rents " + f1.getName());
         this.showAll();
         k1.doRent(f1);

我的客户类别:

package eric;

public class Customer {

 String name;
    public Customer(String nameCus){
        name = nameCus;
    }

    public String getName(){
        return name;
    }

    public String show(){

        return name;

    }


    public void doRent(Movie f1) {

     System.out.println(" -"+ " RentData" + "[" + getName() +"," + f1.getName() + "]" );

        }

        }

MY MOVIE CLASS:

public class Movie {

    String name;
    int x = 0;



    public Movie(String nameMov){
        name = nameMov;
    }
    public String getName(){
        return name;
    }  


        public String show(){
            return name+"\n"+" - average: "+x +" days\n"+" - number of rentings: "+x ;


        }

}

我的问题是我无法找到一种方法以Jonah的名义修复-RentData [Jonah,StarWars] ......相反,它会在输出结束时发生..我需要一个帮助我来解释ganna是如何做到这一点的。谢谢

1 个答案:

答案 0 :(得分:0)

您在k1.doRent(f1)之前调用了this.showAll(),因此在打印名称之前,您将自然地打印出“RentData ...”行。你的代码现在的方式不利于你想要做的事情。每次在Customer对象上调用rentedMovies时,doRent(...)类都应该有一个名为Customer的成员列表。然后,Customer.show()应该打印客户的名称,然后打印来自rentedMovies的“RentData ...”内容。