Untitled
unknown
plain_text
4 years ago
2.4 kB
11
Indexable
Tính Tiền Phòng/Kh.java
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
public class Kh implements Comparable<Kh> {
private String id,name,room;
private int stay;
private Integer money;
public Kh(int i, String name, String room, String dayIn, String dayOut, String sub) throws ParseException{
this.id = String.format("KH%02d", i);
this.room = room;
int dongia;
String tmp = room.substring(0,1);
if(tmp.equals("1")) dongia = 25;
else if(tmp.equals("2")) dongia = 34;
else if(tmp.equals("3")) dongia = 50;
else dongia = 80;
this.name = chuanhoa(name);
this.stay = setStay(dayIn, dayOut) +1;
this.money = stay*dongia + Integer.parseInt(sub);
}
private Integer setStay(String dayIn, String dayOut) throws ParseException{
int res;
Date den = new SimpleDateFormat("dd/MM/yyyy").parse(dayIn);
Date di = new SimpleDateFormat("dd/MM/yyyy").parse(dayOut);
res = (int) ((di.getTime() - den.getTime())/(1000*3600*24));
return res;
}
private String chuanhoa(String a){
String res="";
String[] tmp = a.trim().toLowerCase().split("\\s+");
for(String i : tmp){
String tmp3 = i.toUpperCase();
char x = tmp3.charAt(0);
String tmp2 = "";
tmp2 =tmp2 + x ;
for(int j = 1; j < i.length(); j++) tmp2= tmp2 + i.charAt(j) ;
res = res + tmp2 + " ";
}
return res;
}
public int compareTo(Kh a){
return -this.money.compareTo(a.money);
}
public String toString(){
return id + " " + name + room + stay + " " + money;
}
}
Tính Tiền Phòng/Main.java
import java.io.*;
import java.text.ParseException;
import java.util.*;
public class Main{
public static void main(String[] args) throws IOException,FileNotFoundException,ClassNotFoundException,ParseException {
Scanner sc = new Scanner(new File("KHACHHANG.in"));
int t = Integer.parseInt(sc.nextLine());
List<Kh> res = new ArrayList<>();
for(int i = 1; i <= t; i ++){
res.add( new Kh(i, sc.nextLine(), sc.nextLine(), sc.nextLine(), sc.nextLine(), sc.nextLine()));
}
res.sort(null);
for(Kh a : res){
System.out.println(a);
}
}
}
Editor is loading...