2016-04-05 58 views
1

我應該拿出這個輸出。需要各種方法來提出這個輸出

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類:

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 ; 


     } 

} 

我的問題是,我無法找到一個方法來解決-RentData [Jonah,StarWars]名約拿下......相反,它是在輸出結束。我需要有人幫我指點迷津感謝

+2

你有很多糟糕的格式正在發生,也許試着清理一下然後轉發?我嘗試了一些編輯來使其更清晰。 –

回答

0

你打電話k1.doRent(f1)之前this.showAll()所以自然你會得到打印名稱前打印的「RentData ...」行。你的代碼現在的方式不利於你想要做什麼。您的Customer課程應該有一個名爲rentedMovies的成員列表,每次您在Customer對象上調用doRent(...)時都會填充此列表。然後,Customer.show()應打印客戶的名稱,然後打印來自rentedMovies的「RentData ...」內容。