如何为方法创建一个新类?
Posted
技术标签:
【中文标题】如何为方法创建一个新类?【英文标题】:How to create a new class for a method? 【发布时间】:2022-01-01 16:18:56 【问题描述】:package HargaSewaKamar;
import java.io.*;
class Main
public static void main (String [] args) throws IOException
String nama="",kamar="",jenis="";
int lama=0,tarif=0;
BufferedReader br = new BufferedReader(new InputStreamReader (System.in));
System.out.println("--**-- SISTEM INFORMASI PERHOTELAN --**--");
System.out.print("Nama Pelanggan \t\t\t: ");
nama = br.readLine();
System.out.println("\nPilih Jenis Kamar");
System.out.println("R. Reguler");
System.out.println("S. Suite");
System.out.println("P. President");
System.out.print("Jenis kamar yang dipesan (R/S/P) \t: ");
jenis = br.readLine();
System.out.print("\nLama Menginap \t\t\t: ");
lama = Integer.parseInt(br.readLine());
if (jenis.equals("R"))
tarif = lama*300000;
kamar="Reguler";
else if (jenis.equals("S"))
tarif = lama*500000;
kamar="Suite";
else if (jenis.equals("P"))
tarif = lama*750000;
kamar="President";
System.out.println("\n\n---***--- NOTA PEMBAYARAN HOTEL ---***---");
System.out.println("Nama Pelanggan \t\t\t: " + nama);
System.out.println("Jenis Kamar \t\t\t: " + kamar);
System.out.println("Tarif Penginapan \t\t: Rp " + tarif + ",-");
System.out.println("====================================================");
我正在为涉及酒店定价的案例研究构建一个简单的程序。 Main.java 应该是用户输入数据的地方,而 Hotel.java 是根据入住天数计算价格类别的地方。这是 Main.java,我希望将“if”方法放在 Hotel.java 中。我这样做了,但编译器一直给我 0。
这是Hotel.java
public class Hotel
public static String hitung(String jenis)
String kamar = "";
int tarif=0;
int lama=0;
if (jenis.equals("R"))
tarif = lama*300000;
kamar="Reguler";
else if (jenis.equals("S"))
tarif = lama*500000;
kamar="Suite";
else if (jenis.equals("P"))
tarif = lama*750000;
kamar="President";
return kamar;
【问题讨论】:
"我这样做了,但编译器一直给我 0。"请显示不起作用的代码 - 目前您还没有向我们显示 Hotel.java,并且您的代码没有引用它。请提供minimal reproducible example。 【参考方案1】:您需要将Hotel
类更改为其他内容。您实际上需要一个具有接受nama
、jenis
和lama
的构造函数的类。在此计算kamar
和tarif
如下(我称它为HotelReservation
,因为它似乎更合适):
public class HotelReservation
private final String nama;
private final String kamar;
private final int tarif;
public HotelReservation(String nama, String jenis, int lama)
this.nama = nama;
switch (jenis)
case "R":
tarif = lama * 300000;
kamar = "Reguler";
break;
case "S":
tarif = lama * 500000;
kamar = "Suite";
break;
case "P":
tarif = lama * 750000;
kamar = "President";
break;
default:
tarif = 0;
kamar = "Invalid";
break;
public String getNama()
return nama;
public String getKamar()
return kamar;
public int getTarif()
return tarif;
有了这个类,你现在可以在你的主方法中根据用户的数据创建一个新的酒店预订:
public class Main
public static void main (String [] args) throws IOException
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("--**-- SISTEM INFORMASI PERHOTELAN --**--");
System.out.print("Nama Pelanggan \t\t\t: ");
String nama = br.readLine();
System.out.println("\nPilih Jenis Kamar");
System.out.println("R. Reguler");
System.out.println("S. Suite");
System.out.println("P. President");
System.out.print("Jenis kamar yang dipesan (R/S/P) \t: ");
String jenis = br.readLine();
System.out.print("\nLama Menginap \t\t\t: ");
int lama = Integer.parseInt(br.readLine());
HotelReservation hotelReservation = new HotelReservation(nama, jenis, lama);
System.out.println("\n\n---***--- NOTA PEMBAYARAN HOTEL ---***---");
System.out.println("Nama Pelanggan \t\t\t: " + hotelReservation.getNama());
System.out.println("Jenis Kamar \t\t\t: " + hotelReservation.getKamar());
System.out.println("Tarif Penginapan \t\t: Rp " + hotelReservation.getTarif() + ",-");
System.out.println("====================================================");
【讨论】:
以上是关于如何为方法创建一个新类?的主要内容,如果未能解决你的问题,请参考以下文章