Java实验报告

班级 计算机科学与技术二班 学号 20188450 姓名 李代传

完成时间 2019/9/19

评分等级

实验二 Java简单类与对象

  1. 实验目的
    1. 掌握类的定义,熟悉属性、构造函数、方法的作用,掌握用类作为类型声明变量和方法返回值;
    2. 理解类和对象的区别,掌握构造函数的使用,熟悉通过对象名引用实例的方法和属性;
    3. 理解static修饰付对类、类成员变量及类方法的影响。
  2. 实验内容

1. 写一个名为Rectangle的类表示矩形。其属性包括宽width、高height和颜色color,width和height都是double型的,而color则是String类型的。要求该类具有:

(1) 使用构造函数完成各属性的初始赋值

(2) 使用get…()和set…()的形式完成属性的访问及修改

(3) 提供计算面积的getArea()方法和计算周长的getLength()方法

2. 银行的账户记录Account有账户的唯一性标识(11个长度的字符和数字的组合),用户的姓名,开户日期,账户密码(六位的数字,可以用0开头),当前的余额。银行规定新开一个账户时,银行方面提供一个标识符、账户初始密码123456,客户提供姓名,开户时客户可以直接存入一笔初始账户金额,不提供时初始余额为0。定义该类,并要求该类提供如下方法:存款、取款、变更密码、可以分别查询账户的标识、姓名、开户日期、当前余额等信息。

  1. 实验过程

1. 写一个名为Rectangle的类表示矩形。其属性包括宽width、高height和颜色color,width和height都是double型的,而color则是String类型的。

实验源码:

实验结果:

实验源码:

package 银行账户1;

import java.text.SimpleDateFormat;

import java.util.ArrayList;

import java.util.Calendar;

import java.util.Date;

import java.util.Scanner;

public class User { //客户端类

static int user=0; //当前客户在AccountList中的序号

public static void main(String[] args) {

// TODO Auto-generated method stub

Scanner input =new Scanner(System.in);

System.out.print("是否开户?\n 请输入:是 \t 或者输入:否 \n");

if(input.next().equals("是")){

AccountList.create(); //开户,创造一个新的Account对象用来保存用户各类数据

}

while(true){ //多次处理用户命令操作

System.out.print("账户操作-->变更密码请输入:1;\t存款请输入:2;\t取款请输入:3;\n查询操作-->查询账户的标识:4;\t查询开户日期:5;\t查询当前余额:6;\t查询姓名:7;\n开户请输入:8;\n退出请输入:0\n"); //客户端页面

int number=input.nextInt();

if(number>=0&&number<=8){

corresponding(number); //处理数字相应的命令

}else{

System.out.print("错误的输入!自动退出!\n"); //如果并不是零到八的数字,就结束所有操作

break;

}

}

AccountList.printfAll(); //输出所有的Account对象的内容

}

public static void corresponding(int number){ //处理数字相应的命令

if(number==8){

opening(); //开户方法

return;

}

int i=trueOrFalse(number); //返回用户是否在AccountList表中有序号,有则返回正确的序号

if(i==-1)return;

if(number==1){

System.out.println("接下来输入您的新密码--->");

AccountList.List.get(i).setPassword(AccountList.inputPassword()); //设置密码为新输入的密码

System.out.println("密码修改完成!");

}else if(number==2){

System.out.print("输入您的存款数:\n");

int blance=AccountList.inputBlance(); //获取输入的存款值

if(blance>0){

AccountList.List.get(i).setBalance(AccountList.List.get(i).getBalance()+blance); //存款大于零就能存入

System.out.print("存款成功!");

}else{

System.out.print("错误的存款数!");

}

}else if(number==3){

System.out.print("输入您的取款数:\n");

int blance=AccountList.inputBlance(); //获取输入的取款值

if(blance>0&&blance<=AccountList.List.get(i).getBalance()){ //小于存款值并大于零就可以取款

AccountList.List.get(i).setBalance(AccountList.List.get(i).getBalance()-blance); // 改变存款值

System.out.println("取款成功!");

}else{

System.out.print("错误的取款数!");

}

}else if(number==4){

System.out.println("您的账户标识为:"+AccountList.List.get(i).getIdentifier());

}else if(number==5){

System.out.println("您的开户日期为:"+AccountList.List.get(i).getOpeningDate());

}else if(number==6){

System.out.println("您的当前余额为:"+AccountList.List.get(i).getBalance());

}else if(number==7){

System.out.println("您的姓名为:"+AccountList.List.get(i).getName());

}

}

public static void opening(){ //开户

Scanner input =new Scanner(System.in);

System.out.print("是否开户?\n 请输入:是 \t 或者输入:否 \n");

String s=input.next();

if(s.equals("是")){

AccountList.create(); //创建一个新的Account对象用来保存用户各类数据

}else if(s.equals("否")){

System.out.println("谢谢使用!");

}else{

System.out.println("请确认您的输入正确!");

}

}

public static int trueOrFalse(int number) //返回用户是否在AccountList表中有序号,有则返回正确的序号

{

if(number==0){

System.out.print("已安全退出!");

System.exit(1);

}else{

System.out.println("是否改变账户?");

Scanner input =new Scanner(System.in);

String s=input.nextLine().trim();

if(s.equals("是")){

int i=AccountList.search(AccountList.inputName()); //判断是否有输入的用户,有则返回大于等于零的序号

if(i!=-1){

if(AccountList.inputPassword().equals(AccountList.List.get(i).getPassword())){ //判断密码是否正确

user=i; //当前更改后的用户的序号

return i;

}else{

System.out.println("密码错误!请重新启动!");

return -1;

}

}else{

System.out.println("没有此用户!请重试!");

return -1;

}

}else if(s.equals("否")){

return user; //继续使用当前用户的序号

}else{

System.out.println("错误的输入!请新开始!");

}

}

return 0;

}

}

