[英]List of 'unique' objects in Java
我正在和我的同学一起做作业。 我们正在制作一个出租车应用程序,您可以在其中启动和停止出租车,询问当前价格并在出租车停止时获得总价。 目前它工作正常。 但是,例如,如果我们启动出租车 1,然后启动出租车 2,就会出现问题。出租车 1 的开始和结束时间被新对象(出租车 2)的开始和结束时间覆盖——即使我们从和基于我在系统中输入的用户编号的arraylist。
Main.java 中的代码:
import java.text.DecimalFormat;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Taxi taxi = new Taxi();
System.out.println("Hej og velkommen til Damn Fast Taxis.");
boolean isEnd = false;
DecimalFormat decimalFormat = new DecimalFormat("#.0");
while(!isEnd) {
Taxi chosenTaxi;
System.out.println("1. Start en taxi.");
System.out.println("2. Stop en taxi.");
System.out.println("3. Pause en taxi.");
System.out.println("4. Spørg efter pris.");
System.out.println("5. Gratis tur.");
System.out.println("6. Tilføj antal taxier.");
Scanner sc = new Scanner(System.in);
String choice = sc.nextLine();
switch (choice) {
case "1":
if (taxi.getTaxiListPrint().size()>=1) {
Scanner startTaxiNumber = new Scanner(System.in);
int numberChoice = startTaxiNumber.nextInt();
chosenTaxi = taxi.chooseTaxi(numberChoice);
chosenTaxi.setStartTime();
break;
} else {
System.out.println("Ingen taxier er oprettet i systemet.");
break;
}
case "2":
if (taxi.getTaxiListPrint().size()>=1) {
Scanner endTaxiNumber = new Scanner(System.in);
int numberChoice = endTaxiNumber.nextInt();
chosenTaxi = taxi.chooseTaxi(numberChoice);
chosenTaxi.setEndTime();
if (!chosenTaxi.isStopped()) {
System.out.println("Turen varede " + decimalFormat.format(((chosenTaxi.getEndTime() - chosenTaxi.getStartTime()) / 100)*0.1) + " sekunder.");
Price price = new Price();
String finalPrice = price.calculatePrice(chosenTaxi.getStartTime(), chosenTaxi.getEndTime(), decimalFormat);
System.out.println("Pris: " + finalPrice + " dollars.");
chosenTaxi.setStopped(true);
} else {
System.out.println("Denne taxi er allerede blevet stoppet.");
}
break;
} else {
System.out.println("Ingen taxier er oprettet i systemet.");
}
case "3":
break;
case "4":
if (taxi.getTaxiList().size()>=1) {
Scanner currentPriceTaxiNumber = new Scanner(System.in);
int numberChoice = currentPriceTaxiNumber.nextInt();
Taxi currentChosenTaxi = taxi.chooseTaxi(numberChoice);
currentChosenTaxi.setEndTime();
if (!currentChosenTaxi.isStopped()) {
Price priceNow = new Price();
String currentPrice = priceNow.calculatePrice(currentChosenTaxi.getStartTime(), currentChosenTaxi.getEndTime(), decimalFormat);
System.out.println("Pris: " + currentPrice + " dollars.");
} else {
System.out.println("Denne taxi er allerede blevet stoppet.");
}
break;
} else {
System.out.println("Ingen taxier er oprettet i systemet.");
break;
}
case "5":
break;
case "6":
System.out.println("Hvor mange taxier vil du tilføje?");
Scanner taxaNumber = new Scanner(System.in);
int number = taxaNumber.nextInt();
for (int i = 0; i<number;i++) {
taxi.addTaxi(taxi);
}
System.out.println(number + " " + "Taxa'er tilføjet!");
break;
default:
isEnd = true;
break;
出租车等级:
import java.util.ArrayList;
import java.util.List;
public class Taxi {
private long startTime;
private long endTime;
private boolean isStopped = false;
private List<Taxi> taxiList = new ArrayList<>();
public void addTaxi(Taxi taxi) {
taxiList.add(taxi);
}
public Taxi chooseTaxi(int choice) {
return taxiList.get(choice - 1);
}
public List<Taxi> getTaxiListPrint() {
for(int i = 1; i<taxiList.size() + 1;i++) {
System.out.println("Taxi: " + i);
}
return taxiList;
}
public List<Taxi> getTaxiList() {
return taxiList;
}
public long getStartTime() {
return startTime;
}
public long getEndTime() {
return endTime;
}
public boolean isStopped() {
return isStopped;
}
public void setStartTime() {
this.startTime = System.currentTimeMillis();
}
public void setEndTime() {
this.endTime = System.currentTimeMillis();
}
public void setStopped(boolean stopped) {
isStopped = stopped;
}
}
如果我的代码很乱,我很抱歉,我对这门语言很陌生。 简短的问题是:如何定义不同的对象,以便程序在每次创建出租车的新实例时都不会覆盖?
非常感谢。 /缺口
我认为最简单的更改是将 Taxi 类中的这些方法更改为静态:
private static List<Taxi> taxiList = new ArrayList<>();
public static void addTaxi(Taxi taxi) {
taxiList.add(taxi);
}
public static Taxi chooseTaxi(int choice) {
return taxiList.get(choice - 1);
}
public static List<Taxi> getTaxiListPrint() {
for (int i = 1; i < taxiList.size() + 1; i++) {
System.out.println("Taxi: " + i);
}
return taxiList;
}
public static List<Taxi> getTaxiList() {
return taxiList;
}
将这些方法更改为静态形式,例如:
Taxi currentChosenTaxi = taxi.chooseTaxi(numberChoice);
改成
Taxi currentChosenTaxi = Taxi.chooseTaxi(numberChoice);
然后向管理器添加不同的出租车:
case "6":
System.out.println("Hvor mange taxier vil du tilføje?");
Scanner taxaNumber = new Scanner(System.in);
int number = taxaNumber.nextInt();
for (int i = 0; i < number; i++) {
Taxi.addTaxi(new Taxi());
}
System.out.println(number + " " + "Taxa'er tilføjet!");
break;
注意:您不需要每次都创建
new Scanner(System.in)
,如果您将其放在循环之外,则可以使用一个。
Taxi
属性正如评论所说,有taxiList
在Taxi
类是不是一个好主意。 您的Taxi
类应该只有构成Taxi的必要属性。 更好的结构看起来像这样
public class Taxi {
private long startTime;
private long endTime;
private boolean isStopped = false;
// Add the necessary getters & setters for the above attributes here
}
List
或TaxiManager
类管理您的出租车要保存您的taxiList
您有 2 个选择
Main
类中,定义一个List<Taxi> taxiList = new ArrayList<>()
TaxiManager
如果在出租车上有少量可能的操作,第一个选项是一个很好的方法。 如果您想从Main
类中抽象出租车管理逻辑,则第二个选项更好。 它看起来像这样
public class TaxiManager {
private List<Taxi> taxiList;
public class TaxiManager() { taxiList = new ArrayList<>(); }
// Here are some "management" methods you can use
// DON'T FORGET TO HANDLE EXCEPTIONS (for example ArrayOutOfBounds, ...)
public void addTaxi(Taxi newTaxi) { taxiList.add(newTaxi); }
public Taxi getTaxiAtIndex(int index) { return taxiList.get(index); }
public void stopTaxiAtIndex(int index) { taxiList.get(index).stop(); }
// Add the necessary operations here
}
Main
类中使用它创建一个新的TaxiManager
并根据您选择的逻辑调用您的方法( switch case
)
public class Main {
public static void main(String[] args) {
TaxiManager taxiManager = new TaxiManager();
Scanner sc = new Scanner(System.in);
String choice = sc.nextLine();
switch (choice) {
case "add": {
taxiManager.addTaxi(new Taxi());
break;
}
// Include other options
}
}
}
确保一个类只有一个实例,并提供一个全局访问点
如果您想从不同的“类”调用它并仍然保留该manager的唯一性,这对于您的TaxiManager
可能是一个不错的选择。 然后您可以放心,您的出租车不会重复。 如果你想走这条路,你的TaxiManager
看起来像这样
public class TaxiManager {
private List<Taxi> taxiList;
private TaxiManager() {}
private static class SingletonHolder {
private static final TaxiManager INSTANCE = new TaxiManager();
}
public static TaxiManager getInstance() {
return SingletonHolder.INSTANCE;
}
// Add other methods here
}
并从你的Main
类调用它,使用这个
TaxiManager taxiManager = TaxiManager.getInstance();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.