Java阵列无法正常工作

时间:2014-02-26 18:26:58

标签: java arrays

我的设置类:

import static java.lang.System.*;

import java.util.Scanner;

import java.io.*;

public class Setup
{

private String[] roomtype, custAddress, custName;
private int[] cPhone;
private double[] roomPrice;
private int[] roomNumber;
Scanner kb = new Scanner(in);

    public Setup()
{

roomtype = new String[6];

custName = new String[6];

custAddress = new String[6];

roomPrice = new double[6];

cPhone = new int[6];

roomNumber = new int[6];

}

public void unoccupied()
{
    String answer;
    for (int c = 1; c<6; c++)
    {
        if(custName[c] == null)
        {
            out.println("Room" + roomtype[c] + " is not occupied.");
            out.print("Would you like to assign a customer to this room?");
                answer = kb.nextLine();
                if (answer.contains("y"))
                {
                    out.print("Which customer would you like to put in this room?");
                    answer = kb.nextLine();
                    roomtype[c] = answer;
                }
        }
    }
}
public void addName(String[] custName)
{
            for (int c = 1; c<6; c++)
        {
        if(custName[c] == null)
            {

            out.print("Add a name to customer " + c + ": ");
            custName[c] = kb.nextLine();

            }
        }
}
public void addcPhone(int[] cPhone)
{
    for (int p = 1; p<6; p++)
    {
        if(cPhone[p] == 0)
        {
        out.print("Add a cell phone number to customer " + p + ": ");
        cPhone[p] = kb.nextInt();
        }
    }

}
public void addAddress(String[] custAddress)
{
    for (int a = 1; a<6; a++)
        if(custAddress[a] == null)
        {
            if(custName[a] == null)
            {
            out.print("Add an address to customer " + a + ": ");
            custAddress[a] = kb.nextLine();
            }
            else
            out.print("Add an address to " + custName + ": ");
            custAddress[a] = kb.nextLine();
        }

}
public String toString()
{
    String receipt = "";
    receipt += "Customer Name: " + custName ;
    receipt += "Address: " + custAddress ;
    receipt += "Phone number: " + cPhone ;
    receipt += "Thanks for making your room reservation for Geek Speak with the Orozco Hotel!" ;
    receipt += "We have you booked in room number " + roomNumber + ", which is a " + roomtype + "." ;
    receipt += "Your charges for the convention will be $" + roomPrice + "." ;
    receipt += "We hope you enjoy your stay with us and the convention.";
    receipt += "The Orozco Hotel Staff";
    return receipt;
}
}

我的司机班:

import java.util.Scanner;
import static java.lang.System.*;

public class Driver
{

public static void main(String[] args)
{


Scanner kb = new Scanner(in);
Setup[] customer = new Setup[5];
for(int i = 0; i<6; i++)
    customer[i] = new Setup(custName, cPhone, custAddress);

Setup[] room = new Setup[5];
for(int i = 0; i<6; i++)
    room[i] = new Setup(roomtype, roomPrice, roomNumber);
room[1].unoccupied();

}
}

我尝试使用custName,custAddress和cPhone作为参数创建5个客户对象,并使用roomPrice,roomtype和roomNumber作为参数创建5个房间对象。我尝试使用数组创建对象,但我不知道我在做什么,因为我的老师今年没有帮助我。我的驱动程序类不断返回客户和房间对象中的参数错误“找不到符号”。任何有关修复此代码以帮助对象保存参数的帮助都表示赞赏。

1 个答案:

答案 0 :(得分:0)

我认为你需要重新考虑你的抽象。这些是使用更多面向对象方法的快速改造。您可以通过将房间与客户分开来扩展这一想法。您应该将酒店对象设为客房和客户的集合。

import java.util.LinkedList;

public class Hotel{
LinkedList<Customer> customers;

public void addCustomer(Customer c){
    customers.add(c);
}
public void removeCustomer(int roomNumber){
    boolean flag = true;
    for(int i = 0; i < customers.size() && flag; ++i){
        if(customers.get(i).getRoomNumber() == roomNumber)
            customers.remove(i);
            flag = false;
    }
}

public String toString(){
    String s = "";
    for(Customer c : customers)
        s += c.toString() + "\n";
    return s;
}
}

class Customer {

private String roomType;
private String address;
private String name;
private String phone;
private String price;
private int roomNumber;

public Customer(String rmTp, String addrss, String nm, String phn, String prc, int rmNm){
    roomType   = rmTp;
    address    = addrss;
    name       = nm;
    phone      = phn;
    price      = prc;
    roomNumber = rmNm;
}

public int getRoomNumber(){ return roomNumber; }
public String toString(){
    String receipt = "";
    receipt += "Customer Name: " + name ;
    receipt += "Address: " + address ;
    receipt += "Phone number: " + phone ;
    receipt += "Thanks for making your room reservation for Geek Speak with the Orozco Hotel!" ;
    receipt += "We have you booked in room number " + roomNumber + ", which is a " + roomType + "." ;
    receipt += "Your charges for the convention will be $" + price + "." ;
    receipt += "We hope you enjoy your stay with us and the convention.";
    receipt += "The Orozco Hotel Staff";
    return receipt;
}

}