Closed. This question needs details or clarity。它当前不接受答案。












想改善这个问题吗?添加详细信息并通过editing this post阐明问题。

6年前关闭。



Improve this question





我正在使用以下程序进行发行。

这是完整的代码,最近我也开始学习编程,这是我的第一个项目,因此我不太擅长缩进和OOP概念。如果您中的一位专家可以将此程序转换为OOP,将对我有很大的帮助。

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;


class Car {
    private String make;
    private String model;
    private String regNo;
    private int deposit;
    private int rate;

    public Car(String newMake, String newModel, String newRegNo,int newDeposit, int newRate)
    {
            make = newMake;
            model = newModel;
            regNo = newRegNo;
            deposit = newDeposit;
            rate = newRate;
    }



    public String getMake() {
            return make;
    }

    public String getModel() {
            return model;
    }

    public String getRegNo() {
            return regNo;
    }

    public int getDeposit() {
            return deposit;
    }

    public int getRate() {
            return rate;
    }
}

public class carrenta {

public static void main(String[] args) {
    DateFormat dateFormat = new SimpleDateFormat("dd/MM/yy  HH:mm");
    Date date = new Date();
    System.out.println(dateFormat.format(date));

List<Car> carlist = new ArrayList();
carlist.add(new Car("Toyota", "corolla", "TA7896", 1500, 1800));
carlist.add(new Car("Toyota", "vitz", "TV9872", 1500, 1800));
carlist.add(new Car("Nissan", "paso", "NP1543", 1500, 1500));
carlist.add(new Car("Honda", "city", "HC4692", 1800, 1800));
carlist.add(new Car("Honda", "civic", "HC4521", 2000, 1600));
carlist.add(new Car("Honda", " accord", "HA5463", 2500, 2000));
carlist.add(new Car("Mitsubishi", "lancer", "ML4521", 2000, 1500));


            Scanner input = new Scanner(System.in);
            boolean modelFound = false;
            while (!modelFound) {
            System.out.print("Enter model to rent: ");
            String model = input.nextLine();

            for(Car s : carlist){
        if (model.equalsIgnoreCase(s.getModel())) {
         modelFound = true;
        System.out.println("Model  " + model + " is available");
        System.out.print("Enter number of days: ");
        int days = input.nextInt();
                        System.out.println("***************Details*****************");
        int cost = (days * s.getRate()) + s.getDeposit();
        System.out.println("Deposit  DailyRate  Duration  TotalCost");
        System.out.println(s.getDeposit() + "       " + s.getRate()+ "            " + days + "        " + cost);
        System.out.print("Proceed to rent?( y/n ): ");

        String dec = input.next();
       switch (dec) {
            case "y":
               System.out.println("Enter Customer Name: ");
                String name = input.next();
                System.out.println("Enter NIC Number: ");
                int num = input.nextInt();
                System.out.println("************Receipt*************");
                System.out.println(     "     Date                               Name           NICNo         Car        RegNo    Duration   TCost");
          System.out.println(date+"           "+name + "          " + num +    "         " + model
                + "   " + s.getRegNo() + "      " + days + "       "+cost);
                break;
            case "n":
                System.out.println("Serving Next Customer: ");
                break;
                }
    }
        else{
                System.out.println("Please enter a valid model");
        }
        }
    }
}


}

最佳答案

您可以尝试运行while循环:

boolean modelFound = false;
while (!modelFound) {
   // Ask question, take input
   String model = input.next();
   for (Object object : carList) {
   /* etc ... */
   if (model.equalsIgnoreCase(car.getModel()) {
       modelFound = true;
       /* other code */
   }
}


还要假设您的CarList使用Car泛型:List<Car> carList,那么您不必每次都强制转换,只需将您的for-each循环作为for (Car c : carList)运行

10-06 07:04