1.编写“借书卡”类及其测试类。

6.1 “借书卡”类

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。

²   属性:账号、持卡人姓名、身份证号码、地址、已借书数、可借书数、本次借书数、本次还书数

²   方法一:借书,显示本次借书数和可借书数

²   方法二:还书,显示本次还书数和可借书数

6.2 测试类

²   本次借书数 和 本次还书数,从键盘输入

2.编写“电费管理类”及其测试类。

7.1 “电费管理”类

²   属性:上月电表读数、本月电表读数

²   方法一:显示上月、本月电表读数

²   方法二:计算本月用电数

²   方法三:显示本月用电数

²   方法四:假设每度电的价格为1.2元,计算并显示本月电费

7.2 测试类

²   上月电表读数、本月电表读数,从键盘输入

 1 package com.wsy.work.input;
 2 import java.util.Scanner;
 3 public class LibraryCardInput {
 4     Scanner in = new Scanner(System.in);
 5     private String accountNumber;
 6     private String name;
 7     private String IDCard;
 8     private String address;
 9     public void input()
10     {
11         System.out.println("请依次输入您的帐号、名字、身份证号、地址");
12         accountNumber = in.nextLine();
13         name = in.nextLine();
14         IDCard = in.nextLine();
15         address = in.nextLine();
16     }
17     
18     public String getAccountNumber() {
19         return accountNumber;
20     }
21     public void setAccountNumber(String accountNumber) {
22         this.accountNumber = accountNumber;
23     }
24     public String getName() {
25         return name;
26     }
27     public void setName(String name) {
28         this.name = name;
29     }
30     public String getIDCard() {
31         return IDCard;
32     }
33     public void setIDCard(String iDCard) {
34         IDCard = iDCard;
35     }
36     public String getAddress() {
37         return address;
38     }
39     public void setAddress(String address) {
40         this.address = address;
41     }
42     
43 }
 1 package com.wsy.work.type;
 2 public class LibraryCard {
 3     private static int  bookTotal = 100;
 4     private String accountNumber;
 5     private String name;
 6     private String IDCard;
 7     private String address;
 8     private int borrowedNumber = 0;
 9     private int borrowableNumber = bookTotal;
10     public LibraryCard(String accountNumber, String name, String IDCard, String address) {
11         this.accountNumber = accountNumber;
12         this.name = name;
13         this.IDCard = IDCard;
14         this.address = address;
15     }
16     public void prompt()
17     {
18         System.out.println("欢迎来到图书馆");
19         System.out.println("请输入1-4");
20         System.out.println("1:查看书店总书数");
21         System.out.println("2:借书");
22         System.out.println("3:还书");
23         System.out.println("4.退出");
24     }
25     public void borrowBook(int thisBorrowNumber)
26     {
27         if(thisBorrowNumber > borrowableNumber)
28         {
29             System.out.println("已超过最大可借书数");
30             return;
31         }
32         System.out.println("本次需借书:"+thisBorrowNumber+"本");
33         borrowableNumber = borrowableNumber - thisBorrowNumber ; 
34         borrowedNumber = borrowedNumber + thisBorrowNumber ; 
35         System.out.println("图书还可借书:"+borrowableNumber+"本");    
36     }
37     public void returnBook(int thisReturnNumber)
38     {
39         if(thisReturnNumber > borrowedNumber )
40         {
41             System.out.print("error");
42             return ;
43         }
44         System.out.println("本次还书:"+thisReturnNumber+"本");
45         borrowableNumber = borrowableNumber + thisReturnNumber;
46         borrowedNumber = borrowedNumber - thisReturnNumber;
47         System.out.println("图书馆还可借:"+borrowableNumber+"本");
48     }
49     public static int getBookTotal() {
50         return bookTotal;
51     }
52     public static void setBookTotal(int bookTotal) {
53         LibraryCard.bookTotal = bookTotal;
54     }
55     public String getAccountNumber() {
56         return accountNumber;
57     }
58     public void setAccountNumber(String accountNumber) {
59         this.accountNumber = accountNumber;
60     }
61     public String getName() {
62         return name;
63     }
64     public void setName(String name) {
65         this.name = name;
66     }
67     public String getIDCard() {
68         return IDCard;
69     }
70     public void setIDCard(String iDCard) {
71         IDCard = iDCard;
72     }
73     public String getAddress() {
74         return address;
75     }
76     public void setAddress(String address) {
77         this.address = address;
78     }
79     public int getBorrowedNumber() {
80         return borrowedNumber;
81     }
82     public void setBorrowedNumber(int borrowedNumber) {
83         this.borrowedNumber = borrowedNumber;
84     }
85     public int getBorrowableNumber() {
86         return borrowableNumber;
87     }
88     public void setBorrowableNumber(int borrowableNumber) {
89         this.borrowableNumber = borrowableNumber;
90     }
91     
92 }
 1 package com.wsy.work.test;
 2 import com.wsy.work.input.*;
 3 import com.wsy.work.type.*;
 4 import java.util.Scanner;
 5 public class LibraryCardTest {
 6 
 7     public static void main(String[] args) {
 8         LibraryCardInput input = new LibraryCardInput();
 9         input.input();
10         LibraryCard card = new LibraryCard(input.getAccountNumber() , input.getName() , input.getIDCard() , input.getAddress());
11         Scanner in = new Scanner(System.in);
12         int n;
13         int number;
14         exit:
15         while(true)
16         {
17             card.prompt();
18             n = in.nextInt();
19             switch(n)
20             {
21                 case 1:
22                     System.out.println("本书店中还剩下"+card.getBorrowableNumber()+"本书");
23                     break;
24                 case 2:
25                     System.out.print("请输入您需要借多少本书:");
26                     number = in.nextInt();
27                     card.borrowBook(number);
28                     break;
29                 case 3:
30                     System.out.print("请输入您需要还多少本书:");
31                     number = in.nextInt();
32                     card.returnBook(number);
33                     break;
34                 case 4:
35                     System.out.println("成功退出 欢迎下次使用");
36                     break exit;
37             }
38         }
39         in.close();
40     }
41 
42 }
 1 package com.wsy.work.input;
 2 import java.util.Scanner;
 3 public class EleChargeManagementInput {
 4     Scanner in = new Scanner(System.in);
 5     private double readNumberLastMonth;
 6     private double readNumberThisMonth;
 7     public void input()
 8     {
 9         System.out.println("请依次输入上月电表读数、本月电表读数");
10         readNumberLastMonth = in.nextDouble();
11         readNumberThisMonth = in.nextDouble();
12     }
13     public double getReadNumberLastMonth() {
14         return readNumberLastMonth;
15     }
16     public void setReadNumberLastMonth(double readNumberLastMonth) {
17         this.readNumberLastMonth = readNumberLastMonth;
18     }
19     public double getReadNumberThisMonth() {
20         return readNumberThisMonth;
21     }
22     public void setReadNumberThisMonth(double readNumberThisMonth) {
23         this.readNumberThisMonth = readNumberThisMonth;
24     }
25     
26 }
 1 package com.wsy.work.type;
 2 /**
 3  * 在该类中出现了浮点数进行运算后的的精度问题
 4  * 没时间了我暂时用四舍五入来敷衍了一下
 5  * 这种问题可以去使用大浮点数来解决
 6  * BigDecimal(大浮点数)
 7  * @author Administrator
 8  *
 9  */