class Account { //保存用户的各类数据,不能直接访问,以免造成糟糕的后果

private static double id=1; //用户共享的内部id

private double userid; //用户本人的id

private String identifier; //用户唯一的标识码

private String name; //用户名

private String openingDate; //用户的开户日期

private String password="123456";//用户初始密码

private double balance=0; //用户初始余额

public Account(String name){ //构造函数,确定不可更改的用户id,唯一标识符,开户日期。

this.name=name;

this.userid=id;

this.setOpeningDate();

this.setUniqueid();

id++;

}

public double getUserid() {

return userid;

}

public String getOpeningDate() {

return openingDate;

}

public String getIdentifier() {

return identifier;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getPassword() {

return password;

}

public void setPassword(String password) {

this.password = password;

}

public double getBalance() {

return balance;

}

public void setBalance(double balance) {

this.balance = balance;

}

private void setUniqueid(){ //设置唯一的标识符

if(id>=100){

this.identifier="uniqueid"+(int)id;

}else if(id>=10){

this.identifier="uniqueid0"+(int)id;

}else{

this.identifier="uniqueid00"+(int)id;

}

}

private void setOpeningDate(){ //不可更改的开户日期

Date now=new Date();

SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");

this.openingDate=sdf.format(now);

}

}

class AccountList{ //保存所有用户各类数据的表

static ArrayList<Account> List=new ArrayList<Account>(); //Account对象类型的数组表

public static void create(){ //创造一个新的Account

Account account=new Account(AccountList.inputName()); //开户需要用户名,银行提供其他的数据

List.add(account);

System.out.print("请问您要存入初始资金吗?如果不需要请输入数字0;如果需要请输入金额:\n");

Scanner input =new Scanner(System.in);

double aom=input.nextDouble();

if(aom<=0){

System.out.print("您的初始金额为0元!\n");

}else{

account.setBalance(aom);

System.out.print("您的初始金额为"+account.getBalance()+"元!\n");

}

}

public static int search(String name){ //在现有的AccountList中寻找是否有这个用户,找到返回其序号,否则返回-1

int i;

for(i=0;i<List.size();i++){

if(List.get(i).getName().equals(name))

return i;

}

return -1;

}

public static void printfAll(){ //输出所有的用户信息

for(Account i:List){

System.out.println("用户名字:"+i.getName()+"\n用户自己ID:"+i.getUserid()+"\n用户标识码:"+i.getIdentifier().toString()+"\n开户日期:"+i.getOpeningDate()+"\n账户余额:"+i.getBalance()+"\n用户密码:"+i.getPassword()+"\n");

}

}

public static String inputName(){ //返回输入的用户名

Scanner input =new Scanner(System.in);

System.out.print("请输入您的姓名:\n");

String s=input.nextLine().trim();

System.out.print("您的姓名为: "+s+"\n");

return s;

}

public static String inputPassword(){ //返回输入的密码

System.out.print("请输入您的密码:\n");

Scanner input =new Scanner(System.in);

while(true){

String password=input.nextLine().trim();

char [] ch=password.toCharArray();

if(ch.length!=6){

System.out.println("不符合规定的密码!请重试!");

}else{

return password;

}

}

}

public static int inputBlance(){ //返回输入的存取款值

Scanner input =new Scanner(System.in);

return input.nextInt();

}

}

实验结果:

是否开户?

请输入:是      或者输入:否

请输入您的姓名:

李代传

您的姓名为:李代传

;如果需要请输入金额:

您的初始金额为3838373.0元!

账户操作-->变更密码请输入:1;    存款请输入:2;    取款请输入:3;

查询操作-->查询账户的标识:4;    查询开户日期:5;    查询当前余额:6;    查询姓名:7;

开户请输入:8;

是否改变账户?

请输入您的姓名:

李代传

您的姓名为:李代传

请输入您的密码:

接下来输入您的新密码--->

请输入您的密码:

密码修改完成!

账户操作-->变更密码请输入:1;    存款请输入:2;    取款请输入:3;

查询操作-->查询账户的标识:4;    查询开户日期:5;    查询当前余额:6;    查询姓名:7;

开户请输入:8;

是否改变账户?

接下来输入您的新密码--->

请输入您的密码:

密码修改完成!

账户操作-->变更密码请输入:1;    存款请输入:2;    取款请输入:3;

查询操作-->查询账户的标识:4;    查询开户日期:5;    查询当前余额:6;    查询姓名:7;

开户请输入:8;

已安全退出!

课程总结:自我感觉学的不错,主要是老师教的好!李老师讲课非常深入人心,像那些什么构造方法呀,this关键字啊,类的基本构造规则啊,一讲就懂。运用起来非常方便,现在String类也讲了不少东西了,都学到了。虽然我英语不好,但是方法的单词记住开头就行了,简单。java程序感觉非常容易写,行云流水,写完一般能运行也就不会错,不需要像c一样多次调试,感觉非常舒服。

这个作业在哪个具体方面帮助我实现目标:通过构思这个程序,锻炼了我对于学到知识的掌握。并且更容易记住了。

具体思路:矩形的封装思路没什么好说的,肯定都会的,很简单的东西。

关于银行系统的思路很清晰:1,--User类给用户操作,可以实现各种功能,2—Account类,用于保存用户开户后的各类数据及基本操作。3—AccountList类,各种中间操作实现体。

基本上没遇到过问题,就是要贴近现实中的使用嘛,所以我把Account类和User使用类完全分开了,这样使得用户的信息得到了极大的保护。其他的,还考虑到了异常(Exception),比如就是输入用户命令的时候,要是输入了其他类型的数据,就会终止程序并显示异常。这现在我也解决不了,所以等以后学好了在继续修改吧。

最新文章

