2016-12-23 2 views
-3

내 프로그램이 실행되지만 논리적 인 실수가있어서 찾을 수 없습니다. 나는 이름과 모든 물건을 입력하지만 여행을 인쇄하도록 선택할 때 출력을 단지 전화 번호와 여기프로그램이 아무것도 인쇄하지 않습니다 (객체 및 클래스)

import java.util.*; 
class Trip{ 

    private static String customerName; 
    private static String mobileNo; 
    private static String reservationCode; 
    public static int totalReservations=0; 
    public static Scanner input=new Scanner (System.in); 



    public Trip(){//The constructer 
     customerName = " "; 
     mobileNo = " "; 

    } 

    public Trip(String Name, String MobileNo) { 
        customerName=Name; 
        mobileNo=MobileNo; 

      totalReservations++; //increase the total reservations 
    } 

    public void setcustomerName(String Name){ 

     customerName = Name; 
    } 

    public void setmobileNo(String MobileNo){ 

     mobileNo = MobileNo; 
    } 


//Getters 

    public String getcustomerName() { 
     return customerName; 
    } 

    public String getmobileNo() { 
     return mobileNo; 
    } 

    public String getreservationCode() { 
     return reservationCode; 
    } 


    public static void generateReservationCode(){ 

    } 

    public static void printTripInfo(){ 
    System.out.println("Customer Name:"+customerName); 
    System.out.println("Phone Number:"+mobileNo); 



    } 
}//end class trip 


    public class CabCompany{ 
    private static int reservations_size=20; 
    private static Trip[] trip; 
    Trip newTrip; 
    public int totalNumber=0; 
    public static int user; 
    public static Scanner input=new Scanner (System.in); 

    public static void main(String[]args){//MAIN 
     trip=new Trip[reservations_size]; 

     do{ 

     System.out.println("1. Add a new trip\n2. Find a trip by reservation code\n3. List all trips\n"); 
     user=input.nextInt(); 

     if (user == 1) { 

      System.out.println("Name:"); 
      String Name = input.nextLine(); 
      input.nextLine(); 

      System.out.println("MobileNo:"); 
      String MobileNo = input.next(); 



      CabCompany Y = new CabCompany(); 
      boolean flag; 
      flag = Y.addTrip(Name,MobileNo); 
      if(flag == true) { 
       System.out.println("the trip has been added succssfully"); 

      } else if (flag == false){ 
       System.out.println("hasn't been added"); 
      } 
     } 

     if(user == 2) { 
      printAll(); 

     } 

     if(user == 3){ 
     System.out.println("The total Reservations is:"+Trip.totalReservations); 
     } 

     } while (user != 7);//BREAK 
     } 


    public boolean addTrip(String customerName,String mobileNo){ 

      boolean flag = true; 

     if ((mobileNo.length() == 10)) {  
     newTrip=new Trip(customerName, mobileNo); 
     int i = Trip.totalReservations - 1; 
     newTrip.generateReservationCode(); 
     trip[i] = newTrip; 

      flag = true; 




} else { 
      flag = false; 

     } 

     return flag; 
    }  


    public static void printAll() { 

     for(int t=0; t<Trip.totalReservations; t++){ 
      trip[t].printTripInfo();  

     } 
    } 

} 
+1

힌트 : 우리가 ** 당신을 돕기 위해 우리 시간을 보내길 원합니다 **. 따라서 모든 코드를 적절하게 포맷/인 들기위한 시간을 보내십시오. 코드 품질에 대한 참고 사항 : 모든 종류의 블록을 중심으로 {중괄호}를 사용하십시오. 단 한 줄이라도 if/then if/else 일을 사용하는 것이 좋습니다. 그리고 iterating하고 elses에서 휴식을 ... 좀 추한. "깨끗한 코드"에 대해 조금 읽으 려합니다. 귀하의 코드는 그로부터 이익을 얻을 수 있습니다 ... – GhostCat

+0

좋은 조언, 나는 당신을 감사 할 것입니다 – Dipie

+0

그건 프로그래밍 문제에 관한 질문이 아니에요, 프로그래밍 문제 ("내가 뭘하는지 모르겠어, 어떻게 자바 프로그램에? "). 제발, 자바가 작동하는 방법을 배우십시오, 자바에서 프로그래밍에 대한 책이나 튜토리얼을 읽어서 이것을하십시오. SO에서 프로그램하는 방법을 배우려고 시간을 낭비하지 마십시오. – zubergu

답변

0

좋아, 몇 가지 빈 이름 인쇄를 유지하기 때문에 나는 그들이이 저장되지 것 같아요. 귀하의 질문에 대답하기 위해, 난 당신이 첫 번째 줄에, 당신은 당신이 인쇄하는 것이, 새로운 빈 여행 (RR)를 만들고, 여기에 방법

public static void printTripInfo(){ 
    Trip rr=new Trip(); 
    CabCompany K=new CabCompany(); 
    for(int y=0;y<2;y++) 
    System.out.println("Customer Name:"+rr.getcustomerName()); 
    System.out.println("Phone Number:"+rr.getmobileNo()); 
} 

에 대한 이야기입니다 같아요. 이 메서드는 다음과 같아야합니다.

public static void printTripInfo(){ 
     System.out.println("Customer Name:"+getcustomerName()); 
     System.out.println("Phone Number:"+getmobileNo()); 
    } 

또한이 메서드에는 toString()이라는 메서드가 있습니다. 기타 고려 사항 : 변수 이름은 대문자로 시작하지 않아야하고 (TYY, Y는, ..., 좋은 이름은 아니다) 의미가해야하고, 첫 번째 constructer는 다음과 같이 제거하고 교체 할 수 있습니다

class Trip{ 
private String customerName=""; 
private String mobileNo=""; 
private String day=""; 
private String date=""; 
private String time=""; 
... 
... 

을하지만 당신의 코드에서 몇 가지 다른 문제가, 당신이 자바 dev에 대해 좀 더 읽거나 튜토리얼을 따라 권 해드립니다 것이다 printTripInfo() 메소드에서

+0

예, 남자 야. 나는 빨려 죽겠다.하지만 난 단지 초보자에 불과하다.이 프로젝트는 대학에서 프로젝트이기 때문에 나는 생성자를 제거 할 수 없다. 당신은 말하지만 여전히, 나는 여기에 새로운 코드를 붙여 넣을 것이고, 이것에 대해 나를 도와 주신 것에 대하여 정말 고마워요 !!!! – Dipie

+0

당신은 무엇을 의미합니까 : "이것은 대학의 프로젝트이기 때문에 생성자를 제거 할 수 없습니다"? –

+0

글쎄, 그들은 우리에게 지시 사항이 담긴 종이를 주었고, 2 개의 생성자, 하나는 값을 초기화하는 생성자, 또 하나는 사용자로부터 값을 가져 오는 생성자를 만들어야한다고 말했습니다. – Dipie

0

), 당신은 새로운 여행의 목적, 것를 만드는 항상 비어 있어야합니다. -

public static void printTripInfo(){ 
//Creating a new Trip object, which is empty  
Trip rr=new Trip(); 

//Printing the values from above object, which will be blank 
    System.out.println("Customer Name:"+rr.getcustomerName()); 
    System.out.println("Phone Number:"+rr.getmobileNo()); 

} 
+0

나는 그랬지만 여전히 같은 문제가있다. 제발 내가 여기에 새 코드를 게시하도록 도와주세요. – Dipie

관련 문제