10 public class EleChargeManagement {
11     private double readNumberLastMonth;
12     private double readNumberThisMonth;
13     private double numberThisMonth;
14     
15     public EleChargeManagement(double readNumberLastMonth, double readNumberThisMonth) {
16         this.readNumberLastMonth = readNumberLastMonth;
17         this.readNumberThisMonth = readNumberThisMonth;
18     }
19     
20     public void show()
21     {
22         System.out.println("上月电表读数为:"+this.readNumberLastMonth);
23         System.out.println("本月电表读数为:"+this.readNumberThisMonth);
24     }
25     public void computeEleConsumption()
26     {
27         numberThisMonth = Math.round(this.readNumberThisMonth - this.readNumberLastMonth);
28     }
29     public void showEleConsumption()
30     {
31         if(numberThisMonth == 0)
32         {
33             System.out.println("请先计算本月用电数再显示本月用电数!");
34         }
35         else
36         {
37             System.out.println("本月使用电数为:"+numberThisMonth);    
38         }
39     }
40     public void showElePrice()
41     {
42         final double unitPrice = 1.2;
43         double price = numberThisMonth * unitPrice ;
44         System.out.println("本月电费为:"+Math.round(price));
45     }
46 
47     public double getReadNumberLastMonth() {
48         return readNumberLastMonth;
49     }
50 
51     public void setReadNumberLastMonth(double readNumberLastMonth) {
52         this.readNumberLastMonth = readNumberLastMonth;
53     }
54 
55     public double getReadNumberThisMonth() {
56         return readNumberThisMonth;
57     }
58 
59     public void setReadNumberThisMonth(double readNumberThisMonth) {
60         this.readNumberThisMonth = readNumberThisMonth;
61     }
62 
63     public double getNumberThisMonth() {
64         return numberThisMonth;
65     }
66 
67     public void setNumberThisMonth(double numberThisMonth) {
68         this.numberThisMonth = numberThisMonth;
69     }
70     
71 }
 1 package com.wsy.work.test;
 2 import java.util.Scanner;
 3 
 4 import com.wsy.work.input.EleChargeManagementInput;
 5 import com.wsy.work.type.EleChargeManagement;
 6 public class EleChargeManagementTest {
 7 
 8     public static void main(String[] args) {
 9         Scanner in = new Scanner(System.in);
10         EleChargeManagementInput input = new EleChargeManagementInput();
11         input.input();
12         EleChargeManagement ele1 = new EleChargeManagement(input.getReadNumberLastMonth(), input.getReadNumberThisMonth());
13         //显示上月、本月电表读数
14         ele1.show();
15         //计算本月用电数
16         ele1.computeEleConsumption();
17         //显示本月用电数
18         ele1.showEleConsumption();
19         //显示本月电费
20         ele1.showElePrice();
21         in.close();
22     }
23 
24 }

 

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