  1. Android Studio多渠道打包
  2. SEL-消息机制
  3. UIWindow
  4. 数据结构算法C语言实现(十)--- 3.3栈与递归的实现
  5. iOS-程序发布-32位和64位系统的兼容
  6. Linux Netcat 命令——网络工具中的瑞士军刀
  7. 如何理解JS项目
  8. arm tiny6410双网卡桥接问题
  9. 剑指Offer17 二叉树的镜像
  10. Web流量劫持
  11. Microsoft Azure Storage Exployer使用指南
  12. linux环境
  13. SSM-SpringMVC-20:SpringMVC中处理器方法之返回值void篇
  14. iOS开发多线程之NSOperation
  15. Windows Azure Virtual Machine (36) 扩展Azure ARM VM的磁盘大小
  16. Emacs 编辑verilog 学习日记
  17. xampp集成包如何重置mysql的密码
  18. JavaWeb的编码问题
  19. [HAOI2015]T2
  20. HTML5语义元素总结

热门文章

  1. luogu 1373 小a和uim之大逃离 dp
  2. HZOJ 20190819 NOIP模拟26题解
  3. HTML容器标签和文本标签
  4. Python2.x 里解决中文编码的万能钥匙
  5. Nginx之最简单的反向代理机制分析
  6. Nginx-HTTP之静态网页访问流程分析二
  7. tkinter入门-布局方式pack(), grid(), place()
  8. mysql数据库文件的真实的物理存储位置
  9. LC 871. Minimum Number of Refueling Stops 【lock, hard】
  10. js 生成树以及关键字搜索生成树