By GokiSoft.com| 15:33 22/04/2020|
Java Basic

Share Code - Quản lý bể bơi bằng Java - Swimming Pool in Java

Hướng dẫn chữa bài tập

Quản lý bể bơi bằng Java - Swimming Pool in Java


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package lession15;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Diep.Tran
 */
public class Ticket {
    static List<Character> cList = new ArrayList<>();
    static List<Integer> indexList = new ArrayList<>();
    
    String code;
    Date registeredDate;

    public Ticket() {
    }

    public String getCode() {
        return code;
    }

    public Date getRegisteredDate() {
        return registeredDate;
    }

    public void setRegisteredDate(Date registeredDate) {
        this.registeredDate = registeredDate;
    }
    
    public void autoGenerateCode() {
        //thuc hien add tat ca cac ky a-z, A-Z, 0-9 theo yeu cau de bai
        autoGenerateCharacter();
        //code chuc nang sinh code tu dong
        //code => do dai 6 ky tu >> a1a2a3a4a5a6 (a => a-z, A-Z, 0-9)
        //dung giai thuat >> a(1-6) -> index 0 -> cList.size() - 1 -> gia tri tai vi tri index nay
        //0 >> aaaaaa
        //1 >> aaaaab
        //2 >> aaaaac
        //. >> aaaaa9 (0, 0, 0, 0, 0, cList.size() - 1
        //. >> aaaaba (0, 0, 0, 0, 1, 0)
        //. >> aaaabb (0, 0, 0, 0, 1, 1)
        //. >> aaaab9 (0, 0, 0, 0, 1, cList.size() - 1)
        //. >> aaaac0 (0, 0, 0, 0, 2, 0)
        code = "";
        for (int i = 0; i < indexList.size(); i++) {
            int currentIndex = indexList.get(i);
            code += String.valueOf(cList.get(currentIndex));
        }
        int lastIndex = indexList.size() - 1;
        indexList.set(lastIndex, indexList.get(lastIndex) + 1);
        for (int i = indexList.size() - 1; i > 0; i--) {
            if(indexList.get(i) < cList.size()) {
                break;
            }
            indexList.set(i, 0);
            indexList.set(i - 1, indexList.get(i - 1) + 1);
        }
    }
    
    private static void autoGenerateCharacter() {
        if(cList.size() > 0) return;
        //auto generate code
        //Khoi tao duoc mang du lieu trc da.
        for (int i = (int)'a'; i <= (int)'z'; i++) {
            cList.add((char) i);
        }
        for (int i = (int)'A'; i <= (int)'Z'; i++) {
            cList.add((char) i);
        }
        for (int i = (int)'0'; i <= (int)'9'; i++) {
            cList.add((char) i);
        }
        
        //save index tai thoi diem tiep theo se duoc gender
        for (int i = 0; i < 6; i++) {
            indexList.add(0);
        }
    }
    
    public Date convertStringToDate(String str) {
        try {
            SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
            
            return format.parse(str);
        } catch (ParseException ex) {
            Logger.getLogger(Ticket.class.getName()).log(Level.SEVERE, null, ex);
        }
        return new Date();
    }
    
    public void input() {
        Scanner scan = new Scanner(System.in);
        //gen tu dong code
        autoGenerateCode();
        
        System.out.println("Enter registered date (dd/MM/yyyy): ");
        String registeredDateStr = scan.nextLine();
        //convert String date to Date
        registeredDate = convertStringToDate(registeredDateStr);
    }
}



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package lession15;

import java.util.Date;
import java.util.Scanner;

/**
 *
 * @author Diep.Tran
 */
public class MonthTicket extends Ticket{
    String customerName, address, avatar;
    Date expiredDate;
    int price;

    public MonthTicket() {
    }

    public String getCustomerName() {
        return customerName;
    }

    public void setCustomerName(String customerName) {
        this.customerName = customerName;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getAvatar() {
        return avatar;
    }

    public void setAvatar(String avatar) {
        this.avatar = avatar;
    }

    public Date getExpiredDate() {
        return expiredDate;
    }

    public void setExpiredDate(Date expiredDate) {
        this.expiredDate = expiredDate;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }
    
    @Override
    public void input() {
        super.input();
        
        Scanner scan = new Scanner(System.in);
        
        System.out.println("Enter customer name: ");
        customerName = scan.nextLine();
        
        System.out.println("Enter address: ");
        address = scan.nextLine();
        
        System.out.println("Enter avatar: ");
        avatar = scan.nextLine();
        
        System.out.println("Enter price: ");
        price = Integer.parseInt(scan.nextLine());
        
        System.out.println("Enter expired date (dd/MM/yyyy): ");
        String expiredDateStr = scan.nextLine();
        expiredDate = convertStringToDate(expiredDateStr);
    }
}



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package lession15;

import java.util.Scanner;

/**
 *
 * @author Diep.Tran
 */
public class DailyTicket extends Ticket{
    String durationCode;

    public DailyTicket() {
    }

    public String getDurationCode() {
        return durationCode;
    }

    public void setDurationCode(String durationCode) {
        this.durationCode = durationCode;
    }

    @Override
    public void input() {
        super.input(); //To change body of generated methods, choose Tools | Templates.
        Scanner scan = new Scanner(System.in);
        
        //Show all duration time => to choose from them.
        System.out.println("Enter duration code: ");
        durationCode = scan.nextLine();
    }
}



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package lession15;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 *
 * @author Diep.Tran
 */
public class DurationTime {
    static List<Character> cList = new ArrayList<>();
    static List<Integer> indexList = new ArrayList<>();
    
    String durationCode;
    int fromTime, toTime;
    int price;

    public DurationTime() {
    }

    public String getDurationCode() {
        return durationCode;
    }

    public void setDurationCode(String durationCode) {
        this.durationCode = durationCode;
    }

    public int getFromTime() {
        return fromTime;
    }

    public void setFromTime(int fromTime) {
        this.fromTime = fromTime;
    }

    public int getToTime() {
        return toTime;
    }

    public void setToTime(int toTime) {
        this.toTime = toTime;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }
    
    public void input() {
        Scanner scan = new Scanner(System.in);
        //gen tu dong code
        autoGenerateCode();
        
        System.out.println("Enter start time: ");
        fromTime = Integer.parseInt(scan.nextLine());
        
        System.out.println("Enter end time: ");
        toTime = Integer.parseInt(scan.nextLine());
        
        System.out.println("Enter price: ");
        price = Integer.parseInt(scan.nextLine());
    }
    
    public void autoGenerateCode() {
        //thuc hien add tat ca cac ky a0-9 theo yeu cau de bai
        autoGenerateCharacter();
        //code chuc nang sinh code tu dong
        //code => do dai 6 ky tu >> a1a2 (a => 0-9)
        //dung giai thuat >> a(1-2) -> index 0 -> cList.size() - 1 -> gia tri tai vi tri index nay
        durationCode = "";
        for (int i = 0; i < indexList.size(); i++) {
            int currentIndex = indexList.get(i);
            durationCode += String.valueOf(cList.get(currentIndex));
        }
        int lastIndex = indexList.size() - 1;
        indexList.set(lastIndex, indexList.get(lastIndex) + 1);
        for (int i = indexList.size() - 1; i > 0; i--) {
            if(indexList.get(i) < cList.size()) {
                break;
            }
            indexList.set(i, 0);
            indexList.set(i - 1, indexList.get(i - 1) + 1);
        }
    }
    
    private static void autoGenerateCharacter() {
        if(cList.size() > 0) return;
        //auto generate code
        //Khoi tao duoc mang du lieu trc da.
        for (int i = (int)'0'; i <= (int)'9'; i++) {
            cList.add((char) i);
        }
        
        //save index tai thoi diem tiep theo se duoc gender
        for (int i = 0; i < 2; i++) {
            indexList.add(0);
        }
    }
}



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package lession15;

import java.util.Date;
import java.util.Scanner;

/**
 *
 * @author Diep.Tran
 */
public class Booking {
    String ticketCode, durationCode;
    Date checkinTime;

    public Booking() {
    }

    public String getTicketCode() {
        return ticketCode;
    }

    public void setTicketCode(String ticketCode) {
        this.ticketCode = ticketCode;
    }

    public String getDurationCode() {
        return durationCode;
    }

    public void setDurationCode(String durationCode) {
        this.durationCode = durationCode;
    }

    public Date getCheckinTime() {
        return checkinTime;
    }

    public void setCheckinTime(Date checkinTime) {
        this.checkinTime = checkinTime;
    }
    
    public void input() {
        Scanner scan = new Scanner(System.in);
        
        System.out.println("Ticket");
        System.out.println("1. Monthly ticket");
        System.out.println("2. Daily ticket");
        System.out.println("Choose: ");
        
        int choose = Integer.parseInt(scan.nextLine());
        
        switch(choose) {
            case 1:
                System.out.println("Enter ticket code: ");
                ticketCode = scan.nextLine();
                break;
            default:
                DailyTicket dailyTicket = new DailyTicket();
                dailyTicket.input();
                //save daily ticket in Data Manager
                
                //get ticket code
                ticketCode = dailyTicket.getCode();
                break;
        }
        
        //Display all duration times => Choose from them.
        System.out.println("Enter duration code: ");
        durationCode = scan.nextLine();
        
        checkinTime = new Date();
    }
}



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package lession15;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 *
 * @author Diep.Tran
 */
public class SwimmingPool {
    List<MonthTicket> monthTickets = new ArrayList<>();
    List<DailyTicket> dailyTickets = new ArrayList<>();
    List<DurationTime> durationTimes = new ArrayList<>();
    List<Booking> bookings = new ArrayList<>();
    
    String code, address;

    public SwimmingPool() {
    }

    public List<MonthTicket> getMonthTickets() {
        return monthTickets;
    }

    public void setMonthTickets(List<MonthTicket> monthTickets) {
        this.monthTickets = monthTickets;
    }

    public List<DailyTicket> getDailyTickets() {
        return dailyTickets;
    }

    public void setDailyTickets(List<DailyTicket> dailyTickets) {
        this.dailyTickets = dailyTickets;
    }

    public List<DurationTime> getDurationTimes() {
        return durationTimes;
    }

    public void setDurationTimes(List<DurationTime> durationTimes) {
        this.durationTimes = durationTimes;
    }

    public List<Booking> getBookings() {
        return bookings;
    }

    public void setBookings(List<Booking> bookings) {
        this.bookings = bookings;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
    
    public void input() {
        Scanner scan = new Scanner(System.in);
        
        System.out.println("Enter code: ");
        code = scan.nextLine();
        
        System.out.println("Enter address: ");
        address = scan.nextLine();
    }
}



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package lession15;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Diep.Tran
 */
public class DataMgr {
    private static DataMgr instance = null;
    
    List<SwimmingPool> swimmingPools = new ArrayList<>();
    
    private DataMgr() {
        
    }
    
    public synchronized static DataMgr getInstance() {
        if(instance == null) {
            instance = new DataMgr();
        }
        return instance;
    }

    public List<SwimmingPool> getSwimmingPools() {
        return swimmingPools;
    }

    public void setSwimmingPools(List<SwimmingPool> swimmingPools) {
        this.swimmingPools = swimmingPools;
    }
}



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package lession15;

import java.util.Scanner;

/**
 *
 * @author Diep.Tran
 */
public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int choose;
        
        do {
            showMenu();
            choose = Integer.parseInt(scan.nextLine());
            
            switch(choose) {
                case 1:
                    
                    break;
                case 2:
                    
                    break;
                case 3:
                    
                    break;
                case 4:
                    
                    break;
                case 5:
                    
                    break;
                case 6:
                    System.out.println("Exit!!!");
                    break;
                default:
                    System.out.println("Input failed!");
                    break;
            }
        } while(choose != 6);
    }
    
    static void showMenu() {
        System.out.println("1. Add swimming pool");
        System.out.println("2. Add monthly ticket");
        System.out.println("3. Booking");
        System.out.println("4. Statistic profit of day by daily ticket");
        System.out.println("5. Statistic profit of month");
        System.out.println("6. Exit");
        System.out.println("Choose: ");
    }
}


Tags:

Phản hồi từ học viên

5

(Dựa trên đánh giá ngày hôm nay